Пример #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 void SplitWithPartialFillRoundMinsize100()
        {
            lasto = new OrderImpl();
            oc = 0;
            ost = new OversellTracker();
            ost.SendOrderEvent += new OrderDelegate(ost_SendOrderEvent);
            ost.SendDebugEvent += new DebugDelegate(rt.d);
            ost.MinLotSize = 100;

            ost.Split = true;
            // take a position
            ost.GotPosition(new PositionImpl("TST", 100, 58));
            // over sell
            Order o = new SellMarket("TST", 100);
            o.id = 1;
            ost.sendorder(o);
            // verify that flat and adjustment were sent
            Assert.AreEqual(1, oc);
            Assert.AreEqual(-100, lasto.size);
        }
Пример #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 SplitCancelCopy()
        {
            lasto = new OrderImpl();
            oc = 0;
            cancels.Clear();
            ost = new OversellTracker();
            ost.SendOrderEvent += new OrderDelegate(ost_SendOrderEvent);
            ost.SendDebugEvent += new DebugDelegate(rt.d);
            ost.SendCancelEvent += new LongDelegate(ost_SendCancelEvent);

            ost.Split = true;
            // take a position
            ost.GotPosition(new PositionImpl("TST", 100, 300));
            // over sell
            Order o = new SellMarket("TST", 500);
            o.id = 5;
            ost.sendorder(o);
            // verify that only flat was sent
            Assert.AreEqual(2, oc);
            Assert.AreEqual(-200, lasto.size);
            // make sure we've not canceled
            Assert.AreEqual(0, cancels.Count);
            // cancel original order
            ost.sendcancel(5);
            // ensure two cancels sent
            Assert.AreEqual(2, cancels.Count);
            // ensure different cancels
            Assert.AreEqual(5, cancels[0]);
            Assert.AreNotEqual(5, cancels[1]);

            // do it again

            // take a position
            ost.GotPosition(new PositionImpl("TST", 100, 300));

            // over sell
            o = new SellMarket("TST", 500);
            o.id = 10;
            ost.sendorder(o);
            // verify that only flat was sent
            Assert.AreEqual(4, oc);
            Assert.AreEqual(-200, lasto.size);
            // make sure we've not canceled
            Assert.AreEqual(2, cancels.Count);
            // cancel original order
            ost.sendcancel(10);
            // ensure two cancels sent
            Assert.AreEqual(4, cancels.Count);
            // ensure different cancels
            Assert.AreEqual(10, cancels[2]);
            Assert.AreNotEqual(10, cancels[3]);


        }