-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathOptionChainQuery.cpp
454 lines (378 loc) · 14.9 KB
/
OptionChainQuery.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/************************************************************************
* 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 <sstream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include "OptionChainQuery.h"
struct PreRoll {
enum class ECmd { CFU, CFO, CEO, S };
enum class EExtra { LC, ENDMSG, BADSYM, ERROR, V62 };
ECmd cmd;
std::string sSymbol;
EExtra extra;
};
using FuturesList = ou::tf::iqfeed::OptionChainQuery::FuturesList;
using OptionList = ou::tf::iqfeed::OptionChainQuery::OptionList;
BOOST_FUSION_ADAPT_STRUCT(
PreRoll,
(PreRoll::ECmd, cmd)
(std::string, sSymbol)
(PreRoll::EExtra, extra)
)
BOOST_FUSION_ADAPT_STRUCT(
FuturesList,
//(std::string, sKey)
(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vSymbol)
//(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vCall)
//(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vPut)
)
BOOST_FUSION_ADAPT_STRUCT(
OptionList,
//(std::string, sKey)
(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vSymbol) // calls first
(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vSymbol) // puts appended
//(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vCall)
//(ou::tf::iqfeed::OptionChainQuery::vSymbol_t, vPut)
)
namespace qi = boost::spirit::qi;
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
// ==
template<typename Iterator>
struct PreRollParser: qi::grammar<Iterator, PreRoll()> {
PreRollParser(): PreRollParser::base_type( start ) {
cmd.add
( "CFU", PreRoll::ECmd::CFU )
( "CFO", PreRoll::ECmd::CFO )
( "CEO", PreRoll::ECmd::CEO )
( "S", PreRoll::ECmd::S )
;
extra.add
( "LC", PreRoll::EExtra::LC )
( "n", PreRoll::EExtra::BADSYM )
( "E", PreRoll::EExtra::ERROR )
( "!ENDMSG!", PreRoll::EExtra::ENDMSG )
( "6.2", PreRoll::EExtra::V62 )
;
ruleCmd = cmd;
ruleExtra = extra;
ruleSymbol = +( qi::char_ - qi::char_( ',' ) );
start
%=
( ruleCmd >> qi::lit( '-' ) >> ruleSymbol >> qi::lit( ',' ) >> ruleExtra >> qi::lit( ',' ) )
| ( ruleCmd >> qi::lit( ',' ) >> ruleSymbol >> qi::lit( ',' ) >> ruleExtra )
;
}
qi::symbols<char,PreRoll::ECmd> cmd;
qi::symbols<char,PreRoll::EExtra> extra;
qi::rule<Iterator, PreRoll::ECmd()> ruleCmd;
qi::rule<Iterator, std::string()> ruleSymbol;
qi::rule<Iterator, PreRoll::EExtra()> ruleExtra;
qi::rule<Iterator, PreRoll()> start;
};
// ===
template<typename Iterator>
struct FutureChainParser: qi::grammar<Iterator, FuturesList()> {
FutureChainParser(): FutureChainParser::base_type( start ) {
symbol %= (+(qi::char_ - qi::char_(",:"))) >> qi::lit(',');
symbols %= +symbol;
start
%= symbols
//>> qi::eoi
;
}
qi::rule<Iterator, std::string()> symbol;
qi::rule<Iterator, OptionChainQuery::vSymbol_t()> symbols;
qi::rule<Iterator, FuturesList()> start;
};
// ===
template<typename Iterator>
struct OptionChainParser: qi::grammar<Iterator, OptionList()> {
OptionChainParser(): OptionChainParser::base_type( start ) {
symbol %= (+(qi::char_ - qi::char_(",:"))) >> qi::lit(',');
calls %= +symbol;
puts %= +symbol;
start
%= calls
>> -(qi::lit(',')) >> qi::lit(':') >> qi::lit(',')
>> puts
//>> qi::eoi
;
}
qi::rule<Iterator, std::string()> symbol;
qi::rule<Iterator, OptionChainQuery::vSymbol_t()> calls;
qi::rule<Iterator, OptionChainQuery::vSymbol_t()> puts;
qi::rule<Iterator, OptionList()> start;
};
// ==
// http://www.iqfeed.net/dev/api/docs/OptionChainsviaTCPIP.cfm
OptionChainQuery::OptionChainQuery(
fConnected_t&& fConnected
)
: Network<OptionChainQuery>( "127.0.0.1", 9100 ),
m_fConnected( std::move( fConnected ) ),
m_state( EState::quiescent )
{
assert( m_fConnected );
}
OptionChainQuery::~OptionChainQuery() {
}
void OptionChainQuery::Connect() {
ou::Network<OptionChainQuery>::Connect();
}
void OptionChainQuery::OnNetworkConnected() {
//std::cout << "OnHistoryConnected" << std::endl;
this->Send( "S,SET PROTOCOL,6.2\n" );
m_fConnected();
};
void OptionChainQuery::Disconnect() {
ou::Network<OptionChainQuery>::Disconnect();
}
void OptionChainQuery::OnNetworkDisconnected() {
}
void OptionChainQuery::OnNetworkError( size_t e ) {
}
void OptionChainQuery::OnNetworkSendDone() {
}
// 2021/10/29 - amusing response to a query via the new servers:
// "@ESZ21,grep: /data/online/data/commodities/options/underlying/@ESZ21: No such file or directory"
void OptionChainQuery::OnNetworkLineBuffer( linebuffer_t* buffer ) {
using const_iterator_t = linebuffer_t::const_iterator;
const_iterator_t iter = (*buffer).begin();
const_iterator_t end = (*buffer).end();
//std::string s( iter, end );
//std::cout << "chain response: " << s << std::endl;
bool bOk;
switch ( m_state ) {
case EState::quiescent:
//std::cout << "EState::quiescent" << std::endl;
// fall through, simply process all responses
case EState::response:
{
PreRoll preroll;
PreRollParser<const_iterator_t> grammarPreRoll;
iter = (*buffer).begin();
bOk = parse( iter, end, grammarPreRoll,preroll );
if ( bOk ) {
switch ( preroll.extra ) {
case PreRoll::EExtra::LC:
switch ( preroll.cmd ) {
case PreRoll::ECmd::CFU:
{
FuturesList list;
//std::cout << "EState::reply" << std::endl;
FutureChainParser<const_iterator_t> grammarFutureChain;
//const_iterator_t bgn = (*buffer).begin();
//std::string buf( iter, end );
//std::cout << "buf: '" << buf << "'" << std::endl;
bOk = parse( iter, end, grammarFutureChain, list );
assert( 0 < preroll.sSymbol.size() );
if ( bOk ) {
assert( 0 < preroll.sSymbol.size() );
}
else {
std::cout
<< "OptionChainQuery::OnNetworkLineBuffer CFU parse error: "
<< end - iter << ","
<< preroll.sSymbol
<< "'," << list.vSymbol.size()
<< std::endl;
}
bool bProcess( false );
mapFutures_t::const_iterator citer;
{
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
citer = m_mapFutures.find( preroll.sSymbol );
if ( m_mapFutures.end() == citer ) {
}
else {
bProcess = true;
}
}
if ( bProcess ) {
citer->second( list ); // this needs to be outside of lock
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
m_mapFutures.erase( citer );
}
else {
std::cout << "OptionChainQuery::OnNetworkLineBuffer error: can't find CFU key " << preroll.sSymbol << std::endl;
}
}
break;
case PreRoll::ECmd::CEO:
case PreRoll::ECmd::CFO:
{
OptionList list;
//std::cout << "EState::reply" << std::endl;
OptionChainParser<const_iterator_t> grammarOptionChain;
//const_iterator_t bgn = (*buffer).begin();
//std::string buf( iter, end );
//std::cout << "buf: '" << buf << "'" << std::endl;
bOk = parse( iter, end, grammarOptionChain, list );
assert( 0 < preroll.sSymbol.size() );
if ( bOk ) {
assert( 0 < preroll.sSymbol.size() );
list.sUnderlying = std::move( preroll.sSymbol );
}
else {
std::cout
<< "OptionChainQuery::OnNetworkLineBuffer CEO/CFO parse error: "
<< end - iter << ","
<< preroll.sSymbol
<< "'," << list.vSymbol.size()
<< std::endl;
}
bool bProcess( false );
mapOptions_t::const_iterator citer;
{
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
citer = m_mapOptions.find( list.sUnderlying );
if ( m_mapOptions.end() == citer ) {
}
else {
bProcess = true;
}
}
if ( bProcess ) {
citer->second( list ); // this needs to be outside of lock
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
m_mapOptions.erase( citer );
}
else {
std::cout << "OptionChainQuery::OnNetworkLineBuffer error: can't find CEO key " << list.sUnderlying << std::endl;
}
}
break;
case PreRoll::ECmd::S:
// ignore the response, simply an echo of version, maybe validate sometime
break;
}
break;
case PreRoll::EExtra::V62:
m_state = EState::quiescent;
break;
case PreRoll::EExtra::BADSYM:
// TODO: remove from m_mapRequest
std::cout << "OptionChainQuery::OnNetworkLineBuffer badsym: " << preroll.sSymbol << std::endl;
m_state = EState::quiescent;
break;
case PreRoll::EExtra::ERROR:
// TODO: remove from m_mapRequest
std::cout << "OptionChainQuery::OnNetworkLineBuffer error: " << std::string( (*buffer).begin(), (*buffer).end() ) << std::endl;
m_state = EState::quiescent;
break;
case PreRoll::EExtra::ENDMSG:
m_state = EState::quiescent;
break;
}
}
else {
std::cout << "OptionChainQuery::OnNetworkLineBuffer error: unknown response: " << std::string( (*buffer).begin(), (*buffer).end() ) << std::endl;
}
}
break;
case EState::done:
{
m_state = EState::quiescent;
}
break;
}
GiveBackBuffer( buffer );
}
void OptionChainQuery::QueryFuturesChain(
const std::string& sSymbol,
const std::string& sMonthCodes,
const std::string& sYears,
const std::string& sNearMonths,
fFuturesList_t&& fFuturesList
) {
assert( 0 < sSymbol.size() );
assert( std::string::npos == sSymbol.find( ',' ) );
std::stringstream ss;
ss
<< "CFU,"
<< sSymbol << ","
<< sMonthCodes << ","
<< sYears << ","
<< sNearMonths << ","
<< "CFU-" << sSymbol
<< "\n";
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
m_mapFutures.emplace( mapFutures_t::value_type( sSymbol, std::move( fFuturesList ) ) );
m_state = EState::response;
this->Send( ss.str().c_str() );
}
void OptionChainQuery::QueryFuturesOptionChain(
const std::string& sSymbol,
const std::string& sSide,
const std::string& sMonthCodes,
const std::string& sYears,
const std::string& sNearMonths,
fOptionList_t&& fOptionList
) {
assert( 0 < sSymbol.size() );
assert( std::string::npos == sSymbol.find( ',' ) );
std::stringstream ss;
ss
<< "CFO,"
<< sSymbol << ","
<< sSide << ","
<< sMonthCodes << ","
<< sYears << ","
<< sNearMonths << ","
<< "CFO-" << sSymbol
;
std::cout << "request: '" << ss.str() << "'" << std::endl; // for diagnostics
ss << "\n";
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
m_mapOptions.emplace( mapOptions_t::value_type( sSymbol, std::move( fOptionList ) ) );
m_state = EState::response;
this->Send( ss.str().c_str() );
}
void OptionChainQuery::QueryEquityOptionChain(
const std::string& sSymbol,
const std::string& sSide,
const std::string& sMonthCodes, // see above
const std::string& sNearMonths, // number of near contracts to display: values 0 through 4
const std::string& sFilterType, // Optional - "0" (default) = no filter or "1" = filter on a strike range or "2" = filter on the number of In/Out Of The Money contracts
const std::string& sFilterOne, // Ignored if [Filter Type] is "0". If [Filter Type] = "1" then beginning strike price or if [Filter Type] = "2" then the number of contracts in the money
const std::string& sFilterTwo, // Ignored if [Filter Type] is "0". If [Filter Type] = "1" then ending strike price or if [Filter Type] = "2" then the number of contracts out of the money // suggest 12
fOptionList_t&& fOptionList
) {
assert( 0 < sSymbol.size() );
assert( std::string::npos == sSymbol.find( ',' ) );
std::stringstream ss;
ss
<< "CEO,"
<< sSymbol << ","
<< sSide << ","
<< sMonthCodes << ","
<< sNearMonths << ","
<< sFilterType << ","
<< sFilterOne << ","
<< sFilterTwo << ","
<< "CEO-" << sSymbol << ","
<< "1" // 0 = default, exclude non-standard options, 1 = include
;
std::cout << "request: '" << ss.str() << "'" << std::endl; // for diagnostics
ss << "\n";
std::scoped_lock<std::mutex> lock( m_mutexMapRequest );
m_mapOptions.emplace( mapOptions_t::value_type( sSymbol, std::move( fOptionList ) ) );
m_state = EState::response;
this->Send( ss.str().c_str() );
}
} // namespace iqfeed
} // namespace tf
} // namespace ou