Exemple #1
0
        public static void Main(string[] args)
        {
            // Splitting the series into slices
            TimeSeries         series    = CsvTradesLoader.loadBitstampSeries();
            IList <TimeSeries> subseries = series.split(Period.hours(6), Period.weeks(1));

            // Building the map of strategies
            IDictionary <Strategy, string> strategies = buildStrategiesMap(series);

            // The analysis criterion
            AnalysisCriterion profitCriterion = new TotalProfitCriterion();

            foreach (TimeSeries slice in subseries)
            {
                // For each sub-series...
                Console.WriteLine("Sub-series: " + slice.SeriesPeriodDescription);
                foreach (KeyValuePair <Strategy, string> entry in strategies.SetOfKeyValuePairs())
                {
                    Strategy strategy = entry.Key;
                    string   name     = entry.Value;
                    // For each strategy...
                    TradingRecord tradingRecord = slice.run(strategy);
                    double        profit        = profitCriterion.calculate(slice, tradingRecord);
                    Console.WriteLine("\tProfit for " + name + ": " + profit);
                }
                Strategy bestStrategy = profitCriterion.chooseBest(slice, new List <Strategy>(strategies.Keys));
                Console.WriteLine("\t\t--> Best strategy: " + strategies[bestStrategy] + "\n");
            }
        }
Exemple #2
0
        public void BetterThan()
        {
            IAnalysisCriterion criterion = new TotalProfitCriterion();

            Assert.IsTrue(criterion.BetterThan(2.0, 1.5));
            Assert.IsFalse(criterion.BetterThan(1.5, 2.0));
        }
Exemple #3
0
 /// <summary>
 /// Constructor.
 /// (a * x + b) </summary>
 /// <param name="initialAmount"> the initially traded amount </param>
 /// <param name="a"> the a coefficient (e.g. 0.005 for 0.5% per <seealso cref="Order"/>) </param>
 /// <param name="b"> the b constant (e.g. 0.2 for $0.2 per <seealso cref="Order"/>) </param>
 public LinearTransactionCostCriterion(double initialAmount, double a, double b)
 {
     _initialAmount = initialAmount;
     _a             = a;
     _b             = b;
     _profit        = new TotalProfitCriterion();
 }
        public void BetterThan()
        {
            AbstractAnalysisCriterion criterion = new TotalProfitCriterion();

            Assert.IsTrue(criterion.BetterThan(2.0M, 1.5M));
            Assert.IsFalse(criterion.BetterThan(1.5M, 2.0M));
        }
Exemple #5
0
        public void bestShouldBeBuyAndHoldOnLoss()
        {
            MockTimeSeries    series       = new MockTimeSeries(6.0M, 3.0M, 6.0M, 6.0M);
            TimeSeriesManager manager      = new TimeSeriesManager(series);
            IStrategy         bestStrategy = new TotalProfitCriterion().ChooseBest(manager, strategies);

            Assert.AreEqual(buyAndHoldStrategy, bestStrategy);
        }
Exemple #6
0
        public void bestShouldBeAlwaysOperateOnProfit()
        {
            MockTimeSeries    series       = new MockTimeSeries(6.0M, 9.0M, 6.0M, 6.0M);
            TimeSeriesManager manager      = new TimeSeriesManager(series);
            IStrategy         bestStrategy = new TotalProfitCriterion().ChooseBest(manager, strategies);

            Assert.AreEqual(alwaysStrategy, bestStrategy);
        }
        public void CalculateWithNoTradesShouldReturn1()
        {
            MockTimeSeries series = new MockTimeSeries(100, 95, 100, 80, 85, 70);

            AbstractAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual(1M, profit.Calculate(series, new BaseTradingRecord()));
        }
Exemple #8
0
        public void CalculateWithNoTradesShouldReturn1()
        {
            var series = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);

            IAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual(1d, profit.Calculate(series, new TradingRecord()), TaTestsUtils.TaOffset);
        }
Exemple #9
0
        public void CalculateProfitWithTradesThatStartSelling()
        {
            var series        = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);
            var tradingRecord = new TradingRecord(Order.SellAt(0), Order.BuyAt(1), Order.SellAt(2), Order.BuyAt(5));

            IAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual((1 / 0.95) * (1 / 0.7), profit.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
Exemple #10
0
        public void CalculateOnlyWithGainTrades()
        {
            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 profit = new TotalProfitCriterion();

            Assert.AreEqual(1.10 * 1.05, profit.Calculate(series, tradingRecord), TaTestsUtils.TaOffset);
        }
        public void CalculateWithOpenedTradeShouldReturn1()
        {
            MockTimeSeries            series = new MockTimeSeries(100, 95, 100, 80, 85, 70);
            AbstractAnalysisCriterion profit = new TotalProfitCriterion();
            Trade trade = new Trade();

            Assert.AreEqual(1M, profit.Calculate(series, trade));
            trade.Operate(0);
            Assert.AreEqual(1M, profit.Calculate(series, trade));
        }
Exemple #12
0
        public void CalculateWithOpenedTradeShouldReturn1()
        {
            var series = GenerateTimeSeries.From(100, 95, 100, 80, 85, 70);
            IAnalysisCriterion profit = new TotalProfitCriterion();
            var trade = new Trade();

            Assert.AreEqual(1d, profit.Calculate(series, trade), TaTestsUtils.TaOffset);
            trade.Operate(0);
            Assert.AreEqual(1d, profit.Calculate(series, trade), TaTestsUtils.TaOffset);
        }
        public void CalculateProfitWithTradesThatStartSelling()
        {
            MockTimeSeries series        = new MockTimeSeries(100, 95, 100, 80, 85, 70);
            ITradingRecord tradingRecord = new BaseTradingRecord(
                Order.sellAt(0, series), Order.buyAt(1, series),
                Order.sellAt(2, series), Order.buyAt(5, series));

            AbstractAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual((1M / 0.95M) * (1M / 0.7M), profit.Calculate(series, tradingRecord));
        }
        public void CalculateOnlyWithLossTrades()
        {
            MockTimeSeries series        = new MockTimeSeries(100, 95, 100, 80, 85, 70);
            ITradingRecord tradingRecord = new BaseTradingRecord(
                Order.buyAt(0, series), Order.sellAt(1, series),
                Order.buyAt(2, series), Order.sellAt(5, series));

            AbstractAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual(0.95M * 0.7M, profit.Calculate(series, tradingRecord));
        }
        public void CalculateOnlyWithGainTrades()
        {
            MockTimeSeries series        = new MockTimeSeries(100, 105, 110, 100, 95, 105);
            ITradingRecord tradingRecord = new BaseTradingRecord(
                Order.buyAt(0, series), Order.sellAt(2, series),
                Order.buyAt(3, series), Order.sellAt(5, series));

            AbstractAnalysisCriterion profit = new TotalProfitCriterion();

            Assert.AreEqual(1.10M * 1.05M, profit.Calculate(series, tradingRecord));
        }
Exemple #16
0
        private void OutputReport(Strategy strategy, OrderType type, bool showTrades)
        {
            if (type.Equals(OrderType.Sell))
            {
                Console.WriteLine("RUN STRATEGY SHORT:");
            }
            else
            {
                Console.WriteLine("RUN STRATEGY LONG:");
            }

            // Running the strategy
            var tradingRecord = _series.Run(strategy, type, Decimal.Thousand);

            // Total profit
            var totalProfit = new TotalProfitCriterion();

            Console.WriteLine("Total profit: " + totalProfit.Calculate(_series, tradingRecord));
            // Number of ticks
            Console.WriteLine("Number of ticks: " + (new NumberOfTicksCriterion()).Calculate(_series, tradingRecord));
            // Average profit (per tick)
            Console.WriteLine("Average profit (per tick): " + (new AverageProfitCriterion()).Calculate(_series, tradingRecord));
            // Number of trades
            Console.WriteLine("Number of trades: " + (new NumberOfTradesCriterion()).Calculate(_series, tradingRecord));
            // Profitable trades ratio
            Console.WriteLine("Profitable trades ratio: " + (new AverageProfitableTradesCriterion()).Calculate(_series, tradingRecord));
            // Maximum drawdown
            Console.WriteLine("Maximum drawdown: " + (new MaximumDrawdownCriterion()).Calculate(_series, tradingRecord));
            // Reward-risk ratio
            Console.WriteLine("Reward-risk ratio: " + (new RewardRiskRatioCriterion()).Calculate(_series, tradingRecord));
            // Total transaction cost
            Console.WriteLine("Total transaction cost (from $1000): " + (new LinearTransactionCostCriterion(1000, 0.005)).Calculate(_series, tradingRecord));
            // Buy-and-hold
            Console.WriteLine("Buy-and-hold: " + (new BuyAndHoldCriterion()).Calculate(_series, tradingRecord));
            // Total profit vs buy-and-hold
            Console.WriteLine("Custom strategy profit vs buy-and-hold strategy profit: " + (new VersusBuyAndHoldCriterion(totalProfit)).Calculate(_series, tradingRecord));
        }