예제 #1
0
        /// <summary>
        /// Callback method for Launch MA Test button.
        /// Launches the MA strategy backtest.
        /// </summary>
        /// <param name="control">Exposes method to ribbon</param>
        public void TestMovingAverage(CustomUI.IRibbonControl control)
        {
            #region TestMovingAverage sanity checks
            // --> On imported data
            var importer = DataImporter.DataImporter.Instance;
            List <DataTypes.Quote> data = importer.GetData();
            if (data == null)
            {
                MessageBox.Show("Please import data before lauching test");
                return;
            }
            // --> On strategy parameters
            if (this._maAmount == null)
            {
                MessageBox.Show("Please input amount to invest in strategy");
                return;
            }
            if (this._maLongLevel == null)
            {
                MessageBox.Show("Please input Moving Average Long Level in strategy");
                return;
            }
            if (this._maShortLevel == null)
            {
                MessageBox.Show("Please input Moving Average Short Level parameter");
                return;
            }
            if (this._maTakeProfitInBps == null)
            {
                MessageBox.Show("Please input Moving Average Take Profit parameter");
                return;
            }
            #endregion

            // Compute the backtest and get the results
            var strategy = new TradeStrategyLib.Models.MAStrategy((int)this._maShortLevel, (int)this._maLongLevel,
                                                                  (double)this._maAmount, (double)this._maTakeProfitInBps);
            var backtest = new StrategyBacktester(strategy, data);
            backtest.Compute();

            List <double> pnlHistory = backtest.GetPnLHistory();
            if (!pnlHistory.Any())
            {
                MessageBox.Show("Moving average strategy did not generate any trades on this" +
                                " time interval");
                return;
            }

            List <DateTime> dates    = backtest.GetDates();
            double          totalPnl = backtest.GetTotalPnl();
            double?         maxDD    = backtest.GetMaximumDrawdown();
            double          vol      = backtest.GetStrategyVol();

            // Write the results
            DataWriter.WriteBacktestResults("Moving Average", totalPnl, maxDD, vol, pnlHistory, dates);

            // Ensure all COM objects are released
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public void TestMAStrategy()
        {
            var maStrategy = new TradeStrategyLib.Models.MAStrategy(25, 100, 1000000.00, 150);
            var backtest   = new StrategyBacktester(maStrategy, _data);

            backtest.Compute();

            var pnlHistory = backtest.GetPnLHistory();
            var totalPnl   = backtest.GetTotalPnl();
            var maxDD      = backtest.GetMaximumDrawdown();
            var vol        = backtest.GetStrategyVol();

            // Check if results are empty
            // NOTE FRZ: This test should be more rigorous with expected results to test
            // but I don't have the time.
            Assert.IsNotNull(pnlHistory);
            Assert.IsNotNull(totalPnl);
            Assert.IsNotNull(maxDD);
            Assert.IsNotNull(vol);
        }