Heiken_Ashi_V

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

This tool is designed to transform the standard price charts used in trading into Heiken Ashi candles. Unlike traditional Japanese candlesticks, which display raw price data, Heiken Ashi candles use a smoothed average of price action to provide a clearer visual representation of market trends and momentum.

How the Logic Works

The script processes historical market data to calculate four distinct values for every candle: the Open, High, Low, and Close prices. However, instead of using the raw numbers provided by the exchange, it applies specific mathematical formulas:

  • Opening Price Calculation: The tool calculates the opening price by taking an average of the previous candles' highs and lows. By looking back over a customizable number of bars (defined by the user), the script "smooths out" sudden price spikes, making it easier to identify the underlying direction of the market.
  • Closing Price Calculation: The script offers two different methods for determining the closing price. It can calculate this based on a simple average of the current candle's high, low, and closing prices, or it can include the opening price in that average as well. This allows users to choose how much "weight" the current period's volatility should have on the final visual output.
  • High and Low Extremes: To ensure the candles accurately represent the full range of price movement, the script compares the calculated "Heiken Ashi" open and close prices against the actual highest and lowest prices reached during that time frame. It then selects whichever values are the true extremes to form the boundaries of the candle.

Visual Representation

The logic concludes by assigning these calculated values to four different visual buffers:

  1. Body Boundaries: It determines the upper and lower bounds of the candle body based on whether the price moved up (Close is higher than Open) or down (Open is higher than Close).
  2. Reference Points: It stores the smoothed Open and Close values used for the calculations.

By using these averages rather than raw data, the resulting chart helps filter out "market noise," making it easier for a trader to see whether the trend is currently bullish, bearish, or moving sideways.

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
Heiken_Ashi_V
//+------------------------------------------------------------------+
//|                                                  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 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2

//----
extern int CountBar=1;  // Êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà öåíû îòêðûòèÿ
extern int mode=0;      // âûáîð âàðèàíòà ðàñ÷åòà öåíû çàêðûòèÿ
                        // (mode==0)   haClose=(High[pos]+Low[pos]+Close[pos])/3.0;
                        // (mode==1)   haClose=(High[pos]+Low[pos]+Close[pos]+Open[pos])/4.0;

//---- 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);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 2);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 2);
   SetIndexBuffer(3, ExtMapBuffer4);


   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;
   double _Open, _High, _Low, _Close;
   int shift;
   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=0;
      for (int i=1;i<=CountBar;i++) {
         haOpen+=((ExtMapBuffer3[pos+i]+ExtMapBuffer4[pos+i])/2.0);
      }
      haOpen/= (CountBar*1.0);
      if (mode==0) haClose=(High[pos]+Low[pos]+Close[pos])/3.0;
      else haClose=(High[pos]+Low[pos]+Close[pos]+Open[pos])/4.0;
      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