Recent bar problme in backtest

 

The code below runs perfectly fine in Real and demo account, however in the backtest, first bar (index==0) for all quotes received, this bar in particular, gets the same value for OHLC (equal open price) for CopyRates function.  Please tell me, is something wrong in the code, or what the problem.

Thanks in advance



//+------------------------------------------------------------------+
//|                                               CandleBackTest.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int TotalBars = 5; // Count Bars to Draw

int OnInit()
{
  return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   
}

void OnTick()
{
  string   Name   = "";
  color    Color  = clrNONE;
  MqlRates Rates[] = {};
  
  if (CopyRates(_Symbol,_Period,0,TotalBars,Rates)>0) {
    ArraySetAsSeries(Rates,true);
    for (int i=0;i<TotalBars;i++) {
           if (Rates[i].open<Rates[i].close) Color = clrLime;
      else if (Rates[i].open>Rates[i].close) Color = clrRed;
      else                                   Color = clrBlue;
      // Body
      Name = "Canlde." + IntegerToString(i) + ".Body";
      if (ObjectFind(0,Name)<0)
        ObjectCreate(0,Name,OBJ_TREND,0,0,0);
      ObjectSetInteger(0,Name,OBJPROP_TIME1 ,Rates[i].time); 
      ObjectSetDouble (0,Name,OBJPROP_PRICE1,Rates[i].open); 
      ObjectSetInteger(0,Name,OBJPROP_TIME2 ,Rates[i].time); 
      ObjectSetDouble (0,Name,OBJPROP_PRICE2,Rates[i].close); 
      ObjectSetInteger(0,Name,OBJPROP_COLOR,Color); 
      ObjectSetInteger(0,Name,OBJPROP_RAY_LEFT,false); 
      ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false); 
      ObjectSetInteger(0,Name,OBJPROP_WIDTH,5);
      // Shadow
      Name = "Canlde." + IntegerToString(i) + ".Shadow";
      if (ObjectFind(0,Name)<0)
        ObjectCreate(0,Name,OBJ_TREND,0,0,0);
      ObjectSetInteger(0,Name,OBJPROP_TIME1 ,Rates[i].time); 
      ObjectSetDouble (0,Name,OBJPROP_PRICE1,Rates[i].high); 
      ObjectSetInteger(0,Name,OBJPROP_TIME2 ,Rates[i].time); 
      ObjectSetDouble (0,Name,OBJPROP_PRICE2,Rates[i].low); 
      ObjectSetInteger(0,Name,OBJPROP_COLOR,Color); 
      ObjectSetInteger(0,Name,OBJPROP_RAY_LEFT,false); 
      ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false); 
      ObjectSetInteger(0,Name,OBJPROP_WIDTH,1);
      // Comment
      if (i==0) {
        Comment("\n Time : ",TimeToString(Rates[0].time),
                "\n Open : ",DoubleToString(Rates[0].open  ,_Digits),
                "\n High : ",DoubleToString(Rates[0].high  ,_Digits),
                "\n Low : ",DoubleToString(Rates[0].low    ,_Digits),
                "\n Close : ",DoubleToString(Rates[0].close,_Digits),
                "\n",
                "\n Time : ",TimeToString(Rates[1].time),
                "\n Open : ",DoubleToString(Rates[1].open  ,_Digits),
                "\n High : ",DoubleToString(Rates[1].high  ,_Digits),
                "\n Low : ",DoubleToString(Rates[1].low    ,_Digits),
                "\n Close : ",DoubleToString(Rates[1].close,_Digits));
      }
    }
  }     
}

 
DooMGuarD: The code below runs perfectly fine in Real and demo account, however in the backtest, first bar (index==0) for all quotes received, this bar in particular, gets the same value for OHLC (equal open price) for CopyRates function.  Please tell me, is something wrong in the code, or what the problem.
 Name = "Canlde." + IntegerToString(i) + ".Body";
  1. OHLC should not be equal except on the first tick. (Beyond that, I'm currently, too intoxicated to understand your post).
  2. Don't use a bar shift as a object name. After the start of the new bar, the name is wrong. Use:
     Name = "Canlde." + IntegerToString(Time[i]) + ".Body";

 
WHRoeder:
  1. OHLC should not be equal except on the first tick. (Beyond that, I'm currently, too intoxicated to understand your post).
  2. Don't use a bar shift as a object name. After the start of the new bar, the name is wrong. Use:

ok, nom problmem, but my question is


Why, this code

CopyRates(_Symbol,_Period,0,TotalBars,Rates)

return always in the first bar, only open price for Hihg, Low and close for every tick in backtest for bar index "0"....

Rates[0].open = read open price

Rates[0].High = read open price

Rates[0].low = read open price

Rates[0].close = read open price

in every tick for backtest....


best regards


Charles

 
Please check the size of the array
Rates
 

No problem for me :

MT4 build 722.

 

I get the same as the OP

Build 670 

Reason: