Example #1
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
        }
Example #2
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.sendOrder(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
            uint id1 = broker.sendOrder(new BuyLimit(s, x, p2));

            bid   = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.That(bid.isValid && (bid.price == p2) && (bid.size == x), bid.ToString());
            Assert.That(!offer.isValid, offer.ToString());

            // add another bid at same price on another account, make sure it's additive
            uint id2 = broker.sendOrder(new BuyLimit(s, x, p2), new Account("ANOTHER_ACCOUNT"));

            bid   = broker.BestBid(s);
            offer = broker.BestOffer(s);
            Assert.That(bid.isValid && (bid.price == p2) && (bid.size == (2 * x)), bid.ToString());
            Assert.That(!offer.isValid, offer.ToString());

            // cancel order and make sure bbo returns
            broker.CancelOrder(id1);
            broker.CancelOrder(id2);
            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());

            // other test ideas
            // replicate above tests for sell-side
        }
Example #3
0
        void h_GotTick(Tick t)
        {
            // execute pending orders
            SimBroker.Execute(t);
            // only process requested depth
            if (t.depth > tickdepth)
            {
                return;
            }
            if (tickdepth == 0)
            {
                lasttime = t.time;
                lastdate = t.date;
                if (t.isTrade)
                {
                    decimal price = 0;
                    if (last.TryGetValue(t.symbol, out price))
                    {
                        last[t.symbol] = t.trade;
                    }
                    else
                    {
                        last.Add(t.symbol, t.trade);
                    }
                    if (highs.TryGetValue(t.symbol, out price))
                    {
                        if (t.trade > price)
                        {
                            highs[t.symbol] = t.trade;
                        }
                    }
                    else
                    {
                        highs.Add(t.symbol, t.trade);
                    }
                    if (lows.TryGetValue(t.symbol, out price))
                    {
                        if (t.trade < price)
                        {
                            lows[t.symbol] = t.trade;
                        }
                    }
                    else
                    {
                        lows.Add(t.symbol, t.trade);
                    }
                    tl.newTick(t); // notify of the trade
                }
                else
                {   // it's a quote so we need to update the book
                    // first though get the BBO from hist book to detect improvements
                    Order oldbid = SimBroker.BestBid(t.symbol);
                    Order oldask = SimBroker.BestOffer(t.symbol);

                    // then update the historical book
                    PlaceHistoricalOrder(t);

                    // fetch the new book
                    Order newbid = SimBroker.BestBid(t.symbol);
                    Order newask = SimBroker.BestOffer(t.symbol);

                    // reset accounts so equality comparisons work properly in next step
                    oldbid.Account = "";
                    oldask.Account = "";
                    newbid.Account = "";
                    newask.Account = "";

                    // if there are changes, notify clients
                    if (oldbid != newbid)
                    {
                        tl.newTick(OrderToTick(newbid));
                    }
                    if (oldask != newask)
                    {
                        tl.newTick(OrderToTick(newask));
                    }
                }
            }
            else
            {
                tl.newTick(t);
            }
        }