Author: Copyright � 2006, MetaQuotes Software Corp.
COG

This script is a specialized technical analysis tool designed for the MetaTrader platform. Its primary purpose is to calculate and visualize complex price trends by utilizing advanced mathematical modeling rather than simple moving averages.

Core Methodology: Polynomial Regression

The fundamental logic of this script relies on Polynomial Regression. Unlike standard indicators that simply average past prices, this tool attempts to find a "line of best fit" for the price data over a specific look-back period (defined by the bars_back parameter).

By using a polynomial approach, the script can capture the curvature of price movements. It identifies how the price is accelerating or decelerating, allowing it to create a smoother and more structurally sound representation of the market trend than a linear calculation could provide.

Step-by-Step Processing Logic

To arrive at the final visual data on your chart, the script performs the following operations:

  1. Data Summation: It first calculates the powers of the price indices to establish a mathematical foundation for the polynomial curve.
  2. Matrix Construction: The script builds a matrix of values based on closing prices and their respective time positions.
  3. Orthogonalization (The "Cleaning" Phase): It performs a series of iterative calculations to eliminate statistical noise. This ensures that each degree of the polynomial is independent, preventing the calculation from becoming distorted by outliers or erratic price spikes.
  4. Coefficient Calculation: It solves for the specific coefficients needed to draw the regression line. These coefficients determine the exact shape of the curve based on the history provided.
  5. Trend Smoothing: Once the base regression line is established, the script applies a smoothing effect to ensure the visual output is fluid and readable.

Dynamic Volatility Envelopes

The script does not just provide one single line; it creates a "channel" or "envelope" around the primary trend. It achieves this through several layers of calculation:

  • Standard Deviation: The tool calculates how much the price typically deviates from the calculated regression line.
  • Multi-Layered Offsets: It generates several different buffer lines (calculated as additions and subtractions from the main trend). These represent upper and lower boundaries.
  • Mean Convergence: By averaging these upper and lower bounds, the script creates balanced channels that react to market volatility. If the market becomes more volatile, these channels widen; when the market stabilizes, they narrow.

Visual Output Summary

When you view this on a chart, the logic translates into a series of distinct lines:

  • A primary trend line representing the smoothed "path" of the price.
  • Multiple boundary lines that create a visual corridor.

Traders typically use these boundaries to identify overextended price action (when the price touches the outer limits) or to confirm the strength of the current market direction based on the shape and width of the regression channel.

Indicators Used
Standard Deviation indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
COG
//+------------------------------------------------------------------+
//|                                                          COG.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 RoyalBlue
#property indicator_color2 LimeGreen
#property indicator_color3 LimeGreen
#property indicator_color4 Goldenrod
#property indicator_color5 Goldenrod
#property indicator_color6 DeepSkyBlue
#property indicator_color7 DeepSkyBlue

extern int bars_back = 123;
extern int m = 2;
extern int i = 0;
extern double kstd = 2.0;
extern int sName = 1102;
double g_ibuf_100[];
double g_ibuf_104[];
double g_ibuf_108[];
double g_ibuf_112[];
double g_ibuf_116[];
double g_ibuf_120[];
double g_ibuf_124[];
double gda_128[10][10];
double gda_132[10];
double gda_136[10];
double gda_140[20];
double gd_144;
int gi_unused_152;
int g_period_156;
int gi_160;
double gd_168;
double gd_176;
double gd_184;
int gi_192;
int gi_196;
int gi_200;
int gi_204;
int gi_208;
double gd_212;
double gd_220;

int init() {
   IndicatorShortName("COG");
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, g_ibuf_100);
   SetIndexBuffer(1, g_ibuf_104);
   SetIndexBuffer(2, g_ibuf_108);
   SetIndexBuffer(3, g_ibuf_112);
   SetIndexBuffer(4, g_ibuf_116);
   SetIndexBuffer(5, g_ibuf_120);
   SetIndexBuffer(6, g_ibuf_124);
   g_period_156 = MathRound(bars_back);
   gi_208 = m + 1;
   ObjectCreate("pr" + sName, OBJ_ARROW, 0, Time[g_period_156], g_ibuf_100[g_period_156]);
   ObjectSet("pr" + sName, OBJPROP_ARROWCODE, 159);
   return (0);
}

int deinit() {
   ObjectDelete("pr" + sName);
   return (0);
}

int start() {
   gi_unused_152 = iBarShift(Symbol(), Period(), ObjectGet("pr" + sName, OBJPROP_TIME1));
   g_period_156 = bars_back;
   gda_140[1] = g_period_156 + 1;
   SetIndexDrawBegin(0, Bars - g_period_156 - 1);
   SetIndexDrawBegin(1, Bars - g_period_156 - 1);
   SetIndexDrawBegin(2, Bars - g_period_156 - 1);
   SetIndexDrawBegin(3, Bars - g_period_156 - 1);
   SetIndexDrawBegin(4, Bars - g_period_156 - 1);
   SetIndexDrawBegin(5, Bars - g_period_156 - 1);
   SetIndexDrawBegin(6, Bars - g_period_156 - 1);
   for (int li_0 = 1; li_0 <= gi_208 << 1 - 2; li_0++) {
      gd_144 = 0;
      for (gi_160 = i; gi_160 <= i + g_period_156; gi_160++) gd_144 += MathPow(gi_160, li_0);
      gda_140[li_0 + 1] = gd_144;
   }
   for (li_0 = 1; li_0 <= gi_208; li_0++) {
      gd_144 = 0.0;
      for (gi_160 = i; gi_160 <= i + g_period_156; gi_160++) {
         if (li_0 == 1) gd_144 += Close[gi_160];
         else gd_144 += Close[gi_160] * MathPow(gi_160, li_0 - 1);
      }
      gda_132[li_0] = gd_144;
   }
   for (gi_196 = 1; gi_196 <= gi_208; gi_196++) {
      for (gi_192 = 1; gi_192 <= gi_208; gi_192++) {
         gi_200 = gi_192 + gi_196 - 1;
         gda_128[gi_192][gi_196] = gda_140[gi_200];
      }
   }
   for (gi_200 = 1; gi_200 <= gi_208 - 1; gi_200++) {
      gi_204 = 0;
      gd_176 = 0;
      for (gi_192 = gi_200; gi_192 <= gi_208; gi_192++) {
         if (MathAbs(gda_128[gi_192][gi_200]) > gd_176) {
            gd_176 = MathAbs(gda_128[gi_192][gi_200]);
            gi_204 = gi_192;
         }
      }
      if (gi_204 == 0) return (0);
      if (gi_204 != gi_200) {
         for (gi_196 = 1; gi_196 <= gi_208; gi_196++) {
            gd_184 = gda_128[gi_200][gi_196];
            gda_128[gi_200][gi_196] = gda_128[gi_204][gi_196];
            gda_128[gi_204][gi_196] = gd_184;
         }
         gd_184 = gda_132[gi_200];
         gda_132[gi_200] = gda_132[gi_204];
         gda_132[gi_204] = gd_184;
      }
      for (gi_192 = gi_200 + 1; gi_192 <= gi_208; gi_192++) {
         gd_168 = gda_128[gi_192][gi_200] / gda_128[gi_200][gi_200];
         for (gi_196 = 1; gi_196 <= gi_208; gi_196++) {
            if (gi_196 == gi_200) gda_128[gi_192][gi_196] = 0;
            else gda_128[gi_192][gi_196] = gda_128[gi_192][gi_196] - gd_168 * gda_128[gi_200][gi_196];
         }
         gda_132[gi_192] = gda_132[gi_192] - gd_168 * gda_132[gi_200];
      }
   }
   gda_136[gi_208] = gda_132[gi_208] / gda_128[gi_208][gi_208];
   for (gi_192 = gi_208 - 1; gi_192 >= 1; gi_192--) {
      gd_184 = 0;
      for (gi_196 = 1; gi_196 <= gi_208 - gi_192; gi_196++) {
         gd_184 += (gda_128[gi_192][gi_192 + gi_196]) * (gda_136[gi_192 + gi_196]);
         gda_136[gi_192] = 1 / gda_128[gi_192][gi_192] * (gda_132[gi_192] - gd_184);
      }
   }
   for (gi_160 = i; gi_160 <= i + g_period_156; gi_160++) {
      gd_144 = 0;
      for (gi_200 = 1; gi_200 <= m; gi_200++) gd_144 += (gda_136[gi_200 + 1]) * MathPow(gi_160, gi_200);
      g_ibuf_100[gi_160] = gda_136[1] + gd_144;
   }
   gd_212 = 0.0;
   for (gi_160 = i; gi_160 <= i + g_period_156; gi_160++) gd_212 += MathPow(Close[gi_160] - g_ibuf_100[gi_160], 2);
   gd_212 = MathSqrt(gd_212 / (g_period_156 + 1)) * kstd;
   gd_220 = iStdDev(NULL, 0, g_period_156, 0, MODE_SMA, PRICE_CLOSE, i) * kstd;
   for (gi_160 = i; gi_160 <= i + g_period_156; gi_160++) {
      g_ibuf_104[gi_160] = g_ibuf_100[gi_160] + gd_212;
      g_ibuf_108[gi_160] = g_ibuf_100[gi_160] - gd_212;
      g_ibuf_112[gi_160] = g_ibuf_100[gi_160] + gd_220;
      g_ibuf_116[gi_160] = g_ibuf_100[gi_160] - gd_220;
      g_ibuf_120[gi_160] = g_ibuf_104[gi_160] + (g_ibuf_112[gi_160] - g_ibuf_104[gi_160]) / 2.0;
      g_ibuf_124[gi_160] = g_ibuf_116[gi_160] + (g_ibuf_108[gi_160] - g_ibuf_116[gi_160]) / 2.0;
   }
   ObjectMove("pr" + sName, 0, Time[g_period_156], g_ibuf_100[g_period_156]);
   return (0);
}

Comments