-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMarketMaker.cpp
More file actions
200 lines (183 loc) · 5.71 KB
/
Copy pathMarketMaker.cpp
File metadata and controls
200 lines (183 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "Market.h"
#include "OrderBook.h"
#include "Order.h"
#include "Distribution.h"
#include "DistributionUniform.h"
#include "MarketMaker.h"
#include <cmath>
#include <iostream>
MarketMaker::MarketMaker(
Market * a_market,
Distribution * a_ActionTimeDistribution,
Distribution * a_OrderVolumeDistribution,
Distribution * a_OrderPriceDistribution,
double a_buyFrequency,
int a_favouriteStockIdentifier,
double a_cancelBuyFrequency,
double a_cancelSellFrequency,
double a_cancelProbability,
double a_volumeProportion
):Agent(a_market,MARKET_MAKER, a_favouriteStockIdentifier)
{
m_ActionTimeDistribution = a_ActionTimeDistribution ;
m_OrderVolumeDistribution = a_OrderVolumeDistribution ;
m_OrderPriceDistribution = a_OrderPriceDistribution;
m_buyFrequency = a_buyFrequency ;
m_OrderTypeDistribution = new DistributionUniform(a_market->getRNG()) ;
m_cancelBuyFrequency = a_cancelBuyFrequency;
m_cancelSellFrequency = a_cancelSellFrequency;
m_sellFrequency = 1.0 - m_cancelBuyFrequency - m_cancelSellFrequency - m_buyFrequency;
m_cancelProbability = a_cancelProbability ;
m_cancelDistribution = new DistributionUniform(a_market->getRNG()) ;
m_VolumeProportion = a_volumeProportion;
}
MarketMaker::~MarketMaker()
{
delete m_OrderTypeDistribution;
delete m_cancelDistribution;
}
double MarketMaker::getNextActionTime() const
{
return m_ActionTimeDistribution->nextRandom() ;
}
int MarketMaker::getOrderVolume() const
{
return std::max((int)m_OrderVolumeDistribution->nextRandom(),1) ;
}
int MarketMaker::getOrderVolume(double price, int a_OrderBookId, OrderType orderType ) const
{
if (orderType==LIMIT_BUY){
int volumeBid = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidQuantity();
return std::max(int(m_VolumeProportion * volumeBid),5);
}
else {
int volumeAsk = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskQuantity();
return std::max(int(m_VolumeProportion * volumeAsk),5);
}
}
int MarketMaker::getOrderPrice(int a_OrderBookId, OrderType a_OrderType) const
{
int price ;
double lag = m_OrderPriceDistribution->nextRandom() ;
int tickSize = m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize() ;
//int spread = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() - m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice();
//if(spread >= 2 * m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize()){
if(a_OrderType==LIMIT_BUY)
{
int currentPrice = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice() ;
price = currentPrice + tickSize ;
}
else if(a_OrderType==LIMIT_SELL)
{
int currentPrice = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() ;
price = currentPrice - tickSize ;
}
else
{
// GESTION DES ERREURS ET EXCEPTIONS
}
// }
return price ;
}
OrderType MarketMaker::getOrderType() const
{
double l_orderTypeAlea = m_OrderTypeDistribution->nextRandom();
if(l_orderTypeAlea<m_cancelBuyFrequency)
{
// std::cout << "CANCEL_BUY" << std::endl ;
return CANCEL_BUY;
}
else if(l_orderTypeAlea<m_cancelBuyFrequency + m_cancelSellFrequency)
{
// std::cout << "CANCEL_SELL" << std::endl ;
return CANCEL_SELL;
}
else if(l_orderTypeAlea<m_cancelBuyFrequency + m_cancelSellFrequency + m_buyFrequency)
{
//std::cout << "LIMIT_BUY" << std::endl ;
return LIMIT_BUY ;
}
else
{
// std::cout << "LIMIT_SELL " << std::endl ;
return LIMIT_SELL ;
}
}
void MarketMaker::makeAction(int a_OrderBookId, double a_currentTime)
{
//m_linkToMarket->getOrderBook(a_OrderBookId)->cleanOrderBook();
OrderType thisOrderType = getOrderType() ;
if(thisOrderType == CANCEL_BUY ||thisOrderType == CANCEL_SELL)
{
if (a_currentTime == 0.0)// NO Cancellation at the initialisation of the process
{
return;
}
else
{
if(thisOrderType == CANCEL_BUY)
{
chooseOrdersToBeCanceled(a_OrderBookId,true,a_currentTime);
return;
}
else
{
chooseOrdersToBeCanceled(a_OrderBookId,false,a_currentTime);
return;
}
}
return;
}
int spread = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() - m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice();
if(spread < 2 * m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize()){return; }
if(spread >= 2 * m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize()){
int thisOrderPrice = getOrderPrice(a_OrderBookId, thisOrderType) ;
int thisOrderVolume = getOrderVolume(thisOrderPrice, a_OrderBookId, thisOrderType) ;
int tickSize = m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize();
submitOrder(
a_OrderBookId, a_currentTime,
thisOrderVolume,
thisOrderType,
thisOrderPrice
);
if (thisOrderType==LIMIT_BUY){
submitOrder(
a_OrderBookId, a_currentTime,
thisOrderVolume,
LIMIT_SELL,
thisOrderPrice+tickSize
);
}
else if(thisOrderType==LIMIT_SELL){
submitOrder(
a_OrderBookId, a_currentTime,
thisOrderVolume,
LIMIT_BUY,
thisOrderPrice-tickSize
);
}
}
}
void MarketMaker::chooseOrdersToBeCanceled(int a_OrderBookId, bool a_buySide, double a_time)
{
std::map<int,Order> pendingOrdersCopy(m_pendingOrders) ;
std::map<int,Order>::iterator iter = pendingOrdersCopy.begin();
while(iter!=pendingOrdersCopy.end()){
OrderType thisOrderType = iter->second.getType() ;
if((thisOrderType==LIMIT_BUY && a_buySide)||(thisOrderType==LIMIT_SELL && !a_buySide))
{
double cancelAlea = m_cancelDistribution->nextRandom() ;
if(cancelAlea<m_cancelProbability){
submitCancellation(a_OrderBookId,iter->second.getIdentifier(),a_time) ;
}
}
iter++ ;
}
}
void MarketMaker::processInformation()
{
// For exemple, read the market book history and decide to do something within a reaction time
}
void MarketMaker::cleanPending(){
m_pendingOrders.clear();
}