Ascbars_v1
Download scriptThis script is designed to generate a specialized type of price chart known as Heiken Ashi. Unlike standard candlestick charts, which represent the raw price action of a specific time period, Heiken Ashi charts use a mathematical smoothing technique to filter out "market noise." This allows traders to more clearly identify the underlying trend and momentum of the market.
How the Logic Works
The script processes historical price data (the High, Low, Open, and Close prices) and transforms them into four specific components to build the Heiken Ashi visual:
- Average Price Calculation: Instead of relying solely on the current period's opening and closing prices, the script calculates an "average" open and close. This is achieved by combining data from previous periods with the current period's price movements.
- Dynamic Highs and Lows: The script determines the highest and lowest points for each bar by comparing the actual market highs and lows against the newly calculated average values. This ensures that the visual representation remains anchored to real market extremes while still following the smoothed trend.
- Trend Differentiation: The logic uses a conditional check to determine the color of the bars. If the calculated "average close" is higher than the "average open," it signifies upward momentum (bullish). Conversely, if the average close is lower, it represents downward momentum (bearish).
Visual Representation
To provide the best user experience, the script organizes these calculations into four distinct layers:
- Bullish Momentum: Displayed in a bright color (Light Cyan) to highlight periods of strength.
- Bearish Momentum: Displayed in a contrasting color (Pink) to highlight periods of weakness.
- Trend Smoothing: By using these calculated averages, the resulting chart provides a cleaner look, making it easier for users to see whether the market is moving steadily in one direction or experiencing volatile fluctuations.
Summary for Users
Essentially, this script takes the "messy" raw data of the standard market and reorganizes it into a streamlined visual format. It prioritizes the flow of the trend over individual price spikes, helping you distinguish between minor price fluctuations and significant shifts in market direction.
/*-----------------------------+
| |
| 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 Pink
#property indicator_color2 LightCyan
#property indicator_color3 Pink
#property indicator_color4 LightCyan
//---- 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, Pink);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, LightCyan);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Pink);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, LightCyan);
SetIndexBuffer(3, ExtMapBuffer4);
//----
SetIndexDrawBegin(0,5);
//---- 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