good practice with OnCalculate() function

 

Hi


I'm still a beginner, and I'm just getting to grips with MQL5's native functions,

for example, when I need to call native functions like iMA, should I call the Buffer from the OnCalculate() function? 

And sorry if my question is silly, that's okay it's a question.

If you have any links to good or bad practices of this function it might help me,
Thanks in advance for all your insights 🙂

 
ZeroCafeine:

Hi


I'm still a beginner, and I'm just getting to grips with MQL5's native functions,

for example, when I need to call native functions like iMA, should I call the Buffer from the OnCalculate() function? 

And sorry if my question is silly, that's okay it's a question.

If you have any links to good or bad practices of this function it might help me,
Thanks in advance for all your insights 🙂

Good practices start by using the correct wording and clear question.

What do you mean by "call the Buffer" ? I guess you mean using CopyBuffer() ?

If yes, then it's nothing related to good or bad practice, you just call it when you need it. If not, then please clarify.

 
ZeroCafeinefor example, when I need to call native functions like iMA, should I call the Buffer from the OnCalculate() function? 

To supplement Alain's post above ... Is this a question about coding an EA or coding an Indicator?

OnCalculate() is an event handler used only in Indicators, not EAs.

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
OnCalculate - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

tks you @ Fernando Carreiro, So if I've understood correctly, if I'm using EA then I'll never need to call the OnCalculate() function ?


Thank you @Alain Verleyen for taking the time to rephrase my question, 

Alain Verleyen #:
What do you mean by "call the Buffer" ? I guess you mean using CopyBuffer() ?

That's exactly it, So I can use function CopyBuffer() inside OnTick() function for exemple, I don't have to use it inside function OnCalculate() ?



in 1998 : I remember when I was at the high school there was a student in my class who was repeating a year who asked the teacher a question: "But where is the potassium? Inside the calcium?",
In my head I thought: but it's not possible to ask a stupid question like that ?, And over time I learnt Einstein's situation:  There are no stupid questions, Only stupid people., that's why questions always seem silly, especially mine 😅


Thank you again for your kindness and presence 😀.

Fernando Carreiro
Fernando Carreiro
  • 2023.04.19
  • www.mql5.com
Trader's profile
 

ZeroCafeine #: So if I've understood correctly, if I'm using EA then I'll never need to call the OnCalculate() function ?

You never call OnCalculate(), not even in an indicator. It is an event handler. It is MetaTrader that calls it, not your code.

In this case, it is an event handler that is called by MetaTrader for your indicator code to process the rates data and produce buffers and plots.

The buffer data that is produced by OnCalculate can then be retrieved in an EA or another Indicator by using the CopyBuffer function.

 
ZeroCafeine: for example, when I need to call native functions like iMA,

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)