-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathTSHomogenization.h
165 lines (141 loc) · 5.64 KB
/
TSHomogenization.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
/************************************************************************
* Copyright(c) 2012, 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. *
************************************************************************/
// 2015/02/21 this code hasn't really been tested
#pragma once
#include <OUCommon/Delegate.h>
#include <TFTimeSeries/TimeSeries.h>
namespace ou { // One Unified
namespace tf { // TradeFrame
namespace hf { // high frequency
template<typename T>
class TSHomogenization {
private:
static time_duration m_zeroDuration;
public:
enum interpolation_t { eNone, ePreviousTick, eLinear };
TSHomogenization<T>( TimeSeries<T>&, time_duration interval = m_zeroDuration, interpolation_t interpolation = eNone );
TSHomogenization<T>( const TSHomogenization<T>& rhs );
virtual ~TSHomogenization<T>(void);
ou::Delegate<const T&> OnAppend;
protected:
private:
interpolation_t m_interpolation;
TimeSeries<T>& m_ts;
time_duration m_tdHomogenizingInterval; // 0 if no homoginization
ptime m_dtMarker;
T m_datum; // this is Tau at j
void Init( void );
void HandleFirstDatum( const T& );
void HandleDatum( const T& ); // this is Tau at j+1, need to handle Price, Trade, CBar
void FlowThrough( const T& datum ) { OnAppend( datum ); };
void CalcDatum( const Price& price, double ratio );
void CalcDatum( const Trade& trade, double ratio );
};
template<typename T>
time_duration TSHomogenization<T>::m_zeroDuration = time_duration( 0, 0, 0 );
template<typename T>
TSHomogenization<T>::TSHomogenization( TimeSeries<T>& ts, time_duration interval, interpolation_t interpolation )
: m_tdHomogenizingInterval( interval ),
m_ts( ts ), m_interpolation( interpolation ), m_dtMarker( not_a_date_time )
{
if ( m_zeroDuration == interval ) assert( eNone == m_interpolation );
if ( m_zeroDuration != interval ) assert( eNone != m_interpolation );
Init();
}
template<typename T>
TSHomogenization<T>::TSHomogenization( const TSHomogenization<T>& rhs )
: m_interpolation( rhs.m_interpolation ), m_ts( rhs.m_ts ), m_datum( rhs.m_datum ),
m_tdHomogenizingInterval( rhs.m_tdHomogenizingInterval ), m_dtMarker( rhs.m_dtMarker )
{
Init();
}
template<typename T>
TSHomogenization<T>::~TSHomogenization( void ) {
switch ( m_interpolation ) {
case eNone:
m_ts.OnAppend.Remove( MakeDelegate( this, &TSHomogenization<T>::FlowThrough ) );
break;
case ePreviousTick:
case eLinear:
if ( m_dtMarker.is_not_a_date_time() ) { // was m_dtInterval, fixed to m_dtMarker, but is it correct?
m_ts.OnAppend.Remove( MakeDelegate( this, &TSHomogenization<T>::HandleFirstDatum ) );
}
else {
m_ts.OnAppend.Remove( MakeDelegate( this, &TSHomogenization<T>::HandleDatum ) );
}
break;
}
}
template<typename T>
void TSHomogenization<T>::Init( void ) {
switch ( m_interpolation ) {
case eNone:
m_ts.OnAppend.Add( MakeDelegate( this, &TSHomogenization<T>::FlowThrough ) );
break;
case ePreviousTick:
case eLinear:
m_ts.OnAppend.Add( MakeDelegate( this, &TSHomogenization<T>::HandleFirstDatum ) );
break;
}
}
template<typename T>
void TSHomogenization<T>::HandleFirstDatum( const T& datum ) {
m_ts.OnAppend.Remove( MakeDelegate( this, &TSHomogenization<T>::HandleFirstDatum ) );
m_datum = datum;
m_dtMarker = datum.DateTime() % m_tdHomogenizingInterval;
if ( m_dtMarker == datum.DateTime() ) {
HandleDatum( datum );
}
else {
m_dtMarker += m_tdHomogenizingInterval;
assert( m_dtMarker > datum.DateTime() );
}
m_ts.OnAppend.Add( MakeDelegate( this, &TSHomogenization<T>::HandleDatum ) );
}
template<typename T>
void TSHomogenization<T>::HandleDatum( const T& datum ) {
if ( m_dtMarker == datum.DateTime() ) {
OnAppend( datum );
}
else {
if ( datum.DateTime() > m_dtMarker ) {
switch ( m_interpolation ) {
case ePreviousTick:
OnAppend( m_datum );
break;
case eLinear:
time_duration numerator( m_dtMarker - m_datum.DateTime() );
time_duration denomenator( datum.DateTime() - m_datum.DateTime() );
double ratio = ( (double) numerator.total_microseconds() ) / ( (double) denomenator.total_microseconds );
CalcDatum( datum, ratio );
break;
}
while ( m_dtMarker <= datum.DateTime() ) m_dtMarker += m_tdHomogenizingInterval;
}
}
m_datum = datum;
}
template<typename T>
void TSHomogenization<T>::CalcDatum( const Price& datum, double ratio ) {
Price price( m_dtMarker, m_datum.Price() + ratio * ( datum.Value() - m_datum.Price ) );
OnAppend( price );
}
template<typename T>
void TSHomogenization<T>::CalcDatum( const Trade& datum, double ratio ) {
Trade trade( m_dtMarker, m_datum.Trade() + ratio * ( datum.Price() - m_datum.Trade ), m_datum.Volume() );
OnAppend( trade );
}
} // namespace hf
} // namespace tf
} // namespace ou