Пример #1
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);



        }
Пример #2
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));

        }
Пример #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 IdentityStops()
        {
            string sym = "SPY";
            bool side = false;
            int size = -256;
            decimal stop = 134.40m;
            string comment = "Hello, World!";   // not checked for
            long id = 8675309;                  // not checked for

            Order orig = new StopOrder(sym, side, size, stop);
            Order comp;
            
            comp = new StopOrder(sym, size, stop, id);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SignedStop");
            Assert.AreEqual(orig.side, comp.side, "Side, SignedStop");
            Assert.AreEqual(orig.size, comp.size, "Size, SignedStop");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop, SignedStop");
            Assert.AreEqual(orig.price, comp.price, "Price, SignedStop");

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

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

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

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

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

            side = true;
            orig = new StopOrder(sym, side, size, stop);

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

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

            comp = new BuyStop(sym, size, stop, comment);
            Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyStopComment");
            Assert.AreEqual(orig.side, comp.side, "Side, BuyStopComment");
            Assert.AreEqual(orig.size, comp.size, "Size, BuyStopComment");
            Assert.AreEqual(orig.stopp, comp.stopp, "Stop, BuyStopComment");
            Assert.AreEqual(orig.price, comp.price, "Price, BuyStopComment");
        }
Пример #5
0
        public void Basic()
        {
            long id = 1;
            sho = new REGSHO_ShortTracker();
            sho.SendDebugEvent+=new DebugDelegate(rt.d);
            Order o = new OrderImpl();

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

            // accept two exits
            o = new SellLimit(sym, 100, 200, id++);
            Assert.IsFalse(sho.isOrderShort(o));
            sho.GotOrder(o);
            o = new SellLimit(sym, 200, 105, id++);
            Assert.IsFalse(sho.isOrderShort(o));
            sho.GotOrder(o);

            // send another short
            o = new SellStop(sym, 300, 99);
            Assert.IsTrue(sho.isOrderShort(o));


        }
Пример #6
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);

        }
Пример #7
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);
        }