MT5 - Bug in strategy tester? Creating trendline

 

Hi,

I have coded an indicator, which creates trendlines (MT5). It is working without any problems with live ticks - but I am not able to get it working in strategy tester. Please have a look at the code below for creating the trendlines - with the test data, it should create a short horizontal line between two points. Pls find the result below the code

//+------------------------------------------------------------------+
//|  Creating short horizontal lines                                 |
//+------------------------------------------------------------------+
void CreateTline
(
   long     chart_id,      // chart ID
   string   name,          // object name
   int      subWindow,     // window index
   double   price,         // horizontal level price
   datetime leftTime,      // leftTime
   datetime rightTime,     // rightTime
   color    Color,         // line color
   int      style,         // line style
   int      width,         // line width
   bool     background,    // background display of the line
   string   text           // text
)
 
{
   ResetLastError();

   //--- test input
   price = 1.33125;
   leftTime = D'2017.01.05 00:00:00';
   rightTime = D'2017.01.08 00:00:00';
   //--- end test input

   //--- return if h-line exists
   if (ObjectGetInteger(chart_id,name,OBJPROP_TYPE) == OBJ_TREND && 
       ObjectGetDouble(chart_id,name,OBJPROP_PRICE,0) == price &&
       ObjectGetInteger(chart_id,name,OBJPROP_TIME,2) == rightTime) return; 
      
   
   if (!ObjectCreate(chart_id,name,OBJ_TREND,subWindow,leftTime,price,rightTime,price))
   {
       Print("CreateTline---------- Failed to create a trend line! Error code = ", GetLastError());
       return;   
   }    
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetInteger(chart_id,name,OBJPROP_BACK,background);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY_LEFT,false);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,false);
   ObjectSetInteger(chart_id,name,OBJPROP_HIDDEN,false); 
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,false);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,false);

   
 }

Result in strategy tester:



So the starting point of the line is correct, but not the end point. Again, the indicator is working flawlessly on live ticks, but I need the strategy tester for optimizing.

Any ideas would be appreciated...

Regards,

blackfriday






 

further tests show, that the first point (leftTime, price) will be ignored by the strat tester. Now reported to the service desk.

 

Set anchor points separately and it will work.

 
Doerk Hilger:

Set anchor points separately and it will work.

Thx for your reply. As far as I understand anchor points and corners, they are not needed / valid for trend lines. Or am I wrong?
 
blackfriday:
Thx for your reply. As far as I understand anchor points and corners, they are not needed / valid for trend lines. Or am I wrong?
Dirk meant the trendline anchor points (time1,price1) and (time2,price2).
 

I am sorry, I still don't get it...

How can I set the points (time1, price1) and (time2,price2) of a trendline other than I did?

if (!ObjectCreate(chart_id,name,OBJ_TREND,subWindow,time1,price,time2,price))
   {
       Print("CreateTline---------- Failed to create a trend line! Error code = ", GetLastError());
       return;   
   }    
BTW, this is a copy from here and it works perfectly well outside the strategy tester
 
blackfriday:

I am sorry, I still don't get it...

How can I set the points (time1, price1) and (time2,price2) of a trendline other than I did?

BTW, this is a copy from here and it works perfectly well outside the strategy tester
Please provide a code that compiles, so I can reproduce your issue.
 
Alain Verleyen:
Please provide a code that compiles, so I can reproduce your issue.
Thx for your help... please find an example code from an indi below. It shows daily open lines on the chart and is working well. Please attach it to a profile and load the profile while testing a whatever expert in the strategy tester. You will see what I mean
//+------------------------------------------------------------------+
//|                                               X_ShowDOL_test.mq5 |

//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "blackfriday 2016"
#property version   "1.0"

#property indicator_chart_window 
#property indicator_buffers 3
#property indicator_plots   0



//+------------------------------------------------+
//| Indicator input parameters                     |
//+------------------------------------------------+


sinput string              A0;                                       //GENERAL SETTINGS
input bool                 OnOff = true;                             //Indicator on

sinput string              A21;                                      // ................................................................
sinput string              A2;                                       //DOL AND YESTERDAY'S H/L
input bool                 ShowDOL = true;                           //Show DOL Marker
input color                DOLMarkerColor = clrCornflowerBlue;       //Color of DOL Marker
input int                  MarkerLength = 0;                         //DOL Marker length [points]; if 0 full length


//+----------------------------------------------+



double lowArray[], highArray[], openArray[];
datetime timeArray[];
datetime leftTime, rightTime;
double leftPrice;
int win, ChartWidth;
MqlDateTime timeStruct, timeStructCurrent;
double high_, low_, open_, close_;
int X_Distance = 190;


// Number of daily open lines
int NoOfDOL = 100;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//----
   SetIndexBuffer(0,lowArray,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1,highArray,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,openArray,INDICATOR_CALCULATIONS);
   
   
   
   //--- chart width in pixels
   ChartWidth = int(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0));
   
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+    
void OnDeinit(const int reason)
  {
  
   DeleteChartObjects();
   ChartRedraw(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[]
                )
{
  
   if (!OnOff) return(rates_total); 
  
   
      
   if (ShowDOL == true)
   {
      ArraySetAsSeries(open,true);
      ArraySetAsSeries(openArray,true);
      ArraySetAsSeries(time,true);
      ArraySetAsSeries(timeArray,true);
      CopyOpen(_Symbol,PERIOD_D1,0,NoOfDOL,openArray);
      CopyTime(_Symbol,PERIOD_D1,0,NoOfDOL,timeArray);
         
      if (_Period <= PERIOD_H4) 
      {
         for (int i = 0; i < NoOfDOL; i++)
         {
            int x, y;
            double price;
            long chart_id = 0;
            string name = "DOL" + IntegerToString(i);
               
            //-- no weekends
            TimeToStruct(timeArray[i],timeStruct);
            TimeToStruct(TimeCurrent(),timeStructCurrent);
            if (timeStruct.day_of_week != 0) 
            {
               ChartTimePriceToXY(0,0,timeArray[i],openArray[i],x,y);
               if (x > 0 && x < ChartGetInteger(0,CHART_WIDTH_IN_PIXELS))
               {
                  if (MarkerLength != 0)
                  {
                     ChartXYToTimePrice(0,x + MarkerLength,y,win,rightTime,price);
                     ObjectCreate(chart_id,name,OBJ_TREND,0,timeArray[i],openArray[i],rightTime,openArray[i]);
                  }
                  else
                  {
                     if (i == 0) ObjectCreate(chart_id,name,OBJ_TREND,0,timeArray[i],openArray[i],TimeCurrent(),openArray[i]);
                     else ObjectCreate(chart_id,name,OBJ_TREND,0,timeArray[i],openArray[i],timeArray[i-1],openArray[i]);  
                  }     
                  ObjectSetInteger(chart_id,name,OBJPROP_COLOR,DOLMarkerColor);
                  ObjectSetInteger(chart_id,name,OBJPROP_STYLE,STYLE_SOLID);
                  ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,2);
                  ObjectSetString(chart_id,name,OBJPROP_TEXT,name);
                  ObjectSetInteger(chart_id,name,OBJPROP_BACK,false);
                  ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
                  ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,false);
                  ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,false);
                    
               }
            }
         
         }
      }
   }
  
   
//----
   ChartRedraw(0); 
   return(rates_total);
}
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//|  Relocation of short horizontal line                             |
//+------------------------------------------------------------------+
void SetTline
(
   long     chart_id,      // chart ID
   string   name,          // object name
   int      nwin,          // window index
   double   price,         // horizontal level price
   color    Color,         // line color
   int      style,         // line style
   int      width,         // line width
   bool     background,    // background display of the line
   string   text           // text
)
//---- 
  {
//----
   
    CreateTline(chart_id,name,nwin,price,Color,style,width,background,text);
    ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
    ChartXYToTimePrice(0,ChartWidth - X_Distance,0,win,leftTime,leftPrice);
    ChartXYToTimePrice(0,ChartWidth - X_Distance - 120,0,win,rightTime,leftPrice);   
    ObjectMove(chart_id,name,int(leftTime),rightTime,price);
    ObjectMove(chart_id,name + "L",int(leftTime),rightTime,price);
   
   
}


//+------------------------------------------------------------------+
//|  Creating short horizontal lines                                 |
//+------------------------------------------------------------------+
void CreateTline
(
   long     chart_id,      // chart ID
   string   name,          // object name
   int      nwin,          // window index
   double   price,         // horizontal level price
   color    Color,         // line color
   int      style,         // line style
   int      width,         // line width
   bool     background,    // background display of the line
   string   text           // text
)
 
{
   ChartXYToTimePrice(0,ChartWidth - X_Distance,0,win,leftTime,leftPrice);
   if (StringSubstr(name,0,2) == "PW" ) ChartXYToTimePrice(0,ChartWidth - X_Distance - 150,0,win,rightTime,leftPrice);
   else ChartXYToTimePrice(0,ChartWidth - X_Distance - 120,0,win,rightTime,leftPrice);
   if (!ObjectCreate(chart_id,name,OBJ_TREND,nwin,leftTime,price,rightTime,price))
   {
       Print("CreateTline---------- Failed to create a trend line! Error code = ", GetLastError());
       return;   
   }    
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
   ObjectSetInteger(chart_id,name,OBJPROP_BACK,background);
   ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,false);
   ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,false);
  
   
//-----------------------------------------------------
}


void DeleteChartObjects()
{
   //---- Delete levels
   
   ObjectDelete(0,"DOL");
   ObjectDelete(0,"DOL" + "L");
   for (int i = 0; i <= NoOfDOL; i++) ObjectDelete(0,"DOL" + IntegerToString(i));
   
   
   
}   

edit: extra spaces reduced
 

Hi Alain,

any idea on the trendline problem?


BR

blackfriday

 
is there a way i could use any line atol in mt5 strategy tester? whether it be trendline, horizontal line, vertical line ? 
 
@xxxxtttt777 #: is there a way i could use any line atol in mt5 strategy tester? whether it be trendline, horizontal line, vertical line ? 

Your question is unclear. You can use all of those types of line objects in the tester, both on MT4 and MT5 Strategy Tester. So, please explain your question in more detail.

Reason: