bool loadsim()
        {
            _historicalSimulator          = new MultiSimImpl(_historicalDataFiles.ToArray());
            _historicalSimulator.GotTick += historicalSimulator_GotTick;

            _broker                 = new Broker();
            _broker.GotOrder       += broker_GotOrder;
            _broker.GotFill        += broker_GotFill;
            _broker.UseBidAskFills  = _useBidAskFills;
            _broker.GotOrderCancel += broker_GotOrderCancel;

            try
            {
                status("Loaded tickdata: " + prettyTickDataFiles());
                return(true);
            }
            catch (IOException ex)
            {
                if (ex.Message.Contains("used by another process"))
                {
                    status("Simulation file still in use.");
                    debug("Try again, one of following in use: " + string.Join(",", _historicalDataFiles.ToArray()));
                }
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// returns true if sim event binding is needed
        /// </summary>
        /// <param name="sim"></param>
        /// <param name="ga"></param>
        bool prepsim_needsbind(ref HistSim sim, GauntArgs ga)
        {
            // see if filter or folder has changed
            string thistff   = TickFileFilter.Serialize(ga.Filter) + ga.Folder;
            bool   fullreset = lasttff != thistff;

            if ((sim == null) || fullreset)
            {
                if (_portfoliosim.Checked)
                {
                    if (_siminmemory.Checked)
                    {
                        sim = new HistSimMemory(ga.Folder, ga.Filter);
                    }
                    else
                    {
                        debug("Using portfolio simulation. (realistic)");
                        sim = new MultiSimImpl(ga.Folder, ga.Filter);
                    }
                }
                else
                {
                    debug("Using sequential symbol simulation. (faster)");
                    sim = new SingleSimImpl(ga.Folder, ga.Filter);
                }
                lasttff = thistff;
                return(true);
            }
            else
            {
                sim.Reset();
            }
            return(false);
        }
Ejemplo n.º 3
0
        public void BarPerformance()
        {
            HistSim h = new HistSim(Environment.CurrentDirectory + "\\");

            h.GotTick += new TradeLink.API.TickDelegate(h_GotTick);

            h.Initialize();

            tickcount = 0;
            lasttime  = 0;

            Assert.AreEqual(0, lasttime);
            Assert.Greater(h.TicksPresent, 0);
            if (Environment.ProcessorCount == 1)
            {
                EXPECTBARS *= 2.5;
            }

            DateTime start = DateTime.Now;

            h.PlayTo(HistSim.ENDSIM);

            double time = DateTime.Now.Subtract(start).TotalSeconds;

            Assert.GreaterOrEqual(tickcount, 50000);
            Assert.AreEqual(3, bt.SymbolCount);
            Assert.LessOrEqual(time, EXPECTBARS);
            Console.WriteLine("BarList runtime: " + time.ToString("N2") + "sec, versus: " + EXPECTBARS + "sec expected.");
            Console.WriteLine("BarList " + ((double)tickcount / time).ToString("N0") + " ticks/sec");
        }
Ejemplo n.º 4
0
 bool loadsim()
 {
     h                         = new MultiSimImpl(epffiles.ToArray());
     SimBroker                 = new Broker();
     SimBroker.GotOrder       += new OrderDelegate(broker_GotOrder);
     SimBroker.GotFill        += new FillDelegate(broker_GotFill);
     h.GotTick                += new TickDelegate(h_GotTick);
     SimBroker.UseBidAskFills  = Properties.Settings.Default.UseBidAskFills;
     SimBroker.GotOrderCancel += new OrderCancelDelegate(broker_GotOrderCancel);
     try
     {
         updatetitle();
         status("Loaded tickdata: " + PrettyEPF());
         return(true);
     }
     catch (IOException ex)
     {
         if (ex.Message.Contains("used by another process"))
         {
             status("Simulation file still in use.");
             debug("Try again, one of following in use: " + string.Join(",", epffiles.ToArray()));
         }
         return(false);
     }
 }
Ejemplo n.º 5
0
        private void playbut_Click(object sender, EventArgs e)
        {
            status("preparing simulation");
            if (_playback.IsBusy)
            {
                status("simulation already in progress");
                return;
            }

            // setup simulation (portfolio realistic)
            h = new MultiSimImpl(tickfolder, FileFilter);
            // bind events
            h.GotTick  += new TickDelegate(h_GotTick);
            h.GotDebug += new DebugDelegate(_dw.GotDebug);
            // setup playback
            _playback = new Playback(h);
            _playback.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_playback_RunWorkerCompleted);
            _playback.ProgressChanged    += new ProgressChangedEventHandler(_playback_ProgressChanged);


            // clear highs and lows
            highs = new Dictionary <string, decimal>();
            lows  = new Dictionary <string, decimal>();
            // start playback
            _playback.RunWorkerAsync(new PlayBackArgs((int)trackBar1.Value / 5));
            // notify user
            status("Playback started...");
            // update user interface options
            playbut.Enabled   = false;
            stopbut.Enabled   = true;
            trackBar1.Enabled = false;
        }
Ejemplo n.º 6
0
 void unbindsim(ref HistSim h)
 {
     if (h != null)
     {
         h.GotDebug -= new DebugDelegate(h_GotDebug);
         h.GotTick  -= new TickDelegate(h_GotTick);
     }
 }
Ejemplo n.º 7
0
 public Playback(HistSim simulator)
 {
     h = simulator;
     h.Reset();
     h.Initialize();
     WorkerSupportsCancellation = true;
     WorkerReportsProgress      = true;
 }
Ejemplo n.º 8
0
        public bool Start()
        {
            //ensure everything is ready to start the optimization
            if (!isValid)
            {
                debug("Invalid optimization, Must configure this optimization completely before re-starting.");
                status("Optimization not configured.");
                return(false);
            }
            SIM   = myhistsim;
            DLL   = Dll;
            RNAME = ResponseName;

            debug("Starting optimization, Queueing up all " + this.OptimizeCount + " combinations...");

            //queue up all the possibilities

            /*
             * List<List<string[]>> paramListList = new List<List<string[]>>();
             * foreach (OptimizationParam op in varListControl.Items)
             * {
             *  List<string[]> paramList = new List<string[]>();
             *  string testClass = (String)reslist.SelectedItem;
             *  string testName = op.name.ToString();
             *  decimal val = decimal.MinValue;
             *  decimal max = decimal.MaxValue;
             *  while (val < max)
             *  {
             *      if (val == decimal.MinValue) { val = op.low; max = op.high; }
             *      else val += op.step;
             *      paramList.Add(new string[] { testClass, testName, val.ToString() });
             *  }
             *  paramListList.Add(paramList);
             * }
             *
             * //now we have all the possibilities in a list
             * //we need to combine them into all possible combos
             *
             * //keep all possible combinations in a list
             * //each string[] is a parameter value
             * //each List<string[]> is a parameter set
             * List<List<string[]>> comboList = new List<List<string[]>>();
             *
             * foreach (List<string[]> paramList in paramListList)
             * {
             *  comboList = appendList(comboList, paramList);
             * }*/

            debug("All combinations queued, Starting Gauntlet Threads");


            var rh = RunHelper.run(runopt, null, debug, "runopt: " + this.ToString());

            status("Optimizaton started with " + OptimizeCount + " combinations.");
            return(rh.isStarted);
        }
        void initializeSim()
        {
            _backgroundWorker    = new BackgroundWorker();
            _historicalSimulator = new MultiSimImpl();
            _broker = new Broker();
            //_resultsModel = new ResultsModel();

            _broker.UseBidAskFills    = _useBidAskFills;
            _backgroundWorker.DoWork += play;
            _backgroundWorker.WorkerReportsProgress      = false;
            _backgroundWorker.WorkerSupportsCancellation = true;
            _backgroundWorker.RunWorkerCompleted        += PlayComplete;

            debug(Util.TLSIdentity());
            debug(RunTracker.CountNewGetPrettyRuns(_programName, Util.PROGRAM));
        }
Ejemplo n.º 10
0
        private bool loadfile(string path)
        {
            string f = path;

            if (isResponse(f))
            {
                responsedll = f;
                reslist.DropDownItems.Clear();
                List <string> l = Util.GetResponseList(responsedll);
                if (System.IO.File.Exists(f))
                {
                    if (!isRecent(f))
                    {
                        recent.DropDownItems.Add(f);
                    }
                }
                for (int i = 0; i < l.Count; i++)
                {
                    reslist.DropDownItems.Add(l[i]);
                }
                status("Found " + l.Count + " responses.  Please select one from Responses drop-down.");
                return(true);
            }
            else if (isEPF(f))
            {
                if (System.IO.File.Exists(f))
                {
                    if (!isRecent(f) && Util.SecurityFromFileName(f).isValid)
                    {
                        recent.DropDownItems.Add(f);
                    }
                }
                epffiles.Add(f);
                h = new HistSim(epffiles.ToArray());
                h.SimBroker.GotOrder       += new OrderDelegate(broker_GotOrder);
                h.SimBroker.GotFill        += new FillDelegate(broker_GotFill);
                h.GotTick                  += new TickDelegate(h_GotTick);
                h.SimBroker.GotOrderCancel += new OrderCancelDelegate(broker_GotOrderCancel);

                updatetitle();
                status("Loaded tickdata: " + PrettyEPF());
                return(true);
            }

            return(false);
        }
Ejemplo n.º 11
0
        public void RawPerformance()
        {
            HistSim h = new HistSim(Environment.CurrentDirectory + "\\");

            h.Initialize();
            h.GotTick += new TradeLink.API.TickDelegate(raw_GotTick);

            tickcount = 0;
            syms.Clear();
            lasttime = 0;

            Assert.AreEqual(0, tickcount);
            Assert.AreEqual(0, syms.Count);
            Assert.AreEqual(0, lasttime);
            Assert.Greater(h.TicksPresent, 0);
            if (Environment.ProcessorCount == 1)
            {
                EXPECTRAW *= 2.5;
            }

            DateTime start = DateTime.Now;

            h.PlayTo(HistSim.ENDSIM);


            double time = DateTime.Now.Subtract(start).TotalSeconds;

            // make sure ticks arrived in order
            Assert.IsTrue(GOODTIME, "Tick arrived out-of-order.");
            // check running time
            Assert.LessOrEqual(time, EXPECTRAW, "may fail on slow machines");
            Assert.AreEqual(3, syms.Count);
            // tick count is = 42610 (FTI) + 5001 (SPX) + 8041 (ABN)
            Assert.AreEqual(42610 + 4991 + 8041, tickcount);
            // variance from approximate count should be less than 1%
            Assert.Less((tickcount - h.TicksPresent) / h.TicksPresent, .01);
            // actual count should equal simulation count
            Assert.AreEqual(h.TicksProcessed, tickcount);
            // last time is 1649 on SPX
            Assert.AreEqual(20080318155843, lasttime);
            // printout simulation runtime
            Console.WriteLine("Raw runtime: " + time.ToString("N2") + "sec, versus: " + EXPECTRAW + "sec expected.");
            Console.WriteLine("Raw speed: " + ((double)tickcount / time).ToString("N0") + " ticks/sec");
        }
Ejemplo n.º 12
0
        // runs the simulation in background
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            // get simulation arguments
            GauntArgs ga = (GauntArgs)e.Argument;

            // notify user
            debug("Run started: " + ga.Name);
            // prepare simulator
            h           = new HistSim(ga.Folder, ga.Filter);
            h.GotDebug += new DebugDelegate(h_GotDebug);
            h.GotTick  += new TickDelegate(h_GotTick);
            // start simulation
            h.PlayTo(ga.PlayTo);
            // end simulation
            ga.Stopped        = DateTime.Now;
            ga.TicksProcessed = h.TicksProcessed;
            ga.Executions     = h.FillCount;
            // save result
            e.Result = ga;
        }
Ejemplo n.º 13
0
        public DarkLightSimBroker(IHub hub, BrokerConfigurationModel config)
            : base(hub, config)
        {
            //Plug into Tradelink
            _broker = new Broker();
            _broker.UseBidAskFills  = config.SimUseBidAskFills;
            _broker.GotFill        += PublishFill;
            _broker.GotOrder       += PublishOrderAck;
            _broker.GotOrderCancel += PublishOrderCancelAck;

            loadTickData(config.TickFiles);
            _historicalSimulator          = new MultiSimImpl(_historicalDataFiles.ToArray());
            _historicalSimulator.GotTick += PublishTick;

            //Set up tick player
            _backgroundWorker         = new BackgroundWorker();
            _backgroundWorker.DoWork += play;
            _backgroundWorker.WorkerReportsProgress      = false;
            _backgroundWorker.WorkerSupportsCancellation = true;
            _backgroundWorker.RunWorkerCompleted        += playComplete;

            _playTo = config.PlayToValue;
        }
Ejemplo n.º 14
0
        // runs the simulation in background
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            FillCount = 0;
            // get simulation arguments
            GauntArgs ga = (GauntArgs)e.Argument;

            // notify user
            debug("Run started: " + ga.Name);
            status("Started: " + ga.ResponseName);
            // prepare simulator
            bool portreal = _portfoliosim.Checked;

            if (_portfoliosim.Checked)
            {
                debug("Using portfolio simulation. (realistic)");
                h = new MultiSimImpl(ga.Folder, ga.Filter);
            }
            else
            {
                debug("Using sequential symbol simulation. (faster)");
                h = new SingleSimImpl(ga.Folder, ga.Filter);
            }
            h.GotDebug += new DebugDelegate(h_GotDebug);
            SimBroker.UseBidAskFills = _usebidask.Checked;
            h.GotTick                += new TickDelegate(h_GotTick);
            SimBroker.GotFill        += new FillDelegate(SimBroker_GotFill);
            SimBroker.GotOrder       += new OrderDelegate(args.Response.GotOrder);
            SimBroker.GotOrderCancel += new OrderCancelDelegate(SimBroker_GotOrderCancel);
            // start simulation
            h.PlayTo(ga.PlayTo);
            // end simulation
            ga.Stopped        = DateTime.Now;
            ga.TicksProcessed = h.TicksProcessed;
            ga.Executions     = FillCount;
            // save result
            e.Result = ga;
        }
Ejemplo n.º 15
0
 public Playback(HistSim simulator)
 {
     h = simulator;
     WorkerSupportsCancellation = true;
     WorkerReportsProgress      = true;
 }
Ejemplo n.º 16
0
 public Optimize(string dll, string response, HistSim sim) : base(ResponseLoader.FromDLL(response, dll), sim)
 {
     Dll          = dll;
     ResponseName = response;
 }
Ejemplo n.º 17
0
 public gauntletArgs(string _dll, string _response, HistSim h)
 {
     dll      = _dll;
     response = _response;
     hsim     = h;
 }
Ejemplo n.º 18
0
 void bindsim(ref HistSim h)
 {
     h.GotDebug += new DebugDelegate(h_GotDebug);
     h.GotTick  += new TickDelegate(h_GotTick);
 }
Ejemplo n.º 19
0
        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));
                }
            }
        }