This script is designed for the MetaTrader platform and aims to visually represent potential trends in price movements on a chart, separate from the main price chart. It essentially identifies how many consecutive bars the price continues to rise or fall before changing direction, up to a certain limit.
Here's the breakdown:
-
Input: The script takes one setting you can adjust, called "Rasrad". Think of "Rasrad" as a look-back window or a maximum search distance. This number determines the maximum number of bars the script will look back to confirm a trend. The default value is 16.
-
Data Storage: The script uses two separate arrays (lists) to store its calculations. One array, displayed as a red histogram, tracks the strength of upward trends, while the other, displayed as a blue histogram, tracks the strength of downward trends. These histograms will be drawn on a separate window within MetaTrader.
-
Calculation Logic: For each bar on the chart, the script performs the following:
-
Upward Trend Check (Red Histogram):
- It compares the high price of the current bar to the high price of the previous bar.
- If the current bar's high is higher than the previous bar's high, it assumes there might be an uptrend.
- It then counts how many consecutive bars in the past (up to the "Rasrad" limit) had increasing high prices. If the price is not higher than the previous bar, it records a 0.
- The count represents the "strength" of the upward trend. This value is stored in the red histogram array. A longer bar suggests a stronger uptrend.
-
Downward Trend Check (Blue Histogram):
- It compares the low price of the current bar to the low price of the previous bar.
- If the current bar's low is lower than the previous bar's low, it assumes there might be a downtrend.
- It then counts how many consecutive bars in the past (up to the "Rasrad" limit) had decreasing low prices. If the price is not lower than the previous bar, it records a 0.
- The count represents the "strength" of the downward trend. The negative of this value is stored in the blue histogram array. A longer bar suggests a stronger downtrend.
-
-
Visual Representation: The script displays the results as two histograms in a separate window. The red histogram shows upward trends, and the blue histogram shows downward trends. The length of each bar in the histogram represents the number of consecutive bars the price was moving in that direction (up to the "Rasrad" limit), effectively visualizing the strength and duration of potential trends.
In simpler terms, imagine the script is a trend spotter. It looks back a certain number of bars (determined by "Rasrad") and tries to find consecutive increases in high prices (uptrend) or decreases in low prices (downtrend). The resulting histograms show how many bars the trend lasted.
//+------------------------------------------------------------------+
//| Trend.mq4 |
//| Yuriy Tokman |
//| yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman"
#property link "yuriytokman@gmail.com"
#property indicator_separate_window
#property indicator_minimum -25
#property indicator_maximum 25
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_level7 -15
#property indicator_level8 15
//---- input parameters
extern int Rasrad=16;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int rasr,i,a,k;
double price;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
if(counted_bars==0) limit-=1+MathMax(1,Rasrad);
rasr=Rasrad;
i=limit;
while(i>=0)
{
rasr=Rasrad;
price=High[i];
if(price>High[i+1])
{
k=1;
while(price>High[i+k])
{
k=k+1;
if(k>rasr){break;}
}
ExtMapBuffer1[i]=k-1;
}
else{ExtMapBuffer1[i]=0;}
rasr=Rasrad;
price=Low[i];
if(price<Low[i+1])
{
k=1;
while(price<Low[i+k])
{
k=k+1;
if(k>rasr){break;}
}
a=k-1;
ExtMapBuffer2[i]=-a;
}
else{ExtMapBuffer2[i]=0;}
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments