#HeikenAshi
Download scriptThis script is designed to transform standard price charts into a specialized visual format known as Heiken Ashi. Unlike traditional Japanese candlesticks, which simply show the raw high, low, open, and close prices of a specific time period, this tool applies a mathematical smoothing technique to help traders identify the underlying trend and momentum of the market more clearly.
How the Logic Works
The core functionality of this script relies on recalculating every price point on your chart using a specific set of formulas. Instead of plotting raw data, it generates four new values for every bar:
- Smoothed Opening Price: The starting point of each bar is calculated by taking the average of the previous bar's open and close values. This helps reduce "noise" from erratic price spikes.
- Average Closing Price: The closing price for the current bar is determined by taking the average of four components: the current high, low, open, and close.
- Adjusted Highs and Lows: To ensure the visual representation remains accurate to the actual market activity, the script compares the calculated opening and closing prices against the real-time highs and lows of the period. It then selects the highest and lowest values among these to define the boundaries of each bar.
Visual Representation
The script organizes these calculations into a series of visual "buffers" that appear on your chart:
- Trend Differentiation: The logic automatically distinguishes between upward and downward movements. If the calculated closing price is higher than the opening price, it signals an upward trend (typically represented in blue). If the closing price is lower, it signals a downward trend (typically represented in red).
- Histogram Style: Rather than standard thin lines or wicks, the script renders these values as histograms. This provides a thicker, more substantial visual cue of price action, making it easier for a viewer to see whether the market is pushing steadily in one direction or experiencing volatile shifts.
Summary for the User
By using this script, you are essentially viewing a "filtered" version of the market. By averaging out the raw volatility of every tick, the logic aims to provide a cleaner picture of price flow, allowing you to see the primary direction of the market with less distraction from minor, temporary price 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 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
//---- 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, Red);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, Blue);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, Blue);
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