Category Trend  Published on 09/11/2022

KF_Mtf_Channels_v5

Description

For those of us who utilise price channels on our charts, this indicator makes life easier 

You can add up to 4 moving average channels of (different timeframes) super-imposed onto the current chart 


using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    public enum TimeFrameE
    {
        Current_Chart,
        M1,
        M2,
        M3,
        M4,
        M5,
        M6,
        M7,
        M8,
        M9,
        M10,
        M15,
        M20,
        M30,
        M45,

        H1,
        H2,
        H3,
        H4,
        H6,
        H8,
        H12,

        D1,
        D2,
        D3,

        W1,

        MN1
    }

    [Cloud("C1Top", "C1Btm")]
    [Cloud("C2Top", "C2Btm")]
    [Cloud("C3Top", "C3Btm")]
    [Cloud("C4Top", "C4Btm")]
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class KF_Mtf_Channels_v5 : Indicator
    {
        #region Settings

        [Parameter("C1_Enabled", DefaultValue = true, Group = "Channel 1")]
        public bool C1_Enabled { get; set; }

        [Parameter("C1_Period", DefaultValue = 200, Group = "Channel 1")]
        public int C1_Period { get; set; }

        [Parameter("C1_MaType", DefaultValue = MovingAverageType.Exponential, Group = "Channel 1")]
        public MovingAverageType C1_MaType { get; set; }

        [Parameter("C1_TimeFrame", DefaultValue = TimeFrameE.Current_Chart, Group = "Channel 1")]
        public TimeFrameE C1_TimeFrame { get; set; }




        [Parameter("C2_Enabled", DefaultValue = true, Group = "Channel 2")]
        public bool C2_Enabled { get; set; }

        [Parameter("C2_Period", DefaultValue = 480, Group = "Channel 2")]
        public int C2_Period { get; set; }

        [Parameter("C2_MaType", DefaultValue = MovingAverageType.WilderSmoothing, Group = "Channel 2")]
        public MovingAverageType C2_MaType { get; set; }

        [Parameter("C2_TimeFrame", DefaultValue = TimeFrameE.Current_Chart, Group = "Channel 2")]
        public TimeFrameE C2_TimeFrame { get; set; }





        [Parameter("C3_Enabled", DefaultValue = true, Group = "Channel 3")]
        public bool C3_Enabled { get; set; }

        [Parameter("C3_Period", DefaultValue = 20, Group = "Channel 3")]
        public int C3_Period { get; set; }

        [Parameter("C3_MaType", DefaultValue = MovingAverageType.Simple, Group = "Channel 3")]
        public MovingAverageType C3_MaType { get; set; }

        [Parameter("C3_TimeFrame", DefaultValue = TimeFrameE.H4, Group = "Channel 3")]
        public TimeFrameE C3_TimeFrame { get; set; }





        [Parameter("C4_Enabled", DefaultValue = false, Group = "Channel 4")]
        public bool C4_Enabled { get; set; }

        [Parameter("C4_Period", DefaultValue = 10, Group = "Channel 4")]
        public int C4_Period { get; set; }

        [Parameter("C4_MaType", DefaultValue = MovingAverageType.WilderSmoothing, Group = "Channel 4")]
        public MovingAverageType C4_MaType { get; set; }

        [Parameter("C4_TimeFrame", DefaultValue = TimeFrameE.H4, Group = "Channel 4")]
        public TimeFrameE C4_TimeFrame { get; set; }

        #endregion

        #region Lines & Styles

        [Output("C1Top", LineColor = "Aqua")]
        public IndicatorDataSeries C1Top { get; set; }

        [Output("C1Btm", LineColor = "Aqua")]
        public IndicatorDataSeries C1Btm { get; set; }

        [Output("C1Mid", LineColor = "Aqua", Thickness = 5, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries C1Mid { get; set; }



        [Output("C2Top", LineColor = "Chartreuse")]
        public IndicatorDataSeries C2Top { get; set; }

        [Output("C2Btm", LineColor = "Chartreuse")]
        public IndicatorDataSeries C2Btm { get; set; }

        [Output("C2Mid", LineColor = "Chartreuse", Thickness = 4, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries C2Mid { get; set; }



        [Output("C3Top", LineColor = "Red")]
        public IndicatorDataSeries C3Top { get; set; }

        [Output("C3Btm", LineColor = "Red")]
        public IndicatorDataSeries C3Btm { get; set; }

        [Output("C3Mid", LineColor = "Red", Thickness = 4, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries C3Mid { get; set; }



        [Output("C4Top", LineColor = "Indigo")]
        public IndicatorDataSeries C4Top { get; set; }

        [Output("C4Btm", LineColor = "Indigo")]
        public IndicatorDataSeries C4Btm { get; set; }

        [Output("C4Mid", LineColor = "Indigo", Thickness = 4, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries C4Mid { get; set; }

        #endregion

        private MovingAverage _c1T, _c1B, _c1M, _c2T, _c2B, _c2M, _c3T, _c3B, _c3M, _c4T,
        _c4B, _c4M;

        private Bars _c1, _c2, _c3, _c4;

        private TimeFrame ToTimeFrame(TimeFrameE tfe)
        {
            TimeFrame tmp = Chart.TimeFrame;

            switch (tfe)
            {
                case TimeFrameE.Current_Chart:
                    tmp = Chart.TimeFrame;
                    break;
                case TimeFrameE.D1:
                    tmp = TimeFrame.Daily;
                    break;
                case TimeFrameE.D2:
                    tmp = TimeFrame.Day2;
                    break;
                case TimeFrameE.D3:
                    tmp = TimeFrame.Day3;
                    break;
                case TimeFrameE.H1:
                    tmp = TimeFrame.Hour;
                    break;
                case TimeFrameE.H12:
                    tmp = TimeFrame.Hour12;
                    break;
                case TimeFrameE.H2:
                    tmp = TimeFrame.Hour2;
                    break;
                case TimeFrameE.H3:
                    tmp = TimeFrame.Hour3;
                    break;
                case TimeFrameE.H4:
                    tmp = TimeFrame.Hour4;
                    break;
                case TimeFrameE.H6:
                    tmp = TimeFrame.Hour6;
                    break;
                case TimeFrameE.H8:
                    tmp = TimeFrame.Hour8;
                    break;
                case TimeFrameE.M1:
                    tmp = TimeFrame.Minute;
                    break;
                case TimeFrameE.M10:
                    tmp = TimeFrame.Minute10;
                    break;
                case TimeFrameE.M15:
                    tmp = TimeFrame.Minute15;
                    break;
                case TimeFrameE.M2:
                    tmp = TimeFrame.Minute2;
                    break;
                case TimeFrameE.M20:
                    tmp = TimeFrame.Minute20;
                    break;
                case TimeFrameE.M3:
                    tmp = TimeFrame.Minute3;
                    break;
                case TimeFrameE.M30:
                    tmp = TimeFrame.Minute30;
                    break;
                case TimeFrameE.M4:
                    tmp = TimeFrame.Minute4;
                    break;
                case TimeFrameE.M45:
                    tmp = TimeFrame.Minute45;
                    break;
                case TimeFrameE.M5:
                    tmp = TimeFrame.Minute5;
                    break;
                case TimeFrameE.M6:
                    tmp = TimeFrame.Minute6;
                    break;
                case TimeFrameE.M7:
                    tmp = TimeFrame.Minute7;
                    break;
                case TimeFrameE.M8:
                    tmp = TimeFrame.Minute8;
                    break;
                case TimeFrameE.M9:
                    tmp = TimeFrame.Minute9;
                    break;
                case TimeFrameE.MN1:
                    tmp = TimeFrame.Monthly;
                    break;
                case TimeFrameE.W1:
                    tmp = TimeFrame.Weekly;
                    break;
            }

            return tmp;
            ;
        }


        protected override void Initialize()
        {
            if (C1_Enabled)
            {
                var c1_Tf = ToTimeFrame(C1_TimeFrame);

                _c1 = c1_Tf == Chart.TimeFrame ? Bars : MarketData.GetBars(c1_Tf);

                _c1T = Indicators.MovingAverage(_c1.HighPrices, C1_Period, C1_MaType);
                _c1B = Indicators.MovingAverage(_c1.LowPrices, C1_Period, C1_MaType);
                _c1M = Indicators.MovingAverage(_c1.ClosePrices, C1_Period, C1_MaType);
            }

            if (C2_Enabled)
            {
                var c2_Tf = ToTimeFrame(C2_TimeFrame);

                _c2 = c2_Tf == Chart.TimeFrame ? Bars : MarketData.GetBars(c2_Tf);

                _c2T = Indicators.MovingAverage(_c2.HighPrices, C2_Period, C2_MaType);
                _c2B = Indicators.MovingAverage(_c2.LowPrices, C2_Period, C2_MaType);
                _c2M = Indicators.MovingAverage(_c2.ClosePrices, C2_Period, C2_MaType);
            }

            if (C3_Enabled)
            {
                var c3_Tf = ToTimeFrame(C3_TimeFrame);

                _c3 = c3_Tf == Chart.TimeFrame ? Bars : MarketData.GetBars(c3_Tf);

                _c3T = Indicators.MovingAverage(_c3.HighPrices, C3_Period, C3_MaType);
                _c3B = Indicators.MovingAverage(_c3.LowPrices, C3_Period, C3_MaType);
                _c3M = Indicators.MovingAverage(_c3.ClosePrices, C3_Period, C3_MaType);
            }

            if (C4_Enabled)
            {
                var c4_Tf = ToTimeFrame(C4_TimeFrame);

                _c4 = c4_Tf == Chart.TimeFrame ? Bars : MarketData.GetBars(c4_Tf);

                _c4T = Indicators.MovingAverage(_c4.HighPrices, C4_Period, C4_MaType);
                _c4B = Indicators.MovingAverage(_c4.LowPrices, C4_Period, C4_MaType);
                _c4M = Indicators.MovingAverage(_c4.ClosePrices, C4_Period, C4_MaType);
            }
        }


        public override void Calculate(int index)
        {
            if (C1_Enabled)
            {
                var index1 = _c1.TimeFrame == Chart.TimeFrame ? index : _c1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
                if (index1 < 0)
                {
                    return;
                }

                _c1T.Calculate(index1);
                _c1B.Calculate(index1);
                _c1M.Calculate(index1);

                C1Top[index] = _c1T.Result[index1];
                C1Btm[index] = _c1B.Result[index1];
                C1Mid[index] = _c1M.Result[index1];

                var tmp1 = _c1.TimeFrame == Chart.TimeFrame ? index - 1 : _c1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

                int lastIdx1 = Bars.OpenTimes.GetIndexByTime(_c1.OpenTimes[index1]);
                int lastIdx2 = Bars.OpenTimes.GetIndexByTime(_c1.OpenTimes[tmp1]);
                if (lastIdx1 == lastIdx2 && _c1.TimeFrame != Chart.TimeFrame)
                {
                    C1Top[index - 1] = double.NaN;
                    C1Btm[index - 1] = double.NaN;
                    C1Mid[index - 1] = double.NaN;
                }
            }

            if (C2_Enabled)
            {
                var index2 = _c2.TimeFrame == Chart.TimeFrame ? index : _c2.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
                if (index2 < 0)
                {
                    return;
                }

                _c2T.Calculate(index2);
                _c2B.Calculate(index2);
                _c2M.Calculate(index2);

                C2Top[index] = _c2T.Result[index2];
                C2Btm[index] = _c2B.Result[index2];
                C2Mid[index] = _c2M.Result[index2];

                var tmp2 = _c2.TimeFrame == Chart.TimeFrame ? index - 1 : _c2.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

                int lastIdx1 = Bars.OpenTimes.GetIndexByTime(_c2.OpenTimes[index2]);
                int lastIdx2 = Bars.OpenTimes.GetIndexByTime(_c2.OpenTimes[tmp2]);
                if (lastIdx1 == lastIdx2 && _c2.TimeFrame != Chart.TimeFrame)
                {
                    C2Top[index - 1] = double.NaN;
                    C2Btm[index - 1] = double.NaN;
                    C2Mid[index - 1] = double.NaN;
                }
            }

            if (C3_Enabled)
            {
                var index3 = _c3.TimeFrame == Chart.TimeFrame ? index : _c3.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
                if (index3 < 0)
                {
                    return;
                }

                _c3T.Calculate(index3);
                _c3B.Calculate(index3);
                _c3M.Calculate(index3);

                C3Top[index] = _c3T.Result[index3];
                C3Btm[index] = _c3B.Result[index3];
                C3Mid[index] = _c3M.Result[index3];

                var tmp3 = _c3.TimeFrame == Chart.TimeFrame ? index - 1 : _c3.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

                int lastIdx1 = Bars.OpenTimes.GetIndexByTime(_c3.OpenTimes[index3]);
                int lastIdx2 = Bars.OpenTimes.GetIndexByTime(_c3.OpenTimes[tmp3]);
                if (lastIdx1 == lastIdx2 && _c3.TimeFrame != Chart.TimeFrame)
                {
                    C3Top[index - 1] = double.NaN;
                    C3Btm[index - 1] = double.NaN;
                    C3Mid[index - 1] = double.NaN;
                }
            }

            if (C4_Enabled)
            {
                var index4 = _c4.TimeFrame == Chart.TimeFrame ? index : _c4.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
                if (index4 < 0)
                {
                    return;
                }

                _c4T.Calculate(index4);
                _c4B.Calculate(index4);
                _c4M.Calculate(index4);

                C4Top[index] = _c4T.Result[index4];
                C4Btm[index] = _c4B.Result[index4];
                C4Mid[index] = _c4M.Result[index4];

                var tmp4 = _c4.TimeFrame == Chart.TimeFrame ? index - 1 : _c4.OpenTimes.GetIndexByTime(Bars.OpenTimes[index - 1]);

                int lastIdx1 = Bars.OpenTimes.GetIndexByTime(_c4.OpenTimes[index4]);
                int lastIdx2 = Bars.OpenTimes.GetIndexByTime(_c4.OpenTimes[tmp4]);
                if (lastIdx1 == lastIdx2 && _c4.TimeFrame != Chart.TimeFrame)
                {
                    C4Top[index - 1] = double.NaN;
                    C4Btm[index - 1] = double.NaN;
                    C4Mid[index - 1] = double.NaN;
                }
            }
        }
    }
}


GM
gmkenneyy

Joined on 20.03.2020 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: KF_Mtf_Channels_v5.algo
  • Rating: 0
  • Installs: 695
  • Modified: 08/11/2022 23:58
Comments
Log in to add a comment.
No comments found.