-
Notifications
You must be signed in to change notification settings - Fork 156
/
TSSWRealizedVolatility.cpp
94 lines (82 loc) · 2.83 KB
/
TSSWRealizedVolatility.cpp
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
/************************************************************************
* 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. *
************************************************************************/
#include "stdafx.h"
#include <math.h>
#include "TSSWRealizedVolatility.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
TSSWRealizedVolatility::TSSWRealizedVolatility( Prices& prices, time_duration tdWindowWidth, double p )
: m_dblSum( 0.0 ), m_dblP( p ), m_n( 0 ), m_dt( not_a_date_time ), m_tdScaledWidth( hours( 365 * 24 ) + hours( 6 ) ),
TimeSeriesSlidingWindow<TSSWRealizedVolatility, Price>( prices, tdWindowWidth, 0 )
{
CalcScaleFactor();
}
TSSWRealizedVolatility::~TSSWRealizedVolatility( void ) {
}
void TSSWRealizedVolatility::Add( const Price& price ) {
m_dt = price.DateTime();
double val( price.Value() );
++m_n;
if ( 1.0 == m_dblP ) {
m_dblSum += val;
}
else {
if ( 2.0 == m_dblP ) {
m_dblSum += val * val;
}
else {
m_dblSum += std::pow( std::abs( val ), m_dblP );
}
}
}
void TSSWRealizedVolatility::Expire( const Price& price ) {
--m_n;
double val( price.Value() );
--m_n;
if ( 1.0 == m_dblP ) {
m_dblSum -= val;
}
else {
if ( 2.0 == m_dblP ) {
m_dblSum -= val * val;
}
else {
m_dblSum -= std::pow( std::abs( val ), m_dblP );
}
}
}
void TSSWRealizedVolatility::PostUpdate( void ) {
double result( 0.0 );
if ( 1.0 == m_dblP ) {
result = m_dblSum / m_n;
}
else {
if ( 2.0 == m_dblP ) {
result = std::sqrt( m_dblSum / m_n );
}
else {
result = std::pow( m_dblSum / m_n, 1.0 / m_dblP );
}
}
Prices::Append( Price( m_dt, result * m_dblScaleFactor ) );
}
void TSSWRealizedVolatility::CalcScaleFactor( void ) {
m_dblScaleFactor =
std::sqrt(
(double) m_tdScaledWidth.total_milliseconds() /
( (double) TimeSeriesSlidingWindow<TSSWRealizedVolatility, Price>::WindowWidth().total_milliseconds() / m_n )
);
}
} // namespace tf
} // namespace ou