-
Notifications
You must be signed in to change notification settings - Fork 163
/
SimulationInterface.hpp
303 lines (229 loc) · 10.3 KB
/
SimulationInterface.hpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/************************************************************************
* Copyright(c) 2023, One Unified. All rights reserved. *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
/*
* File: SimulationInterface.hpp
* Author: raymond@burkholder.net
* Project: TFSimulation
* Created: February 8, 2023 15:42:12
*/
#pragma once
#include <mutex>
#include <unordered_map>
#include <TFTrading/Order.h>
#include <TFTrading/ProviderInterface.h>
#include "SimulationSymbol.h"
#include "SimulateOrderExecution.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace sim { // simulation
template <typename P, typename S> // p = provider, S = symbol
class SimulationInterface
: public ou::tf::ProviderInterface<P,S>
{
friend ou::tf::ProviderInterface<P,S>;
public:
using inherited_t = ou::tf::ProviderInterface<P,S>;
using pOrder_t = ou::tf::Order::pOrder_t;
using pSymbol_t = typename inherited_t::pSymbol_t;
using pInstrument_t = ou::tf::Instrument::pInstrument_t;
using pInstrument_cref = ou::tf::Instrument::pInstrument_cref;
SimulationInterface()
: m_bExecutionEnabled( true )
{}
void SetCommission( const std::string& sSymbol, double commission );
void PlaceOrder( pOrder_t pOrder );
void CancelOrder( pOrder_t pOrder );
protected:
bool m_bExecutionEnabled;
pSymbol_t AddCSymbol( pSymbol_t );
virtual void AddQuoteHandler( pInstrument_cref pInstrument, typename S::quotehandler_t );
virtual void RemoveQuoteHandler( pInstrument_cref pInstrument, typename S::quotehandler_t );
virtual void AddTradeHandler( pInstrument_cref pInstrument, typename S::tradehandler_t );
virtual void RemoveTradeHandler( pInstrument_cref pInstrument, typename S::tradehandler_t );
virtual void AddDepthByMMHandler( pInstrument_cref pInstrument, typename S::depthbymmhandler_t );
virtual void RemoveDepthByMMHandler( pInstrument_cref pInstrument, typename S::depthbymmhandler_t );
virtual void AddDepthByOrderHandler( pInstrument_cref pInstrument, typename S::depthbyorderhandler_t );
virtual void RemoveDepthByOrderHandler( pInstrument_cref pInstrument, typename S::depthbyorderhandler_t );
size_t MonitoredSymbolsCount() const { return m_mapOrderExecution.size(); }
private:
struct EventHolders {
OrderExecution oe;
pSymbol_t pSymbol;
EventHolders( pSymbol_t pSymbol_ )
: pSymbol( std::move( pSymbol_ ) ) {}
EventHolders( EventHolders&& rhs )
: pSymbol( std::move( rhs.pSymbol ) ) {}
};
using mapOrderExecution_t = std::unordered_map<std::string,EventHolders>;
mapOrderExecution_t m_mapOrderExecution;
using fOrderExecution_t = std::function<void(EventHolders&)>;
std::mutex m_mutex;
void Update( const std::string& sName, fOrderExecution_t&& f ) {
if ( m_bExecutionEnabled ) {
std::scoped_lock<std::mutex> lock( m_mutex );
typename mapOrderExecution_t::iterator iter = m_mapOrderExecution.find( sName );
assert( m_mapOrderExecution.end() != iter );
EventHolders& eh( iter->second );
f( eh );
}
}
};
template <typename P, typename S>
typename SimulationInterface<P,S>::pSymbol_t SimulationInterface<P,S>::AddCSymbol( pSymbol_t pSymbol ) {
if ( m_bExecutionEnabled ) {
const std::string& sName( pSymbol->GetInstrument()->GetInstrumentName( inherited_t::ID() ) );
std::scoped_lock<std::mutex> lock( m_mutex );
typename mapOrderExecution_t::iterator iter = m_mapOrderExecution.find( sName );
if ( m_mapOrderExecution.end() == iter ) {
auto pair = m_mapOrderExecution.emplace( sName, EventHolders( pSymbol ) );
assert( pair.second );
iter = pair.first;
EventHolders& eh( iter->second );
OrderExecution& oe( eh.oe );
oe.SetOnOrderFill( MakeDelegate( dynamic_cast<P*>( this ), &P::HandleExecution ) );
oe.SetOnCommission( MakeDelegate( dynamic_cast<P*>( this ), &P::HandleCommission ) );
oe.SetOnOrderCancelled( MakeDelegate( dynamic_cast<P*>( this ), &P::HandleCancellation ) );
}
else {
assert( false ); // need better handling, this will be a duplicate
}
}
return inherited_t::AddCSymbol( pSymbol );
}
template <typename P, typename S>
void SimulationInterface<P,S>::AddQuoteHandler( pInstrument_cref pInstrument, typename S::quotehandler_t handler ) {
inherited_t::AddQuoteHandler( pInstrument, handler ); // needs to come first
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetQuoteHandlerCount() ) { // on first insertion add our own
inherited_t::AddQuoteHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewQuote ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::RemoveQuoteHandler( pInstrument_cref pInstrument, typename S::quotehandler_t handler ) {
inherited_t::RemoveQuoteHandler( pInstrument, handler );
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetQuoteHandlerCount() ) { // on first insertion add our own
inherited_t::RemoveQuoteHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewQuote ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::AddTradeHandler( pInstrument_cref pInstrument, typename S::tradehandler_t handler ) {
inherited_t::AddTradeHandler( pInstrument, handler ); // needs to come first
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ) {
if ( 1 == eh.pSymbol->GetTradeHandlerCount() ) { // on first insertion add our own
inherited_t::AddTradeHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewTrade ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::RemoveTradeHandler( pInstrument_cref pInstrument, typename S::tradehandler_t handler ) {
inherited_t::RemoveTradeHandler( pInstrument, handler );
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ) {
if ( 1 == eh.pSymbol->GetTradeHandlerCount() ) {
inherited_t::RemoveTradeHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewTrade ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::AddDepthByMMHandler( pInstrument_cref pInstrument, typename S::depthbymmhandler_t handler ) {
inherited_t::AddDepthByMMHandler( pInstrument, handler ); // needs to come first
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetDepthByMMHandlerCount() ) { // on first insertion add our own
inherited_t::AddDepthByMMHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewDepthByMM ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::RemoveDepthByMMHandler( pInstrument_cref pInstrument, typename S::depthbymmhandler_t handler ) {
inherited_t::RemoveDepthByMMHandler( pInstrument, handler );
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetDepthByMMHandlerCount() ) {
inherited_t::RemoveDepthByMMHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewDepthByMM ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::AddDepthByOrderHandler( pInstrument_cref pInstrument, typename S::depthbyorderhandler_t handler ) {
inherited_t::AddDepthByOrderHandler( pInstrument, handler ); // needs to come first
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetDepthByOrderHandlerCount() ) { // on first insertion add our own
inherited_t::AddDepthByOrderHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewDepthByOrder ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::RemoveDepthByOrderHandler( pInstrument_cref pInstrument, typename S::depthbyorderhandler_t handler ) {
inherited_t::RemoveDepthByOrderHandler( pInstrument, handler );
const std::string& sName( pInstrument->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[this,&pInstrument]( EventHolders& eh ){
if ( 1 == eh.pSymbol->GetDepthByOrderHandlerCount() ) {
inherited_t::RemoveDepthByOrderHandler( pInstrument, MakeDelegate( &eh.oe, &OrderExecution::NewDepthByOrder ) );
}
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::SetCommission( const std::string& sSymbol, double commission ) {
Update(
sSymbol,
[commission]( EventHolders& eh ){
eh.oe.SetCommission( commission );
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::PlaceOrder( pOrder_t pOrder ) {
inherited_t::PlaceOrder( pOrder );
const std::string& sName( pOrder->GetInstrument()->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[pOrder]( EventHolders& eh ){
eh.oe.SubmitOrder( pOrder );
} );
}
template <typename P, typename S>
void SimulationInterface<P,S>::CancelOrder( pOrder_t pOrder ) {
inherited_t::CancelOrder( pOrder );
const std::string& sName( pOrder->GetInstrument()->GetInstrumentName( inherited_t::ID() ) );
Update(
sName,
[pOrder]( EventHolders& eh ){
eh.oe.CancelOrder( pOrder->GetOrderId() );
} );
}
} // namespace sim
} // namespace tf
} // namespace ou