CopyBufferMA1 error =4807

 

This EA ran for almost two full days without any problem at all. Then, suddenly, without absolutely no change to the code, it doesn't work anymore.

I have googled the error and I have no idea what it means. "Wrong indicator handle" is 100% meaningless to me. Can someone please explain what is going on?


double getMovAv(ENUM_TIMEFRAMES timeframe, int maperiod, int mashift, int barshift) {
   double myMovingAverageArray1[];
   int movingAverageDefinition1 = iMA(_Symbol, timeframe, maperiod, mashift, MODE_SMA, PRICE_CLOSE);
   ArraySetAsSeries(myMovingAverageArray1, true);
   if (CopyBuffer(movingAverageDefinition1, 0, 0, 20, myMovingAverageArray1) < 0) {
      Print("CopyBufferMA1 error =",GetLastError());
   }
   return myMovingAverageArray1[barshift];
}

...

mavBase = getMovAv(TF, mavBaseValue, 0, 0);
// the above translates to getMovAv(15, 50, 0, 0);
// sometimes, getMovAv(15, 50, 0, 1); "1" refers to the previous bar
 

I decided to load the EA in another account with another broker, and the code runs fine.

I went back to the account I was using before and the error returned.

Does that make any sense?

 

Never mind, I found an error in my code.

The value of 'maperiod' was being passed incorrectly as 0.

That doesn't explain how the EA ran for so long without any errors, but at least it's working now.

 
   int movingAverageDefinition1 = iMA(_Symbol, timeframe, maperiod, mashift, MODE_SMA, PRICE_CLOSE);
   ArraySetAsSeries(myMovingAverageArray1, true);
   if (CopyBuffer(

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 

William Roeder #:

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.



That approach is terrible. It would force me to "split" my function.

I have this include called AllMyFunctions.mqh. It contains all my functions and I also declare all the global variables in it.

Whenever I write a new EA, I include that file and rely on the usual functions and variables. Only the core logic/strategy is rewritten.

Each EA has its own OnInit() function according to what they really need. Nothing more, nothing else.

That would not work well with the approach you suggest. I would have to put part of a function that is supposed to be shared by multiple EAs on separate individual OnInit() functions. That is asking for trouble. Something will trip up and break sooner or later.

The way I'm doing it works. I will keep it.

Or more likely, I will just go back to MT4. MQL5 is horrendous.

 
whoowl #: That approach is terrible. It would force me to "split" my function. I have this include called AllMyFunctions.mqh. It contains all my functions and I also declare all the global variables in it.

Whenever I write a new EA, I include that file and rely on the usual functions and variables. Only the core logic/strategy is rewritten. Each EA has its own OnInit() function according to what they really need. Nothing more, nothing else. That would not work well with the approach you suggest. I would have to put part of a function that is supposed to be shared by multiple EAs on separate individual OnInit() functions. That is asking for trouble. Something will trip up and break sooner or later. The way I'm doing it works. I will keep it. Or more likely, I will just go back to MT4. MQL5 is horrendous.

It is not horrible. You are not looking at the bigger picture. You don't have to initialise the handle in OnInit necessarily, but it should be your objective if possible. However, you should only be initialising the handle once and then reusing it thereafter.

If you are using OOP (i.e. classes), then have it initialise once when it is first called. However, remember to free the handle during the deconstruction process.

If using Procedural/Functional approach, then have the function keep a static variable of the handle so that you don't have to reinitialise it every time it is called. However, you will need to create a mechanism to free the handle in the end.

Maybe you can combine OOP and Procedural/Functional.

Reason: