Download MetaTrader 5

Recent bar problme in backtest

To add comments, please log in or register
Charles Adriano
468
Charles Adriano  

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));
      }
    }
  }     
}

whroeder1
14776
whroeder1  
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";

Charles Adriano
468
Charles Adriano  
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

Carl Schreiber
6688
Carl Schreiber  
Please check the size of the array
Rates
Alain Verleyen
Moderator
31183
Alain Verleyen  

No problem for me :

MT4 build 722.

Keith Watford
Moderator
9641
Keith Watford  

I get the same as the OP

Build 670 

To add comments, please log in or register