-
Notifications
You must be signed in to change notification settings - Fork 156
/
DatedDatum.h
445 lines (355 loc) · 14.6 KB
/
DatedDatum.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
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
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* email: info@oneunified.net *
* *
* 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
#include <hdf5/H5Cpp.h>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
// http://www.boost.org/doc/html/date_time/posix_time.html#date_time.posix_time.ptime_class
//ptime m_dt(boost::date_time::special_values::not_a_date_time);
namespace ou { // One Unified
namespace tf { // TradeFrame
// TODO: convert to crtp, remove virtual destructor?
class DatedDatum {
public:
using volume_t = unsigned long;
using tradesize_t = volume_t;
using quotesize_t = volume_t;
using dt_t = boost::posix_time::ptime;
using price_t = double;
DatedDatum();
DatedDatum( const dt_t dt );
DatedDatum( const DatedDatum& datum );
DatedDatum( const std::string& dt ); // YYYY-MM-DD HH:MM:SS
virtual ~DatedDatum();
inline bool IsNull() const { return m_dt.is_not_a_date_time(); }
inline bool operator<( const DatedDatum &rhs ) const { return m_dt < rhs.m_dt; }
inline bool operator<=( const DatedDatum& rhs ) const { return m_dt <= rhs.m_dt; }
inline bool operator>( const DatedDatum& rhs ) const { return m_dt > rhs.m_dt; }
inline bool operator>=( const DatedDatum& rhs ) const { return m_dt >= rhs.m_dt; }
inline bool operator==( const DatedDatum& rhs ) const { return m_dt == rhs.m_dt; }
inline bool operator!=( const DatedDatum& rhs ) const { return m_dt != rhs.m_dt; }
inline const dt_t DateTime() const { return m_dt; }
inline void DateTime( const dt_t dt ) { m_dt = dt; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL ); // create new one if null
static uint64_t Signature() { return 9; } // DatedDatum
// Signature() left to right reading: 9=datetime, 8=char, 1=double, 2=16 3=32, 4=64
protected:
dt_t m_dt;
private:
};
//
// Quote
//
class Quote: public DatedDatum {
public:
using bidsize_t = quotesize_t;
using asksize_t = quotesize_t;
Quote();
Quote( const dt_t dt );
Quote( const Quote& quote );
Quote( const dt_t dt, double dblBid, bidsize_t nBidSize, double dblAsk, asksize_t nAskSize );
Quote( const std::string& dt,
const std::string& bid, const std::string& bidsize,
const std::string& ask, const std::string& asksize );
virtual ~Quote();
inline price_t Bid() const { return m_dblBid; }
inline price_t Ask() const { return m_dblAsk; }
inline bidsize_t BidSize() const { return m_nBidSize; }
inline asksize_t AskSize() const { return m_nAskSize; }
bool IsValid() const;
bool IsNonZero() const;
bool SameBidAsk( const Quote& rhs ) const { return ( m_dblBid == rhs.m_dblBid ) && ( m_dblAsk == rhs.m_dblAsk ); }
bool CrossedQuote() const { return ( m_dblBid >= m_dblAsk ); }
inline price_t Midpoint() const { return ( m_dblBid + m_dblAsk ) / 2.0; }
inline price_t Spread() const { return m_dblAsk - m_dblBid; }
price_t GeometricMidPoint() const { return std::sqrt( m_dblBid * m_dblAsk ); }; // pg 53, Intro HF Finance
price_t LogarithmicMidPointA() const { return ( std::log( m_dblBid ) + std::log( m_dblAsk ) ) / 2.0; } // eq 3.4 pg 39, Intro HF Finance
price_t LogarithmicMidPointB() const { return std::log( std::sqrt( m_dblBid * m_dblAsk ) ); } // eq 3.4 pg 39, Intro HF Finance
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return DatedDatum::Signature() * 10000 + 1133; } // DatedDatum -> Quote
protected:
private:
price_t m_dblBid;
price_t m_dblAsk;
bidsize_t m_nBidSize;
asksize_t m_nAskSize;
};
//
// Trade
//
class Trade: public DatedDatum {
public:
Trade();
Trade( const dt_t dt );
Trade( const Trade &trade );
Trade( const dt_t dt, price_t dblTrade, volume_t nTradeSize );
Trade( const std::string& dt, const std::string& trade, const std::string& size );
virtual ~Trade();
inline price_t Price() const { return m_dblPrice; } // 20120715 was Trace, may cause problems in other areas.
inline volume_t Volume() const { return m_nTradeSize; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return DatedDatum::Signature() * 100 + 13; } // DatedDatum -> Trade
protected:
private:
price_t m_dblPrice;
volume_t m_nTradeSize;
};
//
// Bar
//
class Bar: public DatedDatum {
public:
Bar();
Bar( const dt_t dt );
Bar( const Bar& bar );
Bar( const dt_t dt, price_t dblOpen, price_t dblHigh, price_t dblLow, price_t dblClose, volume_t nVolume );
Bar( const std::string& dt, const std::string& open, const std::string& high,
const std::string& low, const std::string& close, const std::string& volume );
virtual ~Bar();
inline price_t Open() const { return m_dblOpen; }
inline price_t High() const { return m_dblHigh; }
inline price_t Low() const { return m_dblLow; }
inline price_t Close() const { return m_dblClose; }
inline volume_t Volume() const { return m_nVolume; }
inline void Open( price_t price ) { m_dblOpen = price; }
inline void High( price_t price ) { m_dblHigh = price; }
inline void Low( price_t price ) { m_dblLow = price; }
inline void Close( price_t price ) { m_dblClose = price; }
inline void Volume( volume_t vol ) { m_nVolume = vol; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return DatedDatum::Signature() * 100000 + 11113; } // DatedDatum -> Bar
protected:
private:
price_t m_dblOpen;
price_t m_dblHigh;
price_t m_dblLow;
price_t m_dblClose;
volume_t m_nVolume;
};
//
// Depth (common structure to DepthByMM, DepthByOrder)
//
class Depth: public DatedDatum {
public:
Depth();
Depth( const dt_t );
Depth( const Depth& );
explicit Depth( const dt_t, price_t, volume_t ); // quicky temp build
explicit Depth( const dt_t, char chSide, price_t, volume_t ); // quicky temp build
explicit Depth( const dt_t, char chMsgType, char chSide, price_t, quotesize_t );
virtual ~Depth();
inline char MsgType() const { return m_chMsgType; }
inline char Side() const { return m_chSide; }
inline volume_t Volume() const { return m_nShares; }
inline price_t Price() const { return m_dblPrice; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() {
return DatedDatum::Signature() * 10000 + 8813; } // DatedDatum -> Depth
// Signature() left to right reading: 9=datetime, 8=char, 1=double, 2=16 3=32, 4=64
protected:
private:
price_t m_dblPrice;
volume_t m_nShares;
char m_chMsgType; // 6 is summary, 3 is add, 4 is update
char m_chSide; // simplifies insertion into MarketDepth handlers
};
//
// DepthByMM (nasdaq equity only)
//
class DepthByMM: public Depth {
public:
using MMID_t = uint32_t;
DepthByMM();
DepthByMM( const dt_t );
DepthByMM( const DepthByMM& );
explicit DepthByMM( const dt_t, char chMsgType, char chSide, volume_t nShares, price_t dblPrice, char* pch );
explicit DepthByMM( const dt_t, char chMsgType, char chSide, volume_t nShares, price_t dblPrice, MMID_t mmid );
virtual ~DepthByMM();
static MMID_t Cast( const char* rchMMID ) {
unionMMID ummid( rchMMID );
return ummid.mmid;
}
static std::string Cast( MMID_t mmid ) {
unionMMID ummid( mmid );
std::string s( ummid.rch, 4 );
return s;
}
inline MMID_t MMID() const { return m_uMMID.mmid; }
std::string MMIDStr() const { return std::string( m_uMMID.rch, 4 ); }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() {
return DatedDatum::Signature() * 10000 + 8888; } // Depth -> DepthByMM
// Signature() left to right reading: 9=datetime, 8=char, 1=double, 2=16 3=32, 4=64
protected:
private:
union unionMMID {
MMID_t mmid;
char rch[4];
unionMMID() { mmid = 0; }
unionMMID( MMID_t id ): mmid( id ) {}
unionMMID( const unionMMID &u ): mmid( u.mmid ) {}
unionMMID( const char* pch ) {
char* p = rch;
for ( int ix = 0; ix < 4; ix++ ) {
*p = *pch;
p++; pch++;
}
}
unionMMID( const std::string& s ) {
assert( 4 == s.size() );
rch[0] = s[0];
rch[1] = s[1];
rch[2] = s[2];
rch[3] = s[3];
}
} m_uMMID;
};
//
// DepthByOrder (futures)
//
class DepthByOrder: public Depth {
public:
using idorder_t = uint64_t;
DepthByOrder();
DepthByOrder( const dt_t );
DepthByOrder( const DepthByOrder& );
explicit DepthByOrder( const dt_t, const dt_t dtMarket, idorder_t, uint64_t nPriority, char chMsgType, char chSide, price_t dblPrice = 0.0, volume_t nShares = 0 );
virtual ~DepthByOrder();
inline idorder_t OrderID() const { return m_nOrderID; }
inline uint64_t Priority() const { return m_nPriority; }
inline ptime MarketTimeStamp() const { return m_dtMarket; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() {
return DatedDatum::Signature() * 1000 + 944; } // Depth -> DepthByOrder
// Signature() left to right reading: 9=datetime, 8=char, 1=double, 2=16 3=32, 4=64
protected:
private:
dt_t m_dtMarket; // market supplied datetime
idorder_t m_nOrderID;
uint64_t m_nPriority;
// NOTE: probably won't add precision from iqfeed message, seems reduundantly supplied information
};
//
// Greek
//
class Greek: public DatedDatum {
public:
struct greeks_t {
double delta;
double gamma;
double theta;
double vega;
double rho;
greeks_t() : delta( 0.0 ), gamma( 0.0 ), theta( 0.0 ), vega( 0.0 ), rho( 0.0 ) {}
};
Greek();
Greek( const dt_t dt );
Greek( const Greek& greeks );
Greek( const dt_t dt, double dblImpliedVolatility, const greeks_t& greeks );
Greek( const dt_t dt, double dblImpliedVolatility, double dblDelta, double dblGamma, double dblTheta, double dblVega, double dblRho );
virtual ~Greek();
inline double ImpliedVolatility() const { return m_dblImpliedVolatility; }
inline double Delta() const { return m_dblDelta; }
inline double Gamma() const { return m_dblGamma; }
inline double Theta() const { return m_dblTheta; }
inline double Vega() const { return m_dblVega; }
inline double Rho() const { return m_dblRho; }
inline void ImpliedVolatility( double dblImpliedVolatility ) { m_dblImpliedVolatility = dblImpliedVolatility; }
inline void Delta( double dblDelta ) { m_dblDelta = dblDelta; }
inline void Gamma( double dblGamma ) { m_dblGamma = dblGamma; }
inline void Theta( double dblTheta ) { m_dblTheta = dblTheta; }
inline void Vega( double dblVega ) { m_dblVega = dblVega; }
inline void Rho( double dblRho ) { m_dblRho = dblRho; }
void Assign( const dt_t dt, double dblImplVol, double dblDelta, double dblGamma, double dblTheta, double dblVega, double dblRho ) {
m_dt = dt;
m_dblImpliedVolatility = dblImplVol;
m_dblDelta = dblDelta;
m_dblGamma = dblGamma;
m_dblTheta = dblTheta;
m_dblVega = dblVega;
m_dblRho = dblRho;
};
static H5::CompType* DefineDataType( H5::CompType *pType = NULL );
static uint64_t Signature() { return DatedDatum::Signature() * 1000000 + 111111; } // DatedDatum > Greek
protected:
private:
double m_dblImpliedVolatility;
double m_dblDelta; // sensitivity to underlying's price changes
double m_dblGamma; // measure of delta's sensitivity to underlying's price changes
double m_dblTheta; // measure of option value's sensitivity to volatility
double m_dblVega; // measure of options value's sensivity to passage of time
double m_dblRho; // measure of option value's sensivity to interest rates
};
//
// Price
//
class Price: public DatedDatum {
public:
Price();
Price( const dt_t dt );
Price( const Price& price );
Price( const dt_t dt, price_t dblPrice );
Price( const std::string &dt, const std::string& price );
virtual ~Price();
inline price_t Value() const { return m_dblPrice; }; // 20120715 was Price, is going to cause some problems in some code somewhere as is now class name
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return DatedDatum::Signature() * 10 + 1; } // DatedDatum > Price
protected:
private:
price_t m_dblPrice;
};
//
// PriceIV
// pg 458 Option Pricing Formulas suggests this structure can be used with 12.2.4 Implied Forward Volatility
//
class PriceIV: public Price {
public:
PriceIV();
PriceIV( const dt_t dt );
PriceIV( const PriceIV& rhs );
PriceIV( const dt_t dtSampled, price_t dblPrice, double dblIVCall, double dblIVPut );
virtual ~PriceIV() {};
inline double IVCall() const { return m_dblIVCall; }
inline double IVPut() const { return m_dblIVPut; }
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return Price::Signature() * 100 + 11; }; // Price -> PriceIV
protected:
private:
double m_dblIVCall;
double m_dblIVPut;
};
//
// PriceIVExpiry
// TODO: PriceIVExpiry inherits from PriceIV?
//
class PriceIVExpiry: public Price {
public:
PriceIVExpiry();
PriceIVExpiry( const dt_t dt );
PriceIVExpiry( const PriceIVExpiry& rhs );
PriceIVExpiry( const dt_t dtSampled, price_t dblPrice, const dt_t& dtExpiry, double dblIVCall, double dblIVPut );
virtual ~PriceIVExpiry() {};
inline double IVCall() const { return m_dblIVCall; };
inline double IVPut() const { return m_dblIVPut; };
inline dt_t Expiry() const { return m_dtExpiry; };
static H5::CompType* DefineDataType( H5::CompType* pType = NULL );
static uint64_t Signature() { return Price::Signature() * 1000 + 411; } // Price -> PriceIVExpiry
protected:
private:
dt_t m_dtExpiry;
double m_dblIVCall;
double m_dblIVPut;
};
} // namespace tf
} // namespace ou