-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathZigZag.h
91 lines (73 loc) · 3.43 KB
/
ZigZag.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
/************************************************************************
* 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
// Bollinger page 91 suggests 0.17 * sqrt( price ) is a good filter width
// could set this number on each new peak
// 2012/03/31 http://www.olsenblog.com/wp-content/uploads/2009/11/conferencemanchester091004.pdf
// 2012/04/07 http://finance.martinsewell.com/stylized-facts/scaling/Guillaume-etal1997.pdf 3.7 Def 7 Direction Change Indicator, refines zigzag for risk profiles
#include "boost/date_time/posix_time/posix_time.hpp"
using namespace boost::posix_time;
using namespace boost::gregorian;
#include <OUCommon/FastDelegate.h>
using namespace fastdelegate;
#include <TFTimeSeries/TimeSeries.h>
namespace ou { // One Unified
namespace tf { // TradeFrame
class ZigZag{
public:
ZigZag();
ZigZag( double FilterWidth );
~ZigZag();
void SetFilterWidth( double width ) { m_dblFilterWidth = width; };
double GetFilterWidth() const { return m_dblFilterWidth; };
void Check( ptime dt, double val );
enum EDirection { Init, Start, Down, Up }; // start, down, up are visible in OnPeakFoundHandler
typedef FastDelegate4<const ZigZag&, ptime, double, EDirection> OnPeakFoundHandler;
void SetOnPeakFound( OnPeakFoundHandler function ) {
OnPeakFound = function;
}
typedef FastDelegate1<const ZigZag&> OnDecisionPointFoundHandler;
void SetUpDecisionPointFound( OnDecisionPointFoundHandler function ) {
UpDecisionPointFound = function;
}
void SetDnDecisionPointFound( OnDecisionPointFoundHandler function ) {
DnDecisionPointFound = function;
}
protected:
double m_dblFilterWidth; // pt1 becomes new anchor when abs(pt0-pt1)>delta
int m_cntNewUp, m_cntNewDown, m_cntTurns;
EDirection m_PatternState;
private:
double m_dblPatternPt0, // pattern end point, drags pt1 away from anchor, but can retrace at will
m_dblPatternPt1; // pattern mid point, can only move away from anchor point
ptime m_dtPatternPt1; // when it was last encountered
OnPeakFoundHandler OnPeakFound;
OnDecisionPointFoundHandler UpDecisionPointFound, DnDecisionPointFound;
};
// template sometime to handle Quote, CTrade, CPrice
class ZigZagTotalMovement: public ZigZag {
public:
ZigZagTotalMovement( Quotes&, double );
~ZigZagTotalMovement();
double Sum() const { return m_sum; };
protected:
private:
double m_sum;
double m_last;
Quotes& m_quotes;
void HandleQuote( const Quote& );
void HandlePeakFound( const ZigZag&, ptime, double, ZigZag::EDirection );
};
} // namespace tf
} // namespace ou