Code for Correct Output Parameters

patzoot@gmail.com since: 30 Sep 2020;

  09 Jun 2021, 23:03
Code for Correct Output Parameters

Hi Everyone,

I got stuck editing the Multi Time Frame (MTF) Trend Indicator so as to add my own set of Indicators. The original indicator purely displays whether your conditions are met on other time frames by displaying the timeframe label in green for buy or red for sell conditions.

I'm trying to create Outputs for each timeframe listed so I may refer to them when using the indicator in conjunction with a robot, so to for instance set buying or selling conditions only to when the 4H, 1H & 15M charts all indicate the same condition. I thought of attributing the value 3 to buy and -3 to sell conditions but seem to be struggling referring to the timeframes in question, hence by gibberish code at the end... Any help and suggestions would be appreciated!

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Text;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MultiTimeFrameTrend : Indicator
    {
        [Parameter("Vertical Alignment", Group = "Position", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment vAlignment { get; set; }

        [Parameter("Horizontal Alignment", Group = "Position", DefaultValue = HorizontalAlignment.Right)]
        public HorizontalAlignment hAlignment { get; set; }


        [Parameter("IKH Kijun Sen", Group = "KijunSen", DefaultValue = 140, MinValue = 1)]
        public int KijunSenPeriod { get; set; }

        [Parameter("Length", Group = "SSL", DefaultValue = 10)]
        public int _length { get; set; }

        [Parameter("MACD Long Cycle", Group = "MACD", DefaultValue = 40, MinValue = 1, Step = 1)]
        public int MACDLC { get; set; }

        [Parameter("MACD Short/Smoothing Cycle", Group = "MACD", DefaultValue = 30, MinValue = 12, Step = 1)]
        public int ShortC { get; set; }

        [Parameter("MACD Signal Period", Group = "MACD", DefaultValue = 9, MinValue = 9, Step = 1)]
        public int SignalP { get; set; }

        //////////////////////////////////////////////////////////////////////// OUTPUTS

        public IndicatorDataSeries Output_barsM5 { get; set; }
        public IndicatorDataSeries Output_barsM15 { get; set; }
        public IndicatorDataSeries Output_barsM30 { get; set; }
        public IndicatorDataSeries Output_barsH1 { get; set; }
        public IndicatorDataSeries Output_barsH4 { get; set; }
        public IndicatorDataSeries Output_barsD1 { get; set; }
        public IndicatorDataSeries Output_barsW1 { get; set; }
        public IndicatorDataSeries Output_barsMN { get; set; }

        //////////////////////////////////////////////////////////////////////// 

        private List<TBox> list1;
        private string lastResult;
        bool condition;

        ////////////////////////////////////////////////////////////////////////

        protected override void Initialize()
        {
            this.list1 = new List<TBox>();

            Bars barsM5 = MarketData.GetBars(TimeFrame.Minute5);
            Bars barsM15 = MarketData.GetBars(TimeFrame.Minute15);
            Bars barsM30 = MarketData.GetBars(TimeFrame.Minute30);
            Bars barsH1 = MarketData.GetBars(TimeFrame.Hour);
            Bars barsH4 = MarketData.GetBars(TimeFrame.Hour4);
            Bars barsD1 = MarketData.GetBars(TimeFrame.Daily);
            Bars barsW1 = MarketData.GetBars(TimeFrame.Weekly);
            Bars barsMN = MarketData.GetBars(TimeFrame.Monthly);

            list1.Add(new TBox("MN", "          ", Indicators.GetIndicator<SSLChannel>(barsMN, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsMN, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("W1", "     ", Indicators.GetIndicator<SSLChannel>(barsW1, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsW1, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("D1", "     ", Indicators.GetIndicator<SSLChannel>(barsD1, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsD1, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("H4", "     ", Indicators.GetIndicator<SSLChannel>(barsH4, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsH4, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("H1", "     ", Indicators.GetIndicator<SSLChannel>(barsH1, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsH1, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("M30", "    ", Indicators.GetIndicator<SSLChannel>(barsM30, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsM30, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("M15", "             ", Indicators.GetIndicator<SSLChannel>(barsM15, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsM15, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));
            list1.Add(new TBox("M5", "          ", Indicators.GetIndicator<SSLChannel>(barsM5, _length, MovingAverageType.Simple), Indicators.IchimokuKinkoHyo(barsM5, 9, KijunSenPeriod, 52), Indicators.MacdHistogram(MACDLC, ShortC, SignalP)));

            this.lastResult = "";
        }

        public override void Calculate(int index)
        {
            string sb_green = "", sb_white = "", sb_red = "";
            double price = (Symbol.Ask + Symbol.Bid) * 0.5;


            foreach (TBox box in this.list1)
            {
                bool IKH_Bullish = true;
                bool SSL_Bullish = true;
                bool MACD_Bullish = true;


                if (price < box.IKH.KijunSen.Last(1))
                {
                    IKH_Bullish = false;
                }

                if (box.SSL._sslUp.Last(1) < box.SSL._sslDown.Last(1))
                {
                    SSL_Bullish = false;
                }

                if (box.MACDH.Histogram.Last(1) < box.MACDH.Signal.Last(1))
                {
                    MACD_Bullish = false;
                }

                // When all conditions are met
                if (IKH_Bullish && SSL_Bullish && MACD_Bullish)
                {
                    sb_green += "  " + box.label;
                    sb_red += "  " + box.empty;
                    sb_white += "  " + box.empty;
                    condition = true;
                }
                // When no conditions are met
                else if (!IKH_Bullish && !SSL_Bullish && !MACD_Bullish)
                {
                    sb_green += "  " + box.empty;
                    sb_red += "  " + box.label;
                    sb_white += "  " + box.empty;
                    condition = false;
                }
                // When some conditions are met
                else
                {
                    sb_green += "  " + box.empty;
                    sb_red += "  " + box.empty;
                    sb_white += "  " + box.label;
                }
                //////////////////////---->

                if (TBox.label.Equals(label))
                {
                    int BC = 3;
                    int SC = -3;
                    int direction = (condition ? BC : SC);

                    Output_barsM5[index] = direction;
                    Output_barsM15[index] = direction;
                }

            }

            string result = sb_green + sb_white + sb_red;
            if (!result.Equals(this.lastResult))
            {
                Chart.DrawStaticText("idtext_green", "\n" + sb_green + ".", this.vAlignment, this.hAlignment, Color.Green);
                Chart.DrawStaticText("idtext_red", "\n" + sb_red + ".", this.vAlignment, this.hAlignment, Color.IndianRed);
                Chart.DrawStaticText("idtext_white", "\n" + sb_white + ".", this.vAlignment, this.hAlignment, Color.Gray);
                this.lastResult = result;
            }



        }
    }

    public class TBox
    {
        public TBox(string l, string e, SSLChannel b, IchimokuKinkoHyo c, MacdHistogram d)
        {
            this.label = l;
            this.empty = e;
            this.SSL = b;
            this.IKH = c;
            this.MACDH = d;
        }
        public string label { get; set; }
        public string empty { get; set; }

        public SSLChannel SSL { get; set; }
        public IchimokuKinkoHyo IKH { get; set; }
        public MacdHistogram MACDH { get; set; }

    }
}
amusleh's avatar

amusleh since: 01 Mar 2021;

  10 Jun 2021, 10:56

Hi,

You can use separate outputs for each time frame, and add Output attribute to your indicator outputs otherwise it will not work.

Community Developer | Spotware | Telegram: @algodeveloper