AlgoGen

Features
Sign up

MetaTrader 4 Keltner Channel

Source code for a custom MT 4 Keltner Channel indicator

Keltner Channel

Keltner Channel

The Keltner Channel is defined by two bands above and below the current price of an asset. The bands are volatility based and placed at a distance that is defined by the average true range of the asset over a number of periods. The bands are anchored on an exponential moving average which defines the middle of the channel.

Source Code

Save the source code under Indicators/AlgoGen.KeltnerChannel.mq4. After compiling it you can include it as a study in your charts or use it in your Expert Advisors for trading.

//+------------------------------------------------------------------+
//|                                       AlgoGen.KeltnerChannel.mq4 |
//|                                          Copyright 2020, AlgoGen |
//|                                           https://www.algogen.io |
//+------------------------------------------------------------------+
input ENUM_APPLIED_PRICE priceMode = PRICE_TYPICAL; // Price
input ENUM_MA_METHOD movingAverageMode = MODE_EMA; // Moving Average
input int movingAveragePeriod = 21; // Keltner Channel Moving Average Period
input int atrPeriod = 21; // Keltner Channel ATR Period 
input double multiplier = 3.0; // Keltner Channel ATR Multiplier

#property copyright "Copyright 2020, AlgoGen."
#property link      "https://www.algogen.io"
#property version   "1.00"
#property strict

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3

#property indicator_label1  "UPPER"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "MAIN"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#property indicator_label3  "LOWER"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

double UPPERBuffer[];
double MAINBuffer[];
double LOWERBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
    SetIndexBuffer(0, UPPERBuffer);
    SetIndexBuffer(1, MAINBuffer);
    SetIndexBuffer(2, LOWERBuffer);

    return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(
        const int rates_total,
        const int prev_calculated,
        const datetime &time[],
        const double &open[],
        const double &high[],
        const double &low[],
        const double &close[],
        const long &tick_volume[],
        const long &volume[],
        const int &spread[]
) {
    int limit;
    int countedBars = IndicatorCounted();
    if (countedBars < 0) return (-1);
    if (countedBars > 0) countedBars--;
    limit = Bars - countedBars;

    for (int i = 0; i < limit; i++) {
        double middle = iMA(NULL, 0, movingAveragePeriod, 0, movingAverageMode, priceMode, i);
        double atr = iATR(NULL, 0, atrPeriod, i);
        double upper = middle + multiplier * atr;
        double lower = middle - multiplier * atr;

        UPPERBuffer[i] = upper;
        MAINBuffer[i] = middle;
        LOWERBuffer[i] = lower;
    }

    return 0;
}

Usage

You can call the Keltner Channel indicator with the built-in iCustom function:

iCustom(
    // symbol 
    NULL,
    // timeframe
    0,
    // name 
    "AlgoGen.KeltnerChannel",
    // applied price,
    PRICE_CLOSE,
    // moving average,
    MODE_EMA,
    // moving average period
    21,
    // atr period
    14,
    // atr multiplier
    3,
    // line index
    0,
    // shift
    0
)

Interested in more? Check out:

Crypto bear markets and leverage

We look at the effect of investing in the top 10 cryptos during a bear market.

Crypto bull runs and leverage

We look at the effect of investing in the top 10 cryptos during a bull run.

The Effects of holding ETFs with leverage

Trading with leverage can be risky because gains and losses are magnified. This post explores the magnitude in change of gains and losses.

The effects of dodging the worst days

We know that avoiding the best trading days has a huge negative impact on our returns, but what happens if we could avoid the worst trading days?

Join the AlgoGen community

Sign up
Blog

Risk Disclosure:

Trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones' financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results.

Hypothetical Performance Disclosure:

Hypothetical performance results have many inherent limitations, some of which are described below. No representation is being made that any account will or is likely to achieve profits or losses similar to those shown; in fact, there are frequently sharp differences between hypothetical performance results and the actual results subsequently achieved by any particular trading program. One of the limitations of hypothetical performance results is that they are generally prepared with the benefit of hindsight. In addition, hypothetical trading does not involve financial risk, and no hypothetical trading record can completely account for the impact of financial risk of actual trading. For example, the ability to withstand losses or to adhere to a particular trading program in spite of trading losses are material points which can also adversely affect actual trading results. There are numerous other factors related to the markets in general or to the implementation of any specific trading program which cannot be fully accounted for in the preparation of hypothetical performance results and all which can adversely affect trading results.

© AlgoGen 2021 All rights reserved.