HeikenAshiCount

Download script
Author: Daniel Frieling
HeikenAshiCount

This tool is designed as a secondary visual aid for MetaTrader users who want to simplify the way they interpret market trends. Rather than cluttering the main price chart, this script functions as an independent indicator that tracks momentum by analyzing the relationship between opening and closing prices on a Heiken Ashi chart.

Core Logic and Functionality

The script operates by "reading" a standard Heiken Ashi indicator to determine the prevailing market direction. It then translates that data into a visual histogram, which provides a clearer picture of trend continuity:

  • Trend Identification: The tool constantly monitors the difference between the opening and closing values of Heiken Ashi candles. If the closing price is higher than the opening price, it interprets this as upward momentum (a "Buy" sentiment). Conversely, if the closing price is lower than the opening price, it identifies downward momentum (a "Sell" sentiment).
  • Visual Representation: Once the direction is identified, the script generates a histogram. It uses specific colors—Green for bullish trends and Red for bearish trends—to give you an immediate visual cue on market sentiment.
  • Dynamic Progression: The indicator is designed to be cumulative in its visualization. When a trend persists, the histogram builds upon the previous data point. This helps traders see not just the current direction, but the strength and duration of that movement over time.
  • Efficiency Features: To ensure the platform remains responsive, the script includes an "IsNewBar" check. This means the logic only performs its heavy calculations when a new candle appears on the chart, rather than constantly refreshing with every price tick.

Key User Requirements

For this tool to function correctly, it relies on a prerequisite: you must have a standard Heiken Ashi indicator installed on your platform. The script is programmed to look for that specific indicator; if it cannot find it, the tool will provide an alert notification. This ensures that the visual data remains accurate and synchronized with your trading environment.

In summary, this script acts as a "trend filter," converting complex price action into a simplified, color-coded histogram that highlights whether the market is currently leaning toward a bullish or bearish bias.

Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
9 Views
0 Downloads
0 Favorites
HeikenAshiCount
//+------------------------------------------------------------------+
//|                                                  Daniel Frieling |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Daniel Frieling"
#property link      "http://meinMetatrader.de"

#property indicator_separate_window
#property indicator_buffers 3

#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 White
#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 4

#property indicator_levelcolor White
#property indicator_level1 9
#property indicator_level2 -9
#property indicator_levelwidth 1

/*#property indicator_maximum 30
#property indicator_minimum -1*/

extern string HaIndicatorName = "Heiken_Ashi";

double GreenHAs[], RedHAs[];
bool StopNow = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorBuffers(2);
  //---- 2 allocated indicator buffers
   SetIndexBuffer(0, GreenHAs);
   SetIndexBuffer(1, RedHAs);
  //---- drawing parameters setting
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   /*SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);*/
  //---- 0 value will not be displayed
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
  //---- displaying in DataWindow
   SetIndexLabel(0, "Green HeikenAshi Candles");
   SetIndexLabel(1, "Red HeikenAshi Candles");
   
   double haOpen = iCustom(Symbol(), Period(), HaIndicatorName, 2, 0);
   
   if(haOpen <= 0)
   {
      Alert("Please install the Heiken Ashi Indicator and set the fileName in inputs.");
      StopNow = true;
   }
   else
      StopNow = false;
    
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   if(!IsNewBar() || StopNow)
      return;

   int limit;
   int counted_bars = IndicatorCounted();
   
 //check for possible errors
   if(counted_bars<0)
      return(-1);
   
 //the last counted bar will be recounted
   if(counted_bars>0)
      counted_bars--;
   
   limit = Bars - counted_bars;
   
  //---- main loop
   //for(int i=0; i < limit; i++)
   for(int i = limit-1; i >= 0; i--)
   //for(int i = 4; i >= 0; i--)
   {
      int prevDir = GetBarsHaDirForShift(i+1, Period());
      
      if(prevDir == OP_BUY)
      {
         GreenHAs[i] = GreenHAs[i+1]+1;
         
         RedHAs[i] = 0;
      }
      else if(prevDir == OP_SELL)
      {
         RedHAs[i] = RedHAs[i+1]-1;
         GreenHAs[i] = 0;
      }
   }
   
   return(0);
}

bool IsNewBar()
{
   static datetime lastBar = 0;
   datetime curBar = Time[0];
   
   if(lastBar != curBar)
   {
      lastBar = curBar;
      
      return(true);
   }
   else
   {
      return(false);
   }
}  

int GetBarsHaDirForShift(int shift, int period)
{
   if(period == -1)
      period = Period();
      
   int dir = -1;
   
   double haOpen = iCustom(Symbol(), period, HaIndicatorName, 2, shift);
   double haClose = iCustom(Symbol(), period, HaIndicatorName, 3, shift);
   
   if(haOpen < haClose)
      dir = OP_BUY;
   else if(haOpen > haClose)
      dir = OP_SELL;
      
   return(dir);
}

//+------------------------------------------------------------------+

Comments