how do i draw default MA on chart

 

am looking for code to either draw the default Moving Average or a custom moving average to chart

the most important thing is that i want to be able to choose applied price, PRICE_CLOSE, OPEN, HIGH & LOW

most custom moving average codes do not have this 

 
Modify one so that it can be selected ?
 
You can set it up on the cart and make a template of it.
 
Marco vd Heijden:
Modify one so that it can be selected ?
how do i add the modification
 
Nurudeen Amedu:
how do i add the modification
Do you have the code? of any you have in mind
 
Marco vd Heijden:
Do you have the code? of any you have in mind
//+------------------------------------------------------------------+
//|                                       Custom Moving Averages.mq4 |
//|                   Copyright 2005-2015, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2015, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Average"
#property strict

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- indicator parameters
input int            InpMAPeriod=65;        // Period
input int            InpMAShift=0;          // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA;  // Method
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input ENUM_APPLIED_PRICE MAPrice = PRICE_CLOSE;
input double PriceRange = 600;

input bool SoundAlert = true;
input bool SendEmail = true;
input bool PushNotification = true;
//--- indicator buffer
double ExtLineBuffer[], value;
double PickPrice;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
   int    draw_begin=InpMAPeriod-1;
//--- indicator short name
   switch(InpMAMethod)
     {
      case MODE_SMA  : short_name="SMA(";                break;
      case MODE_EMA  : short_name="EMA(";  draw_begin=0; break;
      case MODE_SMMA : short_name="SMMA(";               break;
      case MODE_LWMA : short_name="LWMA(";               break;
      default :        return(INIT_FAILED);
     }
   IndicatorShortName(short_name+string(InpMAPeriod)+")");
   IndicatorDigits(Digits);
//--- check for input
   if(InpMAPeriod<2)
      return(INIT_FAILED);
//--- drawing settings
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,InpMAShift);
   SetIndexDrawBegin(0,draw_begin);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtLineBuffer);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|  Moving Average                                                  |
//+------------------------------------------------------------------+
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[])
  {


  Comment(value, "  ", PriceRange*Point);
  
  
//--- check for bars count
if(IsNewCandle()){
   CheckMaAlert();
   }
   if(rates_total<InpMAPeriod-1 || InpMAPeriod<2)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtLineBuffer,false);
   ArraySetAsSeries(close,false);
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
      ArrayInitialize(ExtLineBuffer,0);
//--- calculation


   switch(InpMAMethod)
     {

      case MODE_EMA:  CalculateEMA(rates_total,prev_calculated,close);        break;
      case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,close);       break;
      case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,close); break;
      case MODE_SMA:  CalculateSimpleMA(rates_total,prev_calculated,close);   break;
     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|   simple moving average                                          |
//+------------------------------------------------------------------+
void CalculateSimpleMA(int rates_total,int prev_calculated,const double &price[])
  {
   int i,limit;
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
   
     {
      limit=InpMAPeriod;
      //--- calculate first visible value
      double firstValue=0;
      for(i=0; i<limit; i++)
         firstValue+=price[i];
      firstValue/=InpMAPeriod;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
      ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
//---
  }
//+------------------------------------------------------------------+
//|  exponential moving average                                      |
//+------------------------------------------------------------------+
void CalculateEMA(int rates_total,int prev_calculated,const double &price[])
  {
   int    i,limit;
   double SmoothFactor=2.0/(1.0+InpMAPeriod);
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      limit=InpMAPeriod;
      ExtLineBuffer[0]=price[0];
      for(i=1; i<limit; i++)
         ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
      ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
//---
  }
//+------------------------------------------------------------------+
//|  linear weighted moving average                                  |
//+------------------------------------------------------------------+
void CalculateLWMA(int rates_total,int prev_calculated,const double & price[])
  {
   int        i,limit;
   static int weightsum;
   double     sum;
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      weightsum=0;
      limit=InpMAPeriod;
      //--- calculate first visible value
      double firstValue=0;
      for(i=0;i<limit;i++)
        {
         int k=i+1;
         weightsum+=k;
         firstValue+=k*price[i];
        }
      firstValue/=(double)weightsum;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
     {
      sum=0;
      for(int j=0;j<InpMAPeriod;j++)
         sum+=(InpMAPeriod-j)*price[i-j];
      ExtLineBuffer[i]=sum/weightsum;
     }
//---
  }
//+------------------------------------------------------------------+
//|  smoothed moving average                                         |
//+------------------------------------------------------------------+
void CalculateSmoothedMA(int rates_total,int prev_calculated,const double &price[])
  {
   int i,limit;
//--- first calculation or number of bars was changed
   if(prev_calculated==0)
     {
      limit=InpMAPeriod;
      double firstValue=0;
      for(i=0; i<limit; i++)
         firstValue+=price[i];
      firstValue/=InpMAPeriod;
      ExtLineBuffer[limit-1]=firstValue;
     }
   else
      limit=prev_calculated-1;
//--- main loop
   for(i=limit; i<rates_total && !IsStopped(); i++)
      ExtLineBuffer[i]=(ExtLineBuffer[i-1]*(InpMAPeriod-1)+price[i])/InpMAPeriod;
//---
  }
//+------------------------------------------------------------------+
void CheckMaAlert(){
   value = iMA(Symbol(),TimeFrame,InpMAPeriod,InpMAShift,InpMAMethod,MAPrice,1);
      if(Bid>value)
      {
      if((Bid-value)<=(PriceRange*Point)){
      
         if(SoundAlert)Alert(StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(Bid-value,5))*10000), " pips above Moving Average"));
         if(SendEmail)SendMail("MT4 Indicator Alert", StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(Bid-value,5))*10000), " pips above Moving Average"));
         if(PushNotification)SendNotification(StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(Bid-value,5))*10000), " pips above Moving Average"));
         }
      }
      
      if(value>Bid)
      {
         if ((value-Bid)<=(PriceRange*Point)){
         if(SoundAlert)Alert(StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(value-Bid,5))*10000), " pips below Moving Average"));
         if(SendEmail)SendMail("MT4 Indicator Alert", StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(value-Bid,5))*10000), " pips below Moving Average"));
         if(PushNotification)SendNotification(StringConcatenate(TimeCurrent(), _Symbol,": Price is ", ((NormalizeDouble(value-Bid,5))*10000), " pips below Moving Average"));
      }
   }



}

bool IsNewCandle()
{
      static int BarsOnChart=0;
      if (Bars == BarsOnChart)
      return (false);
      BarsOnChart = Bars;
      return(true);
 }
 
Nurudeen Amedu:
it's already there.
 
Copy the code of the MA indicator, apply changes as your request, then merge it in the EA !!!
 
double  MA8_0;
double  MA8[];
int maNameNumber;
void OnTick()
  {
    drawMA();
    ChartRedraw(0);
}
int OnInit()
  {
  maNameNumber = 0;
  maNameNumber = "A";
  MA8_0 = iMA(_Symbol, timeframe, 8, 0, MODE_EMA, PRICE_CLOSE);
  ArraySetAsSeries(MA8,true);
}
void drawMA(){
    int MA_Thinkness = 3;
    CopyBuffer(MA8_0,0,0,3,MA8);
   
   if (NewBar() || maName == "A"){
   maNameNumber++;
   maName = IntegerToString(maNameNumber);
    }
   else{
    maName = maName;
    }
    ObjectCreate( 0,maName+"8" ,OBJ_TREND ,0, iTime(_Symbol,timeframe,1), MA8[1],iTime(_Symbol,timeframe,0), MA8[0] );
    ObjectSetInteger(0, maName+"8", OBJPROP_RAY,false);
    ObjectSetInteger(0, maName+"8", OBJPROP_COLOR,clrMagenta);
    ObjectSetInteger(0, maName+"8", OBJPROP_WIDTH,MA_Thinkness);
   
 }

here we go , is much easier and straight forward ;)

Reason: