Heiken Ashi2

Download script
Author: Copyright � 2004, MetaQuotes Software Corp.
Heiken Ashi2

This script is designed to transform standard financial price charts into Heiken Ashi candles, which are a specialized type of candlestick chart used by traders to filter out "market noise" and more clearly identify long-term trends.

Core Logic Overview

Instead of plotting the raw high, low, open, and close prices as they occur in real-time, this script recalculates those values using a smoothing formula. The goal is to create a visual representation where each candle represents the "average" price action of a period, making it easier for a user to see if the market is trending upwards or downwards without getting distracted by minor, erratic price fluctuations.

How the Calculations Work

The script processes the data through three primary logical steps:

1. Smoothing the Opening and Closing Prices

  • The Close: The script determines the new "close" of a Heiken Ashi candle by taking the simple average of four components: the current high, low, open, and close prices.
  • The Open: To ensure continuity between candles, the script calculates the "open" of the current candle by averaging the open and close values of the previous Heiken Ashi candle. This creates the smooth, flowing appearance characteristic of this chart style.

2. Defining Extremes (Highs and Lows) Once the new smoothed Open and Close values are established, the script looks at the actual market High and Low for that period. It then selects the highest and lowest points between those four figures to determine the boundaries of the candle. This ensures that the candles still respect the actual price extremes reached during the timeframe.

3. Visual Coloring and Construction The script uses a conditional logic system to assign colors and shapes:

  • If the calculated smoothed Close is higher than the smoothed Open, it indicates upward momentum.
  • If the smoothed Close is lower than the smoothed Open, it indicates downward momentum.
  • The script then maps these values into four distinct data buffers, which the platform uses to draw the final histogram.

User Experience Impact

When applied, this script replaces the standard "jagged" price movement with a more fluid visual. For a trader, this means:

  • Trend Identification: It is easier to see the direction of the market at a glance.
  • Noise Reduction: Minor price spikes that don't change the overall trend are dampened.
  • Visual Clarity: By using these smoothed averages, the chart provides a cleaner "story" of where the price has been and where it is likely headed.
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Heiken Ashi2
//+------------------------------------------------------------------+
//|                                                  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 Green
#property indicator_color3 Red
#property indicator_color4 Green
//----
extern color color1 = Red;
extern color color2 = Green;
extern color color3 = Red;
extern color color4 = Green;
//---- 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, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM, 0, 1, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM, 0, 2, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3, DRAW_HISTOGRAM, 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()
  {
//----
   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