Trend Bands_v1
Download scriptThe Trend Bands indicator is a visual trading tool designed to help market participants identify the prevailing direction of price movement and potential areas of overextension. By creating a multi-layered band system on your chart, it provides a clearer picture of the "normal" range of price action versus extreme fluctuations.
How the Logic Works
The indicator functions by calculating three distinct lines based on moving averages and historical volatility:
1. The Central Trend Line (Middle Band) At its core, the script uses an Exponential Moving Average (EMA). Unlike a standard average that treats all past data equally, this specific calculation places a higher weight on recent price information. This results in a "middle line" that reacts more fluidly to current market shifts, serving as the primary anchor for identifying the trend direction.
2. The Volatility Factor (Average Range)
To determine how wide the outer bands should be, the script performs a custom calculation called findAvg. Instead of looking at just the price, it looks at the "High" and "Low" of each candle over a specific period (defaulting to 34). By subtracting the low from the high for every candle in that timeframe, the script calculates the average daily or hourly range. This helps the indicator understand how much the market typically "swings."
3. The Outer Bands (Upper and Lower) The indicator then takes a Simple Moving Average (SMA) and expands it outward using the volatility factor mentioned above. It multiplies the average price range by a constant factor of 3.5 to create:
- An Upper Band: Representing a zone where prices might be considered overextended or "overbought."
- A Lower Band: Representing a zone where prices might be considered excessively low or "oversold."
Summary for Traders
In practical terms, this script creates a "channel" on your chart. When the price stays within these bands, it suggests the market is moving in a stable trend. When the price pushes toward or breaks through the outer yellow lines, it signals that the market is experiencing high volatility or may be reaching an extreme point, potentially offering a cue to watch for a reversal or a period of consolidation.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Trend Bands.mq4 |
//| Dwt5 and adoleh2000 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Magenta
#property indicator_color3 Yellow
double upper[], middle1[], middle2, lower[];
extern int period = 34;
int init()
{
SetIndexStyle(0,DRAW_LINE,EMPTY,2);
SetIndexShift(0,0);
SetIndexDrawBegin(0,0);
SetIndexBuffer(0,upper);
SetIndexStyle(1,DRAW_LINE,EMPTY,3);
SetIndexShift(1,0);
SetIndexDrawBegin(1,0);
SetIndexBuffer(1,middle1);
SetIndexStyle(2,DRAW_LINE,EMPTY,2);
SetIndexShift(2,0);
SetIndexDrawBegin(2,0);
SetIndexBuffer(2,lower);
//---- indicators
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
double avg;
for(int x=0; x<limit; x++) {
middle1[x] = iMA(NULL, 0, period, 0, MODE_EMA, PRICE_TYPICAL, x);// drawn line
middle2= iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);// only used to calculate outer bands
avg = findAvg(period, x);
upper[x] = middle2 + (3.5*avg);
lower[x] = middle2 - (3.5*avg);
}
return(0);
}
//+------------------------------------------------------------------+
double findAvg(int period, int shift) {
double sum=0;
for (int x=shift;x<(shift+period);x++) {
sum += High[x]-Low[x];
}
sum = sum/period;
return (sum);
}
Comments