Heiken Ashi Real
Download scriptThis script is designed to transform the way price movements are visualized on your trading platform. Instead of using standard candlesticks, which can sometimes appear noisy or cluttered due to minor market fluctuations, this tool implements the Heiken Ashi charting logic. Heiken Ashi is a specialized forecasting and trend-analysis method that smooths out price action to make it easier to identify the primary direction of the market.
How the Logic Works
The core purpose of this script is to calculate "synthetic" prices based on historical data rather than just displaying the raw high, low, open, and close of each period. Here is the step-by-step logic the script follows:
1. Price Smoothing Calculation Rather than looking at a single moment in time, the script takes the average of previous price points to determine current values. It calculates the "Opening" and "Closing" prices of the Heiken Ashi bars using a specific mathematical average of the actual market data from the previous periods. This creates a continuous flow of movement that filters out "jitter" in the price.
2. Determining Extreme Points Once the smoothed Open and Close values are calculated, the script identifies the highest and lowest points (the "High" and "Low") for that specific period. It ensures that these extremes are logically consistent—meaning the high must be at least as high as the smoothed open or close, and the low must be at least as low as the smoothed open or close.
3. Visual Representation The script then organizes these calculated values into four distinct visual components:
- It defines the boundary lines for each bar.
- It compares whether the current smoothed price is rising (Close is higher than Open) or falling (Open is higher than Close).
- It assigns specific colors (Red and White) to these bars based on that trend direction.
The End User Experience
When you apply this script, your chart will transition from standard price bars to a smoother, more fluid visual style. Because the logic relies on averages of past data:
- Trend Clarity: It makes it significantly easier to see if a market is trending upward or downward because the "noise" of small, insignificant price movements is suppressed.
- Consistency: The colors provide an immediate visual cue; consistent color strings suggest a strong trend, while changes in color signal potential shifts in momentum.
- Readability: By using smoothed averages to define each bar's shape, the chart provides a "cleaner" look that helps traders focus on the broader market structure rather than individual tick fluctuations.
//+------------------------------------------------------------------+
//| Heiken Ashi.mq4 |
//| Copyright c 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 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3
//----
extern color color1 = Red;
extern color color2 = White;
extern color color3 = Red;
extern color color4 = White;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_NONE, 0, 1, color1);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_NONE, 0, 1, color2);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE, 0, 2, color3);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_LINE, 0, 2, color4);
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);
}
//+------------------------------------------------------------------+
//| Custom 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