icustom return zero when test multitime

 

Hello friends, I hope you are healthy. Excuse me, my English is very weak. The problem is that when I test the daily timeframe in icustom, it returns zero and does not enter the deal, but it works properly in the monthly timeframe. What is the problem?

//+------------------------------------------------------------------+
//|                                                  mn_bo_price.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include  <Trade\Trade.mqh>
CTrade trade;
CPositionInfo  m_position;
int    v_symb_count;
string v_symb_name="BTCUSD";
double v_low_pday;
int v_pik_cnt=0;
ENUM_TIMEFRAMES    tf=PERIOD_D1;
bool   v_frs_res_find;
int    v_indx;
double      zz[];                // array for the indicator iMA
int         zz_handle;           // handle of the indicator iMA
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   zz_handle=iCustom(v_symb_name,PERIOD_MN1,"ZigZag (separate)",3,3,3);
//Comment("zz_handle = ",zz_handle,"  error = ",GetLastError());
   return(0);
//  if(zz_handle==INVALID_HANDLE) { Comment("zz_handle, error: ",_LastError); return INIT_FAILED; }
// else Comment("zz_handle, ok");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
/* void LongPositionClose()
 {
  for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions
     if(m_position.SelectByIndex(i))
           if(m_position.PositionType()==POSITION_TYPE_BUY)
                  trade.PositionClose(m_position.Ticket());
 }*/
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int v_pivot_mn1=0;
   double v_prc_close_shf1=0;
   double v_prc_hi_pvt=0;
   /*double v_price_shif1=iClose(v_symb_name,PERIOD_D1,1);
   double v_price_shif2=iClose(v_symb_name,PERIOD_D1,2);
    if( v_price_shif1<v_price_shif2)
      {
        LongPositionClose();
      }*/
   int bar_calc=BarsCalculated(zz_handle);
//Comment("bar_calc = ",bar_calc,"  error = ",GetLastError());
// zz_handle=iCustom(v_symb_name,PERIOD_MN1,"ZigZag (separate)",3,3,3);
//Comment("zz_handle = ",zz_handle,"  error = ",GetLastError());
   int cp_bf=CopyBuffer(zz_handle,0,0,TERMINAL_MAXBARS,zz);
   Comment("cp_bf = ",cp_bf,"  error = ",GetLastError());
   ArraySetAsSeries(zz,true);
   v_indx=1;
   v_frs_res_find=false;
   double ask=NormalizeDouble(SymbolInfoDouble(v_symb_name,SYMBOL_ASK),SymbolInfoInteger(v_symb_name,SYMBOL_DIGITS));
   double point=SymbolInfoDouble(v_symb_name,SYMBOL_POINT);
// Comment("cp_bf="+cp_bf);
//Comment(v_indx);
   Comment("cp_bf="+cp_bf+"--zz[v_indx+1]="+zz[v_indx+1]+"--zz_handle = ",zz_handle,"  error = ",GetLastError());
   while(!v_frs_res_find && v_indx+1<=zz_handle)  //&& v_indx<rsiHandle_daily
     {
      // Comment("zz[v_indx+1]="+zz[v_indx+1]);
      if(zz[v_indx+1]>zz[v_indx] && zz[v_indx+1]>zz[v_indx+2])
        {
         v_pivot_mn1=v_indx+1;
         v_prc_close_shf1=iClose(v_symb_name,PERIOD_MN1,1);
         v_prc_hi_pvt=iHigh(v_symb_name,PERIOD_MN1,v_pivot_mn1);
         if(v_prc_close_shf1>v_prc_hi_pvt)
           {
            //Comment("v_indx+1="+v_indx+1);
            if(OrdersTotal()==0 && PositionsTotal()==0)
              {
               //trade.Buy(0.05,v_symb_name,ask,0,ask+2500000*point,"");
               trade.Buy(0.05,v_symb_name,ask,0,0,"");
               Comment("Buy() method failed. Return code=",trade.ResultRetcode(),
                       ". Code description: ",trade.ResultRetcodeDescription());
              }
            v_frs_res_find=true;
            break;
           }
         break;
        }
      else
        {
         v_indx++;
        }
     } //end while
  }
//+------------------------------------------------------------------+


 
Someone can help me؟
 

An example of how to work with ZigZag:

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2018.12.27 11:32

An example of working with the ZigZag indicator

Code: ZigZag Example.mq5

Pay attention to the extremum search algorithm: if the value in the indicator buffer is not equal to "0.0" and not equal to "PLOT_EMPTY_VALUE" - it means that We have detected an extremum.


The extremum search goes to " ZigZag: how many candles to check back " bars.


Algorithm of work standard: ONCE in OnInit () we create an indicator.

//---
int   handle_iCustom;                  // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iCustom
   handle_iCustom=iCustom(Symbol(),Period(),"Examples\\ZigZag",InpDepth,InpDeviation,InptBackstep);
//--- if the handle is not created 
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }


Next in OnTick (), we make a copy of the indicator data, while using the indicator handle.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static long counter=0;
   counter++;
   if(counter>=15)
      counter=0;
   else
      return;
//---
   double ZigzagBuffer[];
   ArraySetAsSeries(ZigzagBuffer,true);
   int start_pos=0,count=InpCandlesCheck+1;
   if(!iGetArray(handle_iCustom,0,start_pos,count,ZigzagBuffer))
      return;

   string text="";
   for(int i=0;i<count;i++)
     {
      if(ZigzagBuffer[i]!=PLOT_EMPTY_VALUE && ZigzagBuffer[i]!=0.0)
         text=text+"\n"+IntegerToString(i)+": "+DoubleToString(ZigzagBuffer[i],Digits());
     }
   Comment(text);
  }


The function by which the data is copied indicator:

//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      Print("This a no dynamic array!");
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code 
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code 
      PrintFormat("Failed to copy data from the indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false);
     }
   return(result);
  }


Result of work:

An example of working with the ZigZag indicator


 

Thank you for your answer But my question and problem is that I can use monthly time frame in icustom, but in daily time frame backtest, I also used zigzag separator, not the usual zigzag

ZigZag separate link:

https://www.mql5.com/en/code/20143

ZigZag separate
ZigZag separate
  • www.mql5.com
ZigZag separate is a MetaTrader 5 version of one indicator that was floating around the net as a wonder indicator for MetaTrader 4.
 

The details are as follows when I select the daily timeframe tester strategy:

2021.06.04 18:24:25.639 login (build 2940)
2021.06.04 18:24:25.670 account info found with currency RLS
2021.06.04 18:24:25.674 expert file added: Experts\mn_bo_price.ex5. 30229 bytes loaded
2021.06.04 18:24:25.695 calculate profit in pips, initial deposit 100000, leverage 1:100
2021.06.04 18:24:25.695 successfully initialized
2021.06.04 18:24:25.695 29 Kb of total initialization data received
2021.06.04 18:24:25.695 Intel Core i3  M 330 @ 2.13GHz, 3956 MB
2021.06.04 18:24:25.717 BTCUSD: symbol to be synchronized
2021.06.04 18:24:25.718 BTCUSD: symbol synchronized already, 18 bytes received
2021.06.04 18:24:25.719 BTCUSD: history synchronization started
2021.06.04 18:24:25.726 BTCUSD: load 27 bytes of history data to synchronize in 0:00:00.004
2021.06.04 18:24:25.726 BTCUSD: history synchronized from 2011.03.24 to 2021.06.03
2021.06.04 18:24:26.473 BTCUSD,Daily: history cache allocated for 802 bars and contains 627 bars from 2019.01.01 00:00 to 2020.09.30 00:00
2021.06.04 18:24:26.473 BTCUSD,Daily: history begins from 2019.01.01 00:00
2021.06.04 18:24:26.476 BTCUSD,Daily (MofidSecurities-Server): OHLC bar states generating. OnTick executed on the bar begin only
2021.06.04 18:24:26.476 BTCUSD,Daily: testing of Experts\mn_bo_price.ex5 from 2020.10.01 00:00 to 2021.06.03 00:00 started
2021.06.04 18:24:26.488 program file added: \Indicators\ZigZag (separate).ex5. 27955 bytes loaded
2021.06.04 18:24:27.236 BTCUSD,Monthly: history cache allocated for 31 bars and contains 21 bars from 2019.01.01 00:00 to 2020.09.01 00:00
2021.06.04 18:24:27.237 BTCUSD,Monthly: history begins from 2019.01.01 00:00
2021.06.04 18:24:27.598 2020.10.01 00:00:00   array out of range in 'mn_bo_price.mq5' (104,12)
2021.06.04 18:24:27.598 OnTick critical error
2021.06.04 18:24:27.606 BTCUSD,Daily: 1 ticks, 1 bars generated. Environment synchronized in 0:00:00.082. Test passed in 0:00:01.885 (including ticks preprocessing 0:00:00.359).
2021.06.04 18:24:27.606 BTCUSD,Daily: total time from login to stop testing 0:00:01.967 (including 0:00:00.082 for history data synchronization)
2021.06.04 18:24:27.606 303 Mb memory used including 0.94 Mb of history data, 64 Mb of tick data
2021.06.04 18:24:27.606 log file "C:\Users\hheid\AppData\Roaming\MetaQuotes\Tester\2506E8E7E4116548D478CE2C3598FAB1\Agent-127.0.0.1-3000\logs\20210604.log" written
2021.06.04 18:24:27.606 test Experts\mn_bo_price.ex5 on BTCUSD,Daily thread finished
2021.06.04 18:24:27.713 prepare for shutdown
 
After trying to troubleshoot, I realized that when the time frame is selected in the daily back test, the output value of icustom is always 0. What is the solution? Thank you.
Reason: