-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLiquidityProvider.cpp
More file actions
257 lines (238 loc) · 8.02 KB
/
Copy pathLiquidityProvider.cpp
File metadata and controls
257 lines (238 loc) · 8.02 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "Market.h"
#include "OrderBook.h"
#include "Order.h"
#include "Distribution.h"
#include "DistributionUniform.h"
#include "LiquidityProvider.h"
#include "LiquidityProvider.h"
#include <cmath>
#include <iostream>
LiquidityProvider::LiquidityProvider(
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
):Agent(a_market,LIQUIDITY_PROVIDER, 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()) ;
}
LiquidityProvider::~LiquidityProvider()
{
delete m_OrderTypeDistribution;
delete m_cancelDistribution;
}
double LiquidityProvider::getNextActionTime() const
{
return m_ActionTimeDistribution->nextRandom() ;
}
int LiquidityProvider::getOrderVolume() const
{
return std::max((int)m_OrderVolumeDistribution->nextRandom(),1) ;
}
int LiquidityProvider::getOrderVolume(double price, int a_OrderBookId, OrderType orderType ) const
{
int slotMax = 20;
if (orderType==LIMIT_BUY){
int currentPriceAsk = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() ;
if ( price < currentPriceAsk && price>(currentPriceAsk-0.25*slotMax ) ) {
int volume = std::min((int)m_OrderVolumeDistribution->nextRandom(),100);
return std::max(volume , 1 ) ;
}
else{
int slotNumber;
slotNumber = (int)(currentPriceAsk- price)/(m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize());
DistributionUniform * uniformVolumeDistribution = new DistributionUniform(m_linkToMarket->getRNG(),0,(70*6)/(slotNumber^(3/2)));
int volume = std::max((int)uniformVolumeDistribution->nextRandom(),1) ;
delete uniformVolumeDistribution;
return volume;
}
}
else{
int currentPriceBid = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice() ;
if ( price > currentPriceBid && price<(currentPriceBid+0.25*slotMax ) ) {
int volume = std::min((int)m_OrderVolumeDistribution->nextRandom(),100);
return std::max(volume , 1 ) ;
}
else{
int slotNumber;
slotNumber = (int)(price-currentPriceBid)/(m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize());
DistributionUniform * uniformVolumeDistribution = new DistributionUniform(m_linkToMarket->getRNG(),0,(70*6)/(slotNumber^(3/2)));
int volume = std::max((int)uniformVolumeDistribution->nextRandom(),1) ;
delete uniformVolumeDistribution;
return volume;
}
}
}
int LiquidityProvider::getOrderPrice(int a_OrderBookId, OrderType a_OrderType) const
{
int price ;
double lag = m_OrderPriceDistribution->nextRandom() ;
int tickSize = m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize() ;
if(a_OrderType==LIMIT_BUY)
{
int currentPrice = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() ;
price = currentPrice - (((int)lag)*tickSize) - tickSize ;
}
else if(a_OrderType==LIMIT_SELL)
{
int currentPrice = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice() ;
price = currentPrice + (((int)lag)*tickSize) + tickSize ;
}
else
{
// GESTION DES ERREURS ET EXCEPTIONS
}
return price ;
}
OrderType LiquidityProvider::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 LiquidityProvider::makeAction(int a_OrderBookId, double a_currentTime){
makeAction(a_OrderBookId, a_currentTime,true);
}
void LiquidityProvider::makeAction(int a_OrderBookId, double a_currentTime, bool init)
{
//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;
// }
// }
//}
// int thisOrderPrice = getOrderPrice(a_OrderBookId, thisOrderType) ;
// int thisOrderVolume = getOrderVolume(thisOrderPrice, a_OrderBookId, thisOrderType) ;
//// int thisOrderVolume = getOrderVolume() ;
// submitOrder(
// a_OrderBookId, a_currentTime,
// thisOrderVolume,
// thisOrderType,
// thisOrderPrice
// );
// std::cout<<"LIMIT order!!!"<<std::endl;
//std::cout<<"LIQUIDITY ORDER"<<std::endl;
int tickSize = m_linkToMarket->getOrderBook(a_OrderBookId)->getTickSize();
int i;
int end;
if (init){i=1;end=20;}
else {i=0;end=19;}
DistributionUniform *u = new DistributionUniform(m_linkToMarket->getRNG(), 0, 1);
for (i;i<=end;i++){
if( u->nextRandom() > 0.5) {
int currentPriceAsk = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() ;
//std::cout<<"current Ask : "<<currentPriceAsk<<std::endl;
//std::cout<<"current Bid : "<<currentPriceBid<<std::endl;
int buyPrice = currentPriceAsk-(i)*tickSize;
int buyOrderVolume = getOrderVolume(buyPrice, a_OrderBookId, LIMIT_BUY) ;
submitOrder(
a_OrderBookId, a_currentTime,
buyOrderVolume,
LIMIT_BUY,
buyPrice
);
int currentPriceBid = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice() ;
int sellPrice = currentPriceBid+(i)*tickSize;
int sellOrderVolume = getOrderVolume(sellPrice, a_OrderBookId, LIMIT_SELL) ;
submitOrder(
a_OrderBookId, a_currentTime,
sellOrderVolume,
LIMIT_SELL,
sellPrice
);
}else{
int currentPriceBid = m_linkToMarket->getOrderBook(a_OrderBookId)->getBidPrice() ;
int sellPrice = currentPriceBid+(i)*tickSize;
int sellOrderVolume = getOrderVolume(sellPrice, a_OrderBookId, LIMIT_SELL) ;
submitOrder(
a_OrderBookId, a_currentTime,
sellOrderVolume,
LIMIT_SELL,
sellPrice
);
int currentPriceAsk = m_linkToMarket->getOrderBook(a_OrderBookId)->getAskPrice() ;
//std::cout<<"current Ask : "<<currentPriceAsk<<std::endl;
//std::cout<<"current Bid : "<<currentPriceBid<<std::endl;
int buyPrice = currentPriceAsk-(i)*tickSize;
int buyOrderVolume = getOrderVolume(buyPrice, a_OrderBookId, LIMIT_BUY) ;
submitOrder(
a_OrderBookId, a_currentTime,
buyOrderVolume,
LIMIT_BUY,
buyPrice
);
}
}
delete u;
}
void LiquidityProvider::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 LiquidityProvider::processInformation()
{
// For exemple, read the market book history and decide to do something within a reaction time
}
void LiquidityProvider::cleanPending(){
m_pendingOrders.clear();
}