Ejemplo n.º 1
0
        /// <inheritdoc />
        public override void Save(SettingsStorage settings)
        {
            base.Save(settings);

            settings.SetValue(nameof(Ema), Ema.Save());
            settings.SetValue(nameof(Roc), Roc.Save());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Загрузить настройки.
        /// </summary>
        /// <param name="settings">Хранилище настроек.</param>
        public override void Load(SettingsStorage settings)
        {
            base.Load(settings);

            Ema.LoadNotNull(settings, "Ema");
            Roc.LoadNotNull(settings, "Roc");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Сохранить настройки.
        /// </summary>
        /// <param name="settings">Хранилище настроек.</param>
        public override void Save(SettingsStorage settings)
        {
            base.Save(settings);

            settings.SetValue("Ema", Ema.Save());
            settings.SetValue("Roc", Roc.Save());
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public override void Load(SettingsStorage settings)
        {
            base.Load(settings);

            Ema.LoadNotNull(settings, nameof(Ema));
            Roc.LoadNotNull(settings, nameof(Roc));
        }
Ejemplo n.º 5
0
        public static List <decimal?> Ema(this IEnumerable <decimal?> candles, int period)
        {
            IIndicatorOptions options = new EmaOptions(period, CandleVariableCode.CLOSE);
            Ema ema = new Ema();

            return(ema.Get(candles, options));
        }
Ejemplo n.º 6
0
        public void Indicators_Returns_Empty_Ema_For_TooShort_Source_Array()
        {
            double[] src = { 138700.0, 139350.0, 139970.0, 140160.0, 139845.0, 139705.0, 139895.0, 139765.0, 140495.0 };

            IEnumerable <double> result = Ema.Make(src, 10);

            Assert.AreEqual(0, result.Count());
        }
Ejemplo n.º 7
0
        public void Indicators_Returns_Empty_Ema_For_Empty_Source_Array()
        {
            double[] src = new double[0];

            IEnumerable <double> result = Ema.Make(src, 10);

            Assert.AreEqual(0, result.Count());
        }
Ejemplo n.º 8
0
        public void Ema_Test_10()
        {
            var ema = new Ema(BuildCandleSticks(), 10, pair);

            Assert.AreEqual(22.916m, Math.Round(ema.Value, 3));
            var results = ema.ema.Select(value => Math.Round(value, 3)).ToList();

            Compare(EmaResults().ToList(), results);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Обработать входное значение.
        /// </summary>
        /// <param name="input">Входное значение.</param>
        /// <returns>Результирующее значение.</returns>
        protected override IIndicatorValue OnProcess(IIndicatorValue input)
        {
            var candle   = input.GetValue <Candle>();
            var emaValue = Ema.Process(input.SetValue(this, candle.HighPrice - candle.LowPrice));

            if (Ema.IsFormed)
            {
                return(Roc.Process(emaValue));
            }

            return(input);
        }
Ejemplo n.º 10
0
        /// <inheritdoc />
        protected override IIndicatorValue OnProcess(IIndicatorValue input)
        {
            var candle   = input.GetValue <Candle>();
            var emaValue = Ema.Process(input.SetValue(this, candle.HighPrice - candle.LowPrice));

            if (Ema.IsFormed)
            {
                var val = Roc.Process(emaValue);
                return(new DecimalIndicatorValue(this, val.GetValue <decimal>()));
            }

            return(new DecimalIndicatorValue(this));
        }
Ejemplo n.º 11
0
        public void Indicators_Make_One_More_Ema()
        {
            double[] src = { 447.3, 456.8, 451.0, 452.5, 453.4, 455.5, 456.0, 454.7, 453.5, 456.5, 459.5, 465.2, 460.8, 460.8 };

            IEnumerable <double> result = Ema.Make(src, 10);

            Assert.AreEqual(5, result.Count());
            Assert.AreEqual(453.72, result.ElementAt(0));
            Assert.AreEqual(454.7709, result.ElementAt(1));
            Assert.AreEqual(456.6671, result.ElementAt(2));
            Assert.AreEqual(457.4185, result.ElementAt(3));
            Assert.AreEqual(458.0333, result.ElementAt(4));
        }
        public void EmaTest()
        {
            using (var csvReader = new CsvReader(new StreamReader("Indicators/MovingAverageTests.csv")))
            {
                var expectedData = csvReader.GetRecords<MovingAverageDay>().ToList();

                var days = expectedData.Select(r => new ShareDay { Date = r.Date, Close = r.Price }).ToArray();
                var actual = new Ema().Calculate(days, new Ema.Parameters { Periods = 10 }).ToArray();

                var expected = expectedData.Where(r => r.Ema.HasValue).Select(r => Point.With(r.Date, r.Ema.Value)).ToArray();

                CollectionAssert.AreEqual(expected, actual, new PointComparer(4));
            }
        }
Ejemplo n.º 13
0
        public void Indicators_Calculate_Ema_For_Ten()
        {
            double[] src = { 138700.0, 139350.0, 139970.0, 140160.0, 139845.0, 139705.0, 139895.0, 139765.0, 140495.0, 140830.0, 140745.0, 140710.0, 140830.0, 140730.0, 141435.0 };

            IEnumerable <double> result = Ema.Make(src, 10);

            Assert.AreEqual(6, result.Count());
            Assert.AreEqual(139871.5, result.ElementAt(0));
            Assert.AreEqual(140030.3182, result.ElementAt(1));
            Assert.AreEqual(140153.8967, result.ElementAt(2));
            Assert.AreEqual(140276.8246, result.ElementAt(3));
            Assert.AreEqual(140359.2201, result.ElementAt(4));
            Assert.AreEqual(140554.8164, result.ElementAt(5));
        }
Ejemplo n.º 14
0
        public void Indicators_And_Make_One_More_Ema()
        {
            double[] src = { 137900.0, 138460.0, 139080.0, 139725.0, 139755.0, 139455.0, 139565.0, 139500.0, 139670.0, 140420.0, 140625.0, 140650.0, 140655.0, 140550.0, 140315.0, 141000.0, 141020.0, 140955.0, 140635.0, 140610.0, 140915.0, 140555.0 };

            IEnumerable <double> result = Ema.Make(src, 14);

            Assert.AreEqual(9, result.Count());
            Assert.AreEqual(139715.0, result.ElementAt(0));
            Assert.AreEqual(139795.0, result.ElementAt(1));
            Assert.AreEqual(139955.6667, result.ElementAt(2));
            Assert.AreEqual(140097.5778, result.ElementAt(3));
            Assert.AreEqual(140211.9008, result.ElementAt(4));
            Assert.AreEqual(140268.314, result.ElementAt(5));
            Assert.AreEqual(140313.8721, result.ElementAt(6));
            Assert.AreEqual(140394.0225, result.ElementAt(7));
            Assert.AreEqual(140415.4862, result.ElementAt(8));
        }
Ejemplo n.º 15
0
        protected override IEnumerable <MacdPoint> CalcInit(IList <Candle> initialCandles)
        {
            _shortEMA = new Ema(_periodShort);
            _longEMA  = new Ema(_periodLong);

            // this will init both emas such that ".Points" contains exactly signal length amount of points
            int takeShort           = (_periodSignal + _shortEMA.MinimumInitValuesNecessary - 1);
            int takeLong            = (_periodSignal + _longEMA.MinimumInitValuesNecessary - 1);
            int elementsToLeaveOpen = initialCandles.Count - MinimumInitValuesNecessary;

            _shortEMA.Init(initialCandles.TakeLast(takeShort, skipLast: elementsToLeaveOpen));
            _longEMA.Init(initialCandles.TakeLast(takeLong, skipLast: elementsToLeaveOpen));

            // init macdlist by piecewise subtraction
            List <Candle> macdCandles = new List <Candle>();

            for (int i = 0; i < _periodSignal; i++)
            {
                macdCandles.Add(new Candle(0m, 0m, 0m, (decimal)(_shortEMA.Points[i].Value - _longEMA.Points[i].Value), initialCandles[i + _periodLong - 1].OpenTime, initialCandles[i + _periodLong - 1].CloseTime));
            }

            // init signal ema
            _signalEMA = new Ema(_periodSignal);
            _signalEMA.Init(macdCandles);

            // First signal is different
            List <MacdPoint> result = new List <MacdPoint>
            {
                new MacdPoint(_signalEMA.Points.Last().Timestamp, (double)macdCandles.Last().Close, _signalEMA.Points.Last().Value)
            };

            // ... than the following signals
            int offset = MinimumInitValuesNecessary;

            for (int i = offset; i < initialCandles.Count; i++)
            {
                result.Add(CalcNext(initialCandles[i]));
            }

            return(result);
        }
Ejemplo n.º 16
0
 public Prices()
 {
     Type = Price.Close;
     Map  = new Dictionary <Price, List <double> >();
     foreach (Price type in Enum.GetValues(typeof(Price)))
     {
         Map[type] = new List <double>();
     }
     FastEma = new Ema(Const.DIRECTIONAL_FAST_EMA_LENGTH);
     SlowEma = new Ema(Const.DIRECTIONAL_SLOW_EMA_LENGTH);
     Bb      = new Bb(Const.BB_WIDTH, Const.BB_PERIOD);
     //Kc = new Kc(Const.BB_WIDTH, Const.BB_PERIOD);
     //Mc = new Mc(Const.BB_PERIOD);
     Macd       = new Macd(FastEma, SlowEma, Const.MACD_SIGNAL_LENGTH);
     Rsi        = new Rsi(Const.RSI_LENGTH);
     StochRsi   = new StochRsi(Rsi, Const.STOCH_RSI_LENGTH, Const.STOCH_RSI_D_LENGTH);
     Indicators = new List <IIndicator>
     {
         FastEma, SlowEma, Bb, /*Kc,*/ /*Mc,*/ Macd, Rsi, StochRsi
     };
 }
Ejemplo n.º 17
0
        public void Ema_Test_9()
        {
            Ema sma = new Ema(BuildCandleSticks(), 9, pair);

            Assert.AreEqual(17, sma.Value);
        }
Ejemplo n.º 18
0
        public override void OnItemAdded(Tick item)
        {
            if (item.Symbol != this.strategyHeader.Symbol)
            {
                return;
            }

            BarSettings bs = this.tradingData.Get <IEnumerable <BarSettings> >().SingleOrDefault(s => s.StrategyId == this.strategyHeader.Id);

            if (bs == null)
            {
                return;
            }

            SMASettings ss = this.tradingData.Get <IEnumerable <SMASettings> >().SingleOrDefault(s => s.StrategyId == this.strategyHeader.Id);

            if (ss == null)
            {
                return;
            }


            double strategyAmount = this.tradingData.GetAmount(this.strategyHeader);
            bool   hasLong        = this.tradingData.HasLongPosition(this.strategyHeader);
            bool   hasShort       = this.tradingData.HasShortPosition(this.strategyHeader);

            IEnumerable <Bar> bars = this.tradingData.Get <IEnumerable <Bar> >().GetNewestBars(bs.Symbol, bs.Interval, bs.Period + 1);

            if (strategyAmount > 0)
            {
                return;
            }

            if (this.tradingData.UnfilledExists(this.strategyHeader, OrderType.Limit))
            {
                return;
            }

            if (bars == null || bars.Count() == 0)
            {
                return;
            }

            if (bars.Count() < ss.PeriodFast + 1)
            {
                return;
            }

            if (bars.Count() < ss.PeriodSlow + 1)
            {
                return;
            }

            IEnumerable <double> closePrices = from b in bars
                                               select b.Close;

            IEnumerable <double> fastSMA = Ema.Make(closePrices, ss.PeriodFast);
            IEnumerable <double> slowSMA = Ema.Make(closePrices, ss.PeriodSlow);

            //IEnumerable<double> fastSMA = null;
            //IEnumerable<double> slowSMA = null;

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, fast {2} slow {3}", DateTime.Now, this.GetType().Name, fastSMA.Last(), slowSMA.Last()));

            if (slowSMA.Last() < fastSMA.Last())
            {
                Signal signal = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Limit, item.Price, 0, item.Price);
                this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, сигнал на открытие длинной позиции {2}", DateTime.Now, this.GetType().Name, signal.ToString()));
                this.signalQueue.Enqueue(signal);
            }
            else if (slowSMA.Last() > fastSMA.Last())
            {
                Signal signal = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Limit, item.Price, 0, item.Price);
                this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, сигнал на открытие короткой позиции {2}", DateTime.Now, this.GetType().Name, signal.ToString()));
                this.signalQueue.Enqueue(signal);
            }
        }
Ejemplo n.º 19
0
 public Gsma(IEnumerable <decimal> candleSticks, int length, int averageLength)
 {
     _ema = new Ema(candleSticks, length);
     this.averageLength = averageLength;
     CalculateGsma();
 }
Ejemplo n.º 20
0
        public void Indicators_Make_Ema_for_five_numbers()
        {
            IEnumerable <double> src = new[] { 5.0, 6.0, 7.0, 8.0, 9.0 };

            Assert.AreEqual(7, Ema.Make(src, 5).ElementAt(0));
        }