示例#1
0
        public void ExecutionPerformance()
        {
            execute.GotTick           += new TradeLink.API.TickDelegate(execute_GotTick);
            execute.SimBroker.GotFill += new TradeLink.API.FillDelegate(SimBroker_GotFill);

            execute.Initialize();

            tickcount = 0;
            lasttime  = 0;

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

            DateTime start = DateTime.Now;

            execute.PlayTo(HistSim.ENDSIM);

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

            Assert.AreEqual(desiredfills, fillcount);
            Assert.LessOrEqual(time, EXPECTEX);
            Console.WriteLine("Execution runtime: " + time.ToString("N2") + "sec, versus: " + EXPECTEX + "sec expected.");
            Console.WriteLine("Execution " + ((double)tickcount / time).ToString("N0") + " ticks/sec.  " + ((double)fillcount / time).ToString("N0") + " fills/sec");
        }
示例#2
0
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            if (e.Cancel)
            {
                return;
            }
            PlayBackArgs args     = (PlayBackArgs)e.Argument;
            DateTime     prevtime = DateTime.MinValue;
            DateTime     playto   = h.NextTickTime;

            while (h.NextTickTime != HistSim.ENDSIM)
            {
                if (CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                // if we're past user specified DayStart, use user-specified delay otherwise no delay
                int delay = (h.NextTickTime.TimeOfDay >= args.DayStart.TimeOfDay) ?
                            (int)h.NextTickTime.Subtract(prevtime).TotalMilliseconds *args.DELAYSCALE : 0;
                if (prevtime != DateTime.MinValue)                     // if it's not first time doing this
                {
                    System.Threading.Thread.CurrentThread.Join(delay); // wait realistic time
                }
                prevtime = new DateTime(h.NextTickTime.Ticks);         // save last time mark
                double progress = 100.0 * ((h.TickCount + h.IndexCount) / (double)h.ApproxTotalTicks);
                ReportProgress((int)progress);                         // report progress
                h.PlayTo(h.NextTickTime);                              // this will throw tick/idx events and get next time mark
            }
            base.OnDoWork(e);
        }
示例#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");
        }
        void play(object sender, DoWorkEventArgs e)
        {
            PlayTo type = (PlayTo)e.Argument;

            if (e.Cancel)
            {
                return;
            }
            int  time = (int)(_historicalSimulator.NextTickTime % 100000);
            long date = (_historicalSimulator.NextTickTime / 100000) * 100000;
            int  t    = (int)type;
            long val  = 0;

            _playTo = type;
            switch (type)
            {
            case PlayTo.End:
                val = MultiSimImpl.ENDSIM;
                break;

            case PlayTo.FiveMin:
            case PlayTo.OneMin:
            case PlayTo.TenMin:
            case PlayTo.HalfHour:
                val = date + Util.FTADD(time, (t / 10) * 60);
                break;

            case PlayTo.Hour:
                val = date + Util.FTADD(time, (t / 1000) * 3600);
                break;

            case PlayTo.TwoHour:
                val = date + Util.FTADD(time, 2 * 60 * 60);
                break;

            case PlayTo.FourHour:
                val = date + Util.FTADD(time, 4 * 60 * 60);
                break;

            case PlayTo.OneSec:
            case PlayTo.ThirtySec:
                val = date + Util.FTADD(time, t);
                break;
                //case PlayTo.Custom:
                //    ctime = getcusttime();
                //    if (ctime == 0)
                //    {
                //        _playTo = PlayTo.OneSec;
                //        val = 0;
                //        status("Invalid custom time, playing to next second.");
                //    }
                //    else
                //        val = date + ctime;
                //    break;
            }
            _historicalSimulator.PlayTo(val);
        }
示例#5
0
        private void play(object sender, DoWorkEventArgs e)
        {
            PlayTo type = (PlayTo)e.Argument;

            if (e.Cancel)
            {
                return;
            }
            int  time = (int)(_historicalSimulator.NextTickTime % 100000);
            long date = (_historicalSimulator.NextTickTime / 100000) * 100000;
            int  t    = (int)type;
            long val  = 0;

            _playTo = type;
            switch (type)
            {
            case PlayTo.End:
                val = MultiSimImpl.ENDSIM;
                break;

            case PlayTo.FiveMin:
            case PlayTo.OneMin:
            case PlayTo.TenMin:
            case PlayTo.HalfHour:
                val = date + Util.FTADD(time, (t / 10) * 60);
                break;

            case PlayTo.Hour:
                val = date + Util.FTADD(time, (t / 1000) * 3600);
                break;

            case PlayTo.TwoHour:
                val = date + Util.FTADD(time, 2 * 60 * 60);
                break;

            case PlayTo.FourHour:
                val = date + Util.FTADD(time, 4 * 60 * 60);
                break;

            case PlayTo.OneSec:
            case PlayTo.ThirtySec:
                val = date + Util.FTADD(time, t);
                break;
            }

            _historicalSimulator.PlayTo(val);
        }
示例#6
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");
        }
示例#7
0
        void Play(object sender, DoWorkEventArgs e)
        {
            PlayTo type = (PlayTo)e.Argument;

            if (e.Cancel)
            {
                return;
            }
            int t = (int)type;

            h.Initialize();
            int  maxmin    = (t > 127) && (t < 450) ? t - 127 : 0;
            long firsttime = h.NextTickTime % 1000000;
            long rem       = h.NextTickTime - firsttime;
            int  stop      = Util.FTADD((int)firsttime, maxmin * 60);

            rem += stop;
            h.PlayTo(rem);
        }
示例#8
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;
        }
示例#9
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
            if (prepsim_needsbind(ref h, ga))
            {
                bindsim(ref h);
            }
            // prep broker
            SimBroker = new Broker();
            SimBroker.UseBidAskFills           = _usebidask.Checked;
            SimBroker.UseHighLiquidityFillsEOD = _usehighliquidityEOD.Checked;

            SimBroker.GotFill        += new FillDelegate(SimBroker_GotFill);
            SimBroker.GotOrder       += new OrderDelegate(SimBroker_GotOrder);
            SimBroker.GotOrderCancel += new OrderCancelDelegate(SimBroker_GotOrderCancel);
            // start simulation
            try
            {
                h.PlayTo(ga.PlayTo);
            }
            catch (DirectoryNotFoundException ex)
            {
                debug("Error locating tick files,  err: " + ex.Message + ex.StackTrace);
                debug("You may want to ensure your tick files are in the TradeLinkTicks folder.");
            }
            // end simulation
            ga.Stopped        = DateTime.Now;
            ga.TicksProcessed = h.TicksProcessed;
            ga.Executions     = FillCount;
            // save result
            e.Result = ga;
        }
示例#10
0
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            if (e.Cancel)
            {
                return;
            }
            PlayBackArgs args     = (PlayBackArgs)e.Argument;
            long         prevtime = 0;

            do
            {
                // if cancel was requested, quit
                if (CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                // play first tick
                long next = h.NextTickTime;
                h.PlayTo(next);
                // adjust delay, based on user's setting of 'speed'
                // as well as daystart (no delay before daystart)
                int delay = next >= args.DayStart ?
                            Util.FTDIFF((int)prevtime, (int)next) * 1000 * args.DELAYSCALE : 0;
                // if not first tick, wait realistic time between ticks
                if (prevtime != 0)
                {
                    System.Threading.Thread.CurrentThread.Join(delay);
                }
                // save time for calculating next day
                prevtime = next;
                // calculate progress
                double progress = 100.0 * (h.TicksProcessed / (double)h.TicksPresent);
                // report progress
                ReportProgress((int)progress);
            }while (h.NextTickTime != HistSim.ENDSIM);
            base.OnDoWork(e);
        }
示例#11
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;
        }
示例#12
0
        void Play(object sender, DoWorkEventArgs e)
        {
            PlayTo type = (PlayTo)e.Argument;

            if (e.Cancel)
            {
                return;
            }
            int  time = (int)(h.NextTickTime % 100000);
            long date = (h.NextTickTime / 100000) * 100000;
            int  t    = (int)type;
            long val  = 0;

            pt = type;
            switch (type)
            {
            case PlayTo.End:
                val = MultiSimImpl.ENDSIM;
                break;

            case PlayTo.FiveMin:
            case PlayTo.OneMin:
            case PlayTo.TenMin:
            case PlayTo.HalfHour:
                val = date + Util.FTADD(time, (t / 10) * 60);
                break;

            case PlayTo.Hour:
                val = date + Util.FTADD(time, (t / 1000) * 3600);
                break;

            case PlayTo.TwoHour:
                val = date + Util.FTADD(time, 2 * 60 * 60);
                break;

            case PlayTo.FourHour:
                val = date + Util.FTADD(time, 4 * 60 * 60);
                break;

            case PlayTo.OneSec:
            case PlayTo.ThirtySec:
                val = date + Util.FTADD(time, t);
                break;

            case PlayTo.Custom:
                ctime = getcusttime();
                if (ctime == 0)
                {
                    pt  = PlayTo.OneSec;
                    val = 0;
                    status("Invalid custom time, playing to next second.");
                }
                else
                {
                    val = date + ctime;
                }
                break;
            }
            cleardebugs(); // clear the message box on first box run
            h.PlayTo(val);
        }
示例#13
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));
                }
            }
        }