Heiken_Ashi_Ma_Bars
Download scriptThis script is a specialized technical analysis tool designed for the MetaTrader trading platform. It modifies the visual representation of price action by combining two popular charting methods: Moving Averages and Heiken Ashi candles.
The primary goal of this logic is to smooth out market "noise" and provide traders with a clearer view of the underlying trend. Here is a breakdown of how the logic functions:
Core Mechanics
The script works by looking at historical price data and performing several calculations in real-time to generate a custom visual output. Instead of simply plotting the raw price, it processes the data through three distinct stages:
- Moving Average Integration: The tool first calculates specific moving averages based on user-defined periods and methods. It looks at the "Open," "High," "Low," and "Close" prices to establish a baseline for the current market movement.
- Heiken Ashi Transformation: It then applies Heiken Ashi logic—a type of "average price" candle. This involves calculating a synthetic opening and closing price. By averaging these values, the script creates candles that are less volatile than standard price bars, making it easier to identify whether the momentum is bullish (upward) or bearish (downward).
- Dynamic Price Mapping: Finally, the script compares these transformed Heiken Ashi values against the actual market prices. It uses this comparison to determine the final boundaries of the visual bars on your chart.
Visual Output
The result of this logic is a visual indicator that highlights price trends more fluidly:
- Trend Clarity: By using moving averages to calculate the "body" of the candles, the tool filters out minor price fluctuations that can often lead to "fake-outs."
- Directional Signaling: The script identifies the relationship between the synthetic prices and actual market highs/lows. This allows the chart to display colored bars (Red and Royal Blue) that represent the strength and direction of the trend more effectively than raw price action alone.
Summary for the User
In simple terms, this script acts as a "filter." It takes raw, jagged market data, smooths it out using moving averages, converts it into a simplified candle format (Heiken Ashi), and then projects it back onto your chart. This helps traders see the "big picture" of market momentum without being distracted by every small price tick.
//+------------------------------------------------------------------+
//| Heiken Ashi Ma.mq4 |
//+------------------------------------------------------------------+
//| mod by Raff |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link "http://www.forex-tsd.com/"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 RoyalBlue
//---- parameters
extern int MaMetod = 1;
extern int MaPeriod = 3;
//---- buffers
double val1[];
double val2[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
IndicatorBuffers(6);
SetIndexStyle(0,DRAW_HISTOGRAM,0,1);
SetIndexStyle(1,DRAW_HISTOGRAM,0,1);
SetIndexBuffer(0,val1);
SetIndexBuffer(1,val2);
//----
SetIndexDrawBegin(0,5);
SetIndexDrawBegin(1,5);
//---- indicator buffers mapping
SetIndexBuffer(2,ExtMapBuffer1);
SetIndexBuffer(3,ExtMapBuffer2);
SetIndexBuffer(4,ExtMapBuffer3);
SetIndexBuffer(5,ExtMapBuffer4);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double maOpen, maClose, maLow, maHigh;
double haOpen, haHigh, haLow, haClose;
if(Bars<=10) return(0);
ExtCountedBars=IndicatorCounted();
//---- check for possible errors
if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
if (ExtCountedBars>0) ExtCountedBars--;
int pos=Bars-ExtCountedBars-1;
while(pos>=0)
{
maOpen=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_OPEN,pos);
maClose=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_CLOSE,pos);
maLow=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_LOW,pos+1);
maHigh=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_HIGH,pos+1);
haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
haClose=(maOpen+maHigh+maLow+maClose)/4;
haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
haLow=MathMin(maLow, MathMin(haOpen, haClose));
if (haOpen<haClose)
{
ExtMapBuffer1[pos]=haLow;
ExtMapBuffer2[pos]=haHigh;
}
else
{
ExtMapBuffer1[pos]=haHigh;
ExtMapBuffer2[pos]=haLow;
}
ExtMapBuffer3[pos]=haOpen;
ExtMapBuffer4[pos]=haClose;
if (ExtMapBuffer3[pos]<ExtMapBuffer4[pos])
{
val1[pos]=Low[pos]; val2[pos]=High[pos];
}
if (ExtMapBuffer4[pos]<ExtMapBuffer3[pos])
{
val1[pos]=High[pos]; val2[pos]=Low[pos];
}
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments