Questions from Beginners MQL5 MT5 MetaTrader 5 - page 959

 

How do I change the input parameter in the custom indicator during trading and apply the changes?

Let's say I had ma 25, now I need ma 33.

As far as I understand the input parameters are only set during initialisation. But I need to change indicator during trading

input int            InpMA_ma_period      = 25;        // Параметры МА 
input int            InpMA_ma_shift       = 0;         // MA PRICE_HIGH and PRICE_LOW: horizontal shift 
input ENUM_MA_METHOD InpMA_ma_method      = MODE_SMA;  // MA PRICE_HIGH: smoothing type 

int            handle_MA; 

int OnInit()
  {
    handle_MA=iCustom(m_symbol.Name(),Period(),"MA",InpMA_ma_period,InpMA_ma_shift,InpMA_ma_method,PRICE_CLOSE);
  }

void OnTick()
  {
   // Ставим новый параметр индикатора и делаем пересчет с новым параметром ???
   InpMA_ma_period      = 33;
  }
 
ilvic:

How do I change the input parameter in the custom indicator during trading and apply the changes?

Let's say I had ma 25, now I need ma 33.

As far as I understand the input parameters are only set during initialisation. But I need to change indicator during trading

Create TWO indicators in advance.

 
Vladimir Karputov:

Create TWO indicators in advance.

No, I don't need two indicators. I need to change this one during trading.

I want it to have dynamics, so to speak.

Can I download it and load it with new parameters?

 
ilvic:

No, I don't need two indicators. I need to change this one during trading.

The dynamics, so to speak.

Can it be unloaded somehow and loaded with new parameters?

Create three indicators :).

In general, you have to think first, create the environment and only then get the data.

 
Vladimir Karputov:

Create three indicators :).

In general, you have to think first, create the environment and only then get the data.

Suppose -

At the start of the EA the MA = 25;

With every new tick I change the MA = +1.

I.e. MA = 26;

I won't create 500 handles for different conditions)

 
ilvic:

How do I change the input parameter in the custom indicator during trading and apply the changes?

Let's say I had ma 25, now I need ma 33.

As far as I understand the input parameters are only set during initialisation. But I need to change indicator during trading

Well, it seems that at changing of values in input parameters of the EA OnInit() is triggered, it means that the new values of parameters are applied. Or am I wrong?

 
Vitaly Muzichenko:

If the values in the input parameters of the EA are changed, OnInit() is triggered, which means that the new values of the parameters are applied. Or am I wrong?

I've tried it this way.

It doesn't work

input int            InpMA_ma_period      = 25;        // Параметры МА 
input int            InpMA_ma_shift       = 0;         // MA PRICE_HIGH and PRICE_LOW: horizontal shift 
input ENUM_MA_METHOD InpMA_ma_method      = MODE_SMA;  // MA PRICE_HIGH: smoothing type 

double ExtInpMA_ma_period=InpMA_ma_period;
int            handle_MA; 

int OnInit()
  {
    handle_MA=iCustom(m_symbol.Name(),Period(),"MA",ExtInpMA_ma_period,InpMA_ma_shift,InpMA_ma_method,PRICE_CLOSE);
  }

void OnTick()
  {
   // Ставим новый параметр индикатора и делаем пересчет с новым параметром ???
   ExtInpMA_ma_period      = 33;
  }
 
ilvic:

I tried it like this.

Doesn't work.

Well you can't, you need to callOnInit to re-initialize the indicator.

How often do you change parameters during operation?

 
Vitaly Muzichenko:

Well you can't, you need to callOnInit to re-initialise the indicator.

How often do you change parameters during operation?

I am going to change them often.

So the code should look like this?

void OnTick()
  {
   // Ставим новый параметр индикатора и делаем пересчет с новым параметром ???
   
   ExtInpMA_ma_period      = 33;
   OnInit();

  }
 
ilvic:

I'm going to change it a lot.

So the code should look like this?

You should put a flag to check if the value has changed, so you don't have to yankOnInit on every tick

Something like this:

void OnTick()
  {
   // Ставим новый параметр индикатора и делаем пересчет с новым параметром ???
   static int NewExtInpMA_ma_period;
   if(ExtInpMA_ma_period != NewExtInpMA_ma_period)
    {
     NewExtInpMA_ma_period=ExtInpMA_ma_period;
     OnInit();
    }
  }
Reason: