Heiken_Ashi_v1

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

This script is designed to transform the standard price action data on a trading chart into Heiken Ashi candles. Unlike traditional Japanese candlesticks, which represent raw price fluctuations, Heiken Ashi candles provide a smoothed visual representation of market momentum by using a weighted average of price data.

Core Logic and Functionality

The primary goal of this script is to filter out "market noise"—the small, erratic price movements that can often lead to false signals—to reveal the underlying trend more clearly. It achieves this through several specific calculation steps:

  • Price Averaging: Instead of plotting the exact opening or closing prices for each period, the script calculates a new "synthetic" open and close. The synthetic close is determined by taking the average of the current period's high, low, open, and close prices.
  • Trend Smoothing: The synthetic opening price is derived from the previous period’s average values. By linking the current candle's start to the previous candle's mathematical mean, the script creates a continuous flow that makes it easier to identify if the market is trending upward, downward, or moving sideways.
  • High and Low Boundary Adjustment: To ensure the visual candles accurately reflect the actual price extremes of the period, the script compares the calculated synthetic values against the real market highs and lows. It then selects the highest and lowest points from these figures to define the boundaries of each candle.
  • Dynamic Coloring: The script applies a distinct visual logic based on the relationship between the synthetic open and close prices. If the closing price is higher than the opening price, it signifies bullish momentum (upward movement); if the closing price is lower, it signifies bearish momentum (downward movement).

Visual Presentation

To work correctly, the script requires specific chart settings to ensure the visual output is seamless:

  • It utilizes a Line Chart background format rather than standard bars.
  • The data is rendered as a Histogram, which allows for a clean, vertical representation of the smoothed price action.
  • By using these specialized calculations, traders can more easily identify "swings" in the market, as the resulting candles appear more uniform and less jagged than standard price bars.
Miscellaneous
Implements a curve of type %1
16 Views
0 Downloads
0 Favorites
Heiken_Ashi_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                  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 White
#property indicator_color3 Red
#property indicator_color4 White
//---- 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, White);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, White);
   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) 
        {
         ExtMapBuffer1[pos]=haLow;
         ExtMapBuffer2[pos]=haHigh;
        } 
      else
        {
         ExtMapBuffer1[pos]=haHigh;
         ExtMapBuffer2[pos]=haLow;
        } 
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
 	   pos--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments