Ejemplo n.º 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) ;
        }
Ejemplo n.º 2
0
 public void ObservedQuotes_WrongObservationIndex_Throws(SyncTickers sut, int observationIndex)
 {
     Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(0, observationIndex));
 }
Ejemplo n.º 3
0
 public void ObservedQuotes_WrongTickerIndex_Throws(SyncTickers sut, int tickerIndex)
 {
     Assert.Throws<ArgumentException>(() => sut.ObservedQuotes(tickerIndex, 0));
 }
Ejemplo n.º 4
0
 public void LastObservedQuote_ReturnsCorrectData(
     SyncTickers sut,
     int tickerIndex,
     int observationIndex,
     IReadOnlyList<ISimpleTickerQuote> expected)
 {
     Assert.Equal<ISimpleTickerQuote>(expected.LastOrDefault(), sut.LastObservedQuote(tickerIndex, observationIndex));
 }
Ejemplo n.º 5
0
 public void ObservedQuotes_ReturnsCorrectData(
     SyncTickers sut,
     int tickerIndex,
     int observationIndex,
     IReadOnlyList<ISimpleTickerQuote> expected)
 {
     Assert.True(expected.SequenceEqual(sut.ObservedQuotes(tickerIndex, observationIndex)));
 }
Ejemplo n.º 6
0
 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.");
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 8
0
        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));
        }
Ejemplo n.º 9
0
 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.");
 }
Ejemplo n.º 10
0
 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;
 }
Ejemplo n.º 11
0
 public static double ExpositionToTicker(this SimulationResultQuote quote, int tickerIndex, SyncTickers syncTickers)
 {
     return quote.TickerEquity(tickerIndex, syncTickers) / quote.Equity(syncTickers);
 }
Ejemplo n.º 12
0
 public static double Equity(this SimulationResultQuote quote, SyncTickers syncTickers)
 {
     return quote.Cash + Enumerable.Range(0, syncTickers.TickerCount).Select(iT => quote.TickerEquity(iT, syncTickers)).Sum();
 }
Ejemplo n.º 13
0
 public QuoteContext(SyncTickers syncTickers)
 {
     this.syncTickers = syncTickers;
     this.currentObservationIndex = -1;
 }
Ejemplo n.º 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();
        }