Heiken Ashi_v1
Download scriptThis tool is designed to transform the way price action appears on your trading charts by converting standard Japanese Candlesticks into Heiken Ashi bars. Instead of showing the raw price data of each individual period, this script applies a smoothing logic that helps traders identify the underlying trend more clearly by filtering out minor market "noise."
How the Logic Works
The script operates by recalculating the four core components of every price bar—the Open, High, Low, and Close—using a specific mathematical approach:
- The Opening Price: Rather than using the actual price at which the previous period began, the script calculates a new "synthetic" opening price. It does this by taking the average of the previous period's opening and closing values.
- The Closing Price: The closing price is determined by calculating the average of the current period's four main price points (the actual Open, High, Low, and Close).
- The High and Low Points: To ensure the bars accurately represent the price range, the script compares the real market highs and lows against the newly calculated opening and closing values. It selects the highest and lowest figures from these comparisons to define the boundaries of each bar.
Visual Behavior
Based on these calculations, the script determines how to render the bars on your screen:
- Trend Identification: If the newly calculated closing price is higher than the opening price, it signals upward momentum.
- Color & Construction: The script uses these values to fill in specific visual buffers (represented by colors like Red and White). This creates a cohesive visual flow where the length and color of the bars shift based on the strength of the trend.
- Data Processing: To ensure smooth performance, the script systematically scans through the historical price data, recalculating every bar from the most recent point back to the start of the available history.
Summary for the User
In simple terms, this script acts as a "filter." It takes the erratic movements of standard prices and averages them out into Heiken Ashi candles. The goal is to provide a cleaner, more readable view of the market's direction, making it easier to spot sustained trends and smoother price transitions without the distraction of volatile fluctuations.
//+------------------------------------------------------------------+
//| Heiken Ashi.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
//| For Heiken Ashi we recommend next chart settings ( press F8 or |
//| select on menu 'Charts'->'Properties...'): |
//| - On 'Color' Tab select 'Black' for 'Line Graph' |
//| - On 'Common' Tab disable 'Chart on Foreground' checkbox and |
//| select 'Line Chart' radiobutton |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Red
#property indicator_color4 White
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, Black);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, Black);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, White);
SetIndexBuffer(3, ExtMapBuffer4);
//----
SetIndexDrawBegin(0,10);
SetIndexDrawBegin(1,10);
SetIndexDrawBegin(2,10);
SetIndexDrawBegin(3,10);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexBuffer(3,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 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)
{
haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
haHigh=MathMax(High[pos], MathMax(haOpen, haClose));
haLow=MathMin(Low[pos], 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;
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments