/// <summary> /// Generates profit taking order for a given position, at a specified per-share profit target. /// </summary> /// <param name="p">your position</param> /// <param name="offset">target price, per share/contract</param> /// <param name="percent">percent of the position to close with this order</param> /// <param name="normalizesize">whether to normalize order to be an even-lot trade</param> /// <param name="MINSIZE">size of an even lot</param> /// <returns></returns> public static Order PositionProfit(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)); int size = !normalizesize ? (int)(p.FlatSize * percent) : Norm2Min(p.FlatSize * percent, MINSIZE); o = new LimitOrder(p.Symbol, !p.isLong, size, price); return(o); }
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"); }
public void IdentityLimits() { string sym = "SPY"; bool side = false; int size = -256; decimal Limit = 134.40m; string comment = "Hello, World!"; // not checked for long id = 8675309; // not checked for Order orig = new LimitOrder(sym, side, size, Limit); Order comp; comp = new LimitOrder(sym, size, Limit, id); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SignedLimit"); Assert.AreEqual(orig.side, comp.side, "Side, SignedLimit"); Assert.AreEqual(orig.size, comp.size, "Size, SignedLimit"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop SignedLimit"); Assert.AreEqual(orig.price, comp.price, "Price, SignedLimit"); comp = new LimitOrder(sym, side, size, Limit, id); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, LimitID"); Assert.AreEqual(orig.side, comp.side, "Side, LimitID"); Assert.AreEqual(orig.size, comp.size, "Size, LimitID"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop LimitID"); Assert.AreEqual(orig.price, comp.price, "Price, LimitID"); comp = new LimitOrder(sym, side, size, Limit, comment); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, LimitComment"); Assert.AreEqual(orig.side, comp.side, "Side, LimitComment"); Assert.AreEqual(orig.size, comp.size, "Size, LimitComment"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop LimitComment"); Assert.AreEqual(orig.price, comp.price, "Price, LimitComment"); comp = new SellLimit(sym, size, Limit); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimit"); Assert.AreEqual(orig.side, comp.side, "Side, SellLimit"); Assert.AreEqual(orig.size, comp.size, "Size, SellLimit"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimit"); Assert.AreEqual(orig.price, comp.price, "Price, SellLimit"); comp = new SellLimit(sym, size, Limit, id); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimitID"); Assert.AreEqual(orig.side, comp.side, "Side, SellLimitID"); Assert.AreEqual(orig.size, comp.size, "Size, SellLimitID"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimitID"); Assert.AreEqual(orig.price, comp.price, "Price, SellLimitID"); comp = new SellLimit(sym, size, Limit, comment); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, SellLimitComment"); Assert.AreEqual(orig.side, comp.side, "Side, SellLimitComment"); Assert.AreEqual(orig.size, comp.size, "Size, SellLimitComment"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop SellLimitComment"); Assert.AreEqual(orig.price, comp.price, "Price, SellLimitComment"); side = true; orig = new LimitOrder(sym, side, size, Limit); comp = new BuyLimit(sym, size, Limit); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimit"); Assert.AreEqual(orig.side, comp.side, "Side, BuyLimit"); Assert.AreEqual(orig.size, comp.size, "Size, BuyLimit"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimit"); Assert.AreEqual(orig.price, comp.price, "Price, BuyLimit"); comp = new BuyLimit(sym, size, Limit, id); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimitID"); Assert.AreEqual(orig.side, comp.side, "Side, BuyLimitID"); Assert.AreEqual(orig.size, comp.size, "Size, BuyLimitID"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimitID"); Assert.AreEqual(orig.price, comp.price, "Price, BuyLimitID"); comp = new BuyLimit(sym, size, Limit, comment); Assert.AreEqual(orig.symbol, comp.symbol, "Symbol, BuyLimitComment"); Assert.AreEqual(orig.side, comp.side, "Side, BuyLimitComment"); Assert.AreEqual(orig.size, comp.size, "Size, BuyLimitComment"); Assert.AreEqual(orig.stopp, comp.stopp, "Stop BuyLimitComment"); Assert.AreEqual(orig.price, comp.price, "Price, BuyLimitComment"); }
/// <summary> /// Generates profit taking order for a given position, at a specified per-share profit target. /// </summary> /// <param name="p">your position</param> /// <param name="offset">target price, per share/contract</param> /// <param name="percent">percent of the position to close with this order</param> /// <param name="normalizesize">whether to normalize order to be an even-lot trade</param> /// <param name="MINSIZE">size of an even lot</param> /// <returns></returns> public static Order PositionProfit(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)); int size = !normalizesize ? (int)(p.FlatSize * percent) : Norm2Min(p.FlatSize*percent,MINSIZE); o = new LimitOrder(p.Symbol, !p.isLong, size, price); return o; }
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); } }