Simple Expo Moving Averages_v1
Download scriptThis custom indicator is designed to provide visual cues on a trading chart by calculating a specific type of price average. Instead of drawing a continuous line, it generates distinct markers (arrows) to help identify price trends based on historical data.
Core Functionality
The primary logic revolves around a Simple Moving Average (SMA). This is a standard financial calculation that takes the closing prices of a specific number of candles (determined by a user-defined period, defaulting to 13) and calculates their average. By smoothing out the "noise" of individual price fluctuations, the indicator helps reveal the underlying direction of the market.
How It Works
The script processes market data in the following stages:
- Data Collection: The tool looks back at a set amount of historical bars to calculate the current average price. As new candles form, it continuously updates this calculation by adding the newest price and removing the oldest one from its memory.
- Dual Mapping: The logic creates two separate data streams:
- Primary Average: This represents the standard calculated moving average of the period.
- Offset Average: This calculates the same moving average but adds a fixed numerical offset to the value.
- Visual Representation: Rather than showing a line, the script converts these two values into visual symbols on your chart:
- Red Markers: These correspond to the primary calculated average.
- White Markers: These correspond to the secondary, offset average.
Summary for Traders
In practical terms, this script serves as a tracking tool. It helps you visualize where the "average" price sits over a specific timeframe relative to the current market action. By comparing the position of the Red and White markers against the current price candles, traders can better perceive the momentum and positioning of the market trend.
//+------------------------------------------------------------------+
//| 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