TrendContinuation_v3

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

This script is designed as a technical indicator for MetaTrader, specifically created to visualize and analyze the momentum of price trends. It operates by calculating two distinct trend components—one representing upward movement (displayed in blue) and one representing downward movement (displayed in red)—and then smoothing that data into a continuous line.

Core Logic and Data Processing

The script functions through several layers of mathematical processing to filter out "noise" from the price action:

1. Price Change Identification The logic first scans every historical price bar to determine the direction of movement.

  • If the current price is higher than the previous one, it records the positive difference as a "Price Change."
  • If the current price is lower, it records the negative difference.
  • This allows the script to isolate pure gains from pure losses independently.

2. Accumulation of Movement Once these changes are identified, the script calculates a "Cumulative Factor." Instead of just looking at what happened in a single moment, it aggregates these changes over a specific window of time. This helps the indicator understand the weight of the recent price history rather than reacting impulsively to a single tiny tick.

3. Trend Balancing The script then performs a unique balancing act between opposite directions. It subtracts the accumulated downward movement from the upward movement (and vice versa). By doing this, it highlights where one trend is significantly overpowering the other. This prevents the indicator from becoming "overloaded" by high volatility that doesn't actually result in a significant change in trend direction.

4. Smoothing and Filtering (T3 Calculation) To make the final output readable for a human observer, the script applies a sophisticated smoothing technique known as the T3 moving average logic. This involves:

  • Weighting: It assigns specific weights to the data points based on user-defined periods.
  • Multi-Stage Filtering: It passes the raw data through a series of recursive calculations (e1 through e6). Each step refines the data further, removing jagged edges and minor fluctuations while maintaining the integrity of the overall trend.
  • The Final Output: The result is a smooth, continuous line that identifies the "Trend Continuation Factor."

How to Interpret the Visuals

Because the script separates positive and negative forces into two distinct lines:

  • Blue Line: Represents the strength and continuation of the upward trend.
  • Red Line: Represents the strength and continuation of the downward trend.

By observing these lines in a separate window on your chart, you can see which "force" is currently dominant. When one line moves significantly while the other remains flat or retreats, it signals that the market has a clear directional bias, helping traders identify where a trend might be gaining strength or beginning to exhaust itself.

Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
TrendContinuation_v3

//+------------------------------------------------------------------+
//|               Trend continuation factor.mq4                      |
//+------------------------------------------------------------------+
#property copyright " Copyright © 2004, MetaQuotes Software Corp."
#property link      " http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_color1 Blue
#property indicator_buffers 2
#property indicator_color2 Red
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Common External variables                                        |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| External variables                                               |
//+------------------------------------------------------------------+
extern double n = 20;
extern double CountBars = 5000;
extern double t3_period = 5;
extern double b = 0.618;
//+------------------------------------------------------------------+
//| Special Convertion Functions                                     |
//+------------------------------------------------------------------+
int LastTradeTime;
int shift;
double ExtHistoBuffer[];
double ExtHistoBuffer2[];
void SetLoopCount(int loops)
{
}
void SetIndexValue(int shift, double value)
{
  ExtHistoBuffer[shift] = value;
}
void SetIndexValue2(int shift, double value)
{
  ExtHistoBuffer2[shift] = value;
}
//+------------------------------------------------------------------+
//| End                                                              |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
int init()
{
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, ExtHistoBuffer);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(1, ExtHistoBuffer2);
   return(0);
}
int start()
{
//+------------------------------------------------------------------+
//| Local variables                                                  |
//+------------------------------------------------------------------+
//double shift = 0;
int cnt = 0;
double k_n = 0;
double k_p = 0;
int shiftt = 0;
double ch_p = 0;
double ch_n = 0;
double cff_p = 0;
double cff_n = 0;
double AccountedBars = 0;
double CF_p[5001];
double CF_n[5001];
double Change_p[5001];
double Change_n[5001];
double t3 = 0;
double t32 = 0;
double A1 = 0;
double A2 = 0;
double b2 = 0;
double b3 = 0;
double c1 = 0;
double c2 = 0;
double c3 = 0;
double c4 = 0;
double e1 = 0;
double e2 = 0;
double e3 = 0;
double e4 = 0;
double e5 = 0;
double e6 = 0;
double n1 = 0;
double w1 = 0;
double w2 = 0;
double e12 = 0;
double e22 = 0;
double e32 = 0;
double e42 = 0;
double e52 = 0;
double e62 = 0;
/*[[
 Name := Trend Continuation Factor
 Author :ENG.A`ED AL NAIRAB
 Link := http://www.nairab.com/
 Separate Window := Yes
 First Color := Blue
 First Draw Type := Line
 First Symbol := 217
 Use Second Data := Yes
 Second Color := Red
 Second Draw Type := Line
 Second Symbol := 218
]]*/
 
 
 
 

b2=b*b;
b3=b2*b;
c1=-b3;
c2=(3*(b2+b3));
c3=-3*(2*b2+b+b3);
c4=(1+3*b+b3+3*b2);
n1=t3_period;
if( n1<1 ) n1=1;
n1 = 1 + 0.5*(n1-1);
w1 = 2 / (n1 + 1);
w2 = 1 - w1;
SetLoopCount(0);
// loop from first bar to current bar (with shift=0)
if( AccountedBars == 0 ) AccountedBars = Bars-CountBars;
for(cnt =AccountedBars;cnt <=Bars-1 ;cnt ++){ 
shift = Bars - 1 - cnt;
{
if( Close[shift] > Close[shift+1]) 
  { Change_p[shift] = Close[shift]- Close[shift+1];
  CF_p[shift]= Change_p[shift] + CF_p[shift+1];
   Change_n[shift] = 0;
   CF_n[shift]= 0;
   }
     else 
  { Change_p[shift] = 0;
   CF_p[shift] = 0;
   Change_n[shift]  = Close[shift+1]- Close[shift];
   CF_n[shift] = Change_n[shift]+ CF_n[shift+1];
   }
}
for(shiftt=shift+n;shiftt>=shift ;shiftt--){ 
ch_p = Change_p[shiftt] +ch_p;
ch_n = Change_n[shiftt]+ch_n ;
cff_p =  CF_p[shiftt]+cff_p; 
cff_n =  CF_n[shiftt]+cff_n;
} 
k_p=ch_p-cff_n;
k_n=ch_n-cff_p;
 A1 = k_p;
e1 = w1*A1 + w2*e1;
e2 = w1*e1 + w2*e2;
e3 = w1*e2 + w2*e3;
e4 = w1*e3 + w2*e4;
e5 = w1*e4 + w2*e5;
e6 = w1*e5 + w2*e6;
t3 = c1*e6 + c2*e5 + c3*e4 + c4*e3;
SetIndexValue(shift,t3);
A2 = k_n;
e12 = w1*A2 + w2*e12;
e22 = w1*e12 + w2*e22;
e32 = w1*e22 + w2*e32;
e42 = w1*e32 + w2*e42;
e52 = w1*e42 + w2*e52;
e62 = w1*e52 + w2*e62;
t32 = c1*e62 + c2*e52 + c3*e42 + c4*e32;
SetIndexValue2(shift,t32);
 AccountedBars=AccountedBars+1;
ch_p=0;
ch_n=0;
cff_p=0;
cff_n=0;  
} 
  return(0);
}

// end//
 

Comments