Example #1
0
        public void Add_WhenOneValueAdded_LastEmaValueIsTheSameAsOriginalValue()
        {
            var target = new Ema(10);
            target.Add(10m);

            target.Values.LastOrDefault().ShouldBeEquivalentTo(10m);
        }
Example #2
0
        public void AddRange_WhenTwoDifferentValueAdded_LastEmaValueIsTheAverage()
        {
            var target = new Ema(10);
            target.AddRange(new[] { 0m, 10m });

            target.Values.LastOrDefault().Should().BeApproximately((180m) / 99m, 0.000000001m);
            target.Values.FirstOrDefault().Should().Be(0);
        }
Example #3
0
        public void AddRange_OnlyTakesElementsInThePeriodForCalculation()
        {
            var target = new Ema(2);
            target.AddRange(new[] { 0, 10m, 10m });

            target.Values.LastOrDefault().Should().Be(80m / 9m);
            target.Values.FirstOrDefault().Should().Be(0);
        }
Example #4
0
        public void AddRange_OnlyStoresNbOfElementsDefinedInmaxBuffer()
        {
            var target = new Ema(2);
            var elements = Enumerable.Range(1, 10000).Select(x => (decimal)x);
            target.AddRange(elements);

            target.Values.Count().Should().Be(5000);
        }
Example #5
0
        public void Add_WhenTwoValueAddedWithDifferentValue_LastEmaValueIsTheAverage()
        {
            var target = new Ema(10);
            target.Add(0m);
            target.Add(10m);

            target.Values.LastOrDefault().Should().BeApproximately((180m) / 99m, 0.000000001m);
            target.Values.FirstOrDefault().ShouldBeEquivalentTo(0);
        }
Example #6
0
        public void AddRange_TestWithKnownValues_Succeed()
        {
            var target = new Ema(10);

            var elements = (new[]
            {
                22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24, 22.29, 22.15, 22.39, 22.38, 22.61, 23.36, 24.05, 23.75, 23.83, 23.95,
                23.63, 23.82, 23.87, 23.65, 23.19, 23.10, 23.33, 22.68, 23.10, 22.40, 22.17
            }).Select(x => (decimal)x);
            target.AddRange(elements);
            target.Values.LastOrDefault().Should().BeApproximately(22.915536m, 0.000001m);
        }
Example #7
0
        public Cobra(Adx adx,
            Ema fastEmaHigh,
            Ema fastEmaLow,
            Sma slowSmaHigh,
            Sma slowSmaLow,
            IDateProvider dateProvider,
            string instrument,
            int periodInMinutes,
            ITradingAdapter tradingAdapter,
            IRateProvider rateProvider,
            int accountId,
            bool isbacktesting = false,
            IEnumerable<Candle> initialCandles = null)
        {
            if (adx == null)
            {
                throw new ArgumentNullException(nameof(adx));
            }
            if (fastEmaHigh == null)
            {
                throw new ArgumentNullException(nameof(fastEmaHigh));
            }
            if (fastEmaLow == null)
            {
                throw new ArgumentNullException(nameof(fastEmaLow));
            }
            if (slowSmaHigh == null)
            {
                throw new ArgumentNullException(nameof(slowSmaHigh));
            }
            if (slowSmaLow == null)
            {
                throw new ArgumentNullException(nameof(slowSmaLow));
            }
            if (dateProvider == null)
            {
                throw new ArgumentNullException(nameof(dateProvider));
            }
            if (tradingAdapter == null)
            {
                throw new ArgumentNullException(nameof(tradingAdapter));
            }
            if (string.IsNullOrWhiteSpace(instrument))
            {
                throw new ArgumentNullException(nameof(instrument));
            }

            this.adx = adx;
            this.fastEmaHigh = fastEmaHigh;
            this.fastEmaLow = fastEmaLow;
            this.slowSmaHigh = slowSmaHigh;
            this.slowSmaLow = slowSmaLow;
            this.dateProvider = dateProvider;
            this.Instrument = instrument;
            this.tradingAdapter = tradingAdapter;
            this.rateProvider = rateProvider;
            this.AccountId = accountId;
            this.isbacktesting = isbacktesting;
            this.PeriodInMinutes = periodInMinutes;
            this.candles = new List<Candle>();

            if (!(initialCandles?.Any()).GetValueOrDefault(false) && rateProvider != null)
            {
                var _initialCandles = rateProvider.GetLastCandles(instrument, periodInMinutes, MinNbOfCandles).ToList();

                try
                {
                    this.AddCandles(initialCandles);
                }
                catch (Exception ex)
                {
                    Trace.TraceError(ex.ToString());
                }
            }
            else if ((initialCandles?.Any()).GetValueOrDefault(false))
            {
                this.AddCandles(initialCandles);
            }
            else if (rateProvider == null)
            {
                throw new ArgumentNullException(nameof(rateProvider));
            }

            this.Id = Guid.NewGuid().ToString();

            candleBuilder = new CandleBuilder(TimeSpan.FromMinutes(periodInMinutes));
            candleBuilder.NewCandleCreated += (o, e) => {
                try
                {
                    this.AddCandle(e.NewCandle);
                }
                catch(Exception ex)
                {
                    validations.AddErrorMessage(ex.Message);
                }
            };
        }