Author: Coded by Witold Wozniak
AdaptiveCG

Here's a breakdown of the script's logic in plain language:

This script calculates and displays two lines on a chart: the "Adaptive CG" and a "Trigger" line. Think of it like a special kind of moving average that tries to adapt to the market's current cycle, helping you potentially identify turning points in price.

Here's how it works:

  1. The Goal: The script aims to create an oscillator called the "Adaptive CG" (Center of Gravity). This oscillator attempts to identify the center of recent price activity, which can then be used to anticipate future price movements. It adapts by trying to estimate the dominant cycle length in the market.

  2. Gathering Data: The script looks at the high and low prices of each period (e.g., each minute, hour, or day) on the chart. It averages these high and low prices to get a middle price for each period.

  3. Estimating the Cycle Length: The script uses another custom indicator "CyclePeriod" (which isn't part of the code, meaning it needs to exist as another separate indicator) to estimate how long the current market cycle is. This cycle length is a key input to the Adaptive CG calculation. A shorter cycle would mean the market is moving faster, while a longer cycle would mean it's moving slower.

  4. Calculating the Adaptive CG:

    • It uses the cycle length estimated in the previous step to determine a "period" which is half of the cycle length.
    • It then performs a calculation that gives more weight to prices further from the current period, which helps identify the center of gravity.
    • The core calculation involves summing up the price of each period in a certain range, but these prices are weighted. The weights increase linearly.
    • The script ensures it doesn't divide by zero if there is no price data. If there's no data, the CG is simply set to zero.
  5. Creating the Trigger Line: The "Trigger" line is simply the Adaptive CG line shifted one period forward. This means the "Trigger" line shows the Adaptive CG value from the previous period. This is done so that a crossing of the two lines may represent a possible buy or sell trigger.

  6. Displaying the Lines: The script then draws two lines on a separate chart window: the Adaptive CG line (in Red) and the Trigger line (in Blue).

In simple terms:

The script watches the recent high and low prices. It attempts to estimate the "speed" of the market. It then calculates a value based on the prices and the estimated "speed," which is the Adaptive CG. A "trigger" line is calculated by taking the Adaptive CG value from the previous time period. These two lines are displayed, and traders might look for crossovers or other patterns between them to make trading decisions. The Alpha value controls how adaptive the CG line is (smaller means more adaptive).

Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
AdaptiveCG
//+------------------------------------------------------------------+
//|                                                   AdaptiveCG.mq4 |
//|                                                                  |
//| Adaptive CG Oscillator                                           |
//|                                                                  |
//| Algorithm taken from book                                        |
//|     "Cybernetics Analysis for Stock and Futures"                 |
//| by John F. Ehlers                                                |
//|                                                                  |
//|                                              contact@mqlsoft.com |
//|                                          http://www.mqlsoft.com/ |
//+------------------------------------------------------------------+
#property copyright "Coded by Witold Wozniak"
#property link      "www.mqlsoft.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

#property indicator_level1 0

double CG[];
double Trigger[];

extern double Alpha = 0.07;
int buffers = 0;
int drawBegin = 0;

int init() {
    drawBegin = 30;
    initBuffer(CG, "CG", DRAW_LINE);
    initBuffer(Trigger, "Trigger", DRAW_LINE);
    IndicatorBuffers(buffers);
    IndicatorShortName("Adaptive CG [" + DoubleToStr(Alpha, 2) + "]");
    return (0);
}

int start() {
    if (Bars <= drawBegin) return (0);
    int countedBars = IndicatorCounted();
    if (countedBars < 0) return (-1);
    if (countedBars > 0) countedBars--;
    int s, limit = Bars - countedBars - 1;
    for (s = limit; s >= 0; s--) {
        double period = iCustom(0, 0, "CyclePeriod", Alpha, 0, s);
        int intPeriod = MathFloor(period / 2.0);
        double Num = 0.0;
        double Denom = 0.0;
        for (int count = 0; count < intPeriod; count++) {
            Num += (1.0 + count) * P(s + count);
            Denom += P(s + count);
        }
        if (Denom != 0.0) {
            CG[s] = -Num / Denom + (intPeriod + 1.0) / 2.0;
        } else {
            CG[s] = 0.0;
        }
        Trigger[s] = CG[s + 1];        
    }
    return (0);   
}

double P(int index) {
    return ((High[index] + Low[index]) / 2.0);
}

void initBuffer(double array[], string label = "", int type = DRAW_NONE, int arrow = 0, int style = EMPTY, int width = EMPTY, color clr = CLR_NONE) {
    SetIndexBuffer(buffers, array);
    SetIndexLabel(buffers, label);
    SetIndexEmptyValue(buffers, EMPTY_VALUE);
    SetIndexDrawBegin(buffers, drawBegin);
    SetIndexShift(buffers, 0);
    SetIndexStyle(buffers, type, style, width);
    SetIndexArrow(buffers, arrow);
    buffers++;
}

Comments