public void BetterThan()
        {
            IAnalysisCriterion criterion = new RewardRiskRatioCriterion();

            Assert.IsTrue(criterion.BetterThan(3.5, 2.2));
            Assert.IsFalse(criterion.BetterThan(1.5, 2.7));
        }
        public void BetterThan()
        {
            AbstractAnalysisCriterion criterion = new RewardRiskRatioCriterion();

            Assert.IsTrue(criterion.BetterThan(3.5M, 2.2M));
            Assert.IsFalse(criterion.BetterThan(1.5M, 2.7M));
        }
        public void WithOneTrade()
        {
            MockTimeSeries series = new MockTimeSeries(100, 95, 95, 100, 90, 95, 80, 120);
            Trade          trade  = new Trade(Order.buyAt(0, series), Order.sellAt(1, series));

            RewardRiskRatioCriterion ratioCriterion = new RewardRiskRatioCriterion();

            Assert.AreEqual((95M / 100) / ((1M - 0.95M)), ratioCriterion.Calculate(series, trade));
        }
        public void WithOneTrade()
        {
            var trade = new Trade(Order.BuyAt(0), Order.SellAt(1));

            var series = GenerateTimeSeries.From(100, 95, 95, 100, 90, 95, 80, 120);

            var ratioCriterion = new RewardRiskRatioCriterion();

            Assert.AreEqual((95d / 100) / ((1d - 0.95d)), TaTestsUtils.TaOffset, ratioCriterion.Calculate(series, trade));
        }
Example #5
0
        public void ToStringMethod()
        {
            AbstractAnalysisCriterion c1 = new AverageProfitCriterion();

            Assert.AreEqual("Average Profit", c1.ToString());
            AbstractAnalysisCriterion c2 = new BuyAndHoldCriterion();

            Assert.AreEqual("Buy And Hold", c2.ToString());
            AbstractAnalysisCriterion c3 = new RewardRiskRatioCriterion();

            Assert.AreEqual("Reward Risk Ratio", c3.ToString());
        }
 public void SetUp()
 {
     _rrc = new RewardRiskRatioCriterion();
 }
Example #7
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!
        }