Category Trend  Published on 10/11/2022

KF_Mtf_MacdStoc_v1

Description

A great indicator that combines the Macd & Stochastics of different timefames to give you a birds eye view of the current chart

Will find time to put all the code in just one class and then re-upload. At the momment only coders will know what to do with this current code. Bear with me.

 

 

** Source code **

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

namespace cAlgo
{
    public class ErrorMessage : CustomControl
    {
        public ErrorMessage(string text)
        {
            VerticalAlignment = VerticalAlignment.Center;
            HorizontalAlignment = HorizontalAlignment.Center;
            IsHitTestVisible = false;

            var border = new Border 
            {
                CornerRadius = 8,
                Padding = "40 20",
                BorderColor = Color.DimGray,
                BackgroundColor = "#aa3b3b3b",
                BorderThickness = 1
            };

            var textBlock = new TextBlock 
            {
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                Text = text,
                ForegroundColor = Color.White
            };

            border.Child = textBlock;
            AddChild(border);
        }
    }

    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class KF_Mtf_MacdStoc_v1 : Indicator
    {
        [Parameter(DefaultValue = "26|12|9", Group = "Macd")]
        public string D3_L_S_Sig { get; set; }

        [Parameter(DefaultValue = "26|12|9", Group = "Macd")]
        public string D1_L_S_Sig { get; set; }

        [Parameter(DefaultValue = "26|12|9", Group = "Macd")]
        public string H4_L_S_Sig { get; set; }

        [Parameter(DefaultValue = "26|12|9", Group = "Macd")]
        public string H1_L_S_Sig { get; set; }



        [Parameter(DefaultValue = "9|3|3", Group = "Stochastic")]
        public string D3_kP_kS_dP { get; set; }

        [Parameter(DefaultValue = "9|3|3", Group = "Stochastic")]
        public string D1_kP_kS_dP { get; set; }

        [Parameter(DefaultValue = "9|3|3", Group = "Stochastic")]
        public string H4_kP_kS_dP { get; set; }

        [Parameter(DefaultValue = "9|3|3", Group = "Stochastic")]
        public string H1_kP_kS_dP { get; set; }



        [Parameter("Vertical position", Group = "Dashboard", DefaultValue = VerticalAlignment.Bottom)]
        public VerticalAlignment DB_Vert_Posn { get; set; }

        [Parameter("Horizontal position", Group = "Dashboard", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment DB_Horz_Posn { get; set; }


        private Bars D3_brs, D1_brs, H4_brs, H1_brs;
        private StochasticOscillator D3_sto, D1_sto, H4_sto, H1_sto;
        private MacdCrossOver D3_mco, D1_mco, H4_mco, H1_mco;


        private Button btnD3_sto, btnD1_sto, btnH4_sto, btnH1_sto;
        private Button btnD3_mco, btnD1_mco, btnH4_mco, btnH1_mco;
        private Button btnMicro, btnMacro;

        private readonly Color clrBU = Color.FromHex("#009345"), clrBE = Color.FromHex("#F05824");

        private Button RenderBtn(string txt, int btnHeight = 1, int btnHwidth = 110)
        {
            return new Button 
            {
                Width = btnHwidth,
                Height = 20 * btnHeight,
                Margin = 2,
                Text = txt,
                ForegroundColor = Color.Snow,
                FontWeight = FontWeight.Light,
                FontSize = 10,
                FontFamily = "Arial"
            };
        }

        protected override void Initialize()
        {
            RefreshData();

            H1_brs = MarketData.GetBars(TimeFrame.Hour);
            H4_brs = MarketData.GetBars(TimeFrame.Hour4);
            D1_brs = MarketData.GetBars(TimeFrame.Daily);
            D3_brs = MarketData.GetBars(TimeFrame.Day3);

            while (H1_brs.OpenTimes[0] > Bars.OpenTimes[0])
                H1_brs.LoadMoreHistory();
            while (H4_brs.OpenTimes[0] > Bars.OpenTimes[0])
                H4_brs.LoadMoreHistory();



            D3_mco = Indicators.MacdCrossOver(D3_brs.ClosePrices, D3_L_S_Sig.GV(0), D3_L_S_Sig.GV(1), D3_L_S_Sig.GV(2));
            D1_mco = Indicators.MacdCrossOver(D1_brs.ClosePrices, D1_L_S_Sig.GV(0), D1_L_S_Sig.GV(1), D1_L_S_Sig.GV(2));
            H4_mco = Indicators.MacdCrossOver(H4_brs.ClosePrices, H4_L_S_Sig.GV(0), H4_L_S_Sig.GV(1), H4_L_S_Sig.GV(2));
            H1_mco = Indicators.MacdCrossOver(H1_brs.ClosePrices, H1_L_S_Sig.GV(0), H1_L_S_Sig.GV(1), H1_L_S_Sig.GV(2));


            D3_sto = Indicators.StochasticOscillator(D3_brs, D3_kP_kS_dP.GV(0), D3_kP_kS_dP.GV(1), D3_kP_kS_dP.GV(2), MovingAverageType.Simple);
            D1_sto = Indicators.StochasticOscillator(D1_brs, D1_kP_kS_dP.GV(0), D1_kP_kS_dP.GV(1), D1_kP_kS_dP.GV(2), MovingAverageType.Simple);
            H4_sto = Indicators.StochasticOscillator(H4_brs, H4_kP_kS_dP.GV(0), H4_kP_kS_dP.GV(1), H4_kP_kS_dP.GV(2), MovingAverageType.Simple);
            H1_sto = Indicators.StochasticOscillator(H1_brs, H1_kP_kS_dP.GV(0), H1_kP_kS_dP.GV(1), H1_kP_kS_dP.GV(2), MovingAverageType.Simple);

            const int half = 55;

            var hdr_St = RenderBtn("Stoc", 1, half);
            var hdr_Md = RenderBtn("Macd", 1, half);

            btnD3_sto = RenderBtn("D3", 1, half);
            btnD3_mco = RenderBtn("D3", 1, half);
            btnD1_sto = RenderBtn("D1", 1, half);
            btnD1_mco = RenderBtn("D1", 1, half);
            btnH4_sto = RenderBtn("H4", 1, half);
            btnH4_mco = RenderBtn("H4", 1, half);
            btnH1_sto = RenderBtn("H1", 1, half);
            btnH1_mco = RenderBtn("H1", 1, half);

            btnMicro = RenderBtn("Micro-trend", 2, 116);
            btnMacro = RenderBtn("Macro-trend", 2, 116);

            var d3_Hd = new WrapPanel 
            {
                Orientation = Orientation.Horizontal
            };
            d3_Hd.AddChild(hdr_St);
            d3_Hd.AddChild(hdr_Md);

            var d3_Wp = new WrapPanel 
            {
                Orientation = Orientation.Horizontal
            };
            d3_Wp.AddChild(btnD3_sto);
            d3_Wp.AddChild(btnD3_mco);

            var d1_Wp = new WrapPanel 
            {
                Orientation = Orientation.Horizontal
            };
            d1_Wp.AddChild(btnD1_sto);
            d1_Wp.AddChild(btnD1_mco);

            var h4_Wp = new WrapPanel 
            {
                Orientation = Orientation.Horizontal
            };
            h4_Wp.AddChild(btnH4_sto);
            h4_Wp.AddChild(btnH4_mco);

            var h1_Wp = new WrapPanel 
            {
                Orientation = Orientation.Horizontal
            };
            h1_Wp.AddChild(btnH1_sto);
            h1_Wp.AddChild(btnH1_mco);

            var vSpacer = 2;

            var sp = new StackPanel 
            {
                VerticalAlignment = DB_Vert_Posn,
                HorizontalAlignment = DB_Horz_Posn
            };

            sp.AddChild(d3_Hd);
            sp.AddChild(d3_Wp);
            sp.AddChild(d1_Wp);
            sp.AddChild(h4_Wp);
            sp.AddChild(h1_Wp);

            sp.AddChild(new TextBlock 
            {
                Height = vSpacer
            });

            sp.AddChild(btnMacro);

            sp.AddChild(new TextBlock 
            {
                Height = vSpacer
            });

            sp.AddChild(btnMicro);

            var bdr = new Border 
            {
                CornerRadius = 8,
                Padding = "6 4",
                BorderColor = Color.DimGray,
                BackgroundColor = Color.White,
                //"#aa3b3b3b",
                BorderThickness = 1,
                Child = sp,
                Height = 224,
                Width = 136
            };

            this.Chart.AddControl(bdr);
        }

        public override void Calculate(int index)
        {
            const int BU = 1, BE = -1;

            var d3_stoBU = D3_sto.Dir(BU, index, Bars, D3_brs);
            var d3_stoBE = D3_sto.Dir(BE, index, Bars, D3_brs);
            btnD3_sto.BackgroundColor = d3_stoBU ? clrBU : d3_stoBE ? clrBE : Color.DarkGray;

            var d1_stoBU = D1_sto.Dir(BU, index, Bars, D1_brs);
            var d1_stoBE = D1_sto.Dir(BE, index, Bars, D1_brs);
            btnD1_sto.BackgroundColor = d1_stoBU ? clrBU : d1_stoBE ? clrBE : Color.DarkGray;

            var h4_stoBU = H4_sto.Dir(BU, index, Bars, H4_brs);
            var h4_stoBE = H4_sto.Dir(BE, index, Bars, H4_brs);
            btnH4_sto.BackgroundColor = h4_stoBU ? clrBU : h4_stoBE ? clrBE : Color.DarkGray;

            var h1_stoBU = H1_sto.Dir(BU, index, Bars, H1_brs);
            var h1_stoBE = H1_sto.Dir(BE, index, Bars, H1_brs);
            btnH1_sto.BackgroundColor = h1_stoBU ? clrBU : h1_stoBE ? clrBE : Color.DarkGray;


            var d3_macd_BU = D3_mco.Dir(BU, index, Bars, D3_brs);
            var d3_macd_BE = D3_mco.Dir(BE, index, Bars, D3_brs);
            btnD3_mco.BackgroundColor = d3_macd_BU ? clrBU : d3_macd_BE ? clrBE : Color.DarkGray;

            var d1_macd_BU = D1_mco.Dir(BU, index, Bars, D1_brs);
            var d1_macd_BE = D1_mco.Dir(BE, index, Bars, D1_brs);
            btnD1_mco.BackgroundColor = d1_macd_BU ? clrBU : d1_macd_BE ? clrBE : Color.DarkGray;

            var h4_macd_BU = H4_mco.Dir(BU, index, Bars, H4_brs);
            var h4_macd_BE = H4_mco.Dir(BE, index, Bars, H4_brs);
            btnH4_mco.BackgroundColor = h4_macd_BU ? clrBU : h4_macd_BE ? clrBE : Color.DarkGray;

            var h1_macd_BU = H1_mco.Dir(BU, index, Bars, H1_brs);
            var h1_macd_BE = H1_mco.Dir(BE, index, Bars, H1_brs);
            btnH1_mco.BackgroundColor = h1_macd_BU ? clrBU : h1_macd_BE ? clrBE : Color.DarkGray;


            var macBU = (!d3_macd_BE && d1_macd_BU) && (d3_macd_BU && !d1_macd_BE);
            var macBE = (!d3_macd_BU && d1_macd_BE) && (d3_macd_BE && !d1_macd_BU);

            btnMacro.BackgroundColor = macBU ? clrBU : macBE ? clrBE : Color.DarkGray;

            btnMicro.BackgroundColor = ((h1_macd_BU && !h1_macd_BE) || (!h1_macd_BE && h1_macd_BU)) ? clrBU : ((h1_macd_BE && !h1_macd_BU) || (!h1_macd_BU && h1_macd_BE)) ? clrBE : Color.DarkGray;

        }

    }
}


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

namespace cAlgo
{
    public static class Extensions
    {
        public static int GV(this String str, int idx)
        {
            var cleaned = str.Trim();

            if (cleaned.Length > 0)
            {
                var value = cleaned.Split("|".ToCharArray())[idx];

                int val;

                return int.TryParse(value, out val) ? val : -1;
            }
            else
                return -1;
        }

        public static bool Dir(this MacdCrossOver mco, int dir, int index, Bars lbrs, Bars hbrs)
        {
            bool ok = false;

            var idx = hbrs.OpenTimes.GetIndexByTime(lbrs.OpenTimes[index]);

            switch (dir)
            {
                case 1:
                    ok = mco.MACD[idx].CompareTo(mco.MACD[idx - 1]) > 0 && 
                         mco.MACD[idx].CompareTo(mco.Signal[idx]) > 0 && 
                         mco.Histogram[idx].CompareTo(mco.Histogram[idx - 1]) > 0;
                    break;

                case -1:
                    ok = mco.MACD[idx].CompareTo(mco.MACD[idx - 1]) < 0 &&
                         mco.MACD[idx].CompareTo(mco.Signal[idx]) < 0 &&
                         mco.Histogram[idx].CompareTo(mco.Histogram[idx - 1]) < 0;
                    break;
            }

            return ok;
        }

        public static bool Dir(this StochasticOscillator sto, int dir, int index, Bars lbrs, Bars hbrs)
        {
            bool ok = false;

            var idx = hbrs.OpenTimes.GetIndexByTime(lbrs.OpenTimes[index]);

            var g0 = sto.PercentK[idx];
            var g1 = sto.PercentK[idx -1];

            var r0 = sto.PercentD[idx]; 

            switch (dir)
            {
                case 1:
                    ok = g0.CompareTo(g1) > 0 && g0.CompareTo(r0) > 0;
                    break;

                case -1:
                    ok = g0.CompareTo(g1) < 0 && g0.CompareTo(r0) < 0;
                    break;
            }

            return ok;
        }
    }
}


GM
gmkenneyy

Joined on 20.03.2020 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: KF_Mtf_MacdStoc_v1.algo
  • Rating: 0
  • Installs: 879
  • Modified: 09/11/2022 00:50
Comments
Log in to add a comment.
GM
gmkenneyy · 2 years ago

@velu130486 Telegram: TG_KFTD