Пример #1
0
        public GauntletEngine(TradeLink.API.Response r, HistSim h)
        {
            responseengine = r;
            bindresponse(ref responseengine);

            myhistsim = h;
            bindsim(ref myhistsim);
            
            SimBroker = new Broker();
            SimBroker.GotOrderCancel += new OrderCancelDelegate(SimBroker_GotOrderCancel);
            SimBroker.GotOrder += new OrderDelegate(responseengine.GotOrder);
            SimBroker.GotFill += new FillDelegate(responseengine.GotFill);
        }
Пример #2
0
        public void Basics()
        {
            Broker broker = new Broker();
            broker.GotFill += new FillDelegate(broker_GotFill);
            broker.GotOrder += new OrderDelegate(broker_GotOrder);
            OrderImpl o = new OrderImpl();
            int error = broker.SendOrderStatus(o);
            Assert.AreNotEqual((int)MessageTypes.OK,error);
            Assert.That(orders == 0);
            Assert.That(fills == 0);
            o = new BuyMarket(s, 100);
            broker.SendOrderStatus(o);
            Assert.That(orders == 1);
            Assert.That(fills == 0);
            Assert.That(broker.Execute(TickImpl.NewTrade(s,10,200)) == 1);
            Assert.That(fills == 1);

            // test that a limit order is not filled outside the market
            o = new BuyLimit(s, 100, 9);
            broker.SendOrderStatus(o);
            Assert.AreEqual(0, broker.Execute(TickImpl.NewTrade(s, 10, 100)));
            Assert.That(fills == 1); // redudant but for counting

            // test that limit order is filled inside the market
            Assert.AreEqual(1, broker.Execute(TickImpl.NewTrade(s, 8, 100)));
            Assert.That(fills == 2);

            OrderImpl x = new OrderImpl();
            // test that a market order is filled when opposite book exists
            o = new SellLimit(s, 100, 11);
            x = new BuyMarket(s, 100);
            const string t2 = "trader2";
            x.Account = t2;
            broker.SendOrderStatus(o);
            broker.SendOrderStatus(x);
            Assert.AreEqual(3, fills); 

            // test that a market order is not filled when no book exists
            // on opposite side

            // clear existing orders
            broker.CancelOrders();
            o = new SellMarket(s, 100);
            o.Account = t2;
            broker.SendOrderStatus(o);
            Assert.AreEqual(3, fills);
            

            
        }
Пример #3
0
        public void Basics()
        {
            // setup trail tracker
            TrailTracker tt = new TrailTracker();
            tt.SendOrder += new OrderDelegate(tt_SendOrder);
            // set 15c trailing stop
            tt.DefaultTrail = new OffsetInfo(0,.15m);
            // verify it's set
            Assert.AreEqual(.15m,tt.DefaultTrail.StopDist);
            // get feed
            Tick [] tape = SampleData();
            // test broker
            Broker b = new Broker();
            // get fills over to trail tracker
            b.GotFill += new FillDelegate(tt.Adjust);
            // take initial position
            b.SendOrderStatus(new MarketOrder(SYM, 100));
            // get orders from trail tracker
            tt.SendOrder += new OrderDelegate(b.SendOrder);
            // no orders to start
            oc = 0;
            // iterate through feed
            for (int i = 0; i < tape.Length; i++ )
            {
                Tick k = tape[i];
                // set a date and time
                k.date = 20070926;
                k.time = 95500;
                // execute orders, nothing to do on first two ticks
                b.Execute(k);
                // pass every tick to tracker
                tt.newTick(k);

            }
            // get position
            Position p = b.GetOpenPosition(SYM);
            // verify position is flat
            Assert.IsTrue(p.isFlat,p.ToString());
            // one retrace sent at the end
            Assert.AreEqual(1, oc);
            
        }
Пример #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;
             }

         }
Пример #5
0
 void reset(bool reloadcurrentstudy)
 {
     try
     {
         // clear all GUIs
         _msg = new StringBuilder(10000);
         SimBroker = new Broker();
         debugControl1.Clear();
         dt.Clear();
         ptab.Clear();
         poslist.Clear();
         _tradelist.Clear();
         ot.Clear();
         ft.Clear();
         _tabs.Refresh();
         c = new ChartControl();
         _tr.Clear();
         if (it != null) 
         { 
             it.Clear(); 
             it.Columns.Clear(); 
             ig.Invalidate();
             SafeBindingSource.refreshgrid(ig, ibs,true);
         }
         unbindresponseevents();
         if (reloadcurrentstudy)
         {
             loadsim();
             loadboxname(resname);
         }
         else
         {
             epffiles.Clear();
             
         }
         nowtime = "0";
     }
     catch (Exception ex)
     {
         status("An error occured, try again.");
         debug("reset error: " + ex.Message + ex.StackTrace);
     }
 }
Пример #6
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;
        }
Пример #7
0
        public void BBO()
        {
            Broker broker = new Broker();
            const string s = "TST";
            const decimal p1 = 10m;
            const decimal p2 = 11m;
            const int x = 100;
            Order bid,offer;

            // send bid, make sure it's BBO (since it's only order on any book)
            broker.SendOrderStatus(new BuyLimit(s, x, p1));
            bid = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.That(bid.isValid && (bid.price==p1) && (bid.size==x), bid.ToString());
            Assert.That(!offer.isValid, offer.ToString());

            // add better bid, make sure it's BBO
            Order o;
            // Order#1... 100 shares buy at $11 
            o = new BuyLimit(s, x, p2,1);
            broker.SendOrderStatus(o);
            bid = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.IsTrue(bid.isValid);
            Assert.AreEqual(p2,bid.price);
            Assert.AreEqual(x, bid.size);
            Assert.That(!offer.isValid, offer.ToString());

            // add another bid at same price on another account, make sure it's additive
            //order #2... 100 shares buy at $11
            o = new BuyLimit(s, x, p2,2);
            o.Account = "ANOTHER_ACCOUNT";
            broker.SendOrderStatus(o);
            bid = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.IsTrue(bid.isValid);
            Assert.AreEqual(p2, bid.price);
            Assert.AreEqual(x*2, bid.size); 
            Assert.That(!offer.isValid, offer.ToString());

            // cancel order and make sure bbo returns
            broker.CancelOrder(1);
            broker.CancelOrder(2);
            bid = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.IsTrue(bid.isValid);
            Assert.AreEqual(p1,bid.price);
            Assert.AreEqual(x,bid.size);
            Assert.IsTrue(!offer.isValid, offer.ToString());

            // other test ideas
            // replicate above tests for sell-side


        }
Пример #8
0
        public void OPGs()
        {
            Broker broker = new Broker();
            const string s = "TST";
            // build and send an OPG order
            OrderImpl opg = new BuyOPG(s, 200, 10);
            broker.SendOrderStatus(opg);

            // build a tick on another exchange
            TickImpl it = TickImpl.NewTrade(s, 9, 100);
            it.ex = "ISLD";

            // fill order (should fail)
            int c = broker.Execute(it);
            Assert.AreEqual(0, c);

            // build opening price for desired exchange
            TickImpl nt = TickImpl.NewTrade(s, 9, 10000);
            nt.ex = "NYS";
            // fill order (should work)

            c = broker.Execute(nt);

            Assert.AreEqual(1, c);

            // add another OPG, make sure it's not filled with another tick

            TickImpl next = TickImpl.NewTrade(s, 9, 2000);
            next.ex = "NYS";

            OrderImpl late = new BuyOPG(s, 200, 10);
            broker.SendOrderStatus(late);
            c = broker.Execute(next);
            Assert.AreEqual(0, c);

        }
Пример #9
0
        public void MultiFire()
        {
            // setup trail tracker
            TrailTracker tt = new TrailTracker();
            tt.VerboseDebugging = true;
            tt.SendOrder += new OrderDelegate(tt_SendOrder);
            tt.SendDebug += new DebugDelegate(tt_SendDebug);
            // set 15c trailing stop
            tt.DefaultTrail = new OffsetInfo(0, .15m,0,.5m);
            // verify it's set
            Assert.AreEqual(.15m, tt.DefaultTrail.StopDist);
            // get feed
            Tick[] tape = MultiFireSampleData();
            // test broker
            Broker b = new Broker();
            b.Reset();
            // get fills over to trail tracker
            b.GotFill += new FillDelegate(tt.Adjust);
            // take initial position
            b.SendOrderStatus(new MarketOrder(SYM, 400));
            // get orders from trail tracker
            tt.SendOrder += new OrderDelegate(b.SendOrder);
            // no orders to start
            oc = 0;
            // iterate through feed
            for (int i = 0; i < tape.Length; i++)
            {
                Tick k = tape[i];
                // set a date and time
                k.date = 20070926;
                k.time = 95500;
                // execute orders, nothing to do on first two ticks
                b.Execute(k);
                // pass every tick to tracker
                tt.newTick(k);

            }
            // get position
            Position p = b.GetOpenPosition(SYM);
            // verify position is flat
            Assert.AreEqual(200,p.UnsignedSize, "position is not flat: " + p.ToString());
            // one retrace sent at the end
            Assert.AreEqual(1, oc,"too many fires");
        }
Пример #10
0
        public void TrailStartedAfterFirePoint()
        {
            // setup trail tracker
            TrailTracker tt = new TrailTracker();
            tt.isValid = true;
            const string acct = "DEFAULT";
            tt.pt.DefaultAccount = acct;
            tt.SendOrder += new OrderDelegate(tt_SendOrder);
            //tt.SendDebug += new DebugFullDelegate(tt_SendDebug);
            // set 15c trailing stop
            tt.DefaultTrail = new OffsetInfo(0, .15m);
            // verify it's set
            Assert.AreEqual(.15m, tt.DefaultTrail.StopDist);
            //
            tt.TrailByDefault = true;
            // put in a position to track
            tt.Adjust(new PositionImpl(SYM, 11.00m, 100, 0m,acct));
            // check position in tt-pt
            Assert.AreEqual(1, tt.pt.Count);
            // manually enter a trail
            tt[SYM] = new OffsetInfo(0m, .25m);
            // check if trail entered for symbol
            Assert.AreEqual(.25m, tt[SYM].StopDist);

            // get feed
            Tick[] tape = SampleData();
            // test broker
            Broker b = new Broker();
            // get fills over to trail tracker
            b.GotFill += new FillDelegate(tt.Adjust);
            // get orders from trail tracker
            tt.SendOrder += new OrderDelegate(b.SendOrder);
            // no orders to start
            oc = 0;
            // iterate through feed
            for (int i = 0; i < tape.Length; i++)
            {
                Tick k = tape[i];
                // set a date and time
                k.date = 20070926;
                k.time = 95500;
                // execute orders, nothing to do on first two ticks
                b.Execute(k);
                // pass every tick to tracker
                tt.newTick(k);

            }

            Assert.AreEqual(1, oc);
        }
Пример #11
0
        public void MOCs()
        {
            Broker broker = new Broker();
            const string s = "TST";
            OrderImpl moc = new MOCOrder(s, true, 200);
            Assert.IsTrue(moc.ValidInstruct == OrderInstructionType.MOC, "unexpected order instruction: " + moc.ValidInstruct);
            Assert.AreEqual(0, broker.SendOrderStatus(moc), "send moc order failed");

            TickImpl openingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(9, 30, 00), 9, 10000, "NYS");
            TickImpl endMornTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(12, 00, 00), 9, 10000, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(14, 15, 00), 9, 10000, "NYS");
            TickImpl closingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(16, 00, 00), 9, 10000, "NYS");

            int c = 0;
            c = broker.Execute(openingTick); Assert.AreEqual(0, c,"MOC filled on open");
            c = broker.Execute(endMornTick); Assert.AreEqual(0, c, "MOC filled in morning");
            c = broker.Execute(endLunchTick); Assert.AreEqual(0, c, "MOC filled at lunch");
            c = broker.Execute(closingTick); Assert.AreEqual(1, c, "MOC did not fill at close"); // should execute on the first tick at/after 16:00:00
        }
Пример #12
0
        public void DayFill()
        {
            Broker broker = new Broker();
            const string s = "TST";
            OrderImpl day = new MarketOrder(s, true, 200);
            broker.SendOrderStatus(day);

            TickImpl openingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(9, 30, 00), 9, 10000, "NYS");
            TickImpl endMornTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(12, 00, 00), 9, 10000, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(14, 15, 00), 9, 10000, "NYS");
            TickImpl closingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(16, 00, 00), 9, 10000, "NYS");

            int c;
            c = broker.Execute(openingTick); Assert.AreEqual(1, c); // should execute on first received tick
            c = broker.Execute(endMornTick); Assert.AreEqual(0, c);
            c = broker.Execute(endLunchTick); Assert.AreEqual(0, c);
            c = broker.Execute(closingTick); Assert.AreEqual(0, c);
        }
Пример #13
0
        public void Fill_HighLiquidity()
        {
            Broker broker = new Broker();
            broker.UseHighLiquidityFillsEOD = true;
            const string s = "SPY";
            OrderImpl limitBuy = new LimitOrder(s, true, 1, 133m);
            OrderImpl limitSell = new LimitOrder(s, false, 2, 133.5m);
            OrderImpl stopBuy = new StopOrder(s, true, 3, 135.70m);
            OrderImpl stopSell = new StopOrder(s, false, 4, 135.75m);
            broker.SendOrderStatus(limitBuy);
            broker.SendOrderStatus(limitSell);
            broker.SendOrderStatus(stopBuy);
            broker.SendOrderStatus(stopSell);

            // OHLC for 6/21/2012 on SPY
            TickImpl openingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(9, 30, 00), 135.67m, 10670270, "NYS");
            TickImpl endMornTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(12, 00, 00), 135.78m, 10670270, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(14, 15, 00), 132.33m, 10670270, "NYS");
            TickImpl closingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(16, 00, 00), 132.44m, 10670270, "NYS");

            broker.Execute(openingTick);
            broker.Execute(endMornTick);
            broker.Execute(endLunchTick);
            broker.Execute(closingTick);

            List<Trade> trades = broker.GetTradeList();
            Assert.IsTrue(trades.Count == 4);

            foreach (Trade trade in trades)
            {
                if (trade.xsize == 1)
                    Assert.AreEqual(133m, trade.xprice);
                else if (trade.xsize == 2)
                    Assert.AreEqual(133.5m, trade.xprice);
                else if (trade.xsize == 3)
                    Assert.AreEqual(135.7m, trade.xprice);
                else if (trade.xsize == 4)
                    Assert.AreEqual(135.75m, trade.xprice);
            }
        }
Пример #14
0
 void reset()
 {
     try
     {
         // clear all GUIs
         _msg = new StringBuilder(10000);
         SimBroker = new Broker();
         debugControl1.Clear();
         dt.Clear();
         ptab.Clear();
         poslist.Clear();
         _tradelist.Clear();
         ot.Clear();
         ft.Clear();
         _tabs.Refresh();
         c = new ChartControl();
         _tr.Clear();
         if (it != null) { it.Clear(); it.Columns.Clear(); ig.Invalidate(); }
         loadsim();
         unbindresponseevents();
         loadboxname(resname);
         nowtime = "0";
     }
     catch (Exception ex)
     {
         status("An error occured, try again.");
         debug("reset error: " + ex.Message + ex.StackTrace);
     }
 }
Пример #15
0
        public void DataProvider()
        {
            TickImpl t = TickImpl.NewTrade(s, 10, 700);
            // feature to pass-through ticks to any subscriber
            // this can be connected to tradelink library to allow filtered subscribptions
            // and interapplication communication
            Broker broker = new Broker();
            broker.GotTick += new TickDelegate(broker_GotTick);
            Assert.That((receivedtickDP == null) && (gottickDP == 0));
            broker.Execute(t); // should fire a gotTick
            Assert.That(gottickDP != 0);
            Assert.That((receivedtickDP != null) && (receivedtickDP.trade == t.trade));

        }