Category Trend  Published on 07/12/2020

TrendTrader3

Description

If you want to use Range charts, use 2pips for short term, 4 pips for mid term and 8 pips for long term. With regard to the remaining Inputs, use as Default .

 

if you want to use Tick charts, i recommend to use a  80 pips chart, and the inputs are: 17, 72,34 (macd1)  89,144,68 (macd2) and finally 42,288,42 (macd 3).

 

 

 

 


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendTrader3 : Indicator
    {
        [Parameter("Data")]

        public DataSeries Data { get; set; }

        [Parameter("Slow Period 1", DefaultValue = 72)]
        public int PeriodFast1 { get; set; }

        [Parameter("Fast Period 1", DefaultValue = 17)]
        public int PeriodSlow1 { get; set; }

        [Parameter("Signal 1", DefaultValue = 34)]
        public int Signal1 { get; set; }

        [Parameter("Doji Percentage", DefaultValue = 0.4)]
        public double Percentage { get; set; }

        [Parameter("Slow Period 2", DefaultValue = 72)]
        public int PeriodFast2 { get; set; }

        [Parameter("Fast Period 2", DefaultValue = 17)]
        public int PeriodSlow2 { get; set; }

        [Parameter("Signal 2", DefaultValue = 34)]
        public int Signal2 { get; set; }

        [Parameter("Slow Period 3", DefaultValue = 15)]
        public int PeriodFast3 { get; set; }

        [Parameter("Fast Period 3", DefaultValue = 3)]
        public int PeriodSlow3 { get; set; }

        [Parameter("Signal 3", DefaultValue = 3)]
        public int Signal3 { get; set; }

        [Parameter("Short Term TimeFrame", DefaultValue = "Daily")]
        public TimeFrame MACDTimeframe1 { get; set; }

        [Parameter("Mid Term Timeframe", DefaultValue = "Daily")]
        public TimeFrame MACDTimeframe2 { get; set; }

        [Parameter("Long Term TimeFrame", DefaultValue = "Daily")]
        public TimeFrame MACDTimeframe3 { get; set; }


        [Parameter("Source")]

        public DataSeries Source { get; set; }

        [Parameter("Moving Average ", DefaultValue = 20)]
        public int MaPeriod { get; set; }

        [Parameter("Bars to Count ", DefaultValue = 20)]
        public int Periods { get; set; }

        [Parameter("Kairi Percentage ", DefaultValue = 27)]
        public int kairiPercent { get; set; }


        //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        [Output("Up Histogram", LineColor = "DarkGreen", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries UpTrend { get; set; }

        [Output("Down Histogram", LineColor = "Red", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries DownTrend { get; set; }

        [Output("Neutral Histogram", LineColor = "White", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Neutral { get; set; }




        private Bars series1, series5, series15;


        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        private MacdCrossOver macd1;
        private MacdCrossOver macd5;
        private MacdCrossOver macd15;


        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        private IndicatorDataSeries mac1buff;
        private IndicatorDataSeries signal1buff;


        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        private IndicatorDataSeries mac5buff;
        private IndicatorDataSeries signal5buff;

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        private IndicatorDataSeries mac15buff;
        private IndicatorDataSeries signal15buff;

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        private MovingAverage average21;
        private MovingAverage average42;

        private IndicatorDataSeries average21bff;
        private IndicatorDataSeries average42bff;


        private IndicatorDataSeries hi_kairi;
        private IndicatorDataSeries lo_kairi;

        public bool isUp;
        public bool isDown;

        private MovingAverage average1;





        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------







        protected override void Initialize()
        {
            mac1buff = CreateDataSeries();
            mac5buff = CreateDataSeries();
            mac15buff = CreateDataSeries();

            signal1buff = CreateDataSeries();
            signal5buff = CreateDataSeries();
            signal15buff = CreateDataSeries();

            series1 = MarketData.GetBars(MACDTimeframe1, Bars.SymbolName);
            series5 = MarketData.GetBars(MACDTimeframe2, Bars.SymbolName);
            series15 = MarketData.GetBars(MACDTimeframe3, Bars.SymbolName);


            macd1 = Indicators.MacdCrossOver(series1.ClosePrices, PeriodSlow1, PeriodFast1, Signal1);
            macd5 = Indicators.MacdCrossOver(series5.ClosePrices, PeriodSlow2, PeriodFast2, Signal2);
            macd15 = Indicators.MacdCrossOver(series15.ClosePrices, PeriodSlow3, PeriodFast3, Signal3);


            average21 = Indicators.MovingAverage(Data, 20, MovingAverageType.Exponential);

            average42 = Indicators.MovingAverage(Data, 42, MovingAverageType.Exponential);


            average21bff = CreateDataSeries();
            average42bff = CreateDataSeries();

            average1 = Indicators.MovingAverage(Source, MaPeriod, MovingAverageType.Exponential);

            hi_kairi = CreateDataSeries();

            lo_kairi = CreateDataSeries();


        }

        public override void Calculate(int index)
        {

            UpTrend[index] = 0;
            DownTrend[index] = 0;
            Neutral[index] = 1;


            hi_kairi[index] = (Math.Abs(Bars.HighPrices[index] - average21.Result[index]) / average21.Result[index]) * 100 * 1000;
            lo_kairi[index] = (Math.Abs(Bars.LowPrices[index] - average21.Result[index]) / average21.Result[index]) * 100 * 1000;



            var index1 = GetIndexByTF(series1, this.Bars, index);


            if (index1 != -1)
            {
                mac1buff[index] = (-1) * macd1.MACD[index1];
                signal1buff[index] = (-1) * macd1.Signal[index1];

            }

            var index2 = GetIndexByTF(series5, this.Bars, index);

            //var index4 = GetIndexByDate(series5, Bars.OpenTimes[index]);


            if (double.IsNaN(index2) == false)
            {
                mac5buff[index] = (-1) * macd5.MACD[index2];
                signal5buff[index] = (-1) * macd5.Signal[index2];
            }



            var index3 = GetIndexByTF(series15, this.Bars, index);

            if (index3 != -1)
            {
                mac15buff[index] = (-1) * macd15.MACD[index3];
                signal15buff[index] = (-1) * macd15.Signal[index3];

            }


            if ((mac1buff[index] > signal1buff[index]) && ((mac5buff[index] > signal5buff[index]) || (mac15buff[index] > signal15buff[index])))
            {

                UpTrend[index] = 1;
                DownTrend[index] = 0;
                Neutral[index] = 0;

                if (Bars.ClosePrices[index] > Bars.OpenPrices[index] && Bars.ClosePrices[index - 1] < Bars.OpenPrices[index - 1] && Bars.ClosePrices[index] > average1.Result[index] && Bars.LowPrices[index] < average1.Result[index])
                {

                    Chart.SetBarColor(index, Color.Lime);
                }

                if (Bars.ClosePrices[index] > Bars.OpenPrices[index])
                {
                    Chart.SetBarColor(index, Color.DarkGreen);





                    if (Math.Abs(Bars.OpenPrices[index] - Bars.ClosePrices[index]) <= Percentage * Math.Abs(Bars.HighPrices[index] - Bars.LowPrices[index]))
                    {

                        if (Bars.ClosePrices[index] > Bars.OpenPrices[index])
                        {

                            if (double.IsNaN(average1.Result[index]) == false && Bars.ClosePrices[index] > average1.Result[index] && Bars.LowPrices[index] > average1.Result[index])
                            {



                                Chart.SetBarColor(index, Color.Lime);



                                if (lo_kairi[index] <= kairiPercent)
                                {

                                    Chart.SetBarColor(index, Color.Aqua);

                                }

                            }

                            if (double.IsNaN(average1.Result[index]) == false && Bars.ClosePrices[index] > average1.Result[index] && Bars.LowPrices[index] < average1.Result[index])
                            {

                                Chart.SetBarColor(index, Color.Lime);

                                if (lo_kairi[index] <= kairiPercent)
                                {

                                    Chart.SetBarColor(index, Color.Aqua);

                                }


                            }

                        }
                    }

                    isUp = true;
                    isDown = false;

                }

                else
                {

                    Chart.SetBarColor(index, Color.FromHex("FF3F3F3F"));
                    // Cor Cinza
                }

            }


            else if ((mac1buff[index] < signal1buff[index]) && ((mac5buff[index] < signal5buff[index]) || mac15buff[index] < signal15buff[index]))
            {

                UpTrend[index] = 0;
                DownTrend[index] = 1;
                Neutral[index] = 0;


                if (Bars.ClosePrices[index] > Bars.OpenPrices[index])
                {
                    Chart.SetBarColor(index, Color.FromHex("FF3F3F3F"));
                    // Cor Cinza
                }
                else
                {

                    Chart.SetBarColor(index, Color.FromHex("FF500003"));

                    //Result[index] = 2;
                    // Cor Vermelho Escuro



                    if (Math.Abs(Bars.OpenPrices[index] - Bars.ClosePrices[index]) <= Percentage * Math.Abs(Bars.HighPrices[index] - Bars.LowPrices[index]))
                    {

                        if (Bars.ClosePrices[index] < Bars.OpenPrices[index])
                        {

                            if (double.IsNaN(average1.Result[index]) == false && Bars.ClosePrices[index] < average1.Result[index] && Bars.LowPrices[index] < average1.Result[index])
                            {

                                Chart.SetBarColor(index, Color.Red);


                                if (hi_kairi[index] <= kairiPercent)
                                {

                                    Chart.SetBarColor(index, Color.Tomato);

                                }



                            }


                            if (double.IsNaN(average1.Result[index]) == false && Bars.ClosePrices[index] < average1.Result[index] && Bars.LowPrices[index] > average1.Result[index])
                            {

                                Chart.SetBarColor(index, Color.Red);

                                if (hi_kairi[index] <= kairiPercent)
                                {

                                    Chart.SetBarColor(index, Color.Tomato);

                                }



                            }


                        }
                    }


                    isUp = false;
                    isDown = true;

                }

            }
        }


        //Chart.SetBarColor(index, Color.Red);

        public int GetIndexByTF(Bars B1, Bars B2, int index)
        {
            var index2 = B1.OpenTimes.GetIndexByTime(B2.OpenTimes[index]);
            return index2;
        }

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        private int GetIndexByDate(Bars B1, DateTime time)
        {
            for (int i = B1.OpenTimes.Count - 1; i > 0; i--)
            {
                if (time == B1.OpenTimes[i])
                    return i;
            }
            return -1;
        }

        //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    }
}


AN
andurei

Joined on 30.09.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: TrendTrader3.algo
  • Rating: 5
  • Installs: 2173
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
No comments found.