Heiken Ashi-Overlay
Download scriptThis script is designed to integrate a specialized type of Japanese Candlestick chart known as "Heiken Ashi" directly onto your trading platform. Unlike standard candlesticks, which show the raw price action of every individual bar, Heiken Ashi candles are calculated using a mathematical average of the previous price movements. This smoothing effect helps traders identify the underlying trend of the market more clearly by filtering out "market noise."
How the Logic Works
The script functions by performing a real-time recalculation of price data to generate these smoothed candles. It follows a specific logical flow:
1. Data Averaging (The Smoothing Process) Instead of simply plotting the current market price, the script takes four different data points—the Open, High, Low, and Close prices—and calculates an average for each. Specifically:
- It determines the "Close" of a Heiken Ashi candle by averaging the total range of the current period.
- It determines the "Open" of a Heiken Ashi candle by averaging the previous period's data. This mathematical transition ensures that the resulting bars flow more fluidly, making it easier to see if the market is in a steady state of growth or decline.
2. Dynamic Range Calculation For every new bar on your chart, the script automatically finds the highest and lowest points reached during that timeframe. It compares these extreme prices against the calculated Heiken Ashi "Open" and "Close" values to ensure the visual representation stays accurate to the actual price boundaries of the market.
3. Visual Trend Mapping The logic includes a specific rule for how these results are displayed on your screen:
- Bullish Movement: If the calculated "Close" is higher than the "Open," the script identifies this as upward momentum.
- Bearish Movement: If the "Close" is lower than the "Open," it identifies downward momentum.
The script then assigns specific positions and colors based on these results, allowing you to see a clear visual distinction between buying and selling pressure.
4. Custom Display Options The script includes a built-in toggle that allows you to choose where the data appears on your screen. Depending on your preference, it can be configured to sit at the top of a specific area or occupy a different vertical position, ensuring that the visual information does not clutter your primary price action while still providing high-quality trend analysis.
//+------------------------------------------------------------------+
//| Heiken Ashi-Overlay.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//| Modififed by Igorad for display in separate window |
//| Modified by Rupp for overlay display |
//| "http://www.forex-tsd.com/"|
//+------------------------------------------------------------------+
//| 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_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_width1 3
#property indicator_color2 ForestGreen
#property indicator_width2 3
#property indicator_maximum 0.7000
#property indicator_minimum 0.0000
//#property indicator_color3 Red/
//#property indicator_color4 White
extern bool OverlayTop = false;
double OverlayPosition = 0.00;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
IndicatorBuffers(4);
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,167);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,167);
SetIndexBuffer(1, ExtMapBuffer2);
// SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
SetIndexBuffer(2, ExtMapBuffer3);
// SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, ForestGreen);
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)
{
if (OverlayTop == false) OverlayPosition = 0.01;
if (OverlayTop == true) OverlayPosition = 0.68;
ExtMapBuffer1[pos]=OverlayPosition;
ExtMapBuffer2[pos]=1;
}
else
{
ExtMapBuffer1[pos]=1;
ExtMapBuffer2[pos]=OverlayPosition;
}
ExtMapBuffer3[pos]=haOpen;
ExtMapBuffer4[pos]=haClose;
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments