示例#1
0
        private void HandleBackTestingChange(object sender, PrePostEventArgs e)
        {
            // If back testing is about to be enabled...
            if (!BackTesting && e.Before)
            {
                // Turn off the update thread for each exchange
                foreach (var exch in Exchanges)
                {
                    exch.UpdateThreadActive = false;
                }

                // Turn off the update thread for each price data instance
                foreach (var pd in PriceData)
                {
                    pd.UpdateThreadActive = false;
                }

                // Deactivate all live trading bots (actually, deactivate all of them)
                Bots.Clear();

                // Integrate market updates so that updates from live data don't end up in back testing data.
                IntegrateDataUpdates();

                // Save and reset the current Fund container
                Funds.SaveToSettings(Exchanges);
                Funds.AssignFunds(new FundData[0]);

                // Save and reset the indicator container
                Indicators.Save();
                Indicators.Clear();

                // Disable live trading
                AllowTrades = false;
            }

            // If back testing has just been enabled...
            if (BackTesting && e.After)
            {
                // Create the fund container from the back testing settings
                Funds.AssignFunds(SettingsData.Settings.BackTesting.TestFunds);

                // Create the back testing bots
                Bots.LoadFromSettings();

                // Reset the balance collections in each exchange
                foreach (var exch in Exchanges)
                {
                    exch.Balance.Clear();
                }

                // Create the simulation manager
                Simulation = new Simulation(TradingExchanges, PriceData, Bots);

                // Enable backtesting bots based on their settings
                foreach (var bot in Bots.Where(x => x.BackTesting))
                {
                    bot.Active = bot.BotData.Active;
                }

                // Restore the backtesting indicators
                Indicators.Load();
            }

            // If back testing is about to be disabled...
            if (BackTesting && e.Before)
            {
                // Deactivate all back testing bots (actually, deactivate all of them)
                Bots.Clear();

                // Remove (without saving) the current fund container
                Funds.AssignFunds(new FundData[0]);

                // Save and reset the indicator container
                Indicators.Save();
                Indicators.Clear();
            }

            // If back testing has just been disabled...
            if (!BackTesting && e.After)
            {
                // Clean up the simulation
                Simulation = null;

                // Restore the fund container from settings
                Funds.AssignFunds(SettingsData.Settings.LiveFunds);

                // Restore the indicators
                Indicators.Load();

                // Restore the live trading bots
                Bots.LoadFromSettings();

                // Reset the balance collections in each exchange
                foreach (var exch in Exchanges)
                {
                    exch.Balance.Clear();
                }

                // Turn on the update thread for each price data instance
                foreach (var pd in PriceData)
                {
                    pd.UpdateThreadActive = pd.RefCount != 0;
                }

                // Turn on the update thread for each exchange
                foreach (var exch in Exchanges)
                {
                    exch.UpdateThreadActive = exch.Enabled;
                }

                // Enable live trading bots based on their settings
                foreach (var bot in Bots.Where(x => !x.BackTesting))
                {
                    bot.Active = bot.BotData.Active;
                }
            }
        }