Example #1
0
 /**
  * Fills with last value till the end of the series.
  */
 private void FillToTheEnd()
 {
     if (_timeSeries.GetEndIndex() >= _values.Count)
     {
         decimal lastValue = _values[_values.Count - 1];
         _values.AddRange(Enumerable.Range(0, _timeSeries.GetEndIndex() - _values.Count + 1).Select(_ => lastValue));
     }
 }
Example #2
0
        public void subSeriesCreation()
        {
            ITimeSeries subSeries = defaultSeries.GetSubSeries(2, 5);

            Assert.AreEqual(defaultSeries.Name, subSeries.Name);
            Assert.AreEqual(0, subSeries.GetBeginIndex());
            Assert.AreEqual(defaultSeries.GetBeginIndex(), subSeries.GetBeginIndex());
            Assert.AreEqual(2, subSeries.GetEndIndex());
            Assert.AreNotEqual(defaultSeries.GetEndIndex(), subSeries.GetEndIndex());
            Assert.AreEqual(3, subSeries.GetBarCount());

            subSeries = defaultSeries.GetSubSeries(-1000, 1000);
            Assert.AreEqual(0, subSeries.GetBeginIndex());
            Assert.AreEqual(defaultSeries.GetEndIndex(), subSeries.GetEndIndex());
        }
Example #3
0
        public override T GetValue(int index)
        {
            ITimeSeries series = TimeSeries;

            if (series != null)
            {
                int seriesEndIndex = series.GetEndIndex();
                if (index <= seriesEndIndex)
                {
                    // We are not after the end of the series
                    int removedBarsCount = series.GetRemovedBarsCount();
                    int startIndex       = Math.Max(removedBarsCount, HighestResultIndex);
                    if (index - startIndex > RECURSION_THRESHOLD)
                    {
                        // Too many unCalculated values; the risk for a StackOverflowError becomes high.
                        // Calculating the previous values iteratively
                        for (int prevIdx = startIndex; prevIdx < index; prevIdx++)
                        {
                            base.GetValue(prevIdx);
                        }
                    }
                }
            }

            return(base.GetValue(index));
        }
Example #4
0
        /**
         * Calculates the Maximum drawdown from a cash flow over a series.
         * @param series the time series
         * @param cashFlow the cash flow
         * @return the Maximum drawdown from a cash flow over a series
         */
        private decimal CalculateMaximumDrawdown(ITimeSeries series, CashFlow cashFlow)
        {
            decimal MaximumDrawdown = Decimals.Zero;
            decimal MaxPeak         = Decimals.Zero;

            if (!series.IsEmpty())
            {
                // The series is not empty
                for (int i = series.GetBeginIndex(); i <= series.GetEndIndex(); i++)
                {
                    decimal value = cashFlow.GetValue(i);
                    if (value.IsGreaterThan(MaxPeak))
                    {
                        MaxPeak = value;
                    }

                    decimal drawdown = MaxPeak.Minus(value).DividedBy(MaxPeak);
                    if (drawdown.IsGreaterThan(MaximumDrawdown))
                    {
                        MaximumDrawdown = drawdown;
                    }
                }
            }
            return(MaximumDrawdown);
        }
Example #5
0
 public void getEndGetBeginGetBarCountIsEmpty()
 {
     //  series
     Assert.AreEqual(0, defaultSeries.GetBeginIndex());
     Assert.AreEqual(bars.Count - 1, defaultSeries.GetEndIndex());
     Assert.AreEqual(bars.Count, defaultSeries.GetBarCount());
     Assert.IsFalse(defaultSeries.IsEmpty());
     // Constrained series
     Assert.AreEqual(2, constrainedSeries.GetBeginIndex());
     Assert.AreEqual(4, constrainedSeries.GetEndIndex());
     Assert.AreEqual(3, constrainedSeries.GetBarCount());
     Assert.IsFalse(constrainedSeries.IsEmpty());
     // Empty series
     Assert.AreEqual(-1, emptySeries.GetBeginIndex());
     Assert.AreEqual(-1, emptySeries.GetEndIndex());
     Assert.AreEqual(0, emptySeries.GetBarCount());
     Assert.IsTrue(emptySeries.IsEmpty());
 }
Example #6
0
        public override decimal Calculate(ITimeSeries series, Trade trade)
        {
            ITradingRecord fakeRecord = new BaseTradingRecord();

            fakeRecord.Enter(series.GetBeginIndex());
            fakeRecord.Exit(series.GetEndIndex());

            return(_criterion.Calculate(series, trade) / _criterion.Calculate(series, fakeRecord));
        }
Example #7
0
        public void addBar()
        {
            defaultSeries = new BaseTimeSeries();
            IBar firstBar  = new MockBar(new DateTime(2014, 6, 13, 0, 0, 0, 0), 1M);
            IBar secondBar = new MockBar(new DateTime(2014, 6, 14, 0, 0, 0, 0), 2M);

            Assert.AreEqual(0, defaultSeries.GetBarCount());
            Assert.AreEqual(-1, defaultSeries.GetBeginIndex());
            Assert.AreEqual(-1, defaultSeries.GetEndIndex());

            defaultSeries.AddBar(firstBar);
            Assert.AreEqual(1, defaultSeries.GetBarCount());
            Assert.AreEqual(0, defaultSeries.GetBeginIndex());
            Assert.AreEqual(0, defaultSeries.GetEndIndex());

            defaultSeries.AddBar(secondBar);
            Assert.AreEqual(2, defaultSeries.GetBarCount());
            Assert.AreEqual(0, defaultSeries.GetBeginIndex());
            Assert.AreEqual(1, defaultSeries.GetEndIndex());
        }
Example #8
0
        public void dpo()
        {
            DPOIndicator        dpo = new DPOIndicator(series, 9);
            ClosePriceIndicator cp  = new ClosePriceIndicator(series);
            SMAIndicator        sma = new SMAIndicator(cp, 9);
            int timeShift           = 9 / 2 + 1;

            for (int i = series.GetBeginIndex(); i <= series.GetEndIndex(); i++)
            {
                Assert.AreEqual(dpo.GetValue(i), cp.GetValue(i).Minus(sma.GetValue(i - timeShift)));
            }

            Assert.AreEqual(dpo.GetValue(9), 0.112M);
            Assert.AreEqual(dpo.GetValue(10), -0.02M);
            Assert.AreEqual(dpo.GetValue(11), 0.211428571428571428571428571M);
            Assert.AreEqual(dpo.GetValue(12), 0.17M);
        }
Example #9
0
        public void test()
        {
            AroonOscillatorIndicator aroonOscillator = new AroonOscillatorIndicator(data, 25);

            Assert.AreEqual(aroonOscillator.GetValue(data.GetBeginIndex()), 0);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetBeginIndex() + 25), 84M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetBeginIndex() + 26), 80M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetBeginIndex() + 27), 76M);

            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex() - 5), 56M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex() - 4), 52M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex() - 3), 48M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex() - 2), 44M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex() - 1), 40M);
            Assert.AreEqual(aroonOscillator.GetValue(data.GetEndIndex()), 32M);
        }
Example #10
0
 /**
  * Runs the provided strategy over the managed series.
  * <p>
  * @param strategy the trading strategy
  * @param orderType the {@link OrderType} used to open the trades
  * @param amount the amount used to open/close the trades
  * @return the trading record coMing from the run
  */
 public ITradingRecord Run(IStrategy strategy, OrderType orderType, decimal amount)
 {
     return(Run(strategy, orderType, amount, _timeSeries.GetBeginIndex(), _timeSeries.GetEndIndex()));
 }
Example #11
0
 public override decimal Calculate(ITimeSeries series, ITradingRecord tradingRecord)
 {
     return(series.GetBar(series.GetEndIndex()).ClosePrice.DividedBy(series.GetBar(series.GetBeginIndex()).ClosePrice));
 }