forked from pegasusTrader/PandoraTrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cwDualTrust.cpp
102 lines (82 loc) · 1.82 KB
/
cwDualTrust.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
95
96
97
98
99
100
101
102
#include "cwDualTrust.h"
#include <algorithm>
cwDualTrust::cwDualTrust(const char* szStrategyName)
: cwBasicCTAStrategy(szStrategyName)
{
}
void cwDualTrust::OnBar(bool bFinished, int iTimeScale, cwBasicKindleStrategy::cwKindleSeriesPtr pKindleSeries)
{
//Get Parameter
double days = m_StrategyPara.CTAPara1;
double k1 = m_StrategyPara.CTAPara2;
double k2 = m_StrategyPara.CTAPara3;
//int iKindleCount = pKindleSeries->GetKindleSize();
auto pCurrentKindle = pKindleSeries->GetLastKindleStick();
double LastOpen = pCurrentKindle->Open;
double LastHigh = pCurrentKindle->High;
double LastLow = pCurrentKindle->Low;
double hh = 0.0, hc = 0.0, lc = 0.0, ll = 0.0;
int iStart = 0, iEnd = pKindleSeries->GetKindleSize() - 1;
iStart = (std::max)(iStart, (int)(iEnd - days));
std::deque<cwKindleStickPtr> kindleList;
pKindleSeries->GetKindleSerise(kindleList);
for (int i = iStart; i < iEnd; i++)
{
cwKindleStickPtr KSSptr = kindleList.at(i);
if (i == iStart)
{
hh = KSSptr->High;
hc = KSSptr->Close;
lc = KSSptr->Close;
ll = KSSptr->Low;
continue;
}
if (hh < KSSptr->High)
{
hh = KSSptr->High;
}
if (hc < KSSptr->Close)
{
hc = KSSptr->Close;
}
if (lc > KSSptr->Close)
{
lc = KSSptr->Close;
}
if (ll > KSSptr->Close)
{
ll = KSSptr->Close;
}
}
double upper_bound = LastOpen + k1 * (std::max)(hh - lc, hc - ll);
double lower_bound = LastOpen - k2 * (std::max)(hh - lc, hc - ll);
int iPosition = GetStrategyPosition();
if (iPosition == 0)
{
if (LastHigh >= upper_bound)
{
SetStrategyPosition(1);
}
if (LastLow <= lower_bound)
{
SetStrategyPosition(-1);
}
}
else
{
if (iPosition > 0)
{
if (LastLow <= lower_bound)
{
SetStrategyPosition(0);
}
}
else
{
if (LastHigh >= upper_bound)
{
SetStrategyPosition(0);
}
}
}
}