Donchian Channel indicator for MQL5 need help

 

Hello,


I have been looking for a Donchian Channel indicator to test some strategies. I have found a lot of indicators in this website, however some have problems of buffers (don't consider the current candle) and ALL of them have a problem. The problem is when I use them on a strategy (whatever strategy, lets assume the buy when the upper channel is going higher...), when backtesting or optimizing is taking too much time to get the results. I think the code is using way to much resources (much more than need it).

I'm not an expert but seems the code is wrong since the same optimization on MT4 takes WAY LESS time to finish, I mean 100 times less.


This is the code I have for the indicator, please help me understand what is wrong and why it takes so much time, considering the MT5 is much faster than MT4, obviously is something wrong with the code. Thanks!

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Label1
#property indicator_label1  "High"
#property indicator_type1   DRAW_LINE
#property indicator_color1  C'0,96,255'
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Label2
#property indicator_label2  "Low"
#property indicator_type2   DRAW_LINE
#property indicator_color2  C'255,96,96'
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int InpChannelPeriod=20; // Period
//--- indicator buffers
double ExtHighBuffer[];
double ExtLowBuffer[];
//---
int i,limit,start;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtHighBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLowBuffer,INDICATOR_DATA);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- set first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpChannelPeriod);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpChannelPeriod);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- check for rates
   if(rates_total<InpChannelPeriod) return(0);
//--- preliminary calculations
   if(prev_calculated==0) limit=InpChannelPeriod;
   else limit=prev_calculated;
//--- the main loop of calculations
   for(i=limit;i<rates_total && !IsStopped();i++)
     {
      start=i+1-InpChannelPeriod;
      ExtHighBuffer[i]=high[ArrayMaximum(high,start,InpChannelPeriod)];
      ExtLowBuffer[i]=low[ArrayMinimum(low,start,InpChannelPeriod)];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 

You only need the indicator for visualization, not to trade.

Make an EA that uses the same formula for testing purposes and it will be fast.

 
Marco vd Heijden:

You only need the indicator for visualization, not to trade.

Make an EA that uses the same formula for testing purposes and it will be fast.

Thanks for your response. I did what you said and it's a little faster but not like the version I have for MQL4 of the donchian channel, something is seriously bad with the code.

Mt5 is supposed to be faster than the MT4, and in MT4 the optimization is way faster than this version, so something is bad with the code for sure.

Please someone that can help me. The code is not long, so it must be very easy for someone that really knows MQL5.

 

It looks like you have slow hardware.

What code have you tried ?

Except from that indicator.
 
Marco vd Heijden:

It looks like you have slow hardware.

What code have you tried ?

Except from that indicator.

My friend I don't have a slow hardware, as I explained before, I run the same EA on MT4 and is way faster. My notebook is I7 and 16GB ram. 

I have test a lot of codes in MT4 and MT5, and this is way slower than the rest, like I said, by a factor of 100 at least.

 

Again what code have you tried ?

You can say it is slow all day long but without code there is nothing we can do.

 

So, you don't see anything wrong with the code I posted (the one in MQL5)?

I'm new to MQL5, and the version of MT4 I have for the Donchian is much faster. 

I don't know what else to tell you, Like I said is much faster the test in MQL4 for the SAME strategy than in MQL5, the only difference comes from the indicator.

Hope someone can help with the code for the Donchian indicator, I have tried many on this website and all are slow compared to MT4, I don't know why, all the other robots I have on MT5 use common indicators (like momentum, RSI, Moving Averages), indicators already defined in MQL5, in this case I loked the Donchian Channel and called with "iCustom", have no errors and make trades, but really slow. I think is something to do with the calculations of the indicators itself.

 
patagonia2015:

My friend I don't have a slow hardware, as I explained before, I run the same EA on MT4 and is way faster. My notebook is I7 and 16GB ram. 

I have test a lot of codes in MT4 and MT5, and this is way slower than the rest, like I said, by a factor of 100 at least.

If you test EAs, and compare them on MT4 and MT5, you need to post EA code, not an indicator code.

The indicator you posted is not the problem.

 

Please also post the terminal build.

I am also experiencing extreme lagging issues on the latest beta.

 
Marco vd Heijden:

Please also post the terminal build.

I am also experiencing extreme lagging issues on the latest beta.

Hello,

Thanks for the help, here I add the code of the expert. I'm not an expert and start using other code as base and make changes to make what I want.

The build is Version: 5.00 build 1940, 02 nov 2018.

Please bear in mind I use a library for making orders the same way as in MQL4, because I need it to use the Magic Number so the robot don't interfire with others, and I didn't know how to do that on MQL5 code.

The library is this one ( https://www.mql5.com/en/code/16006 ) I have use it the same way to make orders for others EA and they work fine, is just this EA that I have problem, that's why I think it was the indicator and not the EA code.


#include <Trade/Trade.mqh>
#include <MT4Orders.mqh>

CTrade trade;
long Magic_Number=85;
input int BarsToCount=20;
input double Factor_stop=1;
input int Periodo_ATR=10;
double lot=0.1;

int OnInit()
{return(INIT_SUCCEEDED);}

void OnDeinit(const int reason)
{}

void OnTick()
  {
  
MqlRates PriceInfo [];
ArraySetAsSeries (PriceInfo,true);
int Data=CopyRates(Symbol(),Period(),0,3,PriceInfo);

double myATRArray[];
int myATRDefinition = iATR(_Symbol, _Period, Periodo_ATR);
ArraySetAsSeries (myATRArray, true);
CopyBuffer (myATRDefinition, 0, 1, 1, myATRArray);
double myATRValue= NormalizeDouble(myATRArray[0], 5);

double UpperBandArray [];
double LowerBandArray [];
ArraySetAsSeries(UpperBandArray,true);
ArraySetAsSeries(LowerBandArray,true);
int DonchianDefinition = iCustom(NULL,0,"DonchianChannel",BarsToCount);
CopyBuffer(DonchianDefinition,0,0,2,UpperBandArray);
CopyBuffer(DonchianDefinition,1,0,2,LowerBandArray);
double Donchian_upper0=NormalizeDouble(UpperBandArray[0],5);
double Donchian_lower0=NormalizeDouble(LowerBandArray[0],5);
double Donchian_upper1=NormalizeDouble(UpperBandArray[1],5);
double Donchian_lower1=NormalizeDouble(LowerBandArray[1],5);


double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);

double StopLossPips=Factor_stop*myATRValue; 

   if((PositionsTotal()==0) && (Donchian_upper0>Donchian_upper1) && (Donchian_lower0>Donchian_lower1))
      OrderSend(Symbol(),OP_BUY,lot,Ask,30,Ask-StopLossPips,0,"",Magic_Number,0,clrGreen);  

   if((PositionsTotal()==0) && (Donchian_upper0<Donchian_upper1) && (Donchian_lower0<Donchian_lower1))
      OrderSend(Symbol(),OP_SELL,lot,Bid,30,Bid+StopLossPips,0,"",Magic_Number,0,clrRed); 

   for( int i=OrdersTotal()-1;i>=0;i-- ) {
      
      if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES )){
      
         if(OrderType()==OP_BUY && Magic_Number==85 && PriceInfo[0].close<Donchian_lower1) OrderClose(OrderTicket(),OrderLots(),Bid,30,CLR_NONE); 
         if(OrderType()==OP_SELL && Magic_Number==85 && PriceInfo[0].close>Donchian_upper1) OrderClose(OrderTicket(),OrderLots(),Ask,30,CLR_NONE);
}}}
MT4Orders
MT4Orders
  • www.mql5.com
This library allows to work with the orders in MQL5 (MT5-hedge) in the same way as in MQL4. That is, the order language system (OLS) becomes identical to MQL4. At the same time, it is still possible to use the MQL5 order system in parallel. In particular, the standard MQL5 library will continue to fully operate. It is not necessary to choose...
 

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.

Like I said, it's really strange since is way slower the backtest than other EA on MQL5 I have and the same on MT4. Thanks!

Reason: