Is it possible to change automatically the MA's period parameter during run time ?

 

Is it possible to change automatically the MA's period parameter as per some condition during run time ?

 

Yes,

Value = Someformula() ;

indicatervalue = ima(...,Value,...) ;

 
Ickyrus:

Yes,

Value = Someformula() ;

indicatervalue = ima(...,Value,...) ;


Thanks,Now I want to change the price mode when useing iMA function if the trend is changed.

for example, if the trend is up,I hope to use MODE_LOW in iMA function; if the trend is down,MODE_HIGH will be used.

 
Ickyrus:

Yes,

Value = Someformula() ;

indicatervalue = ima(...,Value,...) ;


Indicator should redraw automatically the MA lines if conditions changed

I am afraid it is impossible.

 
//+------------------------------------------------------------------+
//|                                                      auto_ma.mq4 |
//|                       Copyright ?2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
//
#property copyright "Copyright ?2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red // 30  : fast MA
#property indicator_color2 Blue // 60 : slow MA

extern int ma_period = 30;

//---- buffers
double MaFastBuffer[]; // for 30 (ma_period) MA
double MaSlowBuffer[]; // for 60 MA

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MaFastBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,MaSlowBuffer);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit = Bars-counted_bars;     
   int i;
   
//----
// condtion 1: if the monthly trend is up,use MODE_LOW to calculate....
   if(iClose(NULL,PERIOD_MN1,0)>iClose(NULL,PERIOD_MN1,1)
      && iLow(NULL,PERIOD_MN1,0)>iLow(NULL,PERIOD_MN1,1)) 
      
      for(i=0;i<limit;i++)
      {
      MaFastBuffer[i]=iMA(NULL,0,ma_period,0,MODE_EMA,MODE_LOW,i);
      MaSlowBuffer[i]=iMA(NULL,0,2*ma_period,0,MODE_EMA,MODE_LOW,i);
      }
      
// condition 2 : if the monthly trend is down..... MODE_HIGH.....
   if(iClose(NULL,PERIOD_MN1,0)<iClose(NULL,PERIOD_MN1,1)
      && iHigh(NULL,PERIOD_MN1,0)<iHigh(NULL,PERIOD_MN1,1)) 
      
       for(i=0;i<limit;i++)
      {
      MaFastBuffer[i]=iMA(NULL,0,ma_period,0,MODE_EMA,MODE_HIGH,i);
      MaSlowBuffer[i]=iMA(NULL,0,2*ma_period,0,MODE_EMA,MODE_HIGH,i);
      }
      


//----
   return(0);
  }
//+------------------------------------------------------------------+
 

I was not aware that you needed to see the lines! One posibility would be the similar to removing and reapplying the indicatior.

Another would be to have the function that changes the value in the indicator itself - however dynamicaly shiftng lines can be very unfrendly to the eye.

Have a quick search for indicator redraw function.

 
 
Ickyrus:

I was not aware that you needed to see the lines! One posibility would be the similar to removing and reapplying the indicatior.

Another would be to have the function that changes the value in the indicator itself - however dynamicaly shiftng lines can be very unfrendly to the eye.

Have a quick search for indicator redraw function.


Thank you Ickyrus !

refer to https://www.mql5.com/en/forum/123535, as per gordon's opinion.

the indicator should go through the cycle deinit(),init(),start() by codes, but it maybe result other subtle questions.

Now I have to give up this ideal.

 

Changing MA period on-the-fly => https://www.mql5.com/en/articles/39

 

Ais Please dont confuse MQ5 articles with MQ4 - my understanding is that they are two VERY different versions.

 

It's time to move on.

Reason: