EA ChartIndicatorAdd Fail

 

Friends,

I am trying to do something quite simple... add RSI and MACD to my plots in my EA, in subwindows, so I can track and SEE and visualize what my EA is calculating.  It is opening a blank subwindow, showing no plot or data.  

I looked at other threads that have had similar problems, and am always pointed to https://www.mql5.com/en/docs/chart_operations/chartindicatoradd or told that EA's don't "show" indicators.  If this is true, why does that function exist at all?  Being able to see an indicator in an EA is surely a simple and possible function of MQL5.  What am I doing wrong?  Any advice is appreciated.  

Code that adds only RSI:

Help?  

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
   {
      //--- Get the handle for MA indicator
      MACDHandle  = iMACD(_Symbol,_Period,MACD_EMA_fast,MACD_EMA_slow,MACD_period,PRICE_TYPICAL);
      RSIHandle   = iRSI(_Symbol,_Period,RSI_period,PRICE_CLOSE);
      
      //--- What if handle returns Invalid Handle
      if(MACDHandle<0 || RSIHandle<0)
      {
         Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!");
      }
      
      // Add indicators to the current window
      //--- receive the number of a new subwindow, to which MACD indicator is added
      int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
      PrintFormat("Adding new indicator on %d chart window",subwindow);
      if(!ChartIndicatorAdd(0,subwindow,RSIHandle))
      {
         PrintFormat("Failed to add new indicator on %d chart window. Error code  %d",subwindow,GetLastError());
         //--- reset the error code
         ResetLastError();
      }

   //---
   return(INIT_SUCCEEDED);
   }
Documentation on MQL5: Chart Operations / ChartIndicatorAdd
Documentation on MQL5: Chart Operations / ChartIndicatorAdd
  • www.mql5.com
ChartIndicatorAdd - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Show the complete code. Show how many indicators are already on the chart (check if there are any hidden indicators among them). Show terminal data - example like this:

Forum on trading, automated trading systems and testing trading strategies

Very Glitchy MetaTrader

Vladimir Karputov, 2021.03.16 04:20

If you have a question, first of all you should show the first three lines from the 'Journal' tab


(select these lines, copy to the clipboard and paste into the message using the button Code). It should look like this:

2021.03.16 05:13:07.133 Terminal        MetaTrader 5 x64 build 3003 started for MetaQuotes Software Corp.
2021.03.16 05:13:07.134 Terminal        Windows 10 build 19042, Intel Core i7-9750H  @ 2.60GHz, 26 / 31 Gb memory, 842 / 947 Gb disk, IE 11, UAC, GMT+2
2021.03.16 05:13:07.134 Terminal        C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075

This code worked fine and without errors:

//+------------------------------------------------------------------+
//|                                                     Expert 1.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.00"
//---
int MACDHandle,RSIHandle;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//--- Get the handle for MA indicator
   MACDHandle  = iMACD(Symbol(),Period(),12,26,9,PRICE_TYPICAL);
   RSIHandle   = iRSI(Symbol(),Period(),14,PRICE_CLOSE);
//--- What if handle returns Invalid Handle
   if(MACDHandle<0 || RSIHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!");
     }
// Add indicators to the current window
//--- receive the number of a new subwindow, to which MACD indicator is added
   int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   PrintFormat("Adding new indicator on %d chart window",subwindow);
   if(!ChartIndicatorAdd(0,subwindow,RSIHandle))
     {
      PrintFormat("Failed to add new indicator on %d chart window. Error code  %d",subwindow,GetLastError());
      //--- reset the error code
      ResetLastError();
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
Files:
Expert_1.mq5  5 kb
 
2021.08.17 06:12:01.738 Terminal MetaTrader 5 x64 build 3020 started for MetaQuotes Software Corp.
2021.08.17 06:12:01.738 Terminal Windows 10 build 19041, Intel Core i5-10400  @ 2.90GHz, 7 / 11 Gb memory, 363 / 460 Gb disk, IE 11, UAC, GMT-5
2021.08.17 06:12:01.738 Terminal C:\Users\anbin\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075


//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Bingaman"
#property version   "1.00"

// Open mqh
#include <Trade\Trade.mqh>

// Create instance of CTrade for trading purposes
CTrade trade;


// ------ Input parameters ------
// General
input int      StopLoss          =  5;       // Pips, kk
input int      TakeProfit        =  5;       // Pips, jj
input double   Lot               =  1.0;

// MACD
input int      MACD_EMA_fast     =  10;      // MACD EMA fast period
input int      MACD_EMA_slow     =  10;      // MACD EMA slow period
input int      MACD_period       =  10;      // MACD EMA period

// RSI
input int      RSI_thresh        =  25;      // RSI threshold
input int      RSI_period        =  14;      // RSI Period
input int      RSI_win           =  3;       // Number of candles for open activity after RSI threshold cross
input int      RSI_thresh_hi     =  70;
input int      RSI_thresh_lo     =  30;


// ------ Local parameters ------
// Indicator handles
int MACDHandle;
int RSIHandle;

// Local data
double MACDdata[];      // Local MACD data copied from indicator
double RSIdata[];       // Local RSI data copied from indicator
int n_local_win = 10;   // Number of candles to copy over locally to work with




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
   {
      //--- Get the handle for MA indicator
      MACDHandle  = iMACD(_Symbol,_Period,MACD_EMA_fast,MACD_EMA_slow,MACD_period,PRICE_TYPICAL);
      RSIHandle   = iRSI(_Symbol,_Period,RSI_period,PRICE_CLOSE);
      
      //--- What if handle returns Invalid Handle
      if(MACDHandle<0 || RSIHandle<0)
      {
         Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!");
      }
      
      // Add indicators to the current window
      //--- receive the number of a new subwindow, to which MACD indicator is added
      int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
      PrintFormat("Adding new indicator on %d chart window",subwindow);
      if(!ChartIndicatorAdd(0,1,RSIHandle))
      {
         PrintFormat("Failed to add new indicator on %d chart window. Error code  %d",subwindow,GetLastError());
         //--- reset the error code
         ResetLastError();
      }

   //---
   return(INIT_SUCCEEDED);
   }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
   {
   
   //--- Release indicators
   IndicatorRelease(MACDHandle);
   IndicatorRelease(RSIHandle);
   
   }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
   { 
   }


Thanks for the tip.  Any ideas?  This seems pretty simple.  When I run the exact same code, I get a blank subwindow, repeatable.  Thanks in advance for the help.

 
ABingaman :
2021.08.17 06:12:01.738 Terminal MetaTrader 5 x64 build 3020 started for MetaQuotes Software Corp.
2021.08.17 06:12:01.738 Terminal Windows 10 build 19041, Intel Core i5-10400  @ 2.90GHz, 7 / 11 Gb memory, 363 / 460 Gb disk, IE 11, UAC, GMT-5
2021.08.17 06:12:01.738 Terminal C:\Users\anbin\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075



Thanks for the tip.  Any ideas?  This seems pretty simple.  When I run the exact same code, I get a blank subwindow, repeatable.  Thanks in advance for the help.

Here is the result of your code - everything works ( click on the picture to play )


 

Thanks for checking it out.  I am still getting a blank subwindow.  Is this a glitch?  Is there errata out there?  Any other ideas?

Can you get the same thing to happen with MACD?  Both MACD and RSI in different windows?  I can't get any/either to show.  

Thanks in advance.

Screenshot attached.

Files:
MT5_sucks.png  102 kb
 
ABingaman :

Thanks for checking it out.  I am still getting a blank subwindow.  Is this a glitch?  Is there errata out there?  Any other ideas?

Can you get the same thing to happen with MACD?  Both MACD and RSI in different windows?  I can't get any/either to show.  

Thanks in advance.

Screenshot attached.

I repeat - everything works for me. Look for problems in yourself.

1. Be careful when you conduct experiments - you did not even deign to show the "Data Windows" window.


2. You did not deign to show the number of indicators on the chart (right click on the chart and "Indicator List")


 

1. When you set the parameters of the indicator, you must first think hard: "Are all the parameters correct?"

For MACD, the correct parameters are:

// MACD
input int      MACD_EMA_fast     =  12;      // MACD EMA fast period
input int      MACD_EMA_slow     =  26;      // MACD EMA slow period
input int      MACD_period       =  9;       // MACD EMA period

2. When you add an indicator to a subwindow - you must be very careful and use the subwindow numbers correctly:

//--- receive the number of a new subwindow, to which MACD indicator is added
   int windows_total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   PrintFormat("Adding new indicator on %d chart window",windows_total);
   if(!ChartIndicatorAdd(0,windows_total,MACDHandle))
     {
      PrintFormat("Failed to add new indicator on %d chart window. Error code  %d",windows_total,GetLastError());
      //--- reset the error code
      ResetLastError();
     }
   PrintFormat("Adding new indicator on %d chart window",windows_total+1);
   if(!ChartIndicatorAdd(0,windows_total+1,RSIHandle))
     {
      PrintFormat("Failed to add new indicator on %d chart window. Error code  %d",windows_total+1,GetLastError());
      //--- reset the error code
      ResetLastError();
     }
 

VK,

Thanks for the time and help in working this.  I have been reading and trying to run this down without success.

What I don't get is this:  I am running literally the exact same code that you posted on 8/17 above and still getting a blank subwindow.  The exact same code.  If you are running the code and it works fine, and I am running the EXACT same code and getting a blank subwindow, there is a variable there that I am missing.  Is there a setting in MT5 that I am missing?  What could prevent it from running appropriately?  I am getting a bit desperate to figure this out.  

Thanks again.


AB