-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSchaffTrendCycle.mq5
236 lines (206 loc) · 17.6 KB
/
SchaffTrendCycle.mq5
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//+------------------------------------------------------------------+
// SchaffTrendCycle.mq5 |
//| Copyright © 2011-2019, EarnForex.com |
//| https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011-2019, EarnForex.com"
#property link "https://www.earnforex.com/metatrader-indicators/Schaff-Trend-Cycle/"
#property version "1.04"
#property description "Schaff Trend Cycle - Cyclical Stochastic over Stochastic over MACD."
#property description "Falling below 75 is a sell signal."
#property description "Rising above 25 is a buy signal."
#property description "Four kinds of alert: arrows, text, sound, email, and push."
#property description "Developed by Doug Schaff."
#property description "Code adapted from the original TradeStation EasyLanguage version."
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots 1
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 25
#property indicator_level2 75
#property indicator_width1 2
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrDarkOrchid
#property indicator_label1 "Schaff Trend Cycle"
//---- Input Parameters
input int MAShort = 23;
input int MALong = 50;
input int Cycle = 10;
input bool ShowArrows = false;
input color UpColor = clrBlue;
input color DownColor = clrRed;
input bool ShowAlerts = false;
input bool SoundAlerts = false;
input bool EmailAlerts = false;
input bool PushAlerts = false;
//---- Global Variables
double Factor = 0.5;
int BarsRequired;
datetime LastAlert = D'1980.01.01';
//---- Buffers
double MACD[];
double ST[];
double ST2[];
//---- MA Buffers
double MAShortBuf[];
double MALongBuf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
IndicatorSetString(INDICATOR_SHORTNAME, "STC(" + IntegerToString(MAShort) + "," + IntegerToString(MALong) + "," + IntegerToString(Cycle) + ")");
SetIndexBuffer(0, ST2, INDICATOR_DATA);
SetIndexBuffer(1, ST, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, MACD, INDICATOR_CALCULATIONS);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MALong + Cycle * 2);
IndicatorSetInteger(INDICATOR_DIGITS, 0);
BarsRequired = MALong + Cycle * 2;
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, "ST_down_");
ObjectsDeleteAll(0, "ST_up_");
}
//+------------------------------------------------------------------+
//| Schaff Trend Cycle |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &Time[],
const double &open[],
const double &High[],
const double &Low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if (rates_total <= BarsRequired) return(rates_total);
int counted_bars = prev_calculated;
double LLV = 0, HHV = 0;
int shift, n = 1, i, myMA;
// Static variables are used to flag that we already have calculated curves from the previous indicator run.
static bool st1_pass = false;
static bool st2_pass = false;
int st1_count = 0;
bool check_st1 = false, check_st2 = false;
if (counted_bars < BarsRequired)
{
for (i = 0; i < BarsRequired; i++) ST2[i] = 0;
for (i = 0; i < BarsRequired; i++) ST[i] = 0;
}
if (counted_bars > 0) counted_bars--;
shift = counted_bars - BarsRequired + MALong - 1;
if (shift < 0) shift = 0;
myMA = iMA(NULL, 0, MAShort, 0, MODE_EMA, PRICE_CLOSE);
if (CopyBuffer(myMA, 0, 0, rates_total, MAShortBuf) != rates_total) return(0);
myMA = iMA(NULL, 0, MALong, 0, MODE_EMA, PRICE_CLOSE);
if (CopyBuffer(myMA, 0, 0, rates_total, MALongBuf) != rates_total) return(0);
while (shift < rates_total)
{
MACD[shift] = MAShortBuf[shift] - MALongBuf[shift];
if (n >= Cycle) check_st1 = true;
else n++;
if (check_st1)
{
// Finding Max and Min on Cycle of MA differences (MACD).
for (i = 0; i < Cycle; i++)
{
if (i == 0)
{
LLV = MACD[shift - i];
HHV = MACD[shift - i];
}
else
{
if (LLV > MACD[shift - i]) LLV = MACD[shift - i];
if (HHV < MACD[shift - i]) HHV = MACD[shift - i];
}
}
// Calculating first Stochastic.
if (HHV - LLV != 0) ST[shift] = ((MACD[shift] - LLV) / (HHV - LLV)) * 100;
else ST[shift] = ST[shift - 1];
// Smoothing first Stochastic
if (st1_pass) ST[shift] = Factor * (ST[shift] - ST[shift - 1]) + ST[shift - 1];
st1_pass = true;
// Have enough elements of first Stochastic to proceed to second.
if (st1_count >= Cycle) check_st2 = true;
else st1_count++;
if (check_st2)
{
// Finding Max and Min on Cycle of first smoothed Stochastic.
for (i = 0; i < Cycle; i++)
{
if (i == 0)
{
LLV = ST[shift - i];
HHV = ST[shift - i];
}
else
{
if (LLV > ST[shift - i]) LLV = ST[shift - i];
if (HHV < ST[shift - i]) HHV = ST[shift - i];
}
}
// Calculating second Stochastic.
if (HHV - LLV != 0) ST2[shift] = ((ST[shift] - LLV) / (HHV - LLV)) * 100;
else ST2[shift] = ST2[shift - 1];
// Smoothing second Stochastic.
if (st2_pass) ST2[shift] = Factor * (ST2[shift] - ST2[shift - 1]) + ST2[shift - 1];
st2_pass = true;
}
}
if (shift > 0)
{
if ((ST2[shift] < 75) && (ST2[shift - 1] >= 75))
{
if (ShowArrows)
{
string name = "ST_down_" + TimeToString(Time[shift]);
double offset = (High[shift] - Low[shift]) / 2;
ObjectCreate(0, name, OBJ_ARROW, 0, Time[shift], High[shift] + offset + spread[shift] * Point());
ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 234);
ObjectSetInteger(0, name, OBJPROP_COLOR, DownColor);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
}
if ((shift == rates_total - 2) && (LastAlert != Time[rates_total - 1]))
{
if (ShowAlerts) Alert("Bearish signal on " + Symbol() + ".");
if (SoundAlerts) PlaySound("alert.wav");
if (EmailAlerts) SendMail("Schaff Trend Cycle Alert", "Bearish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");
if (PushAlerts) SendNotification("STC Alert: Bearish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");
LastAlert = Time[rates_total - 1];
}
}
else if ((ST2[shift] > 25) && (ST2[shift - 1] <= 25))
{
if (ShowArrows)
{
string name = "ST_up_" + TimeToString(Time[shift]);
double offset = (High[shift] - Low[shift]) / 2;
ObjectCreate(0, name, OBJ_ARROW, 0, Time[shift], Low[shift] - offset);
ObjectSetInteger(0, name, OBJPROP_ARROWCODE, 233);
ObjectSetInteger(0, name, OBJPROP_COLOR, UpColor);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
}
if ((shift == rates_total - 2) && (LastAlert != Time[rates_total - 1]))
{
if (ShowAlerts) Alert("Bullish signal on " + Symbol() + ".");
if (SoundAlerts) PlaySound("alert.wav");
if (EmailAlerts) SendMail("Schaff Trend Cycle Alert", "Bullish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");
if (PushAlerts) SendNotification("STC Alert: Bullish signal on " + TimeToString(Time[rates_total - 1]) + " on " + Symbol() + ".");
LastAlert = Time[rates_total - 1];
}
}
}
shift++;
}
return(rates_total);
}
//+------------------------------------------------------------------+