Пример #1
0
        public void MultiAccount()
        {
            const string sym   = "TST";
            Order        o     = new BuyMarket(sym, 100);
            const string me    = "tester";
            const string other = "anotherguy";
            Account      a     = new Account(me);
            Account      b     = new Account(other);
            Account      c     = new Account("sleeper");

            // send order to account for jfranta
            Assert.That(broker.sendOrder(o, a) > 0);
            Assert.That(broker.sendOrder(o, b) > 0);
            Tick t = new Tick(sym);

            t.trade = 100m;
            t.size  = 200;
            Assert.That(broker.Execute(t) == 2);
            Position apos = broker.GetOpenPosition(sym, a);
            Position bpos = broker.GetOpenPosition(sym, b);
            Position cpos = broker.GetOpenPosition(sym, c);

            Assert.That(apos.Side && (apos.Size == 100));
            Assert.That(bpos.Side && (bpos.Size == 100));
            Assert.That(cpos.Flat);
            // make sure that default account doesn't register
            // any trades
            Assert.That(broker.GetOpenPosition(sym).Flat);
        }
Пример #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 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)));
        }
Пример #4
0
        public void DoubleBasicWithFlat()
        {
            long id = 1;

            sho = new REGSHO_ShortTracker();
            sho.SendDebugEvent  += new DebugDelegate(sho_SendDebugEvent);
            sho.VerboseDebugging = true;
            Order o = new OrderImpl();

            // take a position
            sho.GotPosition(new PositionImpl(sym, 89.7m, 100));

            // accept two exits
            o = new SellStop(sym, 100, 89.65m, id++);
            long stop1 = o.id;

            Assert.IsFalse(sho.isOrderShort(o), "entry1: first sell was incorrectly short");
            sho.GotOrder(o);
            o = new SellLimit(sym, 100, 89.75m, id++);
            long profit1 = o.id;

            Assert.IsTrue(sho.isOrderShort(o), "entry1: second sell was incorrectly sell");
            sho.GotOrder(o);


            // flat
            o = new SellStop(sym, 100, 89.65m, stop1);
            o.Fill(TickImpl.NewTrade(sym, 89.62m, 100));
            sho.GotFill((Trade)o);
            sho.GotCancel(profit1);

            // do again
            // take a position
            o    = new BuyMarket(sym, 100);
            o.id = id++;
            o.Fill(TickImpl.NewTrade(sym, 89.64m, 100));
            sho.GotFill((Trade)o);

            // accept two exits
            o = new SellStop(sym, 100, 89.65m, id++);
            Assert.IsFalse(sho.isOrderShort(o), "entry2: first sell was incorrectly short");
            sho.GotOrder(o);
            o = new SellLimit(sym, 100, 89.75m, id++);
            Assert.IsTrue(sho.isOrderShort(o), "entry2: second sell was incorrectly NOT short");
            sho.GotOrder(o);
        }
Пример #5
0
        public void MultiAccount()
        {
            const string sym = "TST";

            const string me    = "tester";
            const string other = "anotherguy";
            const int    od    = 20070926;
            const int    ot    = 95400;
            Account      a     = new Account(me);
            Account      b     = new Account(other);
            Account      c     = new Account("sleeper");
            OrderImpl    oa    = new BuyMarket(sym, 100);
            OrderImpl    ob    = new BuyMarket(sym, 100);

            oa.time = ot;
            oa.date = od;
            ob.time = ot;
            ob.date = od;

            oa.Account = me;
            ob.Account = other;
            // send order to account for jfranta
            broker.SendOrderStatus(oa);
            broker.SendOrderStatus(ob);
            TickImpl t = new TickImpl(sym);

            t.trade = 100m;
            t.size  = 200;
            t.date  = od;
            t.time  = ot;
            Assert.AreEqual(2, broker.Execute(t));
            Position apos = broker.GetOpenPosition(sym, a);
            Position bpos = broker.GetOpenPosition(sym, b);
            Position cpos = broker.GetOpenPosition(sym, c);

            Assert.That(apos.isLong);
            Assert.AreEqual(100, apos.Size);
            Assert.That(bpos.isLong);
            Assert.AreEqual(100, bpos.Size);
            Assert.That(cpos.isFlat);
            // make sure that default account doesn't register
            // any trades
            Assert.That(broker.GetOpenPosition(sym).isFlat);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="tOther"></param>
        /// <param name="message"></param>
        protected void Buy(string symbol, decimal signal = 0, string message = null)
        {
            Tick tOther = _kt[symbol];

            if (message != null)
            {
                D(message);
            }
            // Specify we are buying
            _side = true;
            _adj  = (_side ? -1 : +1) * _quasiMarketOrderAdjSize;
            Int64 _orderidLocal = _idtLocal.AssignId;
            int   size          = (int)Math.Round(EntrySize * signal);
            Order order;

            order = new BuyMarket(symbol, EntrySize, _orderidLocal);
            sendorder(order);
            // wait for fill
            _wait[symbol] = true;
        }
Пример #7
0
        public void Basics()
        {
            Broker broker = new Broker();

            broker.GotFill    += new FillDelegate(broker_GotFill);
            broker.GotOrder   += new OrderDelegate(broker_GotOrder);
            broker.GotWarning += new DebugDelegate(broker_GotWarning);
            Order o = new Order();
            uint  failsoninvalid = broker.sendOrder(o);

            Assert.That(failsoninvalid == 0);
            Assert.That(warn == 1);
            Assert.That(orders == 0);
            Assert.That(fills == 0);
            o = new BuyMarket(s, 100);
            Assert.That(broker.sendOrder(o) > 0);
            Assert.That(orders == 1);
            Assert.That(fills == 0);
            Assert.That(broker.Execute(Tick.NewTrade(s, 10, 200)) == 1);
            Assert.That(fills == 1);
            // no warnings since first warning
            Assert.That(warn == 1);
        }
Пример #8
0
        public void Test()
        {
            // reset measurements
            reset();

            // create tests
            long  id = 1;
            Order o  = new BuyMarket(sym, 2 * size);

            o.id = id++;
            // start measuring
            lt.sendorder(o);
            // ack
            lt.GotOrder(o);
            // ensure we got a measurement
            Assert.IsTrue(gotmeasurement(), mez);
            // reset measurements
            reset();

            // fill
            o.Fill(TickImpl.NewTrade(sym, 100, size));
            Trade t = (Trade)o;

            lt.GotFill(t);
            // ensure we got a measurement
            Assert.IsTrue(gotmeasurement(), mez);
            // reset measurements
            reset();

            // cancel
            lt.sendcancel(o.id);
            System.Threading.Thread.Sleep(50);
            // ack cancel
            lt.GotCancel(o.id);
            // ensure we got a measurement
            Assert.IsTrue(gotmeasurement(), mez);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="tOther"></param>
        /// <param name="message"></param>
        void Buy(string symbol, decimal signal, string message = null)
        {
            Tick tOther = _kt[symbol];

            if (message != null)
            {
                D(message);
            }
            // Specify we are buying
            _side = true;
            _adj  = (_side ? -1 : +1) * _quasiMarketOrderAdjSize;
            Int64 _orderidLocal = _idtLocal.AssignId;
            int   size          = (int)Math.Round(EntrySize * signal);
            Order order;

            order = new BuyMarket(symbol, EntrySize, _orderidLocal);
            sendorder(order);

            // If stop loss option is specified
            if (_isStop)
            {
                //BuyStop bsOrder = new BuyStop(symbol,size,
                //order = new BuyStop(symbol, EntrySize, tOther.trade + _adj, _orderidLocal);
                //order = new SellStop(symbol, EntrySize, tOther.trade + _adj, _orderidLocal);
                //sendorder(order);
            }
            else
            {
                //order = new BuyLimit(symbol, EntrySize, tOther.trade - _adj, _orderidLocal);
                //BuyLimit blOrder = new BuyLimit(symbol, (int)Math.Round(EntrySize * signal), tOther.trade - _adj, _orderidLocal);
                //sendorder(blOrder);
            }

            // wait for fill
            _wait[symbol] = true;
        }
Пример #10
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));
        }