Author: Copyright � 2004, MetaQuotes Software Corp.
Rads HA-2

This script is designed to transform the standard visual representation of price action into Heiken Ashi candles. Unlike traditional Japanese Candlesticks, which reflect the raw price movement of a specific time period (the "Open," "High," "Low," and "Close"), Heiken Ashi candles use a smoothed mathematical calculation to provide a clearer picture of market trends and momentum.

How the Logic Works

The script processes historical price data to calculate four distinct values for every bar on your chart. Here is the step-by-step logic it follows:

1. Smoothing the Price Data Instead of plotting the actual price, the script calculates a "Synthetic" Open and Close for each candle:

  • The Synthetic Open: This is calculated by taking the average of the previous candle's real High and Low values. This creates a "rolling" average that helps filter out minor price fluctuations (market noise).
  • The Synthetic Close: This is calculated by averaging the current period's Open, High, Low, and Close prices together.

2. Determining the Range (High and Low) To ensure the candle accurately represents the boundaries of price movement, the script looks at the highest and lowest points reached during that period. It then compares those actual highs and lows against the newly calculated synthetic open and close, ensuring the resulting "candle" encompasses the full range of the price action.

3. Visual Representation and Color Logic Once these four values are established, the script determines how to draw the candle on your screen:

  • Bullish Movement: If the calculated closing price is higher than the opening price, the script identifies an upward trend. It then maps the data points to the appropriate visual buffers to represent a "rising" candle.
  • Bearish Movement: If the closing price is lower than the opening price, it recognizes a downward trend and maps the data accordingly to reflect a "falling" candle.

Summary of Benefits

By applying this logic, the script moves away from the erratic "zig-zag" look of standard charts. Because every point on a Heiken Ashi candle is influenced by the previous candle's data, it creates a smoother visual flow. This makes it significantly easier for a trader to identify the underlying direction of the market—helping to distinguish between a temporary price spike and a sustained trend change.

Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
Rads HA-2
//+------------------------------------------------------------------+
//|                                                  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 Maroon
#property indicator_color2 DarkBlue
#property indicator_color3 Red
#property indicator_color4 Blue


//----
extern color color1 = Maroon;
extern color color2 = DarkBlue;
extern color color3 = Red;
extern color 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, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 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