Category Other  Published on 13/11/2023

TGR (The God Rules)

Description

Search for Dev Team to make big Cbot, contact me at :  https://t.me/nimi012 (direct messaging)

The TGR (The God Rules) is an indicator that compares multiple periods by incrementing them. The concept is based on stochastic calculations but with different sources, additional smoothing, and bar colorations based on the reached levels.

*** 17/11/2023 : correction of bugs ***


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ATGRBARSv3 : Indicator
    {
        [Parameter("Indicator Type", DefaultValue = EnumIndicatorType.Over_Buy_Sell, Group = " TGR Setting")]
        public EnumIndicatorType IndicatorType { get; set; }
        public enum EnumIndicatorType
        {
            Over_Buy_Sell,
            Under_Over_Middle,
            AllType
        }

        [Parameter("Initial Periods", DefaultValue = 55, Group = "Initialization")]
        public int Periods { get; set; }
        [Parameter("Sources Bars", DefaultValue = EnumSourcesBars.High_And_Low, Group = "Initialization")]
        public EnumSourcesBars SourcesBars { get; set; }
        public enum EnumSourcesBars
        {
            Open,
            Close,
            High,
            Low,
            High_And_Low,
            Switch_High_And_Low,
            Median_Price,
            Typical_Price,
            Weighted_Price,
        }
        [Parameter("Sources Over(Buy/Sell)", DefaultValue = EnumSourcesOverBuySell.Switch_High_And_Low, Group = "Initialization")]
        public EnumSourcesOverBuySell SourcesOverBuySell { get; set; }
        public enum EnumSourcesOverBuySell
        {
            Open,
            Close,
            High_And_Low,
            Switch_High_And_Low,
            Median_Price,
            Typical_Price,
            Weighted_Price,
        }

        [Parameter("Nbrs Indicator Load  ", DefaultValue = 10, Group = "LoadCount")]
        public int NbrsLoad { get; set; }
        [Parameter("Periods Multiplicator ", DefaultValue = 1.618, Group = "LoadCount")]
        public double PeriodsMultiTgr { get; set; }

        [Parameter("Smooth Sensibility Over(Buy/Sell) ", DefaultValue = 10, Group = "  Over(Buy/Sell")]
        public int SensibilityOverBuySell { get; set; }
        [Parameter("Smooth Result", DefaultValue = 1, Group = "  Over(Buy/Sell")]
        public int Smooth { get; set; }
        [Parameter("Smooth Type", DefaultValue = MovingAverageType.Weighted, Group = "  Over(Buy/Sell")]
        public MovingAverageType SmoothType { get; set; }

        [Parameter("Period Signal Result", DefaultValue = 89, Group = "Signal")]
        public int PeriodSignalResult { get; set; }
        [Parameter("MaType Signal Result", DefaultValue = MovingAverageType.Weighted, Group = "Signal")]
        public MovingAverageType MaTypeSignalResult { get; set; }

        [Parameter("Period Histo 1", DefaultValue = 2, Group = "Histogram")]
        public int PeriodS1 { get; set; }
        [Parameter("MaType Histo 1", DefaultValue = MovingAverageType.Exponential, Group = "Histogram")]
        public MovingAverageType MaTypeS1 { get; set; }

        [Parameter("Period Histo 2", DefaultValue = 55, Group = "Histogram")]
        public int PeriodS2 { get; set; }
        [Parameter("MaType Histo 2", DefaultValue = MovingAverageType.Exponential, Group = "Histogram")]
        public MovingAverageType MaTypeS2 { get; set; }

        [Parameter("Level Extra High ", DefaultValue = 80, Group = " Level Setting")]
        public double SetLevelExtraHigh { get; set; }
        [Parameter("Level High", DefaultValue = 50, Group = " Level Setting")]
        public double SetLevelHigh { get; set; }
        [Parameter("Level Middle ", DefaultValue = 0, Group = " Level Setting")]
        public double SetLevelMiddle { get; set; }

        [Parameter("Use Bars Color", DefaultValue = true, Group = "Bars Colors")]
        public bool UseBarsColors { get; set; }
        [Parameter("Level 1 Max ", DefaultValue = 95, Group = "Bars Colors")]
        public int LevelBarsColor1 { get; set; }
        [Parameter("Level 2 ", DefaultValue = 85, Group = "Bars Colors")]
        public int LevelBarsColor2 { get; set; }
        [Parameter("Level 3 ", DefaultValue = 70, Group = "Bars Colors")]
        public int LevelBarsColor3 { get; set; }
        [Parameter("Level 4 ", DefaultValue = 60, Group = "Bars Colors")]
        public int LevelBarsColor4 { get; set; }
        [Parameter("Level 5 MiddleMax", DefaultValue = 53, Group = "Bars Colors")]
        public int LevelBarsColor5 { get; set; }
        [Parameter("Level 6 Middle Min", DefaultValue = 47, Group = "Bars Colors")]
        public int LevelBarsColor6 { get; set; }
        [Parameter("Level 7 Middle Min", DefaultValue = 40, Group = "Bars Colors")]
        public int LevelBarsColor7 { get; set; }
        [Parameter("Level 8 ", DefaultValue = 30, Group = "Bars Colors")]
        public int LevelBarsColor8 { get; set; }
        [Parameter("Level 9 ", DefaultValue = 15, Group = "Bars Colors")]
        public int LevelBarsColor9 { get; set; }
        [Parameter("Level 10 ", DefaultValue = 5, Group = "Bars Colors")]
        public int LevelBarsColor10 { get; set; }

        [Output("Histo Up", PlotType = PlotType.Histogram, LineColor = "Lime", Thickness = 3)]
        public IndicatorDataSeries HistolUp { get; set; }
        [Output("Histo Down", PlotType = PlotType.Histogram, LineColor = "Red", Thickness = 3)]
        public IndicatorDataSeries HistoDown { get; set; }
        [Output("Histo Up Retracement", PlotType = PlotType.Histogram, LineColor = "FF268AAD", Thickness = 3)]
        public IndicatorDataSeries HistoUpRetracement { get; set; }
        [Output("Histo Down Retracement", PlotType = PlotType.Histogram, LineColor = "FFFF9B00", Thickness = 3)]
        public IndicatorDataSeries HistoDownRetracement { get; set; }

        [Output("Level Extra High", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "7BFF0000", Thickness = 1)]
        public IndicatorDataSeries LevelExtraHigh { get; set; }
        [Output("Level High", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "81FFA500", Thickness = 1)]
        public IndicatorDataSeries LevelHigh { get; set; }
        [Output("Level Middle", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "40808080", Thickness = 1)]
        public IndicatorDataSeries LevelMiddle { get; set; }
        [Output("Level Low", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "81FFA500", Thickness = 1)]
        public IndicatorDataSeries LevelLow { get; set; }
        [Output("Level ExtraLow", PlotType = PlotType.Line, LineStyle = LineStyle.LinesDots, LineColor = "7BFF0000", Thickness = 1)]
        public IndicatorDataSeries LevelExtraLow { get; set; }

        [Output("Result Count", PlotType = PlotType.Line, LineColor = "White", Thickness = 1)]
        public IndicatorDataSeries Result { get; set; }
        [Output("Signal Result", PlotType = PlotType.Line, LineColor = "DeepSkyBlue", Thickness = 1)]
        public IndicatorDataSeries SignalResult { get; set; }

        private MovingAverage ma, maSignalRes, signal, signal2;
        private IndicatorDataSeries[] res;
        public IndicatorDataSeries totalLoad;
        public IndicatorDataSeries resTgr;

        public IndicatorDataSeries[] Top;
        public IndicatorDataSeries[] Middle;
        public IndicatorDataSeries[] Bottom;
        public IndicatorDataSeries SourceBars;

        public IndicatorDataSeries HistoUpRes;
        public IndicatorDataSeries HistoDownRes;

        private int[] periods;
        private double resIndicator;

        private double barsCount;

        private double indicatorsLoad;
        private double indicatorsNeedLoad;
        private double barCount;
        private int pct = 0;

        protected override void Initialize()
        {
            res = new IndicatorDataSeries[NbrsLoad];
            periods = new int[NbrsLoad];
            Top = new IndicatorDataSeries[NbrsLoad];
            Bottom = new IndicatorDataSeries[NbrsLoad];
            Middle = new IndicatorDataSeries[NbrsLoad];
            resIndicator = 0.00;
            totalLoad = CreateDataSeries();
            resTgr = CreateDataSeries();
            SourceBars = CreateDataSeries();

            barsCount = Bars.Count;
            indicatorsLoad = 0;
            indicatorsNeedLoad = 0;
            pct = 0;

            barCount = Bars.Count;

            for (int i = 0; i < res.Length; i++)
            {
                periods[i] = (int)(Math.Round(Math.Pow(PeriodsMultiTgr, i) * Periods));
                res[i] = CreateDataSeries();
                Top[i] = CreateDataSeries();
                Bottom[i] = CreateDataSeries();
                Middle[i] = CreateDataSeries();

            }

            ma = Indicators.MovingAverage(resTgr, Smooth, SmoothType);
            maSignalRes = Indicators.MovingAverage(Result, PeriodSignalResult, MaTypeSignalResult);
            HistoUpRes = CreateDataSeries();
            HistoDownRes = CreateDataSeries();

            signal = Indicators.MovingAverage(resTgr, PeriodS1, MaTypeS1);
            signal2 = Indicators.MovingAverage(resTgr, PeriodS2, MaTypeS2);
        }

        public override void Calculate(int index)
        {
            LevelExtraHigh[index] = SetLevelExtraHigh;
            LevelHigh[index] = SetLevelHigh;

            LevelMiddle[index] = SetLevelMiddle;

            LevelLow[index] = 100 - SetLevelHigh;
            LevelExtraLow[index] = 100 - SetLevelExtraHigh;

            resIndicator = 0.00;
            barsCount = Bars.Count;
            int tgrUpTotal = 0;
            int tgrDownTotal = 0;
            int tgrMiddleTotal = 0;

            for (int i = 0; i < res.Length; i++)
            {
                if (SourcesOverBuySell == EnumSourcesOverBuySell.Open)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.OpenPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.OpenPrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.Close)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.ClosePrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.ClosePrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.High_And_Low)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.HighPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.LowPrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.Switch_High_And_Low)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.LowPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.HighPrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.Median_Price)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.MedianPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.MedianPrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.Typical_Price)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.TypicalPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.TypicalPrices.Minimum(periods[i]);
                    Middle[i][index + SensibilityOverBuySell] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }
                else if (SourcesOverBuySell == EnumSourcesOverBuySell.Weighted_Price)
                {
                    Top[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.WeightedPrices.Maximum(periods[i]);
                    Bottom[i][index + SensibilityOverBuySell] = index < periods[i] ? double.NaN : Bars.WeightedPrices.Minimum(periods[i]);
                    Middle[i][index] = (Top[i][index] - Bottom[i][index]) / 2 + Bottom[i][index];
                }

                if (SourcesBars == EnumSourcesBars.Close)
                    SourceBars[index] = Bars.ClosePrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Open)
                    SourceBars[index] = Bars.OpenPrices.Last(0);
                if (SourcesBars == EnumSourcesBars.High)
                    SourceBars[index] = Bars.HighPrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Low)
                    SourceBars[index] = Bars.LowPrices.Last(0);
                if (SourcesBars == EnumSourcesBars.High_And_Low)
                    SourceBars[index] = Bars.ClosePrices.Last(0) > Middle[0][index - 5] ? Bars.HighPrices.Last(0) : Bars.ClosePrices.Last(0) < Middle[0][index - 5] ? Bars.LowPrices.Last(0) : Bars.ClosePrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Switch_High_And_Low)
                    SourceBars[index] = Bars.ClosePrices.Last(0) > Middle[0][index - 5] ? Bars.LowPrices.Last(0) : Bars.ClosePrices.Last(0) < Middle[0][index - 5] ? Bars.HighPrices.Last(0) : Bars.ClosePrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Median_Price)
                    SourceBars[index] = Bars.MedianPrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Typical_Price)
                    SourceBars[index] = Bars.TypicalPrices.Last(0);
                if (SourcesBars == EnumSourcesBars.Weighted_Price)
                    SourceBars[index] = Bars.WeightedPrices.Last(0);

                res[i][index] = (SourceBars[index] - Bottom[i][index]) / ((Top[i][index] - Bottom[i][index]) > double.Epsilon ? (Top[i][index] - Bottom[i][index]) : 0.1) * 100;

                resIndicator += res[i][index];

                tgrUpTotal += GetTgrUp(i);
                tgrDownTotal += GetTgrDown(i);
                tgrMiddleTotal += GetTgrMiddle(i);

                if (barsCount > (periods[i]))
                    indicatorsLoad = i + 1;
            }
            var up = -(200.0 * tgrUpTotal / (res.Length * (res.Length - 1.0)));
            var down = (200.0 * tgrDownTotal / (res.Length * (res.Length - 1.0))); ;
            var midd = (200.0 * tgrMiddleTotal / (res.Length * (res.Length - 1.0))); ;

            indicatorsNeedLoad = NbrsLoad - indicatorsLoad;
            totalLoad[index] = indicatorsLoad;

            var res0Smooth = ((resIndicator / (totalLoad[index])));

            resTgr[index] = IndicatorType == EnumIndicatorType.Over_Buy_Sell ? res0Smooth : IndicatorType == EnumIndicatorType.Under_Over_Middle ? ((midd + down + (up + 100)) / 3) : (((midd + down + (up + 100)) / 3) + res0Smooth) / 2;
            Result[index] = ma.Result.Last(0);

            var si2 = signal2.Result.Last(0) > 80 ? 80 : signal2.Result.Last(0) < 20 ? 20 : signal2.Result.Last(0);

            HistoUpRes[index] = signal.Result.Last(0) > si2 ? signal.Result.Last(0) - si2 : double.NaN;
            HistoDownRes[index] = signal.Result.Last(0) < si2 ? signal.Result.Last(0) - si2 : double.NaN;

            HistolUp[index] = HistoUpRes[index] > HistoUpRes[index - 1] || double.IsNaN(HistoUpRes[index - 1]) ? HistoUpRes[index] : double.NaN;
            HistoDown[index] = HistoDownRes[index] < HistoDownRes[index - 1] || double.IsNaN(HistoDownRes[index - 1]) ? HistoDownRes[index] : double.NaN;

            HistoUpRetracement[index] = HistoUpRes[index] < HistoUpRes[index - 1] && !double.IsNaN(HistoUpRes[index - 1]) ? HistoUpRes[index] : double.NaN;
            HistoDownRetracement[index] = HistoDownRes[index] > HistoDownRes[index - 1] && !double.IsNaN(HistoDownRes[index - 1]) ? HistoDownRes[index] : double.NaN;

            SignalResult[index] = maSignalRes.Result.Last(0);

            double test = index / barCount * 100;

            if (pct < test + 0.1 && pct < 105)
            {
                Print("Load : " + test.ToString("F0") + " %");
                pct += 10;
            }
            if (index == periods[NbrsLoad - 1])
            {
                Chart.DrawVerticalLine("Load All Vertical Ligne" + index, index, Color.Lime, 1);
                IndicatorArea.DrawText("Load All Indicators" + index, "100 % Loading", index, 90, Color.Lime);
                IndicatorArea.DrawText("Load All Indicators 2" + index, "100 % Loading", index, 0, Color.White);
                IndicatorArea.DrawText("Load All Indicators 3" + index, "100 % Loading", index, -90, Color.Red);
            }
            if (UseBarsColors == true)
            {

                if (Result.Last(0) > LevelBarsColor1)
                    Chart.SetBarColor(index, "White");
                else if (Result.Last(0) <= LevelBarsColor1 && Result.Last(0) > LevelBarsColor2)
                    Chart.SetBarColor(index, "DeepSkyBlue");
                else if (Result.Last(0) <= LevelBarsColor2 && Result.Last(0) > LevelBarsColor3)
                    Chart.SetBarColor(index, "FF01FF01");
                else if (Result.Last(0) <= LevelBarsColor3 && Result.Last(0) > LevelBarsColor4)
                    Chart.SetBarColor(index, "FF00BF00");
                else if (Result.Last(0) <= LevelBarsColor4 && Result.Last(0) > LevelBarsColor5)
                    Chart.SetBarColor(index, "FF00843B");
                else if (Result.Last(0) <= LevelBarsColor5 && Result.Last(0) > LevelBarsColor6)
                    Chart.SetBarColor(index, "FF262626");
                else if (Result.Last(0) <= LevelBarsColor6 && Result.Last(0) > LevelBarsColor7)
                    Chart.SetBarColor(index, "FF5B0003");
                else if (Result.Last(0) <= LevelBarsColor7 && Result.Last(0) > LevelBarsColor8)
                    Chart.SetBarColor(index, "FF890002");
                else if (Result.Last(0) <= LevelBarsColor8 && Result.Last(0) > LevelBarsColor9)
                    Chart.SetBarColor(index, "FFFE0000");
                else if (Result.Last(0) <= LevelBarsColor9 && Result.Last(0) > LevelBarsColor10)
                    Chart.SetBarColor(index, "Orange");
                else if (Result.Last(0) <= LevelBarsColor10)
                    Chart.SetBarColor(index, "FFFF00C5");
            }
            // Comment for Botting           
            if (indicatorsNeedLoad != 0)
            {
                Chart.DrawStaticText("Indicators", indicatorsLoad + "/" + NbrsLoad + " Indicators are Load (" + (indicatorsLoad / NbrsLoad * 100).ToString("F0") + "%)" + "\nScroll and Load more Bars for " + indicatorsNeedLoad + " Indicators (100%)"
                , VerticalAlignment.Bottom, HorizontalAlignment.Right, Color.Red);
            }
            else
            {
                Chart.RemoveObject("Indicators");
                Chart.DrawStaticText("Indicators Load Ok", " Indicators Load : " + indicatorsLoad + "/" + NbrsLoad + "\n" + "Higher Period : " + periods[res.Length - 1] + "\n" + " Bars at 100% : " + (barCount - periods[res.Length - 1]).ToString("F0"), VerticalAlignment.Bottom, HorizontalAlignment.Right, Color.Green);
            }
        }
        private int GetTgrUp(int reference)
        {
            int count = 0;
            for (int i = reference + 1; i < res.Length; i++)
            {
                if (Top[i].Last(0) > Top[reference].Last(0))
                    count++;
            }
            // Print(count);
            return count;
        }
        private int GetTgrMiddle(int reference)
        {
            int count = 0;
            for (int i = reference + 1; i < res.Length; i++)
            {
                if (Middle[i].Last(0) < Middle[reference].Last(0))
                    count++;
            }
            // Print(count);
            return count;
        }
        private int GetTgrDown(int reference)
        {
            int count = 0;
            for (int i = reference + 1; i < res.Length; i++)
            {
                if (Bottom[i].Last(0) < Bottom[reference].Last(0))
                    count++;
            }
            return count;
        }
    }
}









YE
YesOrNot

Joined on 10.10.2022 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: A TGR BARS V3.algo
  • Rating: 5
  • Installs: 366
Comments
Log in to add a comment.
No comments found.