-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMedianRenko_ADX.mq5
203 lines (191 loc) · 7.09 KB
/
MedianRenko_ADX.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
//+------------------------------------------------------------------+
//| ADX.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property description "Average Directional Movement Index"
#include <MovingAverages.mqh>
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots 3
#property indicator_type1 DRAW_LINE
#property indicator_color1 LightSeaGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_type2 DRAW_LINE
#property indicator_color2 YellowGreen
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
#property indicator_type3 DRAW_LINE
#property indicator_color3 Wheat
#property indicator_style3 STYLE_DOT
#property indicator_width3 1
#property indicator_label1 "ADX"
#property indicator_label2 "+DI"
#property indicator_label3 "-DI"
//--- input parameters
input int InpPeriodADX=14; // Period
//---- buffers
double ExtADXBuffer[];
double ExtPDIBuffer[];
double ExtNDIBuffer[];
double ExtPDBuffer[];
double ExtNDBuffer[];
double ExtTmpBuffer[];
//--- global variables
int ExtADXPeriod;
//
// Initialize MedianRenko indicator for data processing
// according to settings of the MedianRenko indicator already on chart
//
#include <AZ-INVEST/SDK/MedianRenkoIndicator.mqh>
MedianRenkoIndicator medianRenkoIndicator;
//
//
//
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input parameters
if(InpPeriodADX>=100 || InpPeriodADX<=0)
{
ExtADXPeriod=14;
printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);
}
else ExtADXPeriod=InpPeriodADX;
//---- indicator buffers
SetIndexBuffer(0,ExtADXBuffer);
SetIndexBuffer(1,ExtPDIBuffer);
SetIndexBuffer(2,ExtNDIBuffer);
SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS);
//--- indicator digits
IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- set draw begin
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);
//--- indicator short name
string short_name="ADX("+string(ExtADXPeriod)+")";
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- change 1-st index label
PlotIndexSetString(0,PLOT_LABEL,short_name);
//---- end of initialization function
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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 &TickVolume[],
const long &Volume[],
const int &Spread[])
{
//
// Process data through MedianRenko indicator
//
if(!medianRenkoIndicator.OnCalculate(rates_total,prev_calculated,Time))
return(0);
//
// Make the following modifications in the code below:
//
// medianRenkoIndicator.GetPrevCalculated() should be used instead of prev_calculated
//
// medianRenkoIndicator.Open[] should be used instead of open[]
// medianRenkoIndicator.Low[] should be used instead of low[]
// medianRenkoIndicator.High[] should be used instead of high[]
// medianRenkoIndicator.Close[] should be used instead of close[]
//
// medianRenkoIndicator.IsNewBar (true/false) informs you if a renko brick completed
//
// medianRenkoIndicator.Time[] shold be used instead of Time[] for checking the renko bar time.
// (!) medianRenkoIndicator.SetGetTimeFlag() must be called in OnInit() for medianRenkoIndicator.Time[] to be used
//
// medianRenkoIndicator.Tick_volume[] should be used instead of TickVolume[]
// medianRenkoIndicator.Real_volume[] should be used instead of Volume[]
// (!) medianRenkoIndicator.SetGetVolumesFlag() must be called in OnInit() for Tick_volume[] & Real_volume[] to be used
//
// medianRenkoIndicator.Price[] should be used instead of Price[]
// (!) medianRenkoIndicator.SetUseAppliedPriceFlag(ENUM_APPLIED_PRICE _applied_price) must be called in OnInit() for medianRenkoIndicator.Price[] to be used
//
int _prev_calculated = medianRenkoIndicator.GetPrevCalculated();
//
//
//
//--- checking for bars count
if(rates_total<ExtADXPeriod)
return(0);
//--- detect start position
int start;
if(_prev_calculated>1) start=_prev_calculated-1;
else
{
start=1;
ExtPDIBuffer[0]=0.0;
ExtNDIBuffer[0]=0.0;
ExtADXBuffer[0]=0.0;
}
//--- main cycle
for(int i=start;i<rates_total && !IsStopped();i++)
{
//--- get some data
double Hi =medianRenkoIndicator.High[i];
double prevHi=medianRenkoIndicator.High[i-1];
double Lo =medianRenkoIndicator.Low[i];
double prevLo=medianRenkoIndicator.Low[i-1];
double prevCl=medianRenkoIndicator.Close[i-1];
//--- fill main positive and main negative buffers
double dTmpP=Hi-prevHi;
double dTmpN=prevLo-Lo;
if(dTmpP<0.0) dTmpP=0.0;
if(dTmpN<0.0) dTmpN=0.0;
if(dTmpP>dTmpN) dTmpN=0.0;
else
{
if(dTmpP<dTmpN) dTmpP=0.0;
else
{
dTmpP=0.0;
dTmpN=0.0;
}
}
//--- define TR
double tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl));
//---
if(tr!=0.0)
{
ExtPDBuffer[i]=100.0*dTmpP/tr;
ExtNDBuffer[i]=100.0*dTmpN/tr;
}
else
{
ExtPDBuffer[i]=0.0;
ExtNDBuffer[i]=0.0;
}
//--- fill smoothed positive and negative buffers
ExtPDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtPDIBuffer[i-1],ExtPDBuffer);
ExtNDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtNDIBuffer[i-1],ExtNDBuffer);
//--- fill ADXTmp buffer
double dTmp=ExtPDIBuffer[i]+ExtNDIBuffer[i];
if(dTmp!=0.0)
dTmp=100.0*MathAbs((ExtPDIBuffer[i]-ExtNDIBuffer[i])/dTmp);
else
dTmp=0.0;
ExtTmpBuffer[i]=dTmp;
//--- fill smoothed ADX buffer
ExtADXBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtADXBuffer[i-1],ExtTmpBuffer);
}
//---- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+