Personalized EMA

To add comments, please log in or register
Andrei Boca
11
Andrei Boca  
Hi, i want to make an Ema with a difference source different from price_close price_open etc. I want to make ema with volume for example.
Magruder Trading Group, Inc.
151
Chad Magruder  

Write a custom indicator using EMA calculations on iVolume data. Should be easy... here is code for simple moving average of volume. 

Here is a link to the EMA formula: https://www.metatrader5.com/en/terminal/help/indicators/trend_indicators/ma

//+------------------------------------------------------------------+
//|                                                     VolumeMA.mq4 |
//|                                    Copyright 2019, Chad Magruder |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Chad Magruder"
#property link      ""
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Volume
#property indicator_label1  "Volume"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Average
#property indicator_label2  "Average"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      MA_PERIOD=7;
//--- indicator buffers
double         VolumeBuffer[];
double         AverageBuffer[];
double tVolume;
int counted_bars,limit;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,VolumeBuffer);
   SetIndexBuffer(1,AverageBuffer);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   counted_bars=IndicatorCounted();
   if(counted_bars==0){counted_bars=MA_PERIOD+1;}
   if(counted_bars>0){counted_bars--;}
   limit=Bars-counted_bars;
   for(int a=limit;a>=0;a--){
      VolumeBuffer[a]=iVolume(Symbol(),0,a);
      tVolume=0;
      for(int b=0;b<MA_PERIOD;b++){
         tVolume+=iVolume(Symbol(),0,a+b);
      }//loop b
      AverageBuffer[a]=tVolume/MA_PERIOD;
   }//loop a
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Moving Average - Trend Indicators - MetaTrader 5
Moving Average - Trend Indicators - MetaTrader 5
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average, one averages out the instrument price for this time period. As the price changes, its moving average either increases, or decreases. There are four different types of moving averages: Simple (also...
To add comments, please log in or register