Ejemplo n.º 1
0
        public void Add_WhenOneValueAdded_LastSmaValueIsTheSameAsOriginalValue()
        {
            var sma = new Sma(10);
            sma.Add(10m);

            sma.Values.LastOrDefault().ShouldBeEquivalentTo(10m);
        }
Ejemplo n.º 2
0
        public void AddRange_WhenTwoValueAddedWithTheSameValue_LastSmaValueIsTheSameAsOriginalValue()
        {
            var sma = new Sma(10);
            sma.AddRange(new[] { 10m, 10m });

            sma.Values.LastOrDefault().ShouldBeEquivalentTo(10m);
        }
Ejemplo n.º 3
0
        public void AddRange_WhenTwoValueAddedWithDifferentValue_LastSmaValueIsTheAverage()
        {
            var sma = new Sma(10);
            sma.AddRange(new[] { 0, 10m });

            sma.Values.LastOrDefault().Should().Be(5m);
            sma.Values.FirstOrDefault().Should().Be(0);
        }
Ejemplo n.º 4
0
        public void AddRange_OnlyTakesElementsInThePeriodForCalculation()
        {
            var sma = new Sma(2);
            sma.AddRange(new[] { 0, 10m, 10m });

            sma.Values.LastOrDefault().Should().Be(10m);
            sma.Values.FirstOrDefault().Should().Be(0);
        }
Ejemplo n.º 5
0
        public void AddRange_OnlyStoresNbOfElementsDefinedInmaxBuffer()
        {
            var sma = new Sma(2);
            var elements = Enumerable.Range(1, 10000).Select(x => (decimal)x);
            sma.AddRange(elements);

            sma.Values.Count().Should().Be(5000);
        }
Ejemplo n.º 6
0
        public void Add_WhenTwoValueAddedWithDifferentValue_LastSmaValueIsTheAverage()
        {
            var sma = new Sma(10);
            sma.Add(0m);
            sma.Add(10m);

            sma.Values.LastOrDefault().ShouldBeEquivalentTo(5m);
            sma.Values.FirstOrDefault().ShouldBeEquivalentTo(0);
        }
Ejemplo n.º 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);
                }
            };
        }