Please help me with my Keltner Channel Code

Created at 13 Mar 2024, 08:29
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
LF

lfdomingo143

Joined 27.05.2021

Please help me with my Keltner Channel Code
13 Mar 2024, 08:29


I cannot run this indicator code
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class KeltnerChannels : Indicator
    {
        [Parameter("Length", DefaultValue = 20, MinValue = 1)]
        public int Length { get; set; }

        [Parameter("Multiplier", DefaultValue = 2.0)]
        public double Multiplier { get; set; }

        [Parameter("Use Exponential MA", DefaultValue = true)]
        public bool UseExpMA { get; set; }

        [Parameter("Bands Style", DefaultValue = "Average True Range")]
        public string BandsStyle { get; set; }

        [Parameter("ATR Length", DefaultValue = 10)]
        public int ATRLength { get; set; }

        [Output("Upper", LineColor = "Blue", PlotType = PlotType.Line)]
        public IndicatorDataSeries Upper { get; set; }

        [Output("Basis", LineColor = "Blue", PlotType = PlotType.Line)]
        public IndicatorDataSeries Basis { get; set; }

        [Output("Lower", LineColor = "Blue", PlotType = PlotType.Line)]
        public IndicatorDataSeries Lower { get; set; }

        public override void Calculate(int index)
        {
            double ma = ESMA(index, Bars.ClosePrices, Length);
            double rangema = BandsStyle == "True Range" ? TrueRange(index) :
                             BandsStyle == "Average True Range" ? ATR(index) :
                             RMA(index);

            Upper[index] = ma + rangema * Multiplier;
            Basis[index] = ma;
            Lower[index] = ma - rangema * Multiplier;
        }

        private double ESMA(int index, DataSeries source, int length)
        {
            double s = DataSeries.SMA(source, length)[index];
            double e = DataSeries.EMA(source, length)[index];
            return UseExpMA ? e : s;
        }

        private double TrueRange(int index)
        {
            return Math.Max(Math.Max(Bars.HighPrices[index] - Bars.LowPrices[index], Math.Abs(Bars.HighPrices[index] - Bars.ClosePrices[index - 1])), Math.Abs(Bars.LowPrices[index] - Bars.ClosePrices[index - 1]));
        }

        private double ATR(int index)
        {
            return DataSeries.ATR(index, ATRLength);
        }

        private double RMA(int index)
        {
            return DataSeries.RMA(Bars.HighPrices[index] - Bars.LowPrices[index], Length)[index];
        }
    }
}

I got this 4 errors always.
Build failed (4 errors)

Error CS0117: 'DataSeries' does not contain a definition for 'SMA' (C:\sers\fdom\OneDrive\Documents\cAlgo\Sources\ndicators\KC\KC\KC.cs, line: 50, column: 35)
Error CS0117: 'DataSeries' does not contain a definition for 'EMA' (C:Wsers\fdom\OneDrive\Documents\cAlgo\Sources\ndicators\KC\KC\KC.cs, line: 51, column: 35)
Error CS0117: 'DataSeries' does not contain a definition for 'ATR' (C:\Users\lfdom\OneDrive\Documents\cAlgo\Sources\ndicators\KC\KC\KC.cs, line: 62, column: 31)
Error CS0117: 'DataSeries' does not contain a definition for 'RMA' (C:\sers\fdom\OneDrive\Documents\cAlgo\Sources\ndicators\KC\KC\KC.cs, line: 67, column: 31)

my reference parameter is based on trading view pine script
indicator(title="Keltner Channels", shorttitle="KC", overlay=true, timeframe="", timeframe_gaps=true)

length = input.int(20, minval=1)

mult = input(2.0, "Multiplier")

src = input(close, title="Source")

exp = input(true, "Use Exponential MA")

BandsStyle = input.string("Average True Range", options = ["Average True Range", "True Range", "Range"], title="Bands Style")

atrlength = input(10, "ATR Length")

esma(source, length)=>

    s = ta.sma(source, length)

    e = ta.ema(source, length)

    exp ? e : s

ma = esma(src, length)

rangema = BandsStyle == "True Range" ? ta.tr(true) : BandsStyle == "Average True Range" ? ta.atr(atrlength) : ta.rma(high - low, length)

upper = ma + rangema * mult

lower = ma - rangema * mult

u = plot(upper, color=#2962FF, title="Upper")

plot(ma, color=#2962FF, title="Basis")

l = plot(lower, color=#2962FF, title="Lower")

fill(u, l, color=color.rgb(33, 150, 243, 95), title="Background")


@lfdomingo143
Replies

PanagiotisCharalampous
13 Mar 2024, 10:48

Hi there,

The errors are pretty straight forward, so I don't know what kind of further help we could provide. You are calling parameters that do not exist.

Best regards,

Panagiotis


@PanagiotisCharalampous