ChartIndicatorAdd not working with strategy tester ?

To add comments, please log in or register
Richard
18
Richard  

Hi, i'm working on custom indicator but when i added ChartIndicatorAdd that doesn't work on strategy tester, but that's work fine on the main MT5 program.

Here is a sample code:


//+------------------------------------------------------------------+
//|                                       test-ChartIndicatorAdd.mq5 |
//|                                        Copyright 2016, CPM Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, CPM Corp."
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot RSI
#property indicator_label1  "RSI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      InpMaPeriod=10;
input int      InpRsiPeriod=13;
//--- indicator buffers
double         RsiBuffer[];
double         MaBuffer[];
int            MaHandle;
int            RsiHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RsiBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,MaBuffer,INDICATOR_CALCULATIONS);
  
   RsiHandle=iRSI(_Symbol,_Period,InpRsiPeriod,PRICE_CLOSE);
   if(RsiHandle==INVALID_HANDLE)
   {
      Print("The RSI handle is not created: Error ",GetLastError());
      return(INIT_FAILED);
   }

   MaHandle=iMA(_Symbol,_Period,InpMaPeriod,0,MODE_EMA,PRICE_CLOSE);
   if(MaHandle==INVALID_HANDLE)
   {
      Print("The MA handle is not created: Error ",GetLastError());
      return(INIT_FAILED);
   }
   if(!ChartIndicatorAdd(0,0,MaHandle))
   {
      Print("Failed to add MaHandle indicator to chart window: Error ",GetLastError());
   }

//---
   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[])
  {
//---
   CopyBuffer(RsiHandle,0,0,rates_total,RsiBuffer);
   CopyBuffer(MaHandle,0,0,rates_total,MaBuffer);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


Is there something wrong in this code ?

Marco vd Heijden
Moderator
13003
Marco vd Heijden  

if you mean you want the indicator to be visually present then you have to load it manually on the chart.

The indicators are not visually present in the tester because it slows down the speed of testing.

Richard
18
Richard  
Marco vd Heijden:

if you mean you want the indicator to be visually present then you have to load it manually on the chart.

The indicators are not visually present in the tester because it slows down the speed of testing.

Thanks Marco for your answer.

Do you mean that it's possible to load manually an indicator on strategy tester ? How ?

Marco vd Heijden
Moderator
13003
Marco vd Heijden  
Press pause load indicator then continue testing ?
Alain Verleyen
40727
Alain Verleyen  
dnrich:

Hi, i'm working on custom indicator but when i added ChartIndicatorAdd that doesn't work on strategy tester, but that's work fine on the main MT5 program.


Is there something wrong in this code ?

What is the error code ?

Indicators are automatically added by the Strategy Tester.

You can't add indicator manually with MT5  but you can use template.

Richard
18
Richard  
Marco vd Heijden:
Press pause load indicator then continue testing ?
when pause load indicator in strategy tester ... nothing appends. Indicator with ChartIndicatorAdd not displayed.
Richard
18
Richard  
Alain Verleyen:

What is the error code ?

Indicators are automatically added by the Strategy Tester.

You can't add indicator manually with MT5  but you can use template.

Do you mean that indicator are automatically added by the Strategy Tester by using ChartIndicatorAdd ?
George Chen
22
George Chen  

I have the same problem. Eager to know the solution.

In my opinion, your code has no problem. It seems that Strategy Tester does not support an indicator loads another indicator. Avoid this way.

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
George Chen
22
George Chen  

I looked into this problem deeply, have got conclusions as below.

1. My scenario is that EA loads indicator A and in turns indicator A loads indicator B. Use ChartIndicatorAdd to show the indicators. It works well but does not work well in the circustance of Strategy Tester. In Strategy Tester, indicator A shows up, but indicator B never shows up.

2. the calls to ChartIndicatorAdd always return success. However,  ChartIndicatorName() can't return the newly added name, ChartIndicatorsTotal() returns 0.

3. My work-around way is that EA load indicator A and indicator B in OnInit() process. Indicator A searches the handle of indicator B in the event of OnCalcute() for the first time. it shouldn't be done in the event of OnInit(). This work-around works well.

Airton Gomes Finoti
222
Airton Gomes Finoti  
George Chen:

I looked into this problem deeply, have got conclusions as below.

1. My scenario is that EA loads indicator A and in turns indicator A loads indicator B. Use ChartIndicatorAdd to show the indicators. It works well but does not work well in the circustance of Strategy Tester. In Strategy Tester, indicator A shows up, but indicator B never shows up.

2. the calls to ChartIndicatorAdd always return success. However,  ChartIndicatorName() can't return the newly added name, ChartIndicatorsTotal() returns 0.

3. My work-around way is that EA load indicator A and indicator B in OnInit() process. Indicator A searches the handle of indicator B in the event of OnCalcute() for the first time. it shouldn't be done in the event of OnInit(). This work-around works well.

I'm have similar problem. What happens it that sometimes B finds A, sometimes not. ChartIndicatorAdd always returns true, but I can't visualize it on the chart.

Documentation on MQL5: Chart Operations / ChartIndicatorAdd
Documentation on MQL5: Chart Operations / ChartIndicatorAdd
  • www.mql5.com
"the Expert Advisor's properties and specify correct   and   parameters." //| Expert initialization function                                   |                                                                                       ); "Attention! %s: Trying to add MACD(%s/%s) indicator on %s/%s chart. Receiving error 4114" //
To add comments, please log in or register