예제 #1
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);
            

            
        }
예제 #2
0
 public override void GotTick(Tick k)
 {
     // enforce tifs
     tt.newTick(k);
     // ignore quotes
     if (!k.isTrade)
         return;
     // send entry
     if (cancels == sends)
     {
         Order o = new BuyLimit(k.symbol, 100, k.trade * .7m);
         tt.SendOrder(o);
         D(o.symbol + " sent tif-enforced order: " + o);
         sends++;
     }
 }
예제 #3
0
        public void Fill()
        {
            const string s = "TST";
            // market should fill on trade but not on quote
            OrderImpl o = new BuyMarket(s, 100);
            Assert.That(o.Fill(TickImpl.NewTrade(s, 9, 100)));
            Assert.That(!o.Fill(TickImpl.NewBid(s, 8, 100)));

            // buy limit

            // limit should fill if order price is inside market
            o = new BuyLimit(s, 100, 10m);
            Assert.That(o.Fill(TickImpl.NewTrade(s, 9, 100)));
            // shouldn't fill outside market
            o = new BuyLimit(s, 100, 10m);
            Assert.That(!o.Fill(TickImpl.NewTrade(s, 11, 100)));

            // sell limit

            // limit should fill if order price is inside market
            o = new SellLimit(s, 100, 10m);
            Assert.That(o.Fill(TickImpl.NewTrade(s, 11, 100)));
            // shouldn't fill outside market
            o = new SellLimit(s, 100, 10m);
            Assert.That(!o.Fill(TickImpl.NewTrade(s, 9, 100)));

            // buy stop

            o = new BuyStop(s, 100, 10m);
            Assert.That(o.Fill(TickImpl.NewTrade(s, 11, 100)));
            // shouldn't fill outside market
            o = new BuyStop(s, 100, 10m);
            Assert.That(!o.Fill(TickImpl.NewTrade(s, 9, 100)));

            // sell stop

            o = new SellStop(s, 100, 10m);
            Assert.That(o.Fill(TickImpl.NewTrade(s, 9, 100)));
            // shouldn't fill outside market
            o = new SellStop(s, 100, 10m);
            Assert.That(!o.Fill(TickImpl.NewTrade(s, 11, 100)));

            // always fail filling an invalid tick
            o = new BuyMarket(s, 100);
            Assert.IsFalse(o.Fill(TickImpl.NewTrade(s, 0, 0)));

            // always fail filling invalid order
            o = new BuyLimit(s, 100, 10);
            OrderImpl x = new OrderImpl();
            Assert.IsFalse(o.Fill(x));

            // always fail filling an order that doesn't cross market
            x = new BuyMarket(s, 100);
            Assert.IsFalse(o.Fill(x));

            const string t2 = "trader2";
            // suceed on crossing market
            x = new SellMarket(s,100);
            x.Account = t2;
            Assert.IsTrue(o.Fill(x));

            // fail when accounts are the same
            x = new SellMarket(s, 100);
            x.Account = o.Account;
            Assert.IsFalse(o.Fill(x));


            // fail on match outside of market
            x = new SellLimit(s, 100, 11);
            x.Account = t2;
            Assert.IsFalse(o.Fill(x));

            // succeed on limit cross
            o = new BuyLimit(s, 100, 10);
            x = new SellLimit(s, 100, 10);
            x.Account = t2;
            Assert.IsTrue(o.Fill(x));

            // make sure we can stop cross
            o = new SellStop(s, 100, 10);
            x = new BuyMarket(s, 100);
            x.Account = t2;
            Assert.IsTrue(o.Fill(x));

        }
예제 #4
0
        public void FillBidAsk()
        {
            const string s = "TST";
            // market should fill on trade but not on quote
            OrderImpl o = new BuyMarket(s, 100);
            Assert.That(o.FillBidAsk(TickImpl.NewAsk(s, 9, 100)));
            Assert.That(!o.FillBidAsk(TickImpl.NewTrade(s, 9, 100)));
            Assert.That(!o.FillBidAsk(TickImpl.NewBid(s, 8, 100)));

            // buy limit

            // limit should fill if order price is inside market
            o = new BuyLimit(s, 100, 10m);
            Assert.That(o.FillBidAsk(TickImpl.NewAsk(s, 9, 100)));
            // shouldn't fill outside market
            o = new BuyLimit(s, 100, 10m);
            Assert.That(!o.FillBidAsk(TickImpl.NewTrade(s, 11, 100)));
            Assert.That(!o.FillBidAsk(TickImpl.NewAsk(s, 11, 100)));
            Assert.That(!o.FillBidAsk(TickImpl.NewBid(s, 10, 100)));

            // sell limit

            // limit should fill if order price is inside market
            o = new SellLimit(s, 100, 10m);
            Assert.That(o.FillBidAsk(TickImpl.NewBid(s, 11, 100)));
            // shouldn't fill outside market
            o = new SellLimit(s, 100, 10m);
            Assert.That(!o.FillBidAsk(TickImpl.NewTrade(s, 9, 100)));

            // buy stop

            o = new BuyStop(s, 100, 10m);
            Assert.That(o.FillBidAsk(TickImpl.NewAsk(s, 11, 100)));
            // shouldn't fill outside market
            o = new BuyStop(s, 100, 10m);
            Assert.That(!o.FillBidAsk(TickImpl.NewTrade(s, 9, 100)));

            // sell stop

            o = new SellStop(s, 100, 10m);
            Assert.That(o.FillBidAsk(TickImpl.NewBid(s, 9, 100)));
            // shouldn't fill outside market
            o = new SellStop(s, 100, 10m);
            Assert.That(!o.FillBidAsk(TickImpl.NewTrade(s, 11, 100)));

            // always fail filling an invalid tick
            o = new BuyMarket(s, 100);
            Assert.IsFalse(o.FillBidAsk(TickImpl.NewTrade(s, 0, 0)));
        }
예제 #5
0
        public void IdentityLimits()
        {
            string sym = "SPY";
            bool side = false;
            int size = -256;
            decimal Limit = 134.40m;
            string comment = "Hello, World!";   // not checked for
            long id = 8675309;                  // not checked for

            Order orig = new LimitOrder(sym, side, size, Limit);
            Order comp;

            comp = new LimitOrder(sym, size, Limit, id);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SignedLimit");
            Assert.AreEqual(orig.side, comp.side, "Side, SignedLimit");
            Assert.AreEqual(orig.size, comp.size, "Size, SignedLimit");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop SignedLimit");
            Assert.AreEqual(orig.price, comp.price, "Price, SignedLimit");

            comp = new LimitOrder(sym, side, size, Limit, id);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, LimitID");
            Assert.AreEqual(orig.side, comp.side, "Side, LimitID");
            Assert.AreEqual(orig.size, comp.size, "Size, LimitID");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop LimitID");
            Assert.AreEqual(orig.price, comp.price, "Price, LimitID");

            comp = new LimitOrder(sym, side, size, Limit, comment);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, LimitComment");
            Assert.AreEqual(orig.side, comp.side, "Side, LimitComment");
            Assert.AreEqual(orig.size, comp.size, "Size, LimitComment");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop LimitComment");
            Assert.AreEqual(orig.price, comp.price, "Price, LimitComment");

            comp = new SellLimit(sym, size, Limit);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimit");
            Assert.AreEqual(orig.side, comp.side, "Side, SellLimit");
            Assert.AreEqual(orig.size, comp.size, "Size, SellLimit");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimit");
            Assert.AreEqual(orig.price, comp.price, "Price, SellLimit");

            comp = new SellLimit(sym, size, Limit, id);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimitID");
            Assert.AreEqual(orig.side, comp.side, "Side, SellLimitID");
            Assert.AreEqual(orig.size, comp.size, "Size, SellLimitID");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimitID");
            Assert.AreEqual(orig.price, comp.price, "Price, SellLimitID");

            comp = new SellLimit(sym, size, Limit, comment);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimitComment");
            Assert.AreEqual(orig.side, comp.side, "Side, SellLimitComment");
            Assert.AreEqual(orig.size, comp.size, "Size, SellLimitComment");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimitComment");
            Assert.AreEqual(orig.price, comp.price, "Price, SellLimitComment");

            side = true;
            orig = new LimitOrder(sym, side, size, Limit);

            comp = new BuyLimit(sym, size, Limit);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimit");
            Assert.AreEqual(orig.side, comp.side, "Side, BuyLimit");
            Assert.AreEqual(orig.size, comp.size, "Size, BuyLimit");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimit");
            Assert.AreEqual(orig.price, comp.price, "Price, BuyLimit");

            comp = new BuyLimit(sym, size, Limit, id);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimitID");
            Assert.AreEqual(orig.side, comp.side, "Side, BuyLimitID");
            Assert.AreEqual(orig.size, comp.size, "Size, BuyLimitID");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimitID");
            Assert.AreEqual(orig.price, comp.price, "Price, BuyLimitID");

            comp = new BuyLimit(sym, size, Limit, comment);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimitComment");
            Assert.AreEqual(orig.side, comp.side, "Side, BuyLimitComment");
            Assert.AreEqual(orig.size, comp.size, "Size, BuyLimitComment");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimitComment");
            Assert.AreEqual(orig.price, comp.price, "Price, BuyLimitComment");
        }
예제 #6
0
파일: Replay.cs 프로젝트: bluejack2000/core
        void PlaceHistoricalOrder(Tick t)
            // this function converts a historical quote into an order
            // and places it on a special order book replay uses to determine
            // the BBO for historical tick streams and the BBO between historical ticks
            // and the other order books
        {
            if (t.isTrade) return;
            if (t.depth != 0) return;

            if (t.hasAsk)
            {
                // if we already have a book for this side we can get rid of it
                foreach (long oid in hasHistBook(t.symbol, false))
                    SimBroker.CancelOrder(oid); 
                OrderImpl o = new SellLimit(t.symbol, t.AskSize, t.ask);
                o.date = t.date;
                o.time = t.time;
                o.Exchange = t.oe;
                SimBroker.SendOrderAccount(o,HISTBOOK);
            }
            if (t.hasBid)
            {
                // if we already have a book for this side we can get rid of it
                foreach (long oid in hasHistBook(t.symbol, true))
                    SimBroker.CancelOrder(oid);
                OrderImpl o = new BuyLimit(t.symbol, t.BidSize, t.bid);
                o.date = t.date;
                o.time = t.time;
                o.Exchange = t.be;
                SimBroker.SendOrderAccount(o, HISTBOOK);
            }
            
        }
예제 #7
0
        public void BasicStopAndLimit()
        {
            long id = 1;
            sho = new REGSHO_ShortTracker();
            sho.SendDebugEvent += new DebugDelegate(rt.d);
            sho.VerboseDebugging = true;
            Order o = new OrderImpl();

            // take a position
            o = new BuyLimit(sym, 100, 21.18m, id++);
            o.Account = ACCT;
            Assert.IsFalse(sho.isOrderShort(o), "entry buy never a short.");
            sho.GotOrder(o);
            Assert.IsTrue(o.Fill(TickImpl.NewTrade(sym,21.14m,100)),"unable to fill order");
            Trade t = (Trade)o;
            Assert.IsTrue(t.isValid && t.isFilled, "not a valid trade");
            sho.GotFill(t);
            

            // accept two exits
            o = new SellStop(sym, 100, 21.09m, id++);
            o.Account = ACCT;
            Assert.IsFalse(sho.isOrderShort(o),"first exit was wrongly a short");
            sho.GotOrder(o);
            o = new SellLimit(sym, 100, 21.19m, id++);
            o.Account = ACCT;
            Assert.IsTrue(sho.isOrderShort(o), "second exit was wrongly a sell");
            sho.GotOrder(o);

        }
예제 #8
0
        public void FillThenStopAndLimitOversell()
        {
            long id = 1;
            sho = new REGSHO_ShortTracker();
            sho.SendDebugEvent += new DebugDelegate(rt.d);
            sho.VerboseDebugging = true;
            lastids.Clear();
            Order o = new OrderImpl();

            // send some initial orders
            so(new SellLimit(sym, 100, 25.83m, id++));
            so(new SellStop(sym, 100, 25.83m, id++));
            o = new SellStop(sym, 200, 25.83m, id++);
            so(o);
            // cancel first two orders
            sho.GotCancel(lastids[0]);
            sho.GotCancel(lastids[1]);
            // fill last order
            Assert.IsTrue(o.Fill(TickImpl.NewTrade(sym, 25.80m, 200)), "missing initial fill");
            sho.GotFill((Trade)o);
            // check pending size
            Assert.AreEqual(0, pendingsize(sym), "had pending size after cancels and fills");
            // flat position
            sho.GotPosition(new PositionImpl(sym,0,0,0,ACCT));
            


            // take a position
            o = new BuyLimit(sym, 100, 25.83m, id++);
            o.Account = ACCT;
            Assert.IsFalse(sho.isOrderShort(o), "entry buy never a short.");
            sho.GotOrder(o);
            Assert.IsTrue(o.Fill(TickImpl.NewTrade(sym, 25.80m, 100)), "unable to fill order");
            Trade t = (Trade)o;
            Assert.IsTrue(t.isValid && t.isFilled, "not a valid trade");
            sho.GotFill(t);


            // accept two exits
            o = new SellStop(sym, 100, 21.09m, id++);
            o.Account = ACCT;
            Assert.IsFalse(sho.isOrderShort(o), "first exit was wrongly a short");
            sho.GotOrder(o);
            o = new SellLimit(sym, 100, 21.19m, id++);
            o.Account = ACCT;
            Assert.IsTrue(sho.isOrderShort(o), "second exit was wrongly a sell");
            sho.GotOrder(o);
        }
예제 #9
0
        public void FillPendTest()
        {
            long id = 1;
            sho = new REGSHO_ShortTracker();
            sho.SendDebugEvent += new DebugDelegate(rt.d);
            sho.VerboseDebugging = true;
            lastids.Clear();
            Order o = new OrderImpl();

            // send order 
            o = new BuyLimit(sym, 100, 10, id++);
            so(o);
            // verify pending
            Assert.AreEqual(100, pendingsize(sym),"missing pending size");
            // fill it
            Assert.IsTrue(o.Fill(TickImpl.NewTrade(sym,10,100)),"no fill");
            sho.GotFill((Trade)o);
            // verify it's not pending
            Assert.AreEqual(0, pendingsize(sym),"missing pending size");

        }
예제 #10
0
        // this function converts a historical quote into an order
        // and places it on a special order book replay uses to determine
        // the BBO for historical tick streams and the BBO between historical ticks
        // and the other order books
        void PlaceHistoricalOrder(Tick t)
        {
            if (t.isTrade) return;
            if (t.depth != 0) return;

            if (t.hasAsk)
            {
                // if we already have a book for this side we can get rid of it
                foreach (long oid in hasHistBook(t.symbol, false))
                    SimBroker.CancelOrder(oid);
                OrderImpl o = new SellLimit(t.symbol, t.AskSize, t.ask);
                o.date = t.date;
                o.time = t.time;
                o.Exchange = t.oe;
                SimBroker.SendOrderAccount(o,HISTBOOK);
            }
            if (t.hasBid)
            {
                // if we already have a book for this side we can get rid of it
                foreach (long oid in hasHistBook(t.symbol, true))
                    SimBroker.CancelOrder(oid);
                OrderImpl o = new BuyLimit(t.symbol, t.BidSize, t.bid);
                o.date = t.date;
                o.time = t.time;
                o.Exchange = t.be;
                SimBroker.SendOrderAccount(o, HISTBOOK);
            }
        }
예제 #11
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


        }