-
Notifications
You must be signed in to change notification settings - Fork 156
/
TSSWStats.h
179 lines (150 loc) · 6.93 KB
/
TSSWStats.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
/************************************************************************
* Copyright(c) 2010, 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
#include "RunningStats.h"
#include "TimeSeriesSlidingWindow.h"
// continuously updated series based upon attachment to an underlying time series.
namespace ou { // One Unified
namespace tf { // TradeFrame
template<class T, class D>
class TimeSeriesSlidingWindowStats
: public TimeSeriesSlidingWindow<T,D> {
friend TimeSeriesSlidingWindow<T,D>;
public:
TimeSeriesSlidingWindowStats<T,D>( TimeSeries<D>& Series, time_duration tdWindowWidth, size_t WindowSizeCount = 0 );
TimeSeriesSlidingWindowStats<T,D>( TimeSeries<D>& Series, size_t nPeriods, time_duration tdPeriodWidth, size_t WindowSizeCount = 0 );
TimeSeriesSlidingWindowStats<T,D>( const TimeSeriesSlidingWindowStats<T,D>& rhs );
TimeSeriesSlidingWindowStats<T,D>( TimeSeriesSlidingWindowStats<T,D>&& rhs );
virtual ~TimeSeriesSlidingWindowStats<T,D>();
// double Accel( void ) const { return m_stats.B2(); };
double Slope() const { return m_stats.Slope(); };
double Offset() const { return m_stats.Offset(); };
double MeanY() const { return m_stats.MeanY(); };
double RR() const { return m_stats.RR(); };
double R() const { return m_stats.R(); };
double SD() const { return m_stats.SD(); };
double BBOffset() const { return m_stats.BBOffset(); };
double BBUpper() const { return m_stats.BBUpper(); };
double BBLower() const { return m_stats.BBLower(); };
void SetBBMultiplier( double mult ) { m_stats.SetBBMultiplier( mult ); };
double GetBBMultiplier() const { return m_stats.GetBBMultiplier(); };
void Reset() {
TimeSeriesSlidingWindow<T,D>::Reset();
m_stats.Reset();
};
struct Results {
boost::posix_time::ptime dt;
const linear::Stats& stats;
Results( boost::posix_time::ptime dt_, const linear::Stats& stats_ )
: dt( dt_ ), stats( stats_ ) {}
};
Delegate<const Results&> OnUpdate;
protected:
// void Add( const T &datum ) {}; // override to process elements passing into window scope
// void Expire( const T &datum ) {}; // override to process elements passing out of window scope
boost::posix_time::ptime m_dtLast;
RunningStats m_stats;
private:
void PostUpdate() { // CRTP based call
m_stats.CalcStats();
OnUpdate( Results( m_dtLast, m_stats.Get() ) );
};
};
// constructor
template<class T, class D>
TimeSeriesSlidingWindowStats<T,D>::TimeSeriesSlidingWindowStats(
TimeSeries<D>& Series, time_duration tdWindowWidth, size_t WindowSizeCount )
: TimeSeriesSlidingWindow<T,D>( Series, tdWindowWidth, WindowSizeCount )
{
m_stats.SetBBMultiplier( 2.0 );
}
template<class T, class D>
TimeSeriesSlidingWindowStats<T,D>::TimeSeriesSlidingWindowStats(
TimeSeries<D>& Series, size_t nPeriods, time_duration tdPeriodWidth, size_t WindowSizeCount )
: TimeSeriesSlidingWindow<T,D>( Series, nPeriods, tdPeriodWidth, WindowSizeCount )
{
m_stats.SetBBMultiplier( 2.0 );
}
template<class T, class D>
TimeSeriesSlidingWindowStats<T,D>::TimeSeriesSlidingWindowStats(
const TimeSeriesSlidingWindowStats<T,D>& rhs )
: TimeSeriesSlidingWindow<T,D>( rhs ), m_stats( rhs.m_stats )
{}
template<class T, class D>
TimeSeriesSlidingWindowStats<T,D>::TimeSeriesSlidingWindowStats( TimeSeriesSlidingWindowStats<T,D>&& rhs )
: TimeSeriesSlidingWindow<T,D>( rhs ), m_stats( std::move( rhs.m_stats ) )
{}
template<class T, class D> TimeSeriesSlidingWindowStats<T,D>::~TimeSeriesSlidingWindowStats() {
}
// Convert the following flavours into template based actors so can be used among different indicators
//
// with Trade
//
class TSSWStatsTrade: public TimeSeriesSlidingWindowStats<TSSWStatsTrade, Trade> {
friend TimeSeriesSlidingWindow<TSSWStatsTrade, Trade>;
public:
TSSWStatsTrade( TimeSeries<Trade>& series, time_duration tdWindowWidth, size_t WindowSizeCount = 0 );
TSSWStatsTrade( const TSSWStatsTrade& rhs );
virtual ~TSSWStatsTrade();
protected:
void Add( const Trade &trade ); // override to process elements passing into window scope
void Expire( const Trade &trade ); // override to process elements passing out of window scope
private:
};
//
// with Quote, bid, ask
//
class TSSWStatsQuote: public TimeSeriesSlidingWindowStats<TSSWStatsQuote, Quote> {
friend TimeSeriesSlidingWindow<TSSWStatsQuote, Quote>;
public:
TSSWStatsQuote( TimeSeries<Quote>& series, time_duration tdWindowWidth, size_t WindowSizeCount = 0 );
TSSWStatsQuote( const TSSWStatsQuote& rhs );
virtual ~TSSWStatsQuote();
protected:
void Add( const Quote "e ); // override to process elements passing into window scope
void Expire( const Quote "e ); // override to process elements passing out of window scope
private:
};
//
// with Quote, midquote
//
class TSSWStatsMidQuote: public TimeSeriesSlidingWindowStats<TSSWStatsMidQuote, Quote> {
friend TimeSeriesSlidingWindow<TSSWStatsMidQuote, Quote>;
public:
TSSWStatsMidQuote( Quotes& series, time_duration tdWindowWidth, size_t WindowSizeCount = 0 );
TSSWStatsMidQuote( Quotes& series, size_t nPeriods, time_duration tdPeriodWidth, size_type WindowSizeCount = 0 );
TSSWStatsMidQuote( const TSSWStatsMidQuote& );
TSSWStatsMidQuote( TSSWStatsMidQuote&& );
virtual ~TSSWStatsMidQuote();
protected:
void Add( const Quote "e ); // override to process elements passing into window scope
void Expire( const Quote "e ); // override to process elements passing out of window scope
private:
};
//
// with Price
//
class TSSWStatsPrice: public TimeSeriesSlidingWindowStats<TSSWStatsPrice, Price> {
friend TimeSeriesSlidingWindow<TSSWStatsPrice, Price>;
public:
TSSWStatsPrice( TimeSeries<Price>& series, time_duration tdWindowWidth, size_t WindowSizeCount = 0 );
TSSWStatsPrice( const TSSWStatsPrice& rhs );
virtual ~TSSWStatsPrice();
protected:
void Add( const Price &price ); // override to process elements passing into window scope
void Expire( const Price &price ); // override to process elements passing out of window scope
private:
};
} // namespace tf
} // namespace ou