Example #1
0
        public void CalculateWithNullSeriesSizeShouldReturn0()
        {
            var series = GenerateTimeSeries.From(new double[] {});
            var mdd    = new MaximumDrawdownCriterion();

            Assert.AreEqual(0d, mdd.Calculate(series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
Example #2
0
        public void SetUp()
        {
            // @TODO add volumes
            IList <Tick> ticks = new List <Tick>();

            ticks.Add(GenerateTick.From(44.98, 45.05, 45.17, 44.96));
            ticks.Add(GenerateTick.From(45.05, 45.10, 45.15, 44.99));
            ticks.Add(GenerateTick.From(45.11, 45.19, 45.32, 45.11));
            ticks.Add(GenerateTick.From(45.19, 45.14, 45.25, 45.04));
            ticks.Add(GenerateTick.From(45.12, 45.15, 45.20, 45.10));
            ticks.Add(GenerateTick.From(45.15, 45.14, 45.20, 45.10));
            ticks.Add(GenerateTick.From(45.13, 45.10, 45.16, 45.07));
            ticks.Add(GenerateTick.From(45.12, 45.15, 45.22, 45.10));
            ticks.Add(GenerateTick.From(45.15, 45.22, 45.27, 45.14));
            ticks.Add(GenerateTick.From(45.24, 45.43, 45.45, 45.20));
            ticks.Add(GenerateTick.From(45.43, 45.44, 45.50, 45.39));
            ticks.Add(GenerateTick.From(45.43, 45.55, 45.60, 45.35));
            ticks.Add(GenerateTick.From(45.58, 45.55, 45.61, 45.39));
            ticks.Add(GenerateTick.From(45.45, 45.01, 45.55, 44.80));
            ticks.Add(GenerateTick.From(45.03, 44.23, 45.04, 44.17));
            ticks.Add(GenerateTick.From(44.23, 43.95, 44.29, 43.81));
            ticks.Add(GenerateTick.From(43.91, 43.08, 43.99, 43.08));
            ticks.Add(GenerateTick.From(43.07, 43.55, 43.65, 43.06));
            ticks.Add(GenerateTick.From(43.56, 43.95, 43.99, 43.53));
            ticks.Add(GenerateTick.From(43.93, 44.47, 44.58, 43.93));
            _data = GenerateTimeSeries.From(ticks);
        }
Example #3
0
        public void CalculateWithNoTicksShouldReturn1()
        {
            var _series = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);
            IAnalysisCriterion averageProfit = new AverageProfitCriterion();

            Assert.AreEqual(1d, averageProfit.Calculate(_series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
Example #4
0
        public void CalculateWithNoTrades()
        {
            var series = GenerateTimeSeries.From(1, 2, 3, 6, 5, 20, 3);
            var mdd    = new MaximumDrawdownCriterion();

            Assert.AreEqual(0d, mdd.Calculate(series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
Example #5
0
        public void SetUp()
        {
            IList <Tick> ticks = new List <Tick>();

            ticks.Add(GenerateTick.From(44.98, 45.05, 45.17, 44.96, 1));
            ticks.Add(GenerateTick.From(45.05, 45.10, 45.15, 44.99, 2));
            ticks.Add(GenerateTick.From(45.11, 45.19, 45.32, 45.11, 1));
            ticks.Add(GenerateTick.From(45.19, 45.14, 45.25, 45.04, 3));
            ticks.Add(GenerateTick.From(45.12, 45.15, 45.20, 45.10, 1));
            ticks.Add(GenerateTick.From(45.15, 45.14, 45.20, 45.10, 2));
            ticks.Add(GenerateTick.From(45.13, 45.10, 45.16, 45.07, 1));
            ticks.Add(GenerateTick.From(45.12, 45.15, 45.22, 45.10, 5));
            ticks.Add(GenerateTick.From(45.15, 45.22, 45.27, 45.14, 1));
            ticks.Add(GenerateTick.From(45.24, 45.43, 45.45, 45.20, 1));
            ticks.Add(GenerateTick.From(45.43, 45.44, 45.50, 45.39, 1));
            ticks.Add(GenerateTick.From(45.43, 45.55, 45.60, 45.35, 5));
            ticks.Add(GenerateTick.From(45.58, 45.55, 45.61, 45.39, 7));
            ticks.Add(GenerateTick.From(45.45, 45.01, 45.55, 44.80, 6));
            ticks.Add(GenerateTick.From(45.03, 44.23, 45.04, 44.17, 1));
            ticks.Add(GenerateTick.From(44.23, 43.95, 44.29, 43.81, 2));
            ticks.Add(GenerateTick.From(43.91, 43.08, 43.99, 43.08, 1));
            ticks.Add(GenerateTick.From(43.07, 43.55, 43.65, 43.06, 7));
            ticks.Add(GenerateTick.From(43.56, 43.95, 43.99, 43.53, 6));
            ticks.Add(GenerateTick.From(43.93, 44.47, 44.58, 43.93, 1));
            _data = GenerateTimeSeries.From(ticks);
        }
Example #6
0
 public void SetUp()
 {
     _data       = GenerateTimeSeries.From(1, 2, 3, 4, 3, 4, 5, 4, 3, 3, 4, 3, 2);
     _timeFrame  = 3;
     _closePrice = new ClosePriceIndicator(_data);
     _sma        = new SmaIndicator(_closePrice, _timeFrame);
 }
Example #7
0
        public void CashFlowSize()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(1d, 2d, 3d, 4d, 5d);
            var        cashFlow         = new CashFlow(sampleTimeSeries, new TradingRecord());

            Assert.AreEqual(5, cashFlow.Size);
        }
        public void RewardRiskRatioCriterionOnlyWithGain()
        {
            var series        = GenerateTimeSeries.From(1, 2, 3, 6, 8, 20, 3);
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(2), Order.SellAt(5));

            Assert.IsTrue(double.IsInfinity(_rrc.Calculate(series, tradingRecord)));
        }
Example #9
0
        public void BestShouldBeBuyAndHoldOnLoss()
        {
            var series       = GenerateTimeSeries.From(6.0, 3.0, 6.0, 6.0);
            var bestStrategy = (new TotalProfitCriterion()).ChooseBest(series, _strategies);

            Assert.AreEqual(_buyAndHoldStrategy, bestStrategy);
        }
Example #10
0
        public void AverageDirectionalMovement()
        {
            var tick1 = GenerateTick.From(0, 0, 13, 7);
            var tick2 = GenerateTick.From(0, 0, 11, 5);
            var tick3 = GenerateTick.From(0, 0, 15, 3);
            var tick4 = GenerateTick.From(0, 0, 14, 2);
            var tick5 = GenerateTick.From(0, 0, 13, 0.2);

            IList <Tick> ticks = new List <Tick>();

            ticks.Add(tick1);
            ticks.Add(tick2);
            ticks.Add(tick3);
            ticks.Add(tick4);
            ticks.Add(tick5);

            var series  = GenerateTimeSeries.From(ticks);
            var admdown = new AverageDirectionalMovementDownIndicator(series, 3);

            TaTestsUtils.AssertDecimalEquals(admdown.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(admdown.GetValue(1), 4d / 3);
            TaTestsUtils.AssertDecimalEquals(admdown.GetValue(2), 4d / 3 * 2d / 3);
            TaTestsUtils.AssertDecimalEquals(admdown.GetValue(3), (4d / 3 * 2d / 3) * 2d / 3 + 1d / 3);
            TaTestsUtils.AssertDecimalEquals(admdown.GetValue(4), ((4d / 3 * 2d / 3) * 2d / 3 + 1d / 3) * 2d / 3 + 1.8 * 1d / 3);
        }
Example #11
0
        public void BestShouldBeAlwaysOperateOnProfit()
        {
            var series       = GenerateTimeSeries.From(6.0, 9.0, 6.0, 6.0);
            var bestStrategy = (new TotalProfitCriterion()).ChooseBest(series, _strategies);

            Assert.AreEqual(_alwaysStrategy, bestStrategy);
        }
Example #12
0
        public void GetValue()
        {
            IList <Tick> ticks = new List <Tick>();

            ticks.Add(GenerateTick.From(1355.69, 2739.55));
            ticks.Add(GenerateTick.From(1325.51, 3119.46));
            ticks.Add(GenerateTick.From(1335.02, 3466.88));
            ticks.Add(GenerateTick.From(1313.72, 2577.12));
            ticks.Add(GenerateTick.From(1319.99, 2480.45));
            ticks.Add(GenerateTick.From(1331.85, 2329.79));
            ticks.Add(GenerateTick.From(1329.04, 2793.07));
            ticks.Add(GenerateTick.From(1362.16, 3378.78));
            ticks.Add(GenerateTick.From(1365.51, 2417.59));
            ticks.Add(GenerateTick.From(1374.02, 1442.81));
            TimeSeries series = GenerateTimeSeries.From(ticks);

            var nvi = new NviIndicator(series);

            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(0), 1000);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(1), 1000);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(2), 1000);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(3), 984.0452);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(4), 988.7417);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(5), 997.6255);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(6), 997.6255);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(7), 997.6255);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(8), 1000.079);
            TaTestsUtils.AssertDecimalEquals(nvi.GetValue(9), 1006.3116);
        }
Example #13
0
        public void WithSimpleTrades()
        {
            var series        = GenerateTimeSeries.From(1, 10, 5, 6, 1);
            var mdd           = new MaximumDrawdownCriterion();
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(1), Order.SellAt(2), Order.BuyAt(2), Order.SellAt(3), Order.BuyAt(3), Order.SellAt(4));

            Assert.AreEqual(.9d, mdd.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #14
0
        public void CalculateOnlyWithLossTrades()
        {
            var _series       = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(2), Order.SellAt(5));
            IAnalysisCriterion averageProfit = new AverageProfitCriterion();

            Assert.AreEqual(Math.Pow(95d / 100 * 70d / 100, 1d / 6), averageProfit.Calculate(_series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #15
0
        public void CalculateOnlyWithGainTrades()
        {
            var _series       = GenerateTimeSeries.From(100d, 105d, 110d, 100d, 95d, 105d);
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(2), Order.BuyAt(3), Order.SellAt(5));
            IAnalysisCriterion averageProfit = new AverageProfitCriterion();

            Assert.AreEqual(1.0243, TaTestsUtils.TaOffset, averageProfit.Calculate(_series, tradingRecord));
        }
Example #16
0
        public void CalculateShouldWork()
        {
            var series        = GenerateTimeSeries.From(1, 2, 3, 6, 5, 20, 3);
            var mdd           = new MaximumDrawdownCriterion();
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(3), Order.SellAt(4), Order.BuyAt(5), Order.SellAt(6));

            Assert.AreEqual(.875d, mdd.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #17
0
        public void CalculateWithNoTrades()
        {
            var series = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);

            IAnalysisCriterion buyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());

            Assert.AreEqual(1 / 0.7, buyAndHold.Calculate(series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
        public void CalculateWithNoTrades()
        {
            var series = GenerateTimeSeries.From(100, 105, 110, 100, 95, 105);

            IAnalysisCriterion buyAndHold = new NumberOfTradesCriterion();

            Assert.AreEqual(0d, buyAndHold.Calculate(series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
Example #19
0
        public void CalculateWithOneTrade()
        {
            var series = GenerateTimeSeries.From(100, 105);
            var trade  = new Trade(Order.BuyAt(0), Order.SellAt(1));
            IAnalysisCriterion buyAndHold = new BuyAndHoldCriterion();

            Assert.AreEqual(105d / 100, buyAndHold.Calculate(series, trade), TaTestsUtils.TaOffset);
        }
Example #20
0
        public void RunOnWholeSeries()
        {
            TimeSeries series = GenerateTimeSeries.From(20d, 40d, 60d, 10d, 30d, 50d, 0d, 20d, 40d);

            var allTrades = series.Run(_strategy).Trades;

            Assert.AreEqual(2, allTrades.Count);
        }
        public void CalculateWithNoTrades()
        {
            var series = GenerateTimeSeries.From(100, 105, 110, 100, 95, 105);

            IAnalysisCriterion numberOfTicks = new NumberOfTicksCriterion();

            Assert.AreEqual(0, (int)numberOfTicks.Calculate(series, new TradingRecord()));
        }
Example #22
0
        public void CalculateWithOneTrade()
        {
            var _series = GenerateTimeSeries.From(100, 105);
            var trade   = new Trade(Order.BuyAt(0), Order.SellAt(1));
            IAnalysisCriterion average = new AverageProfitCriterion();

            Assert.AreEqual(Math.Pow(105d / 100, 1d / 2), average.Calculate(_series, trade), TaTestsUtils.TaOffset);
        }
        public void CalculateWithOneTrade()
        {
            var series = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);
            var t      = new Trade(Order.BuyAt(2), Order.SellAt(5));
            IAnalysisCriterion numberOfTicks = new NumberOfTicksCriterion();

            Assert.AreEqual(4, numberOfTicks.Calculate(series, t), TaTestsUtils.TaOffset);
        }
Example #24
0
        public void CalculateWithOnlyGains()
        {
            var series        = GenerateTimeSeries.From(1, 2, 3, 6, 8, 20, 3);
            var mdd           = new MaximumDrawdownCriterion();
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(2), Order.SellAt(5));

            Assert.AreEqual(0d, mdd.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #25
0
        public void WithTradesThatSellBeforeBuying()
        {
            var series        = GenerateTimeSeries.From(2, 1, 3, 5, 6, 3, 20);
            var mdd           = new MaximumDrawdownCriterion();
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(3), Order.SellAt(4), Order.SellAt(5), Order.BuyAt(6));

            Assert.AreEqual(.91, mdd.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #26
0
        public void CalculateWithASimpleTrade()
        {
            var _series       = GenerateTimeSeries.From(100d, 105d, 110d, 100d, 95d, 105d);
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(2));
            IAnalysisCriterion averageProfit = new AverageProfitCriterion();

            Assert.AreEqual(Math.Pow(110d / 100, 1d / 3), averageProfit.Calculate(_series, tradingRecord), TaTestsUtils.TaOffset);
        }
Example #27
0
        public void CalculateLinearRegression()
        {
            var values    = new double[] { 1, 2, 1.3, 3.75, 2.25 };
            var indicator = new ClosePriceIndicator(GenerateTimeSeries.From(values));
            var reg       = new SimpleLinearRegressionIndicator(indicator, 5);

//			SimpleRegression origReg = buildSimpleRegression(values);
            TaTestsUtils.AssertDecimalEquals(reg.GetValue(4), 2.91);         //origReg.predict(4));
        }
Example #28
0
        public void GetValueWithCacheLengthIncrease()
        {
            var data = new double[200];

            TaTestsUtils.ArraysFill(data, 10);
            var sma = new SmaIndicator(new ClosePriceIndicator(GenerateTimeSeries.From(data)), 100);

            TaTestsUtils.AssertDecimalEquals(sma.GetValue(105), 10);
        }
Example #29
0
        public void ReallyLongCashFlow()
        {
            const int  size             = 1000000;
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(Enumerable.Repeat((Tick)GenerateTick.From(10), size).ToList());
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(size - 1));
            var        cashFlow         = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(size - 1), 1);
        }
        public void CalculateWithTwoTrades()
        {
            var series        = GenerateTimeSeries.From(100, 105, 110, 100, 95, 105);
            var tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(2), Order.BuyAt(3), Order.SellAt(5));

            IAnalysisCriterion numberOfTicks = new NumberOfTicksCriterion();

            Assert.AreEqual(6, numberOfTicks.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }