Donchian Channel indicator for MQL5 need help - page 2

 
patagonia2015:

Also, I forgot to tell I'm optimizing on "Open Price Only". I know, I haven't add the code for the check of the EA only when there is a new bar on the chart, I will add that later.

That will speed things up considerably.

But probably not as much as moving the call to iATR() and iCustom() out of OnTick(). These calls return handles, not values, in MQL5. You only need to call them once over the lifetime of your EA.

 

Create the indicator handle(s) in OnInit() function, and only calculate the last bar.

For the Donchian you don't have to call the indicator at all, you can do the calculations directly in the EA.
 
Anthony Garot:

That will speed things up considerably.

But probably not as much as moving the call to iATR() and iCustom() out of OnTick(). These calls return handles, not values, in MQL5. You only need to call them once over the lifetime of your EA.

Thank you so much, that really makes the difference!!! It worked.

 
Marco vd Heijden:

Create the indicator handle(s) in OnInit() function, and only calculate the last bar.

For the Donchian you don't have to call the indicator at all, you can do the calculations directly in the EA.

Marco, can you help me with that. I'm not sure of how to do that. I was thinking the indicator was calculating itself again on each bar, wich is something is not good at all.

I'm not how to do it. Thanks for the help so far.

 
int Donchian_Period=20;

int index_high=iHighest(Symbol(),PERIOD_D1,MODE_HIGH,Donchian_Period,0);
double Donchian_High=iHigh(Symbol(),PERIOD_D1,index_high);

int index_low=iHighest(Symbol(),PERIOD_D1,MODE_LOW,Donchian_Period,0);
double Donchian_Low=iLow(Symbol(),PERIOD_D1,index_low);
 

Forum on trading, automated trading systems and testing trading strategies

Donchian Channel indicator for MQL5 need help

Marco vd Heijden, 2019.01.15 17:36

int Donchian_Period=20;

int index_high=iHighest(Symbol(),PERIOD_D1,MODE_HIGH,Donchian_Period,0);
double Donchian_High=iHigh(Symbol(),PERIOD_D1,index_high);

int index_low=iHighest(Symbol(),PERIOD_D1,MODE_LOW,Donchian_Period,0);
double Donchian_Low=iLow(Symbol(),PERIOD_D1,index_low);

Simple typo . . . it should be iLowest.

OP may also want a middle channel, which is simply MathAvg(Donchian_High,Donchian_Low).
 
int Donchian_Period=20;

int index_high=iHighest(Symbol(),PERIOD_D1,MODE_HIGH,Donchian_Period,0);
double Donchian_High=iHigh(Symbol(),PERIOD_D1,index_high);

int index_low=iLowest(Symbol(),PERIOD_D1,MODE_LOW,Donchian_Period,0);
double Donchian_Low=iLow(Symbol(),PERIOD_D1,index_low);

double Donchian_Center = (Donchian_High+Donchian_High)/2;
Find the typo/
 
Marco vd Heijden:
Find the typo/

Thanks for the help guys, but I found some trouble including the code in the FOR cycle inside the indicator file.

But I think what you are saying is not use the FOR cycle at all right?, like you said, just calculate the last value and nothing more. Please let me know if that is what you say, because I was thinking the code for the indicator could be improved. The weird thing is that I have the Donchian and Metatarder 4 and it works without a problem (and calculate not only one value).

In the EA I need to calculate the [0] and the [1] value for the indicator, please let me know if that is what you said, I'm sorry for the misunderstanding.

 

Look at the code.

It is self explanatory.

 

Thanks for the help. I have the calculations for the Donchian now.

I wanted to ask regarding the ATR calculations. So far I don't know how to calculate any indicator only once without the creation of an array as in the code. In MQL4 it was easy:

double ATR=iATR(NULL,0,ATR_Period,0);

But on MQL5 is not the same and I don't know if there is a better way to call the ATR indicator for one candle, since on the call it doesn't ask for the Shift, as in MQL4:

ATR = iATR(_Symbol, _Period, Periodo_ATR);

I want to make this right also, to have better EA's in the future.

Reason: