Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PortfolioManager"/> class for running a backtest.
        /// </summary>
        /// <param name="portfolioimplementations">The portfolioimplementations.</param>
        /// <param name="simulation">The backtest.</param>
        public PortfolioManager(PortfolioImplementations portfolioimplementations, SimulationMessage simulation)
            : this(portfolioimplementations)
        {
            //Set initial message
            _initialMessageInstance = simulation;

            //Since this is a backtest request
            RunMode = RunMode.Backtester;

            //World clock is depended on data received
            var clock = new WorldClock(() => PortfolioImplementations.DataFeed.LastDataReceivedUtc == DateTime.MinValue ?
                                       simulation.StartDateTime :
                                       portfolioimplementations.DataFeed.LastDataReceivedUtc);

            //Get additional information
            if (!Enum.TryParse(simulation.AccountType, out AccountType accounttype))
            {
                throw new Exception($"Cannot initialize backtest account type {simulation.AccountType}");
            }
            if (!Enum.TryParse(simulation.BrokerType, out BrokerType brokertype))
            {
                throw new Exception($"Cannot initialize backtest broker type {simulation.BrokerType}");
            }
            if (!Enum.TryParse(simulation.BaseCurrency, out CurrencyType basecurrency))
            {
                throw new Exception($"Cannot initialize backtest base currency type {simulation.BaseCurrency}");
            }

            //Get latest currency rates, so we are up to date (trough forced reload)
            _log.Debug($"Initializing currency implementation: {PortfolioImplementations.Currency.GetType().FullName}");
            Config.LoadConfigFile <CurrencyRatesConfig[]>(Config.GlobalConfig.CurrencyRatesConfigFile, true);
            PortfolioImplementations.Currency.Initialize(clock, true);

            //Get broker model
            var brokermodel = BrokerModelFactory.GetBroker(accounttype, brokertype);

            //Check if the currency selected matches the currency of this broker (for instance when using crypto currencies)
            decimal allocatedfunds = simulation.QuantFund.AllocatedFunds;
            brokermodel.GetCompatibleInitialCapital(portfolioimplementations.Currency, ref basecurrency, ref allocatedfunds);
            simulation.QuantFund.AllocatedFunds = allocatedfunds;

            //Create portfolio
            _portfolio = CreatePortfolio(simulation.PortfolioId, Guid.NewGuid().ToString(), brokermodel,
                                         simulation.Leverage, basecurrency, basecurrency, clock, simulation.ExtendedMarketHours);

            //Set initial funds
            _portfolio.CashManager.AddCash(basecurrency, allocatedfunds);

            //Set initial fund message
            _initialFundMessage = simulation.QuantFund;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PortfolioManager"/> class for running a live trading instance.
        /// </summary>
        /// <param name="portfolioImplementations">The portfolio implementations.</param>
        /// <param name="livetrading">The livetrading.</param>
        public PortfolioManager(PortfolioImplementations portfolioImplementations, LiveTradingMessage livetrading)
            : this(portfolioImplementations)
        {
            //Set initial message
            _initialMessageInstance = livetrading;

            //Since this is a live trading request
            RunMode = RunMode.LiveTrading;

            //World clock is current time
            var clock = new WorldClock(() => DateTime.UtcNow);

            //Get additional information
            if (!Enum.TryParse(livetrading.AccountType, out AccountType accounttype))
            {
                throw new Exception($"Cannot initialize backtest account type {livetrading.AccountType}");
            }
            if (!Enum.TryParse(livetrading.BrokerType, out BrokerType brokertype))
            {
                throw new Exception($"Cannot initialize backtest broker type {livetrading.BrokerType}");
            }
            if (!Enum.TryParse(livetrading.BaseCurrency, out CurrencyType basecurrency))
            {
                throw new Exception($"Cannot initialize backtest base currency type {livetrading.BaseCurrency}");
            }
            if (!Enum.TryParse(livetrading.DisplayCurrency, out CurrencyType displaycurrency))
            {
                throw new Exception($"Cannot initialize backtest base currency type {livetrading.DisplayCurrency}");
            }

            //Get latest currency rates, so we are up to date (trough forced reload)
            _log.Debug($"Initializing currency implementation: {PortfolioImplementations.Currency.GetType().FullName}");
            Config.LoadConfigFile <CurrencyRatesConfig[]>(Config.GlobalConfig.CurrencyRatesConfigFile, true);
            PortfolioImplementations.Currency.Initialize(clock, true);

            //Get broker model
            var brokermodel = BrokerModelFactory.GetBroker(accounttype, brokertype);

            //Create portfolio
            _portfolio = CreatePortfolio(livetrading.PortfolioId, livetrading.AccountId, brokermodel,
                                         livetrading.Leverage, basecurrency, displaycurrency, clock,
                                         livetrading.ExtendedMarketHours);

            //Set initial fund message
            _initialFundMessage = livetrading.QuantFund;
        }