-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathParseOptionSymbol.h
219 lines (167 loc) · 6.91 KB
/
ParseOptionSymbol.h
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
/************************************************************************
* Copyright(c) 2013, 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. *
************************************************************************/
#pragma once
// Started 2013/07/12
// This parser is used for OPTION and FOPTION symbol parsing
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/spirit/include/qi_repeat.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/stl.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
struct structParsedOptionSymbol1 {
std::string sText;
std::string sDigits;
std::string sCode;
double dblStrike;
structParsedOptionSymbol1( void ) : dblStrike( 0.0 ) {};
};
struct structParsedOptionSymbol2 {
boost::uint16_t nYear;
boost::uint8_t nDay;
structParsedOptionSymbol2( void ) : nYear( 0 ), nDay( 0 ) {};
};
struct structParsedOptionSymbol3 {
std::string sText;
std::string sMonth;
boost::uint16_t nYear;
std::string sCode;
double dblStrike;
structParsedOptionSymbol3( void ) : dblStrike( 0.0 ), nYear( 0 ) {};
};
} // namespace iqfeed
} // namespace tf
} // namespace ou
using pos1_t = ou::tf::iqfeed::structParsedOptionSymbol1;
BOOST_FUSION_ADAPT_STRUCT(
pos1_t,
(std::string, sText)
(std::string, sDigits)
(std::string, sCode)
(double, dblStrike)
)
using pos2_t = ou::tf::iqfeed::structParsedOptionSymbol2;
BOOST_FUSION_ADAPT_STRUCT(
pos2_t,
(boost::uint16_t, nYear)
(boost::uint8_t, nDay)
)
using pos3_t = ou::tf::iqfeed::structParsedOptionSymbol3; // Future Options
BOOST_FUSION_ADAPT_STRUCT(
pos3_t,
(std::string, sText)
(std::string, sMonth)
(boost::uint16_t, nYear)
(std::string, sCode)
(double, dblStrike)
)
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
using uint2_p = qi::uint_parser<unsigned, 10, 2, 2>;
template<typename Iterator>
struct OptionSymbolParser1: qi::grammar<Iterator, pos1_t()> {
OptionSymbolParser1( void ): OptionSymbolParser1::base_type(start) {
// define option processing rules
ruleText %= +qi::char_( "A-Z" );
ruleDigits %= +qi::char_( "0-9" );
ruleCode %= qi::char_( "A-X" );
ruleStrike %= qi::float_;
start %= ruleText >> ruleDigits >> ruleCode >> ruleStrike;
}
qi::rule<Iterator, std::string()> ruleText; // symbol
qi::rule<Iterator, std::string()> ruleDigits; // yydd
qi::rule<Iterator, std::string()> ruleCode; // month and call/put
qi::rule<Iterator, double()> ruleStrike; // strike
qi::rule<Iterator, pos1_t()> start;
};
template<typename Iterator>
struct OptionSymbolParser2: qi::grammar<Iterator, pos2_t()> {
OptionSymbolParser2( void ): OptionSymbolParser2::base_type(start) {
// define option processing rules
ruleYear %= uint2_p();
ruleDay %= uint2_p();
start %= ruleYear >> ruleDay;
}
qi::rule<Iterator, boost::uint16_t()> ruleYear;
qi::rule<Iterator, boost::uint8_t()> ruleDay;
qi::rule<Iterator, pos2_t()> start;
};
template<typename Iterator>
struct FOptionSymbolParser1: qi::grammar<Iterator, pos1_t()> {
FOptionSymbolParser1( void ): FOptionSymbolParser1::base_type(start) {
// define option processing rules
ruleText %= +qi::char_( "@A-Z" );
ruleDigits %= +qi::char_( "0-9" );
ruleCode %= qi::char_( "CP" );
ruleStrike %= qi::float_;
start %= ruleText >> ruleDigits >> ruleCode >> ruleStrike;
}
qi::rule<Iterator, std::string()> ruleText; // symbol
qi::rule<Iterator, std::string()> ruleDigits; // yydd
qi::rule<Iterator, std::string()> ruleCode; // month and call/put
qi::rule<Iterator, double()> ruleStrike; // strike
qi::rule<Iterator, pos1_t()> start;
};
template<typename Iterator>
struct FOptionSymbolParser3: qi::grammar<Iterator, pos3_t()> {
FOptionSymbolParser3( void ): FOptionSymbolParser3::base_type(start) {
// define option processing rules
ruleMonth %= qi::char_( "FGHJKMNQUVXZ" );
ruleSymbol0 %= qi::char_( "0-9A-Z" );
ruleSymbol4 %= ruleSymbol0 >> ruleSymbol0 >> ruleSymbol0 >> ruleSymbol0;
ruleSymbol3 %= ruleSymbol0 >> ruleSymbol0 >> ruleSymbol0;
ruleSymbol2 %= ruleSymbol0 >> ruleSymbol0;
ruleSymbol1 %= ruleSymbol0;
ruleSymbol7 %= qi::char_( "@" ) >> ruleSymbol0 >> ruleSymbol0 >> ruleSymbol0;
ruleSymbol6 %= qi::char_( "@" ) >> ruleSymbol0 >> ruleSymbol0;
ruleSymbol5 %= qi::char_( "@" ) >> ruleSymbol0;
ruleYear %= uint2_p();
ruleOption %= qi::char_( "CP" );
ruleStrike %= qi::float_;
start %= (
qi::hold[ ( ruleSymbol1 >> &( ruleMonth >> ruleYear ) ) ] // & is non-consuming lookahead
| qi::hold[ ( ruleSymbol2 >> &( ruleMonth >> ruleYear ) ) ]
| qi::hold[ ( ruleSymbol3 >> &( ruleMonth >> ruleYear ) ) ]
| qi::hold[ ( ruleSymbol4 >> &( ruleMonth >> ruleYear ) ) ]
| qi::hold[ ( ruleSymbol5 >> &( ruleMonth >> ruleYear ) ) ]
| qi::hold[ ( ruleSymbol6 >> &( ruleMonth >> ruleYear ) ) ]
| qi::hold[ ( ruleSymbol7 >> &( ruleMonth >> ruleYear ) ) ]
)
>> ruleMonth >> ruleYear>> ruleOption >> ruleStrike;
}
qi::rule<Iterator, std::string()> ruleSymbol0;
qi::rule<Iterator, std::string()> ruleSymbol1;
qi::rule<Iterator, std::string()> ruleSymbol2;
qi::rule<Iterator, std::string()> ruleSymbol3;
qi::rule<Iterator, std::string()> ruleSymbol4;
qi::rule<Iterator, std::string()> ruleSymbol5;
qi::rule<Iterator, std::string()> ruleSymbol6;
qi::rule<Iterator, std::string()> ruleSymbol7;
qi::rule<Iterator, std::string()> ruleMonth; // month
qi::rule<Iterator, boost::uint16_t()> ruleYear; // yy
qi::rule<Iterator, std::string()> ruleOption; // call/put
qi::rule<Iterator, double()> ruleStrike; // strike
qi::rule<Iterator, pos3_t()> start;
};
} // namespace iqfeed
} // namespace tf
} // namespace ou