Slope of a moving average

 

I want to calculate the slope of a moving average of 200 periods.

I want to use the values of periods -12 and -2 (periodes couting from actual period) for calculate this slope.

Can I acess historical values of a moving average?

 

You can use iMA()

 
SDC:

You can use iMA()


How?

If I put iMA() only write the current period, I think.  

 
double iMA(
string symbol, // symbol
int timeframe, // timeframe
int ma_period, // MA averaging period
int ma_shift,  // MA shift
int ma_method, // averaging method
int applied_price, // applied price
int shift // shift
);

 

period 200, shift 2 or 12.

 
SDC:
period 200, shift 2 or 12.


Many thanks

 

you could do it like that, but it would be more efficient if you wrote it into an indicator loop and put the iMA() in a buffer and call your own buffer indexes.

 

Thank you,

In a first attempt I will try the first option, if my strategy has good results i will try apply this with buffer.

 
xspeculator: I want to calculate the slope of a moving average of 200 periods.
I want to use the values of periods -12 and -2 (periodes couting from actual period) for calculate this slope.
Can I acess historical values of a moving average?
  1. I assume you mean shifts of 2 and 12
  2. Of course you can
double MAslope(int length=200, int iFrom=12, int iTo=2, 
               ENUM_MA_METHOD      mode=MODE_SMA, 
               ENUM_APPLIED_PRICE price=PRICE_CLOSE){
   double maF = iMA(NULL, PERIOD_CURRENT, length, 0, mode, price, iFrom);
   double maT = iMA(NULL, PERIOD_CURRENT, length, 0, mode, price, iTo);
   return (maF - maT) / (iFrom - iTo);
}
 
William Roeder:
  1. I assume you mean shifts of 2 and 12
  2. Of course you can

I found this more correct, however thanks for pointing me in the right direction.

double MAslope(int length=250, int iFrom=11, int iTo=1, 
               ENUM_MA_METHOD      mode=MODE_SMA, 
               ENUM_APPLIED_PRICE price=PRICE_CLOSE)
   {
   double maFArray[];
   double maTArray[];
   int count = iFrom-iTo;
   ArrayResize(maFArray,count);
   ArrayResize(maTArray,count);
   ArraySetAsSeries(maFArray,true);
   ArraySetAsSeries(maTArray,true);
   int maF = iMA(_Symbol,PERIOD_M1,length,iFrom,mode,price);   
   int maT = iMA(_Symbol,PERIOD_M1,length,iTo,mode,price);
   CopyBuffer(maF,0,0,count,maFArray);
   CopyBuffer(maT,0,0,count,maTArray);
   return (maTArray[0] - maFArray[0]) / count;
   } 

Uptrend is positive values, downtrend is negative values

 
Masizane Marivate: I found this more correct, however thanks for pointing me in the right direction.

   int maF = iMA(_Symbol,PERIOD_M1,length,iFrom,mode,price);   
   int maT = iMA(_Symbol,PERIOD_M1,length,iTo,mode,price);
   CopyBuffer(maF,0,0,count,maFArray);
   CopyBuffer(maT,0,0,count,maTArray);
Not correct. @Masizane Marivate
  1. This is the MT4 forum, not MT5.
  2. Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (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.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

Reason: