Simple Expo Moving Averages

Download script
Author: Copyright � 2004, MetaQuotes Software Corp.
Simple Expo Moving Averages

This custom indicator is designed to provide visual cues on a trading chart by calculating specific price averages over a set timeframe. Rather than simply drawing a continuous line, this script translates raw price data into actionable visual markers to help traders identify momentum and price levels.

Core Functionality

The primary logic of the tool revolves around a Simple Moving Average (SMA) calculation. It looks at a specific number of previous price points—defined by a user-adjustable period—to determine the average closing price. By smoothing out the "noise" of individual price fluctuations, it helps reveal the underlying trend of the market.

Visual Execution

Instead of standard lines, the script utilizes two distinct types of visual markers (arrows) on the chart:

  • Primary Data Point: The first marker represents the actual calculated Simple Moving Average. This tells you where the "true" average price sits based on the chosen period.
  • Offset Marker: The second marker is programmed to stay at a fixed distance (specifically 5 points) above the primary moving average. This creates a secondary reference point, allowing traders to visualize a specific "buffer zone" or a secondary level of interest relative to the main average.

How it Operates

  1. Data Gathering: The script continuously scans the historical closing prices of the currency pair or asset you are viewing.
  2. Averaging: It sums up the closing prices for the duration of the specified period and divides them by that number to find the mean.
  3. Dynamic Updating: As new price candles form, the script automatically drops the oldest price from its memory and adds the newest one, ensuring the markers move in real-time with the market.
  4. Visual Output: It populates the chart with red and white symbols that correspond to these two different calculations, providing a clear, glanceable representation of price trends and their relative offsets.

In summary, this tool serves as a visual guide that highlights where the market average is currently sitting while simultaneously plotting a secondary reference point, helping traders keep track of trend dynamics at a glance.

Miscellaneous
Implements a curve of type %1
19 Views
0 Downloads
0 Favorites
Simple Expo Moving Averages
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp.; Mod RonT"
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 White

//---- indicator parameters
extern int MA_Period=13;

//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];

//----
int ExtCountedBars=0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;

//---- drawing settings

   SetIndexStyle(0,DRAW_ARROW, 0, 1, Red);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexArrow(0,158);

   SetIndexStyle(1,DRAW_ARROW, 0, 1, White);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexArrow(1,159);

//   SetIndexShift(0,0);
//   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   
   if(MA_Period<2) MA_Period=13;
   draw_begin=MA_Period-1;

   short_name="SMA(";
   IndicatorShortName(short_name+MA_Period+")");
   SetIndexDrawBegin(0,draw_begin);

   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexBuffer(1, ExtMapBuffer2);

   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);

//---- initialization done
   return(0);
  }

//+------------------------------------------------------------------+
//| Main                                                             |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();

//---- check for possible errors
   if (ExtCountedBars<0) return(-1);

//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;

//----
   sma();

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

//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
void sma()
  {
   double sum=0;
   int    i,pos=Bars-ExtCountedBars-1;

//---- initial accumulation
   if(pos<MA_Period) pos=MA_Period;
   for(i=1;i<MA_Period;i++,pos--)
      sum+=Close[pos];

//---- main calculation loop
   while(pos>=0)
     {
      sum+=Close[pos];
      ExtMapBuffer1[pos]=sum/MA_Period;
      ExtMapBuffer2[pos]=(sum/MA_Period)+5;
	   sum-=Close[pos+MA_Period-1];
 	   pos--;
     }

//---- zero initial bars
   if(ExtCountedBars<1)
      for(i=1;i<MA_Period;i++) ExtMapBuffer1[Bars-i]=0;
      for(i=1;i<MA_Period;i++) ExtMapBuffer2[Bars-i]=0;
  }
//+------------------------------------------------------------------+

Comments