-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathProvider.cpp
366 lines (328 loc) · 13.3 KB
/
Provider.cpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* email: [email protected] *
* *
* 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. *
************************************************************************/
#include <boost/lexical_cast.hpp>
#include <TFTrading/KeyTypes.h>
#include <TFTrading/OrderManager.h>
#include "Provider.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
Provider::Provider()
: ou::tf::sim::SimulationInterface<Provider,IQFeedSymbol>()
, IQFeed<Provider>()
{
m_sName = "IQFeed";
m_nID = keytypes::EProviderIQF;
m_bProvidesQuotes = true;
m_bProvidesTrades = true;
m_bProvidesDepths = true;
m_bProvidesBrokerInterface = true; // simulated trades
m_bExecutionEnabled = false; // true required for simulating trades
}
Provider::~Provider() {
}
void Provider::EnableExecution( bool bEnable ) {
if ( bEnable ) {
assert( 0 == MonitoredSymbolsCount() ); // at startup only, when no symbols are watched
}
m_bExecutionEnabled = bEnable;
}
void Provider::Connect() {
if ( !m_bConnected ) {
ProviderInterfaceBase::OnConnecting( 0 );
inherited_t::Connect();
IQFeed_t::Connect();
}
}
void Provider::OnIQFeedConnected() {
m_bConnected = true;
inherited_t::ConnectionComplete();
ProviderInterfaceBase::OnConnected( 0 );
}
void Provider::Disconnect() {
if ( m_bConnected ) {
ProviderInterfaceBase::OnDisconnecting( 0 ); // watches are regsitered here
inherited_t::Disconnecting(); // provider then cleans up
IQFeed_t::Disconnect();
inherited_t::Disconnect();
}
}
void Provider::OnIQFeedDisConnected() {
m_bConnected = false;
ProviderInterfaceBase::OnDisconnected( 0 );
}
void Provider::OnIQFeedError( size_t e ) {
OnError( e );
}
Provider::pSymbol_t Provider::NewCSymbol( pInstrument_t pInstrument ) {
pSymbol_t pSymbol( new IQFeedSymbol( pInstrument->GetInstrumentName( ID() ), pInstrument ) );
inherited_t::AddCSymbol( pSymbol );
return pSymbol;
}
namespace {
static const char transition[4][4] = {
/* from to None Quote Trade Both*/
/* WatchState::None */ { '-', 'w', 't', 'w' },
/* WatchState::Quote */ { 'r', '-', 't', '-' },
/* WatchState::Trade */ { 'r', 'w', '-', 'w' },
/* WatchState::Both */ { 'r', '-', 't', '-' }
}; // - = no change, w = watch, t = trades only, r = reset
// reverse diagonal is illegal as it includes two simultaneous watch changes
}
void Provider::UpdateQuoteTradeWatch( char command, IQFeedSymbol::WatchState next, IQFeedSymbol* pSymbol ) {
if ( '-' != command ) {
std::string s = command + pSymbol->GetId() + "\n";
//std::cout << command + pSymbol->GetId() << std::endl;
IQFeed<Provider>::Send( s );
}
pSymbol->SetWatchState( next );
}
void Provider::StartQuoteWatch( pSymbol_t pSymbol ) {
IQFeedSymbol::WatchState current = pSymbol->GetWatchState();
IQFeedSymbol::WatchState next = IQFeedSymbol::WatchState::None;
switch ( current ) {
case IQFeedSymbol::WatchState::None:
next = IQFeedSymbol::WatchState::WSQuote;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::WSQuote:
// nothing to do
break;
case IQFeedSymbol::WatchState::WSTrade:
next = IQFeedSymbol::WatchState::Both;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::Both:
// nothing to do
break;
}
}
void Provider::StopQuoteWatch(pSymbol_t pSymbol) {
IQFeedSymbol::WatchState current = pSymbol->GetWatchState();
IQFeedSymbol::WatchState next = IQFeedSymbol::WatchState::None;
switch ( current ) {
case IQFeedSymbol::WatchState::None:
std::cout << "iqfeed::Provider::StopQuoteWatch error with None: " << pSymbol->GetId() << std::endl;
break;
case IQFeedSymbol::WatchState::WSQuote:
next = IQFeedSymbol::WatchState::None;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::WSTrade:
std::cout << "iqfeed::Provider::StopQuoteWatch error with Trade: " << pSymbol->GetId() << std::endl;
break;
case IQFeedSymbol::WatchState::Both:
next = IQFeedSymbol::WatchState::WSTrade;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
}
}
void Provider::StartTradeWatch(pSymbol_t pSymbol) {
IQFeedSymbol::WatchState current = pSymbol->GetWatchState();
IQFeedSymbol::WatchState next = IQFeedSymbol::WatchState::None;
switch ( current ) {
case IQFeedSymbol::WatchState::None:
next = IQFeedSymbol::WatchState::WSTrade;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::WSQuote:
next = IQFeedSymbol::WatchState::Both;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::WSTrade:
// nothing to do
break;
case IQFeedSymbol::WatchState::Both:
// nothing to do
break;
}
}
void Provider::StopTradeWatch(pSymbol_t pSymbol) {
IQFeedSymbol::WatchState current = pSymbol->GetWatchState();
IQFeedSymbol::WatchState next = IQFeedSymbol::WatchState::None;
switch ( current ) {
case IQFeedSymbol::WatchState::None:
std::cout << "iqfeed::Provider::StopTradeWatch error with None: " << pSymbol->GetId() << std::endl;
break;
case IQFeedSymbol::WatchState::WSQuote:
std::cout << "iqfeed::Provider::StopTradeWatch error with Quote: " << pSymbol->GetId() << std::endl;
break;
case IQFeedSymbol::WatchState::WSTrade:
next = IQFeedSymbol::WatchState::None;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
case IQFeedSymbol::WatchState::Both:
next = IQFeedSymbol::WatchState::WSQuote;
UpdateQuoteTradeWatch( transition[current][next], next, dynamic_cast<IQFeedSymbol*>( pSymbol.get() ) );
break;
}
}
void Provider::OnIQFeedDynamicFeedUpdateMessage( linebuffer_t* pBuffer, IQFDynamicFeedUpdateMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
auto field = pMsg->Field( IQFDynamicFeedSummaryMessage::DFSymbol );
mapSymbols_iter = m_mapSymbols.find( field );
if ( m_mapSymbols.end() != mapSymbols_iter ) {
pSymbol_t pSym = mapSymbols_iter -> second;
pSym ->HandleDynamicFeedUpdateMessage( pMsg );
}
else {
std::cout << "field " << field << " update not found" << std::endl;
}
this->DynamicFeedUpdateDone( pBuffer, pMsg );
}
void Provider::OnIQFeedDynamicFeedSummaryMessage( linebuffer_t* pBuffer, IQFDynamicFeedSummaryMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
auto field = pMsg->Field( IQFDynamicFeedSummaryMessage::DFSymbol );
mapSymbols_iter = m_mapSymbols.find( field );
if ( m_mapSymbols.end() != mapSymbols_iter ) {
pSymbol_t pSym = mapSymbols_iter -> second;
pSym ->HandleDynamicFeedSummaryMessage( pMsg );
}
else {
std::cout << "field " << field << " summary not found" << std::endl;
}
this->DynamicFeedSummaryDone( pBuffer, pMsg );
}
void Provider::OnIQFeedUpdateMessage( linebuffer_t* pBuffer, IQFUpdateMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
mapSymbols_iter = m_mapSymbols.find( pMsg->Field( IQFUpdateMessage::QPSymbol ) );
pSymbol_t pSym;
if ( m_mapSymbols.end() != mapSymbols_iter ) {
pSym = mapSymbols_iter -> second;
pSym ->HandleUpdateMessage( pMsg );
}
this->UpdateDone( pBuffer, pMsg );
}
void Provider::OnIQFeedSummaryMessage( linebuffer_t* pBuffer, IQFSummaryMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
mapSymbols_iter = m_mapSymbols.find( pMsg->Field( IQFSummaryMessage::QPSymbol ) );
pSymbol_t pSym;
if ( m_mapSymbols.end() != mapSymbols_iter ) {
pSym = mapSymbols_iter -> second;
pSym ->HandleSummaryMessage( pMsg );
}
this->SummaryDone( pBuffer, pMsg );
}
void Provider::OnIQFeedFundamentalMessage( linebuffer_t* pBuffer, IQFFundamentalMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
mapSymbols_iter = m_mapSymbols.find( pMsg->Field( IQFFundamentalMessage::FSymbol ) );
pSymbol_t pSym;
if ( m_mapSymbols.end() != mapSymbols_iter ) {
pSym = mapSymbols_iter -> second;
pSym ->HandleFundamentalMessage(
pMsg,
[this](int nSecurityType )->ESecurityType { return LookupSecurityType( nSecurityType ); },
[this](std::string sExchangeId)->std::string{ // supplied string is in hex
int n {};
int t {};
for ( std::string::iterator iter = sExchangeId.begin(); iter != sExchangeId.end(); iter++ ) {
n = n << 4;
char cur = *iter;
if ( ( 'A' <= cur ) && ( 'F' >= cur ) ) {
t = cur - 'A' + 10;
}
else {
if ( ( 'a' <= cur ) && ( 'f' >= cur ) ) {
t = cur - 'a' + 10;
}
else {
if ( ( '0' <= cur ) && ( '9' >= cur ) ) {
t = cur - '0';
}
}
}
n += t;
}
return LookupListedMarket( n );
}
);
}
this->FundamentalDone( pBuffer, pMsg );
}
void Provider::OnIQFeedNewsMessage( linebuffer_t* pBuffer, IQFNewsMessage *pMsg ) {
inherited_t::mapSymbols_t::iterator mapSymbols_iter;
/*
const char *ixFstColon = pMsg->m_sSymbolList.c_str();
const char *ixLstColon = pMsg->m_sSymbolList.c_str();
string s;
__w64 int cnt;
if ( 0 != *ixLstColon ) {
do {
// each symbol has a surrounding set of colons
if ( ':' == *ixLstColon ) {
if ( ( ixLstColon - ixFstColon ) > 1 ) {
// extract symbol
cnt = ixLstColon - ixFstColon - 1;
s.assign( ++ixFstColon, cnt );
m_mapSymbols_Iter = m_mapSymbols.find( s.c_str() );
IQFeedSymbol *pSym;
if ( m_mapSymbols.end() != m_mapSymbols_Iter ) {
pSym = (IQFeedSymbol *) m_mapSymbols_Iter -> second;
pSym ->HandleNewsMessage( pMsg );
}
ixFstColon = ixLstColon;
}
else {
if ( 1 == ( ixLstColon - ixFstColon ) ) {
// no symbol, move FstColon
ixFstColon = ixLstColon;
}
}
}
ixLstColon++;
} while ( 0 != *ixLstColon );
}
*/
this->NewsDone( pBuffer, pMsg );
}
void Provider::OnIQFeedTimeMessage( linebuffer_t* pBuffer, IQFTimeMessage *pMsg ) {
//map<string, CSymbol*>::iterator m_mapSymbols_Iter;
this->TimeDone( pBuffer, pMsg );
}
void Provider::OnIQFeedSystemMessage( linebuffer_t* pBuffer, IQFSystemMessage *pMsg ) {
//map<string, CSymbol*>::iterator m_mapSymbols_Iter;
this->SystemDone( pBuffer, pMsg );
}
void Provider::HandleExecution( Order::idOrder_t orderId, const Execution &exec ) {
OrderManager::LocalCommonInstance().ReportExecution( orderId, exec );
}
void Provider::HandleCommission( Order::idOrder_t orderId, double commission ) {
OrderManager::LocalCommonInstance().ReportCommission( orderId, commission );
}
void Provider::HandleCancellation( Order::idOrder_t orderId ) {
OrderManager::LocalCommonInstance().ReportCancellation( orderId );
}
//void Provider::AddQuoteHandler( pInstrument_cref pInstrument, Provider::quotehandler_t handler ) {
// if ( m_bExecutionEnabled ) { // this isn't correct
// inherited_t::AddQuoteHandler( pInstrument, handler );
// }
//}
//void Provider::RemoveQuoteHandler( pInstrument_cref pInstrument, Provider::quotehandler_t handler ) {
// if ( m_bExecutionEnabled ) { // this isn't correct
// inherited_t::RemoveQuoteHandler( pInstrument, handler );
// }
//}
//void Provider::AddTradeHandler( pInstrument_cref pInstrument, Provider::tradehandler_t handler ) {
// if ( m_bExecutionEnabled ) { // this isn't correct
// inherited_t::AddTradeHandler( pInstrument, handler );
// }
//}
//void Provider::RemoveTradeHandler( pInstrument_cref pInstrument, Provider::tradehandler_t handler ) {
// if ( m_bExecutionEnabled ) { // this isn't correct
// inherited_t::RemoveTradeHandler( pInstrument, handler );
// }
//}
} // namespace iqfeed
} // namespace tf
} // namespace ou