public void CalculateWithOneTrade()
        {
            ITimeSeries series = new MockTimeSeries(100M, 95M, 102M, 105, 97, 113);
            Trade       trade  = new Trade(Order.buyAt(0, series), Order.sellAt(1, series));

            AverageProfitableTradesCriterion average = new AverageProfitableTradesCriterion();

            Assert.AreEqual(0M, average.Calculate(series, trade));

            trade = new Trade(Order.buyAt(1, series), Order.sellAt(2, series));
            Assert.AreEqual(1M, average.Calculate(series, trade));
        }
        public void CalculateWithOneTrade()
        {
            TimeSeries series = GenerateTimeSeries.From(100d, 95d, 102d, 105d, 97d, 113d);
            var        trade  = new Trade(Order.BuyAt(0), Order.SellAt(1));

            var average = new AverageProfitableTradesCriterion();

            Assert.AreEqual(0d, average.Calculate(series, trade), TaTestsUtils.TaOffset);

            trade = new Trade(Order.BuyAt(1), Order.SellAt(2));
            Assert.AreEqual(1d, average.Calculate(series, trade), TaTestsUtils.TaOffset);
        }
        public void Calculate()
        {
            TimeSeries series        = GenerateTimeSeries.From(100d, 95d, 102d, 105d, 97d, 113d);
            var        tradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(2), Order.SellAt(3), Order.BuyAt(4), Order.SellAt(5));

            var average = new AverageProfitableTradesCriterion();

            Assert.AreEqual(2d / 3, average.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
        public void Calculate()
        {
            ITimeSeries    series        = new MockTimeSeries(100M, 95M, 102M, 105M, 97M, 113M);
            ITradingRecord tradingRecord = new BaseTradingRecord(
                Order.buyAt(0, series), Order.sellAt(1, series),
                Order.buyAt(2, series), Order.sellAt(3, series),
                Order.buyAt(4, series), Order.sellAt(5, series));

            AverageProfitableTradesCriterion average = new AverageProfitableTradesCriterion();

            Assert.AreEqual(2M / 3, average.Calculate(series, tradingRecord));
        }
示例#5
0
        public void QuickStart()
        {
            // Getting a time series (from any provider: CSV, web service, etc.)
            var series = CsvTradesLoader.LoadBitstampSeries();

            // Getting the close price of the ticks
            var firstClosePrice = series.GetTick(0).ClosePrice;

            Console.WriteLine("First close price: " + firstClosePrice.ToDouble());
            // Or within an indicator:
            var closePrice = new ClosePriceIndicator(series);

            // Here is the same close price:
            Console.WriteLine(firstClosePrice.IsEqual(closePrice.GetValue(0))); // equal to firstClosePrice

            // Getting the simple moving average (SMA) of the close price over the last 5 ticks
            var shortSma = new SmaIndicator(closePrice, 5);

            // Here is the 5-ticks-SMA value at the 42nd index
            Console.WriteLine("5-ticks-SMA value at the 42nd index: " + shortSma.GetValue(42).ToDouble());

            // Getting a longer SMA (e.g. over the 30 last ticks)
            var longSma = new SmaIndicator(closePrice, 30);


            // Ok, now let's building our trading rules!

            // Buying rules
            // We want to buy:
            //  - if the 5-ticks SMA crosses over 30-ticks SMA
            //  - or if the price goes below a defined price (e.g $800.00)
            var buyingRule = (new CrossedUpIndicatorRule(shortSma, longSma))
                             .Or(new CrossedDownIndicatorRule(closePrice, Decimal.ValueOf("800")));

            // Selling rules
            // We want to sell:
            //  - if the 5-ticks SMA crosses under 30-ticks SMA
            //  - or if if the price looses more than 3%
            //  - or if the price earns more than 2%
            var sellingRule = (new CrossedDownIndicatorRule(shortSma, longSma))
                              //.Or(new CrossedDownIndicatorRule(new TrailingStopLossIndicator(closePrice, Decimal.ValueOf("30")), closePrice ))
                              .Or(new StopLossRule(closePrice, Decimal.ValueOf("3")))
                              .Or(new StopGainRule(closePrice, Decimal.ValueOf("2")));

            // Running our juicy trading strategy...
            var tradingRecord = series.Run(new Strategy(buyingRule, sellingRule));

            Console.WriteLine("Number of trades for our strategy: " + tradingRecord.TradeCount);

            // Analysis

            // Getting the cash flow of the resulting trades
            var cashFlow = new CashFlow(series, tradingRecord);

            // Getting the profitable trades ratio
            IAnalysisCriterion profitTradesRatio = new AverageProfitableTradesCriterion();

            Console.WriteLine("Profitable trades ratio: " + profitTradesRatio.Calculate(series, tradingRecord));
            // Getting the reward-risk ratio
            IAnalysisCriterion rewardRiskRatio = new RewardRiskRatioCriterion();

            Console.WriteLine("Reward-risk ratio: " + rewardRiskRatio.Calculate(series, tradingRecord));

            // Total profit of our strategy
            // vs total profit of a buy-and-hold strategy
            IAnalysisCriterion vsBuyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());

            Console.WriteLine("Our profit vs buy-and-hold profit: " + vsBuyAndHold.Calculate(series, tradingRecord));

            // Your turn!
        }