Exemplo n.º 1
0
        public void NamedIds()
        {
            IdTracker idt = new IdTracker();
            idt.SendDebugEvent+=new TradeLink.API.DebugDelegate(Console.WriteLine);
            idt.isMagicIdOnMaxName = false;
            const string sym = "TST";
            // get an id
            string id1 = "my market entry";
            string id2 = "my limit entry";
            string id3 = "my exit";
            var entry = new MarketOrder(sym, 100, idt[id1]);
            var lmt = new LimitOrder(sym, 100, 11, idt[id2]);
            var exit = new StopOrder(sym, 100, 9, idt[id3]);

            // verify they are unique
            Assert.AreNotEqual(entry.id, lmt.id, "entry market and limit should not match");
            Assert.AreNotEqual(exit.id, lmt.id, "exit and entry limit should not match");

            // make sure they always return the same value
            var c1 = idt[id1];
            var c2 = idt[id2];
            var c3 = idt[id3];
            Assert.AreEqual(c1, entry.id, id1 + " id changed");
            Assert.AreEqual(c2, lmt.id, id2 + " id changed");
            Assert.AreEqual(c3, exit.id, id3+" id changed");

            // test resetting
            idt[id3] = 0;
            var newc3 = idt[id3];
            Assert.AreNotEqual(newc3, c3, id3 + " did not change after a reset");

            // request it again, should be same

            var newc3compare = idt[id3];
            Assert.AreEqual(newc3, newc3compare, id3 + " changed after a read request");



        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
0
 /// <summary>
 /// Generate a stop order for a position, at a specified per-share/contract price
 /// </summary>
 /// <param name="p">your position</param>
 /// <param name="offset">how far away stop is</param>
 /// <param name="percent">what percent of position to close</param>
 /// <param name="normalizesize">whether to normalize size to even-lots</param>
 /// <param name="MINSIZE">size of an even lot</param>
 /// <returns></returns>
 public static Order PositionStop(Position p, decimal offset, decimal percent, bool normalizesize, int MINSIZE)
 {
     Order o = new OrderImpl();
     if (!p.isValid || p.isFlat) return o;
     decimal price = OffsetPrice(p, Math.Abs(offset)*-1);
     int size = !normalizesize ? (int)(p.FlatSize * percent) : Norm2Min(p.FlatSize * percent, MINSIZE);
     o = new StopOrder(p.Symbol, !p.isLong, size, price);
     return o;
 }
Exemplo n.º 4
0
        public void Fill_HighLiquidity()
        {
            Broker broker = new Broker();
            broker.UseHighLiquidityFillsEOD = true;
            const string s = "SPY";
            OrderImpl limitBuy = new LimitOrder(s, true, 1, 133m);
            OrderImpl limitSell = new LimitOrder(s, false, 2, 133.5m);
            OrderImpl stopBuy = new StopOrder(s, true, 3, 135.70m);
            OrderImpl stopSell = new StopOrder(s, false, 4, 135.75m);
            broker.SendOrderStatus(limitBuy);
            broker.SendOrderStatus(limitSell);
            broker.SendOrderStatus(stopBuy);
            broker.SendOrderStatus(stopSell);

            // OHLC for 6/21/2012 on SPY
            TickImpl openingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(9, 30, 00), 135.67m, 10670270, "NYS");
            TickImpl endMornTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(12, 00, 00), 135.78m, 10670270, "NYS");
            TickImpl endLunchTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(14, 15, 00), 132.33m, 10670270, "NYS");
            TickImpl closingTick = TickImpl.NewTrade(s, Util.ToTLDate(DateTime.Now), Util.TL2FT(16, 00, 00), 132.44m, 10670270, "NYS");

            broker.Execute(openingTick);
            broker.Execute(endMornTick);
            broker.Execute(endLunchTick);
            broker.Execute(closingTick);

            List<Trade> trades = broker.GetTradeList();
            Assert.IsTrue(trades.Count == 4);

            foreach (Trade trade in trades)
            {
                if (trade.xsize == 1)
                    Assert.AreEqual(133m, trade.xprice);
                else if (trade.xsize == 2)
                    Assert.AreEqual(133.5m, trade.xprice);
                else if (trade.xsize == 3)
                    Assert.AreEqual(135.7m, trade.xprice);
                else if (trade.xsize == 4)
                    Assert.AreEqual(135.75m, trade.xprice);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Generate a stop order for a position, at a specified per-share/contract price
 /// </summary>
 /// <param name="p">your position</param>
 /// <param name="offset">how far away stop is</param>
 /// <param name="percent">what percent of position to close</param>
 /// <param name="normalizesize">whether to normalize size to even-lots</param>
 /// <param name="MINSIZE">size of an even lot</param>
 /// <returns></returns>
 public static Order PositionStop(Position p, decimal offset, decimal percent, bool normalizesize, int MINSIZE)
 {
     Order o = new OrderImpl();
     if (!p.isValid || p.isFlat) return o;
     decimal price = Calc.OffsetPrice(p, offset * -1);
     int size = percent == 0 ? 0 : (!normalizesize ? (int)(p.FlatSize * percent) : Calc.Norm2Min(p.FlatSize * percent, MINSIZE));
     o = new StopOrder(p.symbol, !p.isLong, size, price);
     return o;
 }