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) ; }
public void ObservedQuotes_WrongObservationIndex_Throws(SyncTickers sut, int observationIndex) { Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(0, observationIndex)); }
public void ObservedQuotes_WrongTickerIndex_Throws(SyncTickers sut, int tickerIndex) { Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(tickerIndex, 0)); }
public void LastObservedQuote_ReturnsCorrectData( SyncTickers sut, int tickerIndex, int observationIndex, IReadOnlyList<ISimpleTickerQuote> expected) { Assert.Equal<ISimpleTickerQuote>(expected.LastOrDefault(), sut.LastObservedQuote(tickerIndex, observationIndex)); }
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."); }
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(); }
public QuoteContext(SyncTickers syncTickers) { this.syncTickers = syncTickers; this.currentObservationIndex = -1; }
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(); }