Heiken_Ashi_Mod(03SEP05)
Download scriptThis tool is designed to transform the standard way you view price action on a trading chart. Instead of showing traditional candlesticks, which can sometimes be "noisy" and reflect minor price fluctuations, this script generates Heiken Ashi bars. The primary goal of this logic is to smooth out price movements, making it easier for traders to identify the underlying trend of the market at a glance.
How the Logic Works
The script processes historical price data (the high, low, open, and close prices) and recalculates them using a specific mathematical average. Here is the step-by-step breakdown of that process:
- Smoothing the Opening Price: Rather than using the actual price at which a candle started, the script calculates a new "synthetic" opening price by averaging the previous bar's high and low values.
- Calculating the Closing Price: It determines the current "synthetic" closing price by taking the average of the current bar’s four main components: the high, low, open, and close.
- Determining the Extremes: Once the new opening and closing prices are established, the script looks for the highest and lowest points reached during that period to define the boundaries of the new bar.
Visual Representation
The logic applies a specific color-coding system based on the relationship between the calculated opening and closing prices:
- Bullish Trend: If the synthetic closing price is higher than the synthetic opening price, the script identifies this as an upward movement. It maps these values to specific visual colors (typically Blue) to signify strength.
- Bearish Trend: If the synthetic closing price is lower than the synthetic opening price, it identifies a downward move and applies a different color (typically Red) to indicate weakness.
Summary for the User
By replacing standard price bars with these calculated Heiken Ashi bars, the script filters out "market noise." This allows you to see the momentum of a trend more clearly. If you see a consistent string of one color, it suggests a strong, ongoing trend; if the colors begin to flip frequently, it indicates a potential loss of momentum or a change in market direction.
//+------------------------------------------------------------------+
//| 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 RoyalBlue
#property indicator_color3 Red
#property indicator_color4 RoyalBlue
//---- 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, RoyalBlue);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 1, Red);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 1, RoyalBlue);
SetIndexBuffer(3, ExtMapBuffer4);
//----
SetIndexDrawBegin(0,5);
//---- 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