Exemple #1
0
        public void RunAsync(SyncTickers syncTickers, StrategyBase strategy, ISimulationSettings settings)
        {
            if (syncTickers == null) throw new ArgumentNullException("syncTickers");
            if (strategy == null) throw new ArgumentNullException("strategy");
            if (settings == null) throw new ArgumentNullException("settings");

            if (IsBusy) throw new InvalidOperationException("Already running.");

            var asyncSimulationAlgorithm = new SimulationContextAsyncDecorator(SimulationContextFactory.CreateContext());
            cancelSimulation = () => asyncSimulationAlgorithm.Cancel();

            simulationTask = Task.Factory.StartNew(() =>
            {
                using (asyncSimulationAlgorithm)
                {
                    var runResult = base.SimulationAlgorithmFlow(asyncSimulationAlgorithm, syncTickers, strategy, settings);
                    lock (cancelRaceConditionSync) { cancelSimulation = delegate { }; }
                    EndInfo = new SimulationEndEventArgs
                    {
                        Error = runResult.Error,
                        Result = runResult.Value,
                        EndReason =
                            runResult.Error != null ? SimulationEndReason.Error :
                            runResult.Value != null ? SimulationEndReason.Completion :
                            SimulationEndReason.Cancellation
                    };
                    SimulationEnded(this, EndInfo);
                }
            });

            //while (simulationTask.Status < TaskStatus.Running) ;
        }
Exemple #2
0
 public void ObservedQuotes_WrongObservationIndex_Throws(SyncTickers sut, int observationIndex)
 {
     Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(0, observationIndex));
 }
Exemple #3
0
 public void ObservedQuotes_WrongTickerIndex_Throws(SyncTickers sut, int tickerIndex)
 {
     Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(tickerIndex, 0));
 }
Exemple #4
0
 public void LastObservedQuote_ReturnsCorrectData(
     SyncTickers sut,
     int tickerIndex,
     int observationIndex,
     IReadOnlyList<ISimpleTickerQuote> expected)
 {
     Assert.Equal<ISimpleTickerQuote>(expected.LastOrDefault(), sut.LastObservedQuote(tickerIndex, observationIndex));
 }
Exemple #5
0
 public void ObservedQuotes_ReturnsCorrectData(
     SyncTickers sut,
     int tickerIndex,
     int observationIndex,
     IReadOnlyList<ISimpleTickerQuote> expected)
 {
     Assert.True(expected.SequenceEqual(sut.ObservedQuotes(tickerIndex, observationIndex)));
 }
 public void Synchronize_DescriptionsAreCorrect(IReadOnlyList<Ticker> tickers, SimulationSettings settings, SyncTickers expected, string testCase)
 {
     var sut = SyncTickersFactory.Synchronize(tickers, settings);
     Assert.True(expected.Descriptions.SequenceEqual(sut.Descriptions, new TickerDescriptionTests.TickerDescriptionComparer()),
         "Ticker descriptions are not correct.");
 }
Exemple #7
0
        public void ctor_SimplifiedTickers_AreSet()
        {
            var tickers = Ext.ROL(SimplifiedTickerTests.CreateTicker(0));
            var sut = new SyncTickers(
                tickers,
                Ext.ROL(ObservationTests.CreateObservation(DateTime.Now, 0)),
                Ext.ROL(new TickerDescription { Name = "X" }));

            Assert.Same(tickers, sut.SimplifiedTickers);
        }
        public void Synchronize_ObservationsAreCorrect(IReadOnlyList<Ticker> tickers, SimulationSettings settings, SyncTickers expected, string testCase)
        {
            Func<IEnumerable<Observation>, string> printer = x => x.Select((o, iO) => string.Format("[{0}: ({2}) {1}]", iO, o.Date,
                    o.CurrentQuoteCount.Select(i => i.ToString()).Aggregate((s1, s2) => s1 + "," + s2))).Aggregate((s1, s2) => s1 + "," + s2);

            var sut = SyncTickersFactory.Synchronize(tickers, settings);
            Assert.True(expected.SequenceEqual(sut, new ObservationTests.ObservationComparer()),
                "Observation are not correct."+
                " Expected: "+printer(expected)+
                ". Got: "+printer(sut));
        }
 public void Synchronize_TickersAreCorrect(IReadOnlyList<Ticker> tickers, SimulationSettings settings, SyncTickers expected, string testCase)
 {
     var sut = SyncTickersFactory.Synchronize(tickers, settings);
     Assert.True(expected.SimplifiedTickers.SequenceEqual(sut.SimplifiedTickers, new SimplifiedTickerTests.SimplifiedTickerComparer()),
         "Simplified tickers are not correct.");
 }
 public static double TickerEquity(this SimulationResultQuote quote, int tickerIndex, SyncTickers syncTickers)
 {
     return quote.TickerQuantity[tickerIndex] == 0 ?
         0
         :
         quote.TickerQuantity[tickerIndex]
             * syncTickers.SimplifiedTickers[tickerIndex][quote.Observation.CurrentQuoteCount[tickerIndex] - 1].Value;
 }
 public static double ExpositionToTicker(this SimulationResultQuote quote, int tickerIndex, SyncTickers syncTickers)
 {
     return quote.TickerEquity(tickerIndex, syncTickers) / quote.Equity(syncTickers);
 }
 public static double Equity(this SimulationResultQuote quote, SyncTickers syncTickers)
 {
     return quote.Cash + Enumerable.Range(0, syncTickers.TickerCount).Select(iT => quote.TickerEquity(iT, syncTickers)).Sum();
 }
Exemple #13
0
 public QuoteContext(SyncTickers syncTickers)
 {
     this.syncTickers = syncTickers;
     this.currentObservationIndex = -1;
 }
Exemple #14
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();
        }