# Example Bayesian Market Maker

This example runs a market maker based upon bayesian updating of the probability of the bid or ask price.

## Contents

```addpath('Data')
hold on
plot(msftbid)
hold off
``` ## Bayesian Market Maker

Parameters

```alpha = 0.9;
sigma = 0.13;
P0 = 28.585;
```

Inital position and profit

```pos = 0;
profit = 0;
```

Initialize probablility distribution

```addpath('Models')
[P,V] = initializeProbability(P0,sigma);
plot(V,P)
xlabel('Price')
ylabel('Probability')
title('MSFT')
``` ```[PBid,PAsk] = calcBidAsk(V,P,alpha)
```
```PBid =

28.58

28.59

```

iniitlialize plot

```init = nan(size(msftask));
marketMakerPlot(init,init,init,init,0)
``` Continue to loop over the prices and update the different states of the model.

```for i = 1:length(msftask)
bid = msftbid(i);

% When a quote is received, can either place a buy or sell at market.
% market sell order
pos = pos - 1;
% Contol inventory
pos = pos + 1;
P = Probability(PBid,-1,alpha,P,V);
% Contol inventory
end

% update plots
end
``` ## Run Strategy as Event Based System

Create a replay service

```clear all, close all, clc
'DateTime','Type'});
theFeed = DataFeedReplayService(data)
data(1:5,:)
clear data msftask msftbid msftema dates
```
```theFeed =

DataFeedReplayService with properties:

ReplaySpeed: 2.00
Index: 1.00
SymbolList: {'MSFT'}
Data: [50000x1 struct]
Period: 2.00
Timer: [1x1 timer]

ans =

______    ________    ________    _______    _______    ______________________    _______

'MSFT'    28.60       28.57       82.00      63.00      '20-Aug-2015 09:30:00'    'quote'
'MSFT'    28.60       28.57       91.00       3.00      '20-Aug-2015 09:30:01'    'quote'
'MSFT'    28.59       28.57       13.00      48.00      '20-Aug-2015 09:30:01'    'quote'
'MSFT'    28.59       28.57       92.00      50.00      '20-Aug-2015 09:30:01'    'quote'
'MSFT'    28.59       28.57       64.00      33.00      '20-Aug-2015 09:30:02'    'quote'

```

```b = Broker;
```

Create the Market Maker using above strategy and assign Broker

```bmm = BayesianMarketMaker(28.585,0.13,'MSFT','MSFTMarketMaker');
display(b)
```
```b =

Broker with properties:

QuoteBook: [0x0 table]

```

Create a quote monitor

```qm = createNBBODisplay(theFeed.SymbolList);
@(~,e) updateNBBODisplay(qm,e.Data,theFeed.SymbolList));
``` Set replay speed to 0.1 seconds

```setPeriod(theFeed,0.1);
display(theFeed)
```
```theFeed =

DataFeedReplayService with properties:

ReplaySpeed: 2.00
Index: 1.00
SymbolList: {'MSFT'}
Data: [50000x1 struct]
Period: 0.10
Timer: [1x1 timer]

```

```init = nan(size(theFeed.Data));
marketMakerPlot(init,init,init,init,0)
``` ```start(theFeed)

while theFeed.Index < length(theFeed.Data)
% wait for all data to stream
pause(10)
end

stop(theFeed);
```
```Error while evaluating TimerFcn for timer 'timer-1'

Index exceeds matrix dimensions.

```  