-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLimit channels.mq5
138 lines (122 loc) · 10 KB
/
Limit channels.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
//------------------------------------------------------------------
#property copyright "© mladen, 2021"
#property link "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots 6
#property indicator_label1 "Level up 3"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrLimeGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_label2 "Level up 2"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrLimeGreen
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_label3 "Level up 1"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrLimeGreen
#property indicator_style3 STYLE_DOT
#property indicator_label4 "Level down 1"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrCoral
#property indicator_style4 STYLE_DOT
#property indicator_label5 "Level down 2"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrCoral
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
#property indicator_label6 "Level down 3"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrCoral
#property indicator_style6 STYLE_SOLID
#property indicator_width6 2
//
//
//
input int inpAtrPeriod = 50; // ATR period
input double inpAtrMultiplier = 5; // ATR multiplier
//
//
//
double up1[],up2[],up3[],down1[],down2[],down3[];
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
int OnInit()
{
SetIndexBuffer(0,up3 ,INDICATOR_DATA);
SetIndexBuffer(1,up2 ,INDICATOR_DATA);
SetIndexBuffer(2,up1 ,INDICATOR_DATA);
SetIndexBuffer(3,down1,INDICATOR_DATA);
SetIndexBuffer(4,down2,INDICATOR_DATA);
SetIndexBuffer(5,down3,INDICATOR_DATA);
//
//
//
return(INIT_SUCCEEDED);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
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[])
{
int limit = (prev_calculated>0) ? prev_calculated-1 : 0;
//
//
//
struct sWorkStruct
{
double tr;
double trSum;
};
static sWorkStruct m_work[];
static int m_workSize = -1;
if (m_workSize<rates_total) m_workSize = ArrayResize(m_work,rates_total+500,2000);
//
//
//
for (int i=limit; i<rates_total && !_StopFlag; i++)
{
m_work[i].tr = (i>0) ? (high[i]>close[i-1] ? high[i] : close[i-1]) - (low[i]<close[i-1] ? low[i] : close[i-1]) : high[i] - low[i];
if (i>inpAtrPeriod)
{ m_work[i].trSum = m_work[i-1].trSum + m_work[i].tr - m_work[i-inpAtrPeriod].tr; }
else { m_work[i].trSum = m_work[i].tr; for (int k=1; k<inpAtrPeriod && i>=k; k++) m_work[i].trSum += m_work[i-k].tr; }
//
//
//
if (i==0)
{
up1[i] = up2[i] = up3[i] = high[i];
down1[i] = down2[i] = down3[i] = low[i];
}
else
{
double _atr = inpAtrMultiplier*m_work[i-1].trSum/(double)inpAtrPeriod;
if (high[i] > up3[i-1]) up3[i] = high[i]; else if (high[i] < up3[i-1]) up3[i] = MathMin(high[i] + _atr*1.0, up3[i-1]); else up3[i] = up3[i-1];
if (high[i] > up2[i-1]) up2[i] = high[i]; else if (high[i] < up2[i-1]) up2[i] = MathMin(high[i] + _atr*0.5, up2[i-1]); else up2[i] = up2[i-1];
if (high[i] > up1[i-1]) up1[i] = high[i]; else if (high[i] < up1[i-1]) up1[i] = MathMin(high[i] + _atr*0.1, up1[i-1]); else up1[i] = up1[i-1];
if (low[i] < down1[i-1]) down1[i] = low[i]; else if (low[i] > down1[i-1]) down1[i] = MathMax(low[i] - _atr*0.1, down1[i-1]); else down1[i] = down1[i-1];
if (low[i] < down2[i-1]) down2[i] = low[i]; else if (low[i] > down2[i-1]) down2[i] = MathMax(low[i] - _atr*0.5, down2[i-1]); else down2[i] = down2[i-1];
if (low[i] < down3[i-1]) down3[i] = low[i]; else if (low[i] > down3[i-1]) down3[i] = MathMax(low[i] - _atr*1.0, down3[i-1]); else down3[i] = down3[i-1];
}
}
return(rates_total);
}