Keltner_Channel_v2

Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Keltner_Channel_v2
//+------------------------------------------------------------------+
//|                                              Keltner_Channel.mq5 |
//+------------------------------------------------------------------+
#property link      "https://t.me/ForexEaPremium"
#property version   "1.01"
#property strict

#property description "Displays classical Keltner Channel technical indicator."
#property description "You can modify main MA period, mode of the MA and type of prices used in MA."
#property description "Buy when candle closes above the upper band."
#property description "Sell when candle closes below the lower band."
#property description "Use a *very conservative* stop-loss and 3-4 times higher take-profit."

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
#property indicator_color1 clrRed
#property indicator_type1 DRAW_LINE
#property indicator_label1 "KC-Up"
#property indicator_color2 clrBlue
#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_DASHDOT
#property indicator_label2 "KC-Mid"
#property indicator_color3 clrRed
#property indicator_type3 DRAW_LINE
#property indicator_label3 "KC-Low"

input int MA_Period = 10;
input ENUM_MA_METHOD Mode_MA = MODE_SMA;
input ENUM_APPLIED_PRICE Price_Type = PRICE_TYPICAL;

double Upper[], Middle[], Lower[], MA_Buffer;

void OnInit()
{
    SetIndexBuffer(0, Upper, INDICATOR_DATA);
    SetIndexBuffer(1, Middle, INDICATOR_DATA);
    SetIndexBuffer(2, Lower, INDICATOR_DATA);

    IndicatorSetString(INDICATOR_SHORTNAME, "Keltner Channel (" + IntegerToString(MA_Period) + ")");
}

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;
    double avg;

    int counted_bars = prev_calculated;

    if (counted_bars < 0) return -1;
    if (counted_bars > 0) counted_bars--;

    limit = counted_bars;

    if (limit < MA_Period) limit = MA_Period;

    int myMA = iMA(NULL, 0, MA_Period, 0, Mode_MA, Price_Type);
    if  (CopyBuffer(myMA, 0, 0, rates_total, Middle) != rates_total) return 0;

    for (int i = rates_total - 1; i >= limit; i--)
    {
        avg = findAvg(MA_Period, i, High, Low);
        Upper[i] = Middle[i] + avg;
        Lower[i] = Middle[i] - avg;
    }

    return rates_total;
}

//+------------------------------------------------------------------+
//| Finds the moving average of the price ranges                     |
//+------------------------------------------------------------------+
double findAvg(int period, int shift, const double &High[], const double &Low[])
{
    double sum = 0;

    for (int i = shift; i > (shift - period); i--)
        sum += High[i] - Low[i];

    sum = sum / period;

    return(sum);
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---