Indicator not plotting on strategy tester

To add comments, please log in or register
mbsa
7
mbsa  

Hi,

I wrote an indicator that works fine with live data, but it doesn't draw forward on the strategy tester (prints attached).

Can someone please point out what am I getting wrong? I'm looking for answers for days but couldn't find any..

Thank!


//+------------------------------------------------------------------+
//|                                                       prev_v7.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   7
//--- plot A
#property indicator_label1  "A"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLimeGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot B
#property indicator_label2  "B"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot Signal_Long
#property indicator_label3  "Signal Long"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrWhite
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot Signal_Short
#property indicator_label4  "Signal Short"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  clrDodgerBlue
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot Exit_Long
#property indicator_label5  "Exit Long"
#property indicator_type5   DRAW_ARROW
#property indicator_color5  clrWhite
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot Exit_Short
#property indicator_label6  "Exit Short"
#property indicator_type6   DRAW_ARROW
#property indicator_color6  clrDodgerBlue
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot EMA
#property indicator_label7  "EMAA"
#property indicator_type7   DRAW_NONE
#property indicator_color7  clrDodgerBlue
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1
//---
double         ABuffer[];
double         BBuffer[];
double         Signal_LongBuffer[];
double         Signal_ShortBuffer[];
double         Exit_LongBuffer[];
double         Exit_ShortBuffer[];
double         EMA_Buffer[];
int            MA_handle;

input int lb = 10;
input double sig = 3.5;
input double exit = 1.5;
input int ema = 500;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,BBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,Signal_LongBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,Signal_ShortBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,Exit_LongBuffer,INDICATOR_DATA);
   SetIndexBuffer(5,Exit_ShortBuffer,INDICATOR_DATA);
   SetIndexBuffer(6,EMA_Buffer,INDICATOR_DATA);

   MA_handle = iMA(_Symbol,_Period,ema,0,MODE_EMA,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
      
   int limit = rates_total-prev_calculated;
   
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
   CopyBuffer(MA_handle,0,0,to_copy,EMA_Buffer);
   
   for(int i = lb -1; i<limit ;i++)
   {
      double u = 0, d = 0, s_long = 0, s_short = 0, e_long = 0, e_short = 0, ema_v = 0;
      
      for(int j=0; j<lb; j++)
      {
         if(close[i-j]>open[i-j]) u += close[i-j]-open[i-j];
         if(close[i-j]<open[i-j]) d += open[i-j]-close[i-j];
         
         ema_v = EMA_Buffer[i];
         
         
         if(u>(sig*d) && close[i]>ema_v) s_long = u+50;
            else {
                  s_long = EMPTY_VALUE;
                 }
         
         if(d>(sig*u) && close[i]<ema_v) s_short = d-50;
            else {
                  s_short = EMPTY_VALUE;
                 }
         
         if((d*exit)>u) e_long = u+20; 
            else {
                  e_long = EMPTY_VALUE;
                 }
         
         if((u*exit>d)) e_short = d-20 ;
            else {
                  e_short = EMPTY_VALUE;
                 } 
        
      ABuffer[i] = u;
      BBuffer[i] = d;
      Signal_LongBuffer[i] = s_long;
      Signal_ShortBuffer[i] = s_short;
      Exit_LongBuffer[i] = e_long;
      Exit_ShortBuffer[i] = e_short;
       }
    
   }
   

//--- return value of prev_calculated for next call
   return(rates_total-1);
  }
//+------------------------------------------------------------------+
Files:
unnamed.png 13 kb
tester.PNG 91 kb
Alain Verleyen
38876
Alain Verleyen  
mbsa:

Hi,

I wrote an indicator that works fine with live data, but it doesn't draw forward on the strategy tester (prints attached).

Can someone please point out what am I getting wrong? I'm looking for answers for days but couldn't find any..

Thank!


It's not related to the Strategy Tester. It doesn't update on a live chart either, it only works on history data.

   // On live update this will only be different from 0 on a new candle (=1), is it your intention ?
   int limit=rates_total-prev_calculated;

   int to_copy;
   // Useless as prev_calculated is NEVER greater than rates_total or lower than 0 (this is not allowed by the platform).
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;            
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
   // EMA_Buffer is an indicator buffer, how is it indexed ? What EMA_Buffer values (indexes) the CopyBuffer will update ?
   CopyBuffer(MA_handle,0,0,to_copy,EMA_Buffer);   // ALWAYS check the return value of CopyBuffer(), it can fail.

   ... // I didn't check further
To add comments, please log in or register