Function

 
Good day 

I'm still new to mql5 and the following idea crossed my mind. Is it possible to create a function with a indicator inside the function. For example Check_Entry(). Now I want to create 3 indicator inside this function and call the function into the ontick function but what I want to know is do I need to add each indicator in the oninit function or can I just call the function inside the oninit 

 
Niel05ottoI'm still new to mql5 and the following idea crossed my mind. Is it possible to create a function with a indicator inside the function. For example Check_Entry(). Now I want to create 3 indicator inside this function and call the function into the ontick function but what I want to know is do I need to add each indicator in the oninit function or can I just call the function inside the oninit 

I suggest you first research how Indicators work and how they are programmed, because Indicators are not "created" inside functions. Indicators are separate "programs", that need to be instantiated to obtain a handle, and then their data accessed later.

Instantiating an indicator to obtain a handle, is done in OnInit() or a class constructor or initialiser. After that, you then access their data in OnTick() or OnTimer().

 
Fernando Carreiro #:

I suggest you first research how Indicators work and how they are programmed, because Indicators are not "created" inside functions. Indicators are separate "programs", that need to be instantiated to obtain a handle, and then their data accessed later.

Instantiating an indicator to obtain a handle, is done in OnInit() or a class constructor or initialiser. After that, you then access their data in OnTick() or OnTimer().

Thank you for the response

so do i need to call each handle in the onInit function for example CCIHandle = iCCI(_Symbol,PERIOD_CURRENT,14,PRICE_TYPICAL); or can i just call the function inside the onInit where i have used this code?

 
Niel05otto #:

Thank you for the response

so do i need to call each handle in the onInit function for example CCIHandle = iCCI(_Symbol,PERIOD_CURRENT,14,PRICE_TYPICAL); or can i just call the function inside the onInit where i have used this code?

I just went and test it and it is working. if i call the fanction inside the onInit and you test it, it shows the indicators on the chart. 

 
Niel05otto #I just went and test it and it is working. if i call the fanction inside the onInit and you test it, it shows the indicators on the chart. 

You should not access market data inside OnInit(). There is no guarantee that it will be available at that time. You should only access market data after the first tick has arrived. 

Forum on trading, automated trading systems and testing trading strategies

Issue: AccountInfoDouble(ACCOUNT_BALANCE) returns 0 all the time.

William Roeder, 2025.08.18 12:42

void OnInit()
  {
   Print("OnInit: ", AccountInfoDouble(ACCOUNT_BALANCE));

don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
Fernando Carreiro #:

You should not access market data inside OnInit(). There is no guarantee that it will be available at that time. You should only access market data after the first tick has arrived. 

This is what i was trying to ask 

Files:
1.PNG  17 kb
2.PNG  23 kb
 
Sorry for the pictures but for some reason my print screen don't work
 
Niel05otto #This is what i was trying to ask 

Do you realise that your code is leaking handles, and that you are creating indicator handles on every call and never releasing them?

Luckily, I suspect, that the compiler or the terminal is probably optimising the code or detecting the situation, and internally releasing or reusing Indicator handles to prevent an overflow. However, your code design is very problematic and a bad practice model.

Indicator handles should preferably be instantiated only once during the life-time of the EA's existence. The handles can then be references as many times as needed thereafter, until released explicitly or implicitly at the end when the EA's execution is terminated.

Please study the example code in the documentation ... 

Documentation on MQL5: Technical Indicators / iCCI
Documentation on MQL5: Technical Indicators / iCCI
  • www.mql5.com
The function returns the handle of the Commodity Channel Index indicator. It has only one buffer. Parameters symbol [in] The symbol name of the...
 
Niel05otto #Sorry for the pictures but for some reason my print screen don't work

When showing code, please don't use screenshots. Use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Fernando Carreiro #:

When showing code, please don't use screenshots. Use the CODE button (Alt-S) when inserting code.

Thank you for the feedback and info. Think I will just create the handles of the indicators inside the oninit to be on the save side.