how does the MA indicator work

 
hi there,

I am new to mql5. Really new to this. I am looking at the code  for the simple MA function. I am trying to understand how the codes work and reflect the functions listed on

http://www.metatrader5.com/en/terminal/help/analytics/indicators/trend_indicators/ma#sma.


double SimpleMA(const int position,const int period,const double &price[])
  {
//---
   double result=0.0;
//--- check position
   if(position>=period-1 && period>0)
     {
      //--- calculate value
      for(int i=0;i<period;i++) result+=price[position-i];
      result/=period;
     }
//---
   return(result);


Simple Moving Average (SMA)

Simple, in other words, arithmetical moving average is calculated by summing up the prices of instrument closure over a certain number of single periods (for instance, 12 hours). This value is then divided by the number of such periods.

SMA = SUM (CLOSE (i), N) / N

Where:

SUM — sum;
CLOSE (i) — current period close price;
N — number of calculation periods.



how does the program get those input parameters ie position , period , price ?


lets say the position = 4

and since position>=period-1
        
        4>=period-1

        5 >= period

and solving for period , we get period is less than or equal to 5,



result+=price[position-i];

so for the first iteration, so we start adding from the 4th position.


result = price [4]

and then we divide result by period where period,

result/=period;


so even though we have started summing the price from the 4th position,
we have to divide by a period with period = 5 ?
 
tomyang03:
hi there,

I am new to mql5. Really new to this. I am looking at the code  for the simple MA function. I am trying to understand how the codes work and reflect the functions listed on

http://www.metatrader5.com/en/terminal/help/analytics/indicators/trend_indicators/ma#sma.


<CODE DELETED>

Please read some other posts before posting . . .

When posting code  please use the   SRC   button: How to use the   SRC   button. 

 
tomyang03:
hi there,


so even though we have started summing the price from the 4th position,
we have to divide by a period with period = 5 ?
The first position is 0,  so you have 0, 1, 2, 3, 4  = 5 bars
 
tomyang03:
...
double SimpleMA(const int position,const int period,const double &price[])
  {
//---
   double result=0.0;
//--- check position
   if(position>=period-1 && period>0)
     {
      //--- calculate value
      for(int i=0;i<period;i++) result+=price[position-i];
      result/=period;
     }
//---
   return(result);


Simple Moving Average (SMA)

Simple, in other words, arithmetical moving average is calculated by summing up the prices of instrument closure over a certain number of single periods (for instance, 12 hours). This value is then divided by the number of such periods.

SMA = SUM (CLOSE (i), N) / N

Where:

SUM — sum;
CLOSE (i) — current period close price;
N — number of calculation periods.
...
  • This function is wrongly named SimpleMA( Simple moving average), in facts its only calculating an average of some value of an array and not a moving average.
  • You are supposed to call this function with the index of the first element of the array to begin calculation, with the number of value to calculate the average, and an array of values (ex: array of close price).
  • An array of size S is by default indexed from 0 to S-1. In MT5, for a price array, index 0 is the price of the oldest candle, and index S-1 is the current candle.
  • Suppose a period of 5, so we need 5 values to calculate the average. If you call the function with position <4 (period-1) the average can't be calculated, so the returned value is 0.0
  • Otherwise, the average of 5 values beginning at 'position' is calculated. The first valid position is then 4.
  • Calculation for SimpleSMA(4, 5, close) : Average = (price[4] + price[3] + price[2] + price[1] + price[0]) /5.
 
angevoyageur:
  • This function is wrongly named SimpleMA( Simple moving average), in facts its only calculating an average of some value of an array and not a moving average.
  • You are supposed to call this function with the index of the first element of the array to begin calculation, with the number of value to calculate the average, and an array of values (ex: array of close price).
  • An array of size S is by default indexed from 0 to S-1. In MT5, for a price array, index 0 is the price of the oldest candle, and index S-1 is the current candle.
  • Suppose a period of 5, so we need 5 values to calculate the average. If you call the function with position <4 (period-1) the average can't be calculated, so the returned value is 0.0
  • Otherwise, the average of 5 values beginning at 'position' is calculated. The first valid position is then 4.
  • Calculation for SimpleSMA(4, 5, close) : Average = (price[4] + price[3] + price[2] + price[1] + price[0]) /5.


how does the program get those input parameters  position , period , price  from the trader ?
is there actually a script in C++ which calls the function simpleSMA with users input ?

 
tomyang03:

how does the program get those input parameters  position , period , price  from the trader ?
is there actually a script in C++ which calls the function simpleSMA with users input ?

You pass them when you call the Indicator using iCustom() . . .   or you can use iMA() and pass them when you call the function.
Documentation on MQL5: Technical Indicators / iCustom
Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
Technical Indicators / iCustom - Documentation on MQL5