TrendContinuation

Download script
Author: Copyright � 2007, Doc Gaines

This script is designed as a technical analysis tool that helps traders visualize the momentum and direction of a market trend by identifying "trend continuation." Unlike basic indicators that simply follow the price, this logic focuses on measuring and smoothing the underlying force behind price movements to provide a cleaner picture of market behavior.

Core Logic Overview

The indicator works by analyzing the difference between consecutive price points (the "change" in price) and then categorizing these changes into two distinct components: upward momentum and downward momentum.

By separating these forces, the tool can calculate how much "pressure" is building in either direction independently of the other. It then applies a sophisticated smoothing process to these values to filter out market noise, resulting in a visual representation that highlights whether the current trend is sustaining its path or losing steam.

How it Processes Data

The logic follows a specific three-step sequence to reach its final output:

1. Price Change Differentiation The script looks at the price of each candle and determines if it moved up or down compared to the previous one.

  • If the price moves up, it captures that positive movement.
  • If the price moves down, it captures that negative movement. It stores these values in separate "buckets," effectively isolating the strength of the "plus" (bullish) and "minus" (bearish) movements from one another.

2. Cumulative Momentum Calculation Instead of just looking at the most recent candle, the script calculates a running total of these movements over a specific period (defined by the user settings). It essentially builds a "memory" of the recent price action, allowing it to see not just that the price moved, but the cumulative force behind that move.

3. Advanced Smoothing and Visualization To prevent the indicator from jumping erratically with every small price tick, the script passes the calculated momentum through a smoothing algorithm (specifically a T3 method by default). This produces two distinct lines:

  • Trend Continuation Plus: Represents smoothed bullish momentum.
  • Trend Continuation Minus: Represents smoothed bearish momentum.

Visual Interpretation

The result is displayed in a separate window below the main price chart as a filled "cloud."

  • When the bullish momentum dominates, the visual area shifts to reflect positive trend continuation.
  • When bearish momentum takes over, the visual changes to show negative trend continuation.

By observing this "cloud," a trader can more easily identify the prevailing trend and see where the market is showing significant persistence in one direction versus the other.

8 Views
1 Downloads
0 Favorites
TrendContinuation
//+------------------------------------------------------------------+
//|                                            TrendÑontinuation.mq5 |
//|                                     Copyright © 2007, Doc Gaines |
//|                                      dr_richard_gaines@yahoo.com |
//+------------------------------------------------------------------+
//---- author of the indicator
#property copyright "Copyright © 2007, Doc Gaines"
//---- link to the website of the author
#property link      "dr_richard_gaines@yahoo.com"
//---- indicator version number
#property version   "1.00"
//---- drawing indicator in a separate window
#property indicator_separate_window
//---- two buffers are used for the indicator calculation and drawing
#property indicator_buffers 2
//---- one plot is used
#property indicator_plots   1
//+----------------------------------------------+
//|  Parameters of drawing the bullish indicator |
//+----------------------------------------------+
//---- drawing the indicator 1 as a cloud
#property indicator_type1   DRAW_FILLING
//---- the following colors are used for the indicator
#property indicator_color1  clrLime,clrRed
//---- displaying of the bullish label of the indicator
#property indicator_label1  "TrendÑontinuation Minus";"TrendÑontinuation Plus"

//+----------------------------------------------+
//|  CXMA class description                      |
//+----------------------------------------------+
#include <SmoothAlgorithms.mqh> 
//+----------------------------------------------+

//---- declaration of the CXMA class variables from the SmoothAlgorithms.mqh file
CXMA XMA1,XMA2;
//+----------------------------------------------+
//|  declaration of enumerations                 |
//+----------------------------------------------+
enum Applied_price_ //Type od constant
  {
   PRICE_CLOSE_ = 1,     //Close
   PRICE_OPEN_,          //Open
   PRICE_HIGH_,          //High
   PRICE_LOW_,           //Low
   PRICE_MEDIAN_,        //Median Price (HL/2)
   PRICE_TYPICAL_,       //Typical Price (HLC/3)
   PRICE_WEIGHTED_,      //Weighted Close (HLCC/4)
   PRICE_SIMPL_,         //Simpl Price (OC/2)
   PRICE_QUARTER_,       //Quarted Price (HLOC/4) 
   PRICE_TRENDFOLLOW0_,  //TrendFollow_1 Price 
   PRICE_TRENDFOLLOW1_,  //TrendFollow_2 Price
   PRICE_DEMARK_         //Demark Price
  };
//+----------------------------------------------+
//|  declaration of enumerations                 |
//+----------------------------------------------+
/*enum Smooth_Method - enumeration is declared in SmoothAlgorithms.mqh
  {
   MODE_SMA_,  //SMA
   MODE_EMA_,  //EMA
   MODE_SMMA_, //SMMA
   MODE_LWMA_, //LWMA
   MODE_JJMA,  //JJMA
   MODE_JurX,  //JurX
   MODE_ParMA, //ParMA
   MODE_T3,    //T3
   MODE_VIDYA, //VIDYA
   MODE_AMA,   //AMA
  }; */
//+----------------------------------------------+
//| Indicator input parameters                   |
//+----------------------------------------------+
input uint NPeriod=20; //calculation period
input Smooth_Method XMethod=MODE_T3; //smoothing method
input uint XPeriod=5; //smoothing depth
input int XPhase=61; //smoothing parameter,
                     // for JJMA that can change withing the range -100 ... +100. It impacts the quality of the intermediate process of smoothing;
// For VIDIA, it is a CMO period, for AMA, it is a slow moving average period
input Applied_price_ IPC=PRICE_CLOSE;//price constant
//+----------------------------------------------+
//---- declaration of dynamic arrays that will further be 
// used as indicator buffers
double PlusBuffer[];
double MinusBuffer[];
//---- Declaration of integer variables of data starting point
int min_rates_total,min_rates_,nsize;
//---- declaration of dynamic arrays that will further be 
// used as ring buffers
int Count[];
double Change_p[],Change_n[],CF_p[],CF_n[];
//+------------------------------------------------------------------+
//|  Recalculation of position of the newest element in the array    |
//+------------------------------------------------------------------+   
void Recount_ArrayZeroPos(int &CoArr[],// Return the current value of the price series by the link
                          int Size)
  {
//----
   int numb,Max1,Max2;
   static int count=1;

   Max2=Size;
   Max1=Max2-1;

   count--;
   if(count<0) count=Max1;

   for(int iii=0; iii<Max2; iii++)
     {
      numb=iii+count;
      if(numb>Max1) numb-=Max2;
      CoArr[iii]=numb;
     }
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//---- Initialization of variables of the start of data calculation
   min_rates_=int(NPeriod+1);
   min_rates_total=int(NPeriod+XMA1.GetStartBars(XMethod,XPeriod,XPhase));

//---- memory distribution for variables' arrays
   nsize=int(NPeriod);
   ArrayResize(Count,nsize);
   ArrayResize(Change_p,nsize);
   ArrayResize(Change_n,nsize);
   ArrayResize(CF_p,nsize);
   ArrayResize(CF_n,nsize);

//---- Initialization of arrays of variables
   ArrayInitialize(Count,0.0);
   ArrayInitialize(Change_p,0.0);
   ArrayInitialize(Change_n,0.0);
   ArrayInitialize(CF_p,0.0);
   ArrayInitialize(CF_n,0.0);

//---- set PlusBuffer dynamic array as an indicator buffer
   SetIndexBuffer(0,PlusBuffer,INDICATOR_DATA);
//---- set MinusBuffer dynamic array as an indicator buffer
   SetIndexBuffer(1,MinusBuffer,INDICATOR_DATA);

//---- shifting the starting point for drawing indicator 1 by min_rates_total
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//--- creation of the name to be displayed in a separate sub-window and in a pop up help
   IndicatorSetString(INDICATOR_SHORTNAME,"TrendÑontinuation");
//--- determining the accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//----
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(
                const int rates_total,    // amount of history in bars at the current tick
                const int prev_calculated,// amount of history in bars at the previous tick
                const datetime &time[],
                const double &open[],
                const double& high[],     // price array of maximums of price for the calculation of indicator
                const double& low[],      // price array of price lows for the indicator calculation
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]
                )
  {
//---- checking the number of bars to be enough for calculation
   if(rates_total<min_rates_total) return(0);

//---- declaration of variables with a floating point  
   double dprice,k_p,k_n,ch_p,ch_n,cff_n,cff_p;
//---- Declaration of integer variables and getting the bars already calculated
   int first,bar,iii,i0,i1;

//---- calculation of the starting number first for the bar recalculation loop
   if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of calculation of an indicator
      first=1; // starting number for calculation of all bars
   else first=prev_calculated-1; // starting number for calculation of new bars

//---- Main calculation loop of the indicator
   for(bar=first; bar<rates_total && !IsStopped(); bar++)
     {
      dprice=PriceSeries(IPC,bar,open,low,high,close)-PriceSeries(IPC,bar-1,open,low,high,close);
      i0=Count[0];
      i1=Count[1];
      Change_p[i0]=0.0;
      CF_p[i0]=0.0;
      Change_n[i0]=0.0;
      CF_n[i0]=0.0;

      if(dprice>0)
        {
         Change_p[i0]=-dprice;
         CF_p[i0]=Change_p[i0]+CF_p[i1];
        }
      else if(dprice<0)
        {
         Change_n[i0]=+dprice;
         CF_n[i0]=Change_n[i0]+CF_n[i1];
        }

      ch_p=0.0;
      ch_n=0.0;
      cff_p=0.0;
      cff_n=0.0;

      for(iii=0; iii<nsize; iii++) ch_p+=Change_p[iii];
      for(iii=0; iii<nsize; iii++) ch_n+=Change_n[iii];
      for(iii=0; iii<nsize; iii++) cff_p+=CF_p[iii];
      for(iii=0; iii<nsize; iii++) cff_n+=CF_n[iii];

      k_p=ch_p-cff_n;
      k_n=ch_n-cff_p;

      PlusBuffer[bar]=XMA1.XMASeries(min_rates_,prev_calculated,rates_total,XMethod,XPhase,XMethod,k_p,bar,false)/_Point;
      MinusBuffer[bar]=XMA2.XMASeries(min_rates_,prev_calculated,rates_total,XMethod,XPhase,XMethod,k_n,bar,false)/_Point;

      if(bar<rates_total-1) Recount_ArrayZeroPos(Count,nsize);
     }
//----     
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments