forked from ht-tradestation/kbb-sqz-macd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHT_KBB_Squeeze_MACD.txt
151 lines (137 loc) · 4.92 KB
/
HT_KBB_Squeeze_MACD.txt
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
{ Search Tag: HT-KBB Squeeze MACD }
inputs:
//---------- Keltner Inputs
kPrice(Close) [DisplayName = "Keltner Price", ToolTip = ""],
kLength(20) [DisplayName = "Keltner Length", ToolTip = ""],
numATRs(1.5) [DisplayName = "ATR Multiplier", ToolTip = ""],
//---------- Bollinger Band Inputs
double bbPrice(Close) [DisplayName = "Bollinger BandPrice", ToolTip = ""],
int bbLength(20) [DisplayName = "Length", ToolTip = ""],
double numDevsUp(2) [DisplayName = "Number Std Dev Up", ToolTip = ""],
double numDevsDown(2) [DisplayName = "Number Std Dev Down", ToolTip = ""],
//---------- MACD Inputs
double fastLength(12) [DisplayName = "Fast Length", ToolTip = ""],
double slowLength(26) [DisplayName = "Slow Length", ToolTip = ""],
double macdLength(9) [DisplayName = "MACD Length", ToolTip = ""],
int macdAvgType(1) [DisplayName = "MACD Avg Type (0,1,2,3)", ToolTip = ""],
types(null) [DisplayName = "0=simple;1=exp;2=weighted;3=hull", ToolTip = ""],
//---------- Alert Inputs
bool alertIfCrossUp(False) [DisplayName = "Alert if Cross Up", ToolTip = ""],
bool alertIfCrossDown(False) [DisplayName = "Alert if Cross Down", ToolTip = ""],
bool alertIfUptrendEnd(False) [DisplayName = "Alert if Uptrend Ends", ToolTip = ""],
bool alertIfDowntrendEnd(False) [DisplayName = "Alert if Downtrend Ends", ToolTip = ""],
bool alertIfKbbFires(True) [DisplayName = "Alert if KBB Fires", ToolTip = ""],
//---------- Color Inputs
int posUpColor(Cyan) [DisplayName = "Color MACD Up > 0", ToolTip = ""],
int posDownColor(Blue) [DisplayName = "Color MACD Down > 0", ToolTip = ""],
int negUpColor(Yellow) [DisplayName = "Color MACD Up < 0", ToolTip = ""],
int negDownColor(Red) [DisplayName = "Color MACD Down < 0", ToolTip = ""],
int kbbTightColor(Red) [DisplayName = "Color KBB Tight", ToolTip = ""],
int kbbWideColor(Green) [DisplayName = "Color KBB Wide", ToolTip = ""];
variables:
double macdValue(0.0),
double macdAvg(0.0),
double diff(0.0),
double diffColor(0.0),
double kbbColor(0.0),
int firedCounter(0),
double kAvg(0.0),
double kShift(0.0),
double kLowerBand(0.0),
double kUpperBand(0.0),
double kWidth(0.0),
double bbAvg(0.0),
double stdDev(0.0),
double bbLowerBand(0.0),
double bbUpperBand(0.0),
double bbWidth(0.0),
bool isChart(False),
bool isSqueeze(False);
Once Begin
isChart = GetAppInfo( aiApplicationType ) = cChart;
End;
//---------- Keltner Section
kAvg = Average(kPrice, kLength);
kShift = numATRs * AvgTrueRange(kLength);
kUpperBand = kAvg + kShift;
kLowerBand = kAvg - kShift;
kWidth = kUpperBand - kLowerBand;
//---------- Bollinger Band Section
bbAvg = XAverage( bbPrice, bbLength ) ;
stdDev = StandardDev( bbPrice, bbLength, 1 ) ;
bbUpperBand = bbAvg + numDevsUp * stdDev ;
bbLowerBand = bbAvg - numDevsDown * stdDev ;
bbWidth = bbUpperBand - bbLowerBand;
//---------- KBB Section
isSqueeze = IFFLogic(kWidth > 0, (bbWidth / kWidth) < 1, False);
kbbColor = IFF(isSqueeze, Red, Green);
If (isSqueeze = False And isSqueeze[1]) Then Begin
firedCounter = 1;
End
Else Begin
If (isSqueeze = False) Then Begin
firedCounter += 1;
End
Else Begin
firedCounter = 0;
End;
End;
//---------- MACD Section
macdValue = HT_MACD(Close, fastLength, slowLength, macdAvgType);
Switch (macdAvgType) Begin
Case 0:
macdAvg = AverageFC(macdValue, macdLength);
Case 1:
macdAvg = XAverage(macdValue, macdLength);
Case 2:
macdAvg = WAverage(macdValue, macdLength);
Case 3:
macdAvg = HMA(macdValue, macdLength);
Default:
macdAvg = AverageFC(macdValue, macdLength);
End;
diff = (macdValue - macdAvg) * 3;
diffColor = IFF(diff > 0, IFF(diff > diff[1], posUpColor, posDownColor), IFF(diff < diff[1], negDownColor, negUpColor));
//---------- Plots
Plot4(diff, !("MACD Hist"), IFF(diffColor = posUpColor, Black, IFF(diffColor = negUpColor, Black, White)));
Plot2(macdValue, !("MACD Value"));
Plot3(macdAvg, !("MACD Average"));
// check if we are in a chart
If (isChart) Then Begin
// if in a chart, set plots to display on chart
Plot1(0, !("KBB"), kbbColor);
SetPlotColor(4, diffColor);
SetPlotColor(2, Yellow);
setPlotColor(3, Cyan);
End
Else Begin
// otherwise, set plots to display on radar screen
Plot1(firedCounter, !("KBB"), IFF(isSqueeze, White, Black));
SetPlotBGColor(4, diffColor);
SetPlotBGColor(1, kbbColor);
SetPlotColor(2, Black);
setPlotColor(3, Black);
End;
//---------- Alerts
If (AlertEnabled) Then Begin
If (diff crosses over 0) Then Begin
If (alertIfCrossUp) Then
Alert(!("MACD Hist crossed over 0."));
End
Else If (diff crosses under 0) Then Begin
If (alertIfCrossDown) Then
Alert(!("MACD Hist crossed under 0."));
End;
If (diff > 0 And diff < diff[1] And diff[1] > diff[2]) Then Begin
If (alertIfUptrendEnd) Then
Alert(!("MACD Uptrend Has Ended"));
End
Else If (diff < 0 And diff > diff[1] And diff[1] < diff[2]) Then Begin
If (alertIfDowntrendEnd) Then
Alert(!("MACD Downtrend Has Ended"));
End;
If (firedCounter = 1) Then Begin
If (alertIfKbbFires) Then
Alert(!("KBB Squeeze Has Fired"));
End;
End;