public void Fill_RegularLiquidity() { SimBroker broker = new SimBroker(); broker.BrokerModel = _trans; ForexSecurity tsec = new ForexSecurity(S); tsec.LotSize = 1; tsec.OrderStepSize = 1; OrderImpl limitBuy = new OrderImpl(tsec, Direction.Long, 1, 133m); OrderImpl limitSell = new OrderImpl(tsec, Direction.Short, 1, 133.5m); OrderImpl stopBuy = new OrderImpl(tsec, Direction.Long, 3, 0, 135.70m); OrderImpl stopSell = new OrderImpl(tsec, Direction.Short, 4, 0, 135.75m); PendingOrderImpl plimitBuy = new PendingOrderImpl(limitBuy); PendingOrderImpl plimitSell = new PendingOrderImpl(limitSell); PendingOrderImpl pstopBuy = new PendingOrderImpl(stopBuy); PendingOrderImpl pstopSell = new PendingOrderImpl(stopSell); broker.SendOrderStatus(plimitBuy); broker.SendOrderStatus(plimitSell); broker.SendOrderStatus(pstopBuy); broker.SendOrderStatus(pstopSell); // OHLC for 6/21/2012 on SPY TickImpl openingTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(9, 30, 00, 000), 135.67m, 10670270, "NYS"); TickImpl endMornTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(12, 00, 00, 000), 135.78m, 10670270, "NYS"); TickImpl endLunchTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(14, 15, 00, 000), 132.33m, 10670270, "NYS"); TickImpl closingTick = TickImpl.NewTrade(S, Util.ToQLDate(DateTime.Now), Util.QL2FT(16, 00, 00, 000), 132.44m, 10670270, "NYS"); broker.Execute(openingTick); broker.Execute(endMornTick); broker.Execute(endLunchTick); broker.Execute(closingTick); List <Trade> trades = broker.GetTradeList(); Assert.True(trades.Count == 4); foreach (Trade trade in trades) { if (trade.Xsize == 1) { Assert.Equal(132.33m, trade.Xprice); } else if (trade.Xsize == 2) { Assert.Equal(132.33m, trade.Xprice); } else if (trade.Xsize == 3) { Assert.Equal(135.78m, trade.Xprice); } else if (trade.Xsize == 4) { Assert.Equal(135.78m, trade.Xprice); } } }
void runopt() { isRunning = true; while (isNextAvail) { gauntletArgs ga = new gauntletArgs(DLL, RNAME, SIM); ga.value = NextParam; ga.id = id; Response r = ResponseLoader.FromDLL(ga.response, ga.dll); if (changeVars(ref r, ga.value)) { //GauntletEngine ge = new GauntletEngine(r, ga.hsim); //Thread.SpinWait(1000); //debugControl1.GotDebug("Starting a Gauntlet instance"); debug("Started Gauntlet Engine: " + ga.id); Go(); List <Trade> trades = SimBroker.GetTradeList(); Results results = Results.ResultsFromTradeList(trades, 0, .01m, new DebugDelegate(debug)); GauntletOptimizationResults gor = new GauntletOptimizationResults(); gor.parameter = ga.value; gor.results = results; gor.resultsCalculated = true; allResults.Add(gor); decimal rv = 0; getresult(results, out rv); debug("optimize " + ga.id + " finished. Used: " + gor.parameter + " -> " + OptimizeDecisionsName + ": " + rv + " trades: " + gor.results.Trades + " SimsRemaining: " + OptimizeRemain); progress(id, OptimizeCount); } else { debug("Unable to start gauntlet engine: " + ga.id + " with: " + ga.value); } } debug("All optimization runs complete, computing optimum..."); var sorted = calculateBest(allResults); // unbind UnbindEvents(); if (SendOptimizationCompleteEvent != null) { SendOptimizationCompleteEvent(sorted); } isRunning = false; }
public SimpleBacktester(PortfolioManager nsystem, string folder, int cpunr, string[] filter = null) { try { //Set start time Started = DateTime.Now; var transactioncosts = new GenericBrokerModel() { CommPerLot = 0M, SlippageInPips = 0M, LatencyInMS = 0, SpreadInPips = 0M }; SimBroker = new SimBroker((SimAccount)nsystem.Account, transactioncosts); //Initialize nsystem.Initialize(); foreach (var stream in nsystem.Streams) { ((OHLCBarStream)stream.Value).Initialize(); } //set response portfolio = nsystem; //Set progress if (OnProgress != null) { OnProgress(this, "Starting...", cpunr); } //Set simulator if (filter == null || nsystem.Streams.Count == 1) { histsim = new SingleSimImpl(filter); } else { histsim = new MultiSimImpl(filter); } //Set events histsim.GotTick += histsim_GotTick; SimBroker.GotFill += SimBroker_GotFill; SimBroker.GotOrder += SimBroker_GotOrder; SimBroker.GotOrderCancel += SimBroker_GotOrderCancel; SimBroker.GotOrderUpdate += SimBroker_GotOrderChanged; BindPortfolioEvents(); //Start simulation histsim.PlayTo(SingleSimImpl.Endsim); //Notify Complete if (OnProgress != null) { OnProgress(this, "Finished!", cpunr); } Stopped = DateTime.Now; if (OnMessage != null) { OnMessage(this, string.Format("SystemID: {0} - CPU: {1} - Finished in: {2} seconds - Tick p/s: {3} - Trades - {4}", 0, cpunr, Math.Round((Stopped - Started).TotalSeconds, 2), Math.Round(TicksSecond), SimBroker.GetTradeList().Count )); } Thread.Sleep(1000); WriteResults(); } catch (Exception exc) { if (OnProgress != null) { OnProgress(this, "FAILED: Exception!", cpunr); } if (OnMessage != null) { OnMessage(this, string.Format("Error (cpu {0}) Message: {1} Stacktrace: {2}", cpunr, exc.Message, exc.StackTrace)); } } }