[solved] Help adding visual indicator on EA's chart (sometimes it appears, but usually it does not)

 

Hey guys,


I have an EA that makes usage of some moving averages and a custom indicator. The moving averages are actively used on my algorithms and they work fine, but when I try to add them to my charts they do not appear. The custom indicator I add is not actively used on my algorithms, but it greatly helps understand the functioning of my EA, and it also does not show up.

It's odd because some time ago it used to appear intermitently, but now my indicators usually do not appear.


This is the routine I use to add my indicators to my chart:


   CiMA _MA_Fast, _MA_Medium, _MA_Slow;

   // [...]

   void AssociateGraphicalIndicator(int chart_id, int subwindow1=0, int subwindow2=1) {
      
      int didi = iCustom(_Symbol, _MA_Fast.Period(), "/Indicators/Market/Didi Index",
                       _MA_Fast.MaPeriod(), _MA_Medium.MaPeriod(), _MA_Slow.MaPeriod(), _MA_Fast.MaMethod(), _MA_Fast.Applied());
      
      if (!ChartIndicatorAdd(chart_id, subwindow1, _MA_Fast.Handle())) {
         Print("Failed to add indicator _MA_Fast - "+string(GetLastError()));
      }
      if (!ChartIndicatorAdd(chart_id, subwindow1, _MA_Medium.Handle())) {
         Print("Failed to add indicator _MA_Medium - "+string(GetLastError()));
      }
      if (!ChartIndicatorAdd(chart_id, subwindow1, _MA_Slow.Handle())) {
         Print("Failed to add indicator _MA_Slow - "+string(GetLastError()));
      }
      if (!ChartIndicatorAdd(chart_id, subwindow2, didi)) {
         Print("Failed to add indicator didi - "+string(GetLastError()));
      }
   }

   // [...]

   int windows=ChartGetInteger(0,CHART_WINDOWS_TOTAL); 
   // here I also tried using 0 and 1 respectively instead of windows-1 and window
   Agulhada.AssociateGraphicalIndicator((int)ChartID(), windows-1, windows);


The code above is redacted to show only the relevant bits. 

Is there any reason this would not work to show these indicators after I drag my EA to a symbol chart?  Sometimes these intermitently work on strategy tester visualizations, which is curious. 



Thanks,

Fernando

 

Forum on trading, automated trading systems and testing trading strategies

Learning to code. Need help with this.

Vladimir Karputov, 2020.08.20 15:39

You are making a gross mistake: in MQL5, the work with receiving indicator data is divided into two steps. Step 1: create the HANDL of the indicator (THIS IS DONE ONLY ONCE - In OnInit !!!). Step 2: get the data (as a rule, this is done in OnTick ().


And by the way, the CTrade trading class is the easiest way. Nothing is easier! 

Correct the errors and I'll show you the lightweight code.

 

Is there a ChartRedraw After that function call ? 

 
int didi = iCustom(_Symbol, _MA_Fast.Period(), "/Indicators/Market/Didi Index",
                  _MA_Fast.MaPeriod(), _MA_Medium.MaPeriod(), _MA_Slow.MaPeriod(), _MA_Fast.MaMethod(), _MA_Fast.Applied());

Check your return codes, and report your errors. Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
          What are Function return values ? How do I use them ? - MQL4 programming forum
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

 
Lorentzos Roussos:

Is there a ChartRedraw After that function call ? 

There was not!

But I just added it after adding all the indicators and it did not work too unfortunately 

 
Fernando Henrique Gielow :


Code example: 

Simple EA Three iMA


How to start with MQL5
How to start with MQL5
  • 2020.08.20
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Hi,


I just found the issue.

To help debug first I changed my code to:

      int didi = iCustom(_Symbol, _MA_Fast.Period(), "/Indicators/Market/Didi Index",
                       _MA_Fast.MaPeriod(), _MA_Medium.MaPeriod(), _MA_Slow.MaPeriod(), _MA_Fast.MaMethod(), _MA_Fast.Applied());

      if (didi == INVALID_HANDLE) {
         Print("Failed to create indicator DIDI");
      } else {
         Print("Trying to add handle for didi");
         if (!ChartIndicatorAdd(chart_id, windows, didi)) {
            Print("Failed to add indicator didi - "+string(GetLastError()) + "("+string(chart_id)+","+string(windows)+")");
         }
      }
      
      if (_MA_Fast.Handle() == INVALID_HANDLE) {
         Print("Failed to create handle _MA_Fast");
      } else {
         Print("Trying to add handle for _MA_Fast");
         if (!ChartIndicatorAdd(chart_id, windows-1, _MA_Fast.Handle())) {
            Print("Failed to add indicator _MA_Fast - "+string(GetLastError()) + "("+string(chart_id)+","+string(windows)+")");
         }
      }

      if (_MA_Medium.Handle() == INVALID_HANDLE) {
         Print("Failed to create handle _MA_Medium");
      } else {
         Print("Trying to add handle for _MA_Medium");
         if (!ChartIndicatorAdd(chart_id, windows-1, _MA_Medium.Handle())) {
            Print("Failed to add indicator _MA_Medium - "+string(GetLastError()) + "("+string(chart_id)+","+string(windows)+")");
         }
      }

      if (_MA_Slow.Handle() == INVALID_HANDLE) {
         Print("Failed to create handle _MA_Slow");
      } else {
         Print("Trying to add handle for _MA_Slow");
         if (!ChartIndicatorAdd(chart_id, windows-1, _MA_Slow.Handle())) {
            Print("Failed to add indicator _MA_Slow - "+string(GetLastError()) + "("+string(chart_id)+","+string(windows)+")");
         }
      }

      ChartRedraw(chart_id);

Which then produces the logs:

logs


The chart id there is Bizarre, did not notice the error code 4101 before.

I just replaced ChartId() with a static 0 and it started working again.

For me this is sufficient and now my indicators are appearing nicely again! :)


ChartId() was returning "972554837" rather than 0. I was casting it to (int) rather than (long). Silly mistake.


Thanks very much for your time, regardless. Your comments made me realise this.