-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathSymbol.cpp
359 lines (303 loc) · 14.3 KB
/
Symbol.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
/************************************************************************
* 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/log/trivial.hpp>
#include <boost/asio/post.hpp>
#include <OUCommon/TimeSource.h>
#include <TFTrading/MacroStrand.h>
#include "Symbol.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
IQFeedSymbol::IQFeedSymbol( const idSymbol_t& sSymbol, pInstrument_t pInstrument )
: Symbol<IQFeedSymbol>( pInstrument, sSymbol )
, m_cnt( 0 )
, m_QStatus( qUnknown )
, m_stateWatch( WatchState::None )
, m_bWaitForFirstQuote( true )
{
m_pFundamentals = std::make_shared<Fundamentals>();
m_pSummary = std::make_shared<Summary>();
}
IQFeedSymbol::~IQFeedSymbol() {
}
void IQFeedSymbol::HandleFundamentalMessage(
IQFFundamentalMessage *pMsg,
fLookupSecurityType_t&& fLookupSecurityType,
fLookupListedMarket_t&& fLookupListedMarket
) {
Fundamentals& fundamentals( *m_pFundamentals );
fundamentals.sSymbolName = pMsg->Field( IQFFundamentalMessage::FSymbol );
fundamentals.sCompanyName = pMsg->Field( IQFFundamentalMessage::FCompanyName );
fundamentals.sExchangeRoot = pMsg->Field( IQFFundamentalMessage::FExchangeRoot );
fundamentals.sOptionRoots = pMsg->Field( IQFFundamentalMessage::FRootOptionSymbols );
fundamentals.sExchange = fLookupListedMarket( pMsg->Field( IQFFundamentalMessage::fExchangeID ) );
fundamentals.nPrecision = pMsg->Integer( IQFFundamentalMessage::FPrecision );
fundamentals.nFormatCode = pMsg->Integer( IQFFundamentalMessage::FFormatCode );
fundamentals.nContractSize = pMsg->Integer( IQFFundamentalMessage::FContractSize );
fundamentals.nAverageVolume = pMsg->Integer( IQFFundamentalMessage::FAveVolume );
fundamentals.nSIC = pMsg->Integer( IQFFundamentalMessage::FSIC );
fundamentals.nNAICS = pMsg->Integer( IQFFundamentalMessage::FNAICS );
fundamentals.eSecurityType = fLookupSecurityType( pMsg->Integer( IQFFundamentalMessage::FSecurityType ) );
fundamentals.dblHistoricalVolatility = pMsg->Double( IQFFundamentalMessage::FVolatility );
fundamentals.dblStrikePrice = pMsg->Double( IQFFundamentalMessage::FStrikePrice );
fundamentals.dblPriceEarnings = pMsg->Double( IQFFundamentalMessage::FPriceEarnings );
fundamentals.dblAssets = pMsg->Double( IQFFundamentalMessage::FCurAssets );
fundamentals.dblLiabilities = pMsg->Double( IQFFundamentalMessage::FCurLiab );
fundamentals.dblCommonSharesOutstanding = pMsg->Double( IQFFundamentalMessage::FCommonShares );
fundamentals.dbl52WkHi = pMsg->Double( IQFFundamentalMessage::F52WkHi );
fundamentals.dbl52WkLo = pMsg->Double( IQFFundamentalMessage::F52WkLo );
fundamentals.dblDividendAmount = pMsg->Double( IQFFundamentalMessage::FDivAmt );
fundamentals.dblDividendRate = pMsg->Double( IQFFundamentalMessage::FDivRate );
fundamentals.dblDividendYield = pMsg->Double( IQFFundamentalMessage::FDivYld );
fundamentals.dblTickSize = pMsg->Double( IQFFundamentalMessage::FMinimumTickSize );
fundamentals.datePayed = pMsg->Date( IQFFundamentalMessage::FDivPayDate );
fundamentals.dateExDividend = pMsg->Date( IQFFundamentalMessage::FDivExDate );
fundamentals.dateExpiration = pMsg->Date( IQFFundamentalMessage::FExpirationDate );
fundamentals.timeSessionOpen = pMsg->Time( IQFFundamentalMessage::FSessionOpenTime );
fundamentals.timeSessionClose = pMsg->Time( IQFFundamentalMessage::FSessionCloseTime );
switch ( fundamentals.eSecurityType ) {
case ESecurityType::IEOption:
case ESecurityType::FOption:
{
const std::string& symbol( fundamentals.sSymbolName );
// assumes strike is last part of name
for ( std::string::const_reverse_iterator iter = symbol.rbegin(); iter != symbol.rend(); iter++ ) {
if ( ( '.' == *iter ) || ( ('0' <= *iter ) && ( '9' >= *iter ) ) ) {}
else {
if ( 'L' >= *iter ) fundamentals.eOptionSide = ou::tf::OptionSide::EOptionSide::Call;
if ( 'M' <= *iter ) fundamentals.eOptionSide = ou::tf::OptionSide::EOptionSide::Put;
break;
}
}
}
break;
default: {}
}
STRAND( OnFundamentalMessage( m_pFundamentals ) )
}
template <typename T>
void IQFeedSymbol::DecodeDynamicFeedMessage( IQFDynamicFeedMessage<T>* pMsg ) {
// http://www.iqfeed.net/dev/api/docs/Level1UpdateSummaryMessage.cfm
double dblOpen, dblBid, dblAsk;
int nBidSize, nAskSize;
Summary& summary( *m_pSummary );
summary.bNewTrade = summary.bNewQuote = summary.bNewOpen = false;
std::string content = pMsg->Field( IQFDynamicFeedMessage<T>::DFMessageContents );
for ( const char id: content ) {
switch ( id ) {
case 'C':
summary.dblTrade = pMsg->Double( IQFDynamicFeedMessage<T>::DFMostRecentTrade );
summary.nTradeSize = pMsg->Integer( IQFDynamicFeedMessage<T>::DFMostRecentTradeSize );
summary.cntTrades = pMsg->Integer( IQFDynamicFeedMessage<T>::DFNumTrades );
summary.nTotalVolume = pMsg->Integer( IQFDynamicFeedMessage<T>::DFTtlVol );
summary.bNewTrade = true;
break;
case 'a':
dblAsk = pMsg->Double( IQFDynamicFeedMessage<T>::DFAsk );
if ( summary.dblAsk != dblAsk ) { summary.dblAsk = dblAsk; summary.bNewQuote = true; }
nAskSize = pMsg->Integer( IQFDynamicFeedMessage<T>::DFAskSize );
if ( summary.nAskSize != nAskSize ) { summary.nAskSize = nAskSize; summary.bNewQuote = true; }
break;
case 'b':
dblBid = pMsg->Double( IQFDynamicFeedMessage<T>::DFBid );
if ( summary.dblBid != dblBid ) { summary.dblBid = dblBid; summary.bNewQuote = true; }
nBidSize = pMsg->Integer( IQFDynamicFeedMessage<T>::DFBidSize );
if ( summary.nBidSize != nBidSize ) { summary.nBidSize = nBidSize; summary.bNewQuote = true; }
break;
case 'o':
// TODO: may not be using the correct field here.
dblOpen = pMsg->Double( IQFDynamicFeedMessage<T>::DFMostRecentTrade );
if ( ( summary.dblOpen != dblOpen ) && ( 0 != dblOpen ) ) {
summary.dblOpen = dblOpen;
summary.bNewOpen = true;
//BOOST_LOG_TRIVIAL(info)
// << "IQF new open 1: " << GetId() << "=" << summary.dblOpen;
};
break;
case 'E':
// will need to supply fields
break;
case 'O': // any non C,E trade
break;
case 'v': // volume update
summary.nOpenInterest = pMsg->Integer( IQFDynamicFeedMessage<T>::DFOpenInterest );
break;
}
}
if ( m_bWaitForFirstQuote ) {
if ( summary.bNewQuote ) {
if ( ( -1 == summary.nBidSize ) || ( -1 == summary.nAskSize ) ) {
summary.bNewQuote = false;
}
else {
m_bWaitForFirstQuote = false;
}
}
}
}
template <typename T>
void IQFeedSymbol::DecodePricingMessage( IQFPricingMessage<T>* pMsg ) {
Summary& summary( *m_pSummary );
summary.bNewTrade = summary.bNewQuote = summary.bNewOpen = false;
char chType;
ptime dtLastTrade;
double dblOpen, dblBid, dblAsk;
int nBidSize, nAskSize;
std::string sLastTradeTime = pMsg->Field( IQFPricingMessage<T>::QPLastTradeTime );
if ( sLastTradeTime.length() > 0 ) {
chType = sLastTradeTime[ sLastTradeTime.length() - 1 ];
}
else {
chType = 'q';
}
// TODO: test that data file is available
summary.dtLastTrade = pMsg->LastTradeTime();
switch ( chType ) {
case 't':
case 'T':
summary.dblTrade = pMsg->Double( IQFPricingMessage<T>::QPLast );
summary.dblChange = pMsg->Double( IQFPricingMessage<T>::QPChange );
summary.nTotalVolume = pMsg->Integer( IQFPricingMessage<T>::QPTtlVol );
summary.nTradeSize = pMsg->Integer( IQFPricingMessage<T>::QPLastVol );
summary.dblHigh = pMsg->Double( IQFPricingMessage<T>::QPHigh );
summary.dblLow = pMsg->Double( IQFPricingMessage<T>::QPLow );
summary.dblClose = pMsg->Double( IQFPricingMessage<T>::QPClose );
summary.cntTrades = pMsg->Integer( IQFPricingMessage<T>::QPNumTrades );
summary.bNewTrade = true;
dblOpen = pMsg->Double( IQFPricingMessage<T>::QPOpen );
if ( ( summary.dblOpen != dblOpen ) && ( 0 != dblOpen ) ) {
summary.dblOpen = dblOpen;
summary.bNewOpen = true;
BOOST_LOG_TRIVIAL(info)
<< "IQF new open 2: " << GetId() << "=" << summary.dblOpen;
};
summary.nOpenInterest = pMsg->Integer( IQFPricingMessage<T>::QPOpenInterest );
// fall through to processing bid / ask
case 'q':
case 'b':
case 'a':
dblBid = pMsg->Double( IQFPricingMessage<T>::QPBid );
if ( summary.dblBid != dblBid ) { summary.dblBid = dblBid; summary.bNewQuote = true; }
nBidSize = pMsg->Integer( IQFPricingMessage<T>::QPBidSize );
if ( summary.nBidSize != nBidSize ) { summary.nBidSize = nBidSize; summary.bNewQuote = true; }
dblAsk = pMsg->Double( IQFPricingMessage<T>::QPAsk );
if ( summary.dblAsk != dblAsk ) { summary.dblAsk = dblAsk; summary.bNewQuote = true; }
nAskSize = pMsg->Integer( IQFPricingMessage<T>::QPAskSize );
if ( summary.nAskSize != nAskSize ) { summary.nAskSize = nAskSize; summary.bNewQuote = true; }
break;
case 'o':
break;
default:
BOOST_LOG_TRIVIAL(error)
<< "IQFeedSymbol::DecodePricingMessage: " << this->m_pInstrument->GetInstrumentName() << " Unknown price type: " << chType;
}
// }
if ( false ) {
std::cout
<< m_pInstrument->GetInstrumentName()
<< "," << chType
<< ",t=" << summary.dblTrade
<< ",oi=" << summary.nOpenInterest
<< ",b=" << summary.dblBid
<< ",a=" << summary.dblAsk
<< ",#=" << summary.cntTrades
<< std::endl;
}
}
void IQFeedSymbol::HandleSummaryMessage( IQFSummaryMessage* pMsg ) {
DecodePricingMessage<IQFSummaryMessage>( pMsg );
STRAND( OnSummaryMessage( m_pSummary ) )
Summary& summary( *m_pSummary );
if ( summary.bNewQuote ) { // before or after OnSummaryMessage? UpdateMessage has it after
ptime dt( ou::TimeSource::GlobalInstance().External() );
Quote quote( dt, summary.dblBid, summary.nBidSize, summary.dblAsk, summary.nAskSize );
STRAND_CAPTURE( (Symbol::m_OnQuote( quote )), quote )
}
}
void IQFeedSymbol::HandleUpdateMessage( IQFUpdateMessage* pMsg ) {
if ( qUnknown == m_QStatus ) {
m_QStatus = ( "Not Found" == pMsg->Field( IQFPricingMessage<IQFUpdateMessage>::QPLast ) ) ? qNotFound : qFound;
if ( qNotFound == m_QStatus ) {
BOOST_LOG_TRIVIAL(error)
<< "IQFeedSymbol::HandleUpdateMessage: " << GetId() << " not found";
}
}
if ( qFound == m_QStatus ) {
DecodePricingMessage<IQFUpdateMessage>( pMsg );
STRAND( OnUpdateMessage( m_pSummary ) )
Summary& summary( *m_pSummary );
//ptime dt( microsec_clock::local_time() );
ptime dt( ou::TimeSource::GlobalInstance().External() );
// quote needs to be sent before the trade
if ( summary.bNewQuote ) {
const Quote quote( dt, summary.dblBid, summary.nBidSize, summary.dblAsk, summary.nAskSize );
STRAND_CAPTURE( (Symbol::m_OnQuote( quote )), quote )
}
if ( summary.bNewTrade ) {
Trade trade( dt, summary.dblTrade, summary.nTradeSize );
STRAND_CAPTURE( (Symbol::m_OnTrade( trade )), trade)
if ( summary.bNewOpen ) {
STRAND_CAPTURE( (Symbol::m_OnOpen( trade )), trade)
}
}
}
}
void IQFeedSymbol::HandleDynamicFeedSummaryMessage( IQFDynamicFeedSummaryMessage* pMsg ) {
DecodeDynamicFeedMessage<IQFDynamicFeedSummaryMessage>( pMsg );
STRAND( OnSummaryMessage( m_pSummary ) )
Summary& summary( *m_pSummary );
if ( summary.bNewQuote ) { // before or after OnSummaryMessage? UpdateMessage has it after
ptime dt( ou::TimeSource::GlobalInstance().External() );
Quote quote( dt, summary.dblBid, summary.nBidSize, summary.dblAsk, summary.nAskSize );
STRAND_CAPTURE( (Symbol::m_OnQuote( quote )), quote )
}
}
void IQFeedSymbol::HandleDynamicFeedUpdateMessage( IQFDynamicFeedUpdateMessage* pMsg ) {
// if ( qUnknown == m_QStatus ) {
// m_QStatus = ( "Not Found" == pMsg->Field( IQFPricingMessage<IQFUpdateMessage>::QPLast ) ) ? qNotFound : qFound;
// if ( qNotFound == m_QStatus ) {
// std::cout << GetId() << " not found" << std::endl;
// }
// }
// if ( qFound == m_QStatus ) {
DecodeDynamicFeedMessage<IQFDynamicFeedUpdateMessage>( pMsg );
STRAND( OnUpdateMessage( m_pSummary ) )
Summary& summary( *m_pSummary );
//ptime dt( microsec_clock::local_time() );
ptime dt( ou::TimeSource::GlobalInstance().External() );
// quote needs to be sent before the trade
if ( summary.bNewQuote ) {
const Quote quote( dt, summary.dblBid, summary.nBidSize, summary.dblAsk, summary.nAskSize );
STRAND_CAPTURE( (Symbol::m_OnQuote( quote )), quote )
}
if ( summary.bNewTrade ) {
Trade trade( dt, summary.dblTrade, summary.nTradeSize );
STRAND_CAPTURE( (Symbol::m_OnTrade( trade )), trade )
if ( summary.bNewOpen ) {
STRAND_CAPTURE( (Symbol::m_OnOpen( trade )), trade )
}
}
// }
}
void IQFeedSymbol::HandleNewsMessage( IQFNewsMessage* pMsg ) {
}
void IQFeedSymbol::SubmitMarketDepthByMM( const ou::tf::DepthByMM& md ) {
STRAND_CAPTURE( (Symbol::m_OnDepthByMM( md )), md )
}
void IQFeedSymbol::SubmitMarketDepthByOrder( const ou::tf::DepthByOrder& md ) {
STRAND_CAPTURE( (Symbol::m_OnDepthByOrder( md )), md )
}
} // namespace iqfeed
} // namespace tf
} // namespace ou