示例#1
0
        public TransactionProcessor(
            IQuoteContext quoteContext,
            Account account,
            ISimulationSettings settings)
        {
            this.quoteContext = quoteContext;
            this.account = account;

            this.delay = settings.Get<TradeDelaySetting>();
            this.commission = settings.Get<CommissionSetting>();

            this.avgPrice = new double[quoteContext.TickerCount];
        }
示例#2
0
            public MultiTickerSynchronizer(IReadOnlyList<Ticker> tickers, ISimulationSettings settings)
            {
                this.tickers = tickers;
                this.settings = settings;

                this.stepSpan = settings.Get<PeriodSetting>().Type.ToTimeSpan();

                var range = settings.Get<SimulationRangeSetting>();
                if (range.Type == SimulationRangeType.Common)
                {
                    this.from = tickers.Select(t => t.From).Max();
                    this.to = tickers.Select(t => t.To).Min();
                }
                else
                {
                    this.from = Max(range.From.Date, tickers.Select(t => t.From).Min());
                    this.to = range.To.Date + (TimeSpan.FromDays(1) - TimeSpan.FromTicks(1));
                }

                //currentDate = this.from;
            }
示例#3
0
        public static bool CanSynchronize(IReadOnlyList<Ticker> tickers, ISimulationSettings settings, out string error)
        {
            if (tickers == null) throw new ArgumentNullException("tickers");
            if (settings == null) throw new ArgumentNullException("settings");

            if (tickers.Count == 0)
            {
                error = "There must be least one ticker.";
                return false;
            }

            if (tickers.Select(t => t.Name.ToLowerInvariant()).GroupBy(name => name).Count() < tickers.Count)
            {
                error = "Ticker names must be unique.";
                return false;
            }

            if (settings.Get<SimulationRangeSetting>().Type == SimulationRangeType.Common &&
                tickers.Select(t => t.From).Max() > tickers.Select(t => t.To).Min())
            {
                error = "Tickers do not have common range.";
                return false;
            }

            if (settings.Get<CommissionSetting>().Value < 0)
            {
                error = "Commission cannot be negative.";
                return false;
            }

            if (settings.Get<CommissionSetting>().Type == CommissionType.Percent &&
                settings.Get<CommissionSetting>().Value >= 100)
            {
                error = "Commission of percentage type must be smaller than 100 %.";
                return false;
            }

            if (settings.Get<SimulationRangeSetting>().Type == SimulationRangeType.FromToDates &&
                settings.Get<SimulationRangeSetting>().From > settings.Get<SimulationRangeSetting>().To)
            {
                error = "Simulation range is invalid.";
                return false;
            }

            if (settings.Get<InitialEquitySetting>().Value < 1)
            {
                error = "Initial equity cannot be smaller than 1.";
                return false;
            }

            if (settings.Get<SimulationRangeSetting>().Type == SimulationRangeType.FromToDates &&
                tickers.All(t => t.From > settings.Get<SimulationRangeSetting>().To || t.To < settings.Get<SimulationRangeSetting>().From))
            {
                error = "No quotes matching specified simulation time range.";
                return false;
            }

            error = null;
            return true;
        }
示例#4
0
        bool ISimulationContext.Initialize(SyncTickers syncTickers, StrategyBase strategy, ISimulationSettings settings)
        {
            this.strategy = strategy;
            this.settings = settings;
            this.syncTickers = syncTickers;
            this.account = new Account(settings.Get<InitialEquitySetting>().Value);
            this.strategy.QuoteContext = this.quoteContext = new QuoteContext(syncTickers);
            this.transactionProcessor = new TransactionProcessor(quoteContext, account, settings);
            this.strategy.WalletContext = this.walletContext = new WalletContext(quoteContext, account, transactionProcessor);

            this.resultQuotes = new List<SimulationResultQuote>();

            return strategy.Initialize();
        }