-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathFeatureSet_Level.hpp
255 lines (192 loc) · 7.26 KB
/
FeatureSet_Level.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
/************************************************************************
* Copyright(c) 2022, 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. *
************************************************************************/
/*
* File: FeatureSet_Level.hpp
* Author: [email protected]
* Project: lib/TFIQFeed/Level2
* Created: May 7, 2022 15:39
*/
// based upon the paper:
// Modeling high-frequency limit order book dynamics with support vector machines
// October 24, 2013, Alec N.Kercheval, Yuan Zhang
// page 16, table 2, Feature Vector Sets
// not really integrated into L2Base, as we only need n levels here, not all levels in the real book
// need a signal for level deleted
#pragma once
#include <ostream>
#include <TFTimeSeries/DatedDatum.h>
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace iqfeed { // IQFeed
namespace l2 { // level 2 data
class FeatureSet_Column;
class FeatureSet_Level {
public:
// == -> variables requiring emission flag
// if a level changes, change those plus deeper
// if a level is added / removed, recalc all levels
using price_t = ou::tf::Trade::price_t;
using volume_t = ou::tf::Trade::volume_t;
struct V1 { // absolute
price_t price; // ==
price_t aggregatePrice; // ==
volume_t volume; // ==
double aggregateVolume; // ==
bool bNew; // can't tell a removal, but an addition might be significant
V1()
: price {}, volume {}
, aggregateVolume {}
, aggregatePrice {}
, bNew( false )
{}
};
struct V3 { // diff
price_t diffToTop; // ==
price_t diffToAdjacent; // ==
V3(): diffToTop {}, diffToAdjacent {}
{}
};
struct V4 { // absolute
price_t meanPrice; // ==
volume_t meanVolume; // ==
V4(): meanPrice {}, meanVolume {}
{}
};
struct V6 { // derivative per unit time
ptime dtLast;
double deltaArrival;
price_t dPrice_dt; // ==
volume_t dVolume_dt; // ==
V6()
: dtLast( boost::posix_time::not_a_date_time )
, deltaArrival {}
, dPrice_dt {}, dVolume_dt{}
{}
};
struct V7 { // intensity over per unit time (1 sec)
ptime dtLastLimit;
double intensityLimit; // short term intensity // ==
ptime dtLastMarket;
double intensityMarket; // short term intensity // ==
ptime dtLastCancel;
double intensityCancel; // short term intensity // ==
V7()
: dtLastLimit( boost::posix_time::not_a_date_time ), intensityLimit {}
, dtLastMarket( boost::posix_time::not_a_date_time ), intensityMarket {}
, dtLastCancel( boost::posix_time::not_a_date_time ), intensityCancel {}
{}
};
struct V8 { // relative intensity of short period vs long period (10s vs 900s)
double intensityLimit; // == // long term intensity
double relativeLimit; // == // ratio of short term intensity vs long term intensity
double intensityMarket; // == // long term intensity
double relativeMarket; // == // ratio of short term intensity vs long term intensity
double intensityCancel; // == // long term intensity
double relativeCancel; // == // ratio of short term intensity vs long term intensity
V8()
: intensityLimit {}, relativeLimit {}
, intensityMarket {}, relativeMarket {}
, intensityCancel {}, relativeCancel {}
{}
};
struct V9 { // accelleration of trading type per unit time (vs previous 1 sec) - accel( v7 )
double accelLimit; // ==
double accelMarket; // ==
double accelCancel; // ==
V9()
: accelLimit {}
, accelMarket {}
, accelCancel {}
{}
};
struct BookLevel {
bool bActive; // level has data
V1 v1;
V3 v3;
V4 v4;
V6 v6;
V7 v7;
V8 v8;
V9 v9;
BookLevel(): bActive( false ) {}
BookLevel& operator=( const BookLevel& );
};
BookLevel ask;
BookLevel bid;
struct V2 {
price_t spread; // == // diff
price_t mid; // == // absolute
double imbalanceLvl; // == // (volBid - volAsk ) / ( volBid + volAsk ) -- not in the paper
double imbalanceAgg; // == // (volBid - volAsk ) / ( volBid + volAsk ) -- not in the paper
V2(): spread {}, mid {}, imbalanceLvl {}, imbalanceAgg {} {}
};
struct V5 { // sum(diff)
price_t sumPriceSpreads; // ==
double sumVolumeSpreads; // ==
V5(): sumPriceSpreads {}, sumVolumeSpreads {} {}
};
struct CrossLevel {
V2 v2;
V5 v5;
};
CrossLevel cross;
FeatureSet_Level();
FeatureSet_Level( FeatureSet_Level&& );
~FeatureSet_Level();
using vSentinel_t = std::vector<std::string>;
void Set( int ix, FeatureSet_Level* pTop, FeatureSet_Level* pNext );
void Set( const vSentinel_t& );
void Ask_Activate( bool bActive ) { ask.bActive = bActive; }
void Bid_Activate( bool bActive ) { bid.bActive = bActive; }
void Ask_CopyFrom( const FeatureSet_Level& ); // shuffle for insertion
void Ask_CopyTo( FeatureSet_Level& ); // shuffle after deletion
void Bid_CopyFrom( const FeatureSet_Level& ); // shuffle for insertion
void Bid_CopyTo( FeatureSet_Level& ); // shuffle after deletion
void Ask_Quote( const ou::tf::Depth& );
void Bid_Quote( const ou::tf::Depth& );
void Ask_IncLimit( const ou::tf::Depth& );
void Ask_IncMarket( const ou::tf::Depth& );
void Ask_IncCancel( const ou::tf::Depth& );
void Bid_IncLimit( const ou::tf::Depth& );
void Bid_IncMarket( const ou::tf::Depth& );
void Bid_IncCancel( const ou::tf::Depth& );
static const std::string Header( const size_t level );
void Changed( bool& ) const;
std::ostream& operator<<( std::ostream& s ) const;
protected:
private:
int m_ix; // used as diviser for level number
FeatureSet_Level* m_pTop; // pointer only, no memory
FeatureSet_Level* m_pNext; // pointer only, no memory
FeatureSet_Level& operator=( const FeatureSet_Level& ) = delete;
std::unique_ptr<FeatureSet_Column> m_pFeatureSet_Column;
void QuotePriceUpdates();
void QuoteVolumeUpdates();
void Ask_AggregateP( price_t aggregate ); // aggregate price from previous level
void Bid_AggregateP( price_t aggregate ); // aggregate price from previous level
void Ask_AggregateV( double aggregate ); // aggregate volume from previous level
void Bid_AggregateV( double aggregate ); // aggregate volume from previous level
void Ask_Diff();
void Bid_Diff();
void ImbalanceOnAggregate();
void Ask_Derivatives( const ou::tf::Depth& );
void Bid_Derivatives( const ou::tf::Depth& );
// v7 - common code
void Intensity( const ou::tf::Depth&, ptime&, double&, double&, double& );
};
std::ostream& operator<<( std::ostream&, const FeatureSet_Level& );
} // namespace l2
} // namesapce iqfeed
} // namespace tf
} // namespace ou