Information

Username: septumrar
Member since: 16 Jul 2018
Last login: 16 Jul 2018
Status: Active

Activity

Where Created Comments
Algorithms 0 1
Forum Topics 0 3
Jobs 0 0

Last Algorithm Comments

SE
septumrar · 4 years ago

Please help me make a multi-timeframe. I tried like this. But it didn't work out.

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
    [Indicator("ATR Trailing Stop", AutoRescale = false, IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ATRStops : Indicator
    {

        [Parameter("Timeframe", DefaultValue = "Ticks10")]
        public TimeFrame Timeframe { get; set; }

        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("Period", DefaultValue = 15, MinValue = 2, MaxValue = 50000)]
        public int Period { get; set; }

        [Parameter("Weight", DefaultValue = 3.0, MinValue = 0.1, MaxValue = 400.0)]
        public double Weight { get; set; }

        [Parameter("True:High_Low False:Close", DefaultValue = true)]
        public bool UseHighAndLow { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private AverageTrueRange_Cust_lag _atr;
        private bool _isLong;

        private MarketSeries _marketSeries;

        //private AverageTrueRange_Cust_lag[,] stop_ind = new ATRStops[perCount, weightCount];

        protected override void Initialize()
        {
            Print("1");
            // Result = CreateDataSeries();
            //ATR = CreateDataSeries();
            _marketSeries = MarketData.GetSeries(Timeframe);
            _atr = Indicators.GetIndicator<AverageTrueRange_Cust_lag>(_marketSeries, Period);
            // _atr = Indicators.AverageTrueRange(Period, MaType);
            Print("_marketSeries : {0}", _marketSeries);
            Print("_atr : {0}", _atr);


        }

        public override void Calculate(int index)
        {
            Print("2 Market: {0}", MarketSeries.OpenTime[index]);
            var barIndex = 0;


            if (_marketSeries.OpenTime[0] < MarketSeries.OpenTime[index])
                return;
            else

                barIndex = _marketSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);



            var currentAtr = Weight * _atr.Result[barIndex];

            if (double.IsNaN(currentAtr))
                return;
            // Print("IsNaN {0}", double.IsNaN(Result[barIndex - 1]));
            Print("3");

            if (double.IsNaN(Result[barIndex - 1]) && !double.IsNaN(_atr.Result[barIndex - 1]))
            {
                Print("4");
                var previousATR = Weight * _atr.Result[barIndex - 1];

                _isLong = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices.IsRising();

                var previous = UseHighAndLow ? (_isLong ? MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex - 1] : MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex - 1]) : MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex - 1];

                Result[barIndex] = _isLong ? previous - previousATR : previous + previousATR;
            }


            else
            {
                Print("5");
                //double a = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices.LastValue;
                //  double b = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices.LastValue;
                var current = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex];
                //   var current = MarketData.GetBars(Timeframe, Symbol.Name).ClosePrices[barIndex];

                if (_isLong)
                {
                    Print("6");
                    if (current >= Result[barIndex - 1])
                    {
                        Print("7");
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex];
                        Print("8");
                        Result[barIndex] = Math.Max(Result[barIndex - 1], current - currentAtr);
                    }

                    else
                    {
                        Print("9");
                        _isLong = false;
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex];
                        Print("10");
                        Result[barIndex] = current + currentAtr;
                    }
                }
                else
                {
                    if (current <= Result[barIndex - 1])
                    {
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).LowPrices[barIndex];
                        Result[barIndex] = Math.Min(Result[barIndex - 1], current + currentAtr);
                    }
                    else
                    {
                        _isLong = true;
                        if (UseHighAndLow)
                            current = MarketData.GetBars(Timeframe, Symbol.Name).HighPrices[barIndex];
                        Result[barIndex] = current - currentAtr;
                    }
                }
            }
        }
    }
}