long tl_newSendOrderRequest(Order o) { if (isPaperTradeEnabled) { ptt.sendorder(o); } else { debug("paper trade disabled, ignoring: " + o); } return(0); }
public void TestBidAsk() { ptt = new PapertradeTracker(); pt = new PositionTracker(); fills = 0; const string SYM = "TST"; ptt.SendDebugEvent += new DebugDelegate(debug); ptt.GotFillEvent += new FillDelegate(ptt_GotFillEvent); ptt.GotOrderEvent += new OrderDelegate(ptt_GotOrderEvent); // enable bid ask fills ptt.UseBidAskFills = true; // send an order ptt.sendorder(new BuyMarket(SYM, 1000)); // verify it did not fill Assert.AreEqual(0, pt[SYM].Size); // send a tick ptt.newTick(TickImpl.NewTrade(SYM, 10, 100)); // verify it did not fill Assert.AreEqual(0, pt[SYM].Size); // send bid ptt.newTick(TickImpl.NewBid(SYM, 10, 100)); // verify it did not fill Assert.AreEqual(0, pt[SYM].Size); // send ask ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); // verify it fills Assert.AreEqual(100, pt[SYM].Size); Assert.AreEqual(1, fills); // fill rest of the order ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); Assert.AreEqual(10, fills); Assert.AreEqual(1000, pt[SYM].Size); // send a bunch more ticks and ensure nothing else is filled ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); ptt.newTick(TickImpl.NewAsk(SYM, 12, 100)); Assert.AreEqual(0, ptt.QueuedOrders.Length); Assert.AreEqual(10, fills); Assert.AreEqual(1000, pt[SYM].Size); }
public void TestTradeFill() { ptt = new PapertradeTracker(); ptt.SendDebugEvent += new DebugDelegate(debug); ptt.UseBidAskFills = false; fills = 0; pt = new PositionTracker(); const string SYM = "TST"; ptt.GotFillEvent += new FillDelegate(ptt_GotFillEvent); ptt.GotOrderEvent += new OrderDelegate(ptt_GotOrderEvent); // send an order ptt.sendorder(new BuyMarket(SYM, 1000)); // verify it did not fill Assert.AreEqual(0, pt[SYM].Size); // send a tick ptt.newTick(TickImpl.NewTrade(SYM, 10, 100)); // verify it fills Assert.AreEqual(100, pt[SYM].Size); }
public void FillBidAskPartial() { PapertradeTracker ptt = new PapertradeTracker(); PositionTracker pt = new PositionTracker(); ptt.UseBidAskFills = true; ptt.GotFillEvent += new FillDelegate(pt.GotFill); foreach (bool side in new bool[] { true, false }) { pt.Clear(); Order o = new MarketOrder("IBM", side, 1000); int size = o.size; o.id = 1; ptt.sendorder(o); Tick k = TickImpl.NewQuote("IBM", 100, 101, 400, 400, "", ""); ptt.newTick(k); // partial fill ptt.newTick(k); // partial fill ptt.newTick(k); // partial fill, completed Expect(pt["IBM"].Size, Is.EqualTo(size)); } }
void doqueues(object obj) { while (_bwgo) { bool newsym = false; while (!_newsyms.isEmpty) { _newsyms.Read(); newsym = true; } if (newsym) { // get symbols Basket b = BasketImpl.FromString(_newsymlist); object err = null; foreach (Security s in b) { try { string se = string.Empty; if (vret == null) { vret = _cc.VBRediCache.Submit("L1", "true", ref err); checkerror(ref err, "submit"); } _cc.VBRediCache.AddWatch(0, s.Symbol, string.Empty, ref err); checkerror(ref err, "watch"); } catch (Exception ex) { debug(s.Symbol + " error subscribing: " + ex.Message + ex.StackTrace); } } debug("registered: " + _newsymlist); } while (!_neworders.isEmpty) { Order o = _neworders.Read(); v("received order: " + o.ToString()); if (o.Account == string.Empty) { o.Account = Account; } if (isPaperTradeEnabled) { ptt.sendorder(o); } else { RediLib.ORDER rediOrder = new RediLib.ORDERClass(); // get any current position in symbol Position p = pt[o.symbol]; // determine if a sell order should be a long exit or a short entry string side = !o.side && !p.isLong ? "Sell Short" : (o.side ? "Buy" : "Sell"); rediOrder.Account = o.Account; rediOrder.UserID = _userid; rediOrder.Password = _pwd; rediOrder.TIF = gettiftype(o); rediOrder.Side = side; rediOrder.Symbol = o.symbol; if (o.ex == string.Empty) { o.ex = o.symbol.Length > 3 ? "ARCA" : "NYSE"; } rediOrder.Exchange = o.ex; rediOrder.Quantity = o.UnsignedSize; rediOrder.Price = o.price.ToString(); rediOrder.PriceType = getpricetype(o); rediOrder.StopPrice = o.stopp; rediOrder.Memo = o.comment; rediOrder.Warning = false; object err = null; object transId = null; bool IsSuccessful = rediOrder.Submit2(ref transId, ref err); if (IsSuccessful) { v("successfully sent order: " + o.ToString()); object err1 = null; _messageCache.VBRediCache.AddWatch(1, o.symbol, "", ref err1); if (!(err1 == null)) { debug("FAILED open of Table! Table : Message Error:" + err1.ToString()); } else { v("successfully watching symbol: " + o.symbol); } } else { v("error sending order: " + o.ToString()); if (!(err == null)) { debug("order submission was not successful: " + err.ToString()); } } } } while (!_newcancel.isEmpty) { long id = _newcancel.Read(); v("received cancel request: " + id); if (isPaperTradeEnabled) { ptt.sendcancel(id); } else { object err = null; _messageCache.VBRediCache.Cancel(id, ref err); if (err != null) { v("error canceling id: " + id); } else { v("cancel request sent for: " + id); } } } if (_newcancel.isEmpty && _neworders.isEmpty && _newsyms.isEmpty) { Thread.Sleep(_SLEEP); } } }
void background(object param) { while (_runbg) { try { // orders while (!_orderq.isEmpty) { STIOrder order = new STIOrder(); Order o = _orderq.Read(); if (VerboseDebugging) { debug("client order received: " + o.ToString()); } if ((o.id == 0) && AutosetUnsetId) { o.id = _idt.AssignId; } if (isPaperTradeEnabled) { ptt.sendorder(o); } else { o.price = Math.Round(o.price, FixOrderDecimalPlace); o.stopp = Math.Round(o.stopp, FixOrderDecimalPlace); if (o.ex == string.Empty) { o.ex = o.symbol.Length > 3 ? "NSDQ" : "NYSE"; } order.Destination = o.Exchange; order.Side = getside(o.symbol, o.side); order.Symbol = o.symbol; order.Quantity = o.UnsignedSize; string acct = Account != string.Empty ? Account : string.Empty; order.Account = o.Account != string.Empty ? o.Account : acct; order.Destination = o.Exchange != "" ? o.ex : "NYSE"; bool close = o.ValidInstruct == OrderInstructionType.MOC; bool pegged = (o.ValidInstruct >= OrderInstructionType.PEG2MID) && (o.ValidInstruct <= OrderInstructionType.PEG2BST); order.Tif = tif2tif(o.TIF); if (!pegged) { order.LmtPrice = (double)o.price; order.StpPrice = (double)o.stopp; } if (close) { if (o.isMarket) { order.PriceType = STIPriceTypes.ptSTIMktClo; } else if (o.isLimit) { order.PriceType = STIPriceTypes.ptSTILmtClo; } else { order.PriceType = STIPriceTypes.ptSTIClo; } } else if (pegged) { order.PriceType = STIPriceTypes.ptSTIPegged; if (o.price <= 0) { order.PegDiff = (double)o.price; } else { order.LmtPrice = (double)o.price; } if (o.ValidInstruct == OrderInstructionType.PEG2BST) { order.ExecInst = "T"; } else if (o.ValidInstruct == OrderInstructionType.PEG2MID) { order.ExecInst = "M"; } else if (o.ValidInstruct == OrderInstructionType.PEG2MKT) { order.ExecInst = "P"; } else if (o.ValidInstruct == OrderInstructionType.PEG2PRI) { order.ExecInst = "R"; } } else if (o.isMarket) { order.PriceType = STIPriceTypes.ptSTIMkt; } else if (o.isLimit && o.isStop) { order.PriceType = STIPriceTypes.ptSTISvrStpLmt; } else if (o.isLimit) { order.PriceType = STIPriceTypes.ptSTILmt; } else if (o.isStop) { order.PriceType = STIPriceTypes.ptSTISvrStp; } else if (o.isTrail) { order.PriceType = STIPriceTypes.ptSTITrailStp; } order.ClOrderID = o.id.ToString(); int err = order.SubmitOrder(); if (VerboseDebugging) { debug("client order sent: " + order.ClOrderID); } string tmp = ""; if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp))) { // save account/id relationship for canceling idacct.Add(o.id, order.Account); // wait briefly between orders Thread.Sleep(_ORDERSLEEP); } if (err < 0) { debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString()); } if (err == -1) { debug("Make sure you have set the account in sending program."); } } } // new quotes if (!_symsq.isEmpty) { _symsq.Read(); foreach (string sym in symquotes.Split(',')) { stiQuote.RegisterQuote(sym, "*"); } } // old quotes while (removesym.hasItems) { string rem = removesym.Read(); stiQuote.DeRegisterQuote(rem, "*"); } // cancels if (!_cancelq.isEmpty) { long number = _cancelq.Read(); if (isPaperTradeEnabled) { ptt.sendcancel(number); } else { string acct = ""; if (idacct.TryGetValue(number, out acct)) { // get unique cancel id long cancelid = _canceltracker.AssignId; // save cancel to order id relationship _cancel2order.Add(cancelid, number); bool isman; // see if it's a manual order if (!ismanorder.TryGetValue(number, out isman)) { isman = false; } // send cancel if (isman) // manual orders use nOrderRercordId { stiOrder.CancelOrder(acct, (int)number, null, cancelid.ToString()); } else { stiOrder.CancelOrder(acct, 0, number.ToString(), cancelid.ToString()); } if (VerboseDebugging) { debug("client cancel requested: " + number.ToString() + " " + cancelid.ToString()); } } else { debug("No record of order id: " + number.ToString()); } // see if empty yet if (_cancelq.hasItems) { Thread.Sleep(_CANCELWAIT); } } } // messages if (_msgq.hasItems) { GenericMessage gm = _msgq.Read(); switch (gm.Type) { case MessageTypes.SENDORDERPEGMIDPOINT: { // create order STIOrder order = new STIOrder(); // pegged 2 midmarket order.ExecInst = "M"; // get order Peg2Midpoint o = Peg2Midpoint.Deserialize(gm.Request); if (!o.isValid) { break; } if (VerboseDebugging) { debug("client P2M order: " + o.ToString()); } order.Symbol = o.symbol; order.PegDiff = (double)o.pegdiff; order.PriceType = STIPriceTypes.ptSTIPegged; bool side = o.size > 0; order.Side = getside(o.symbol, side); order.Quantity = Math.Abs(o.size); order.Destination = o.ex; order.ClOrderID = o.id.ToString(); order.Tif = "D"; string acct = Account != string.Empty ? Account : string.Empty; order.Account = o.Account != string.Empty ? o.Account : acct; int err = order.SubmitOrder(); string tmp = ""; if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp))) { idacct.Add(o.id, order.Account); } if (err < 0) { debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString()); } if (err == -1) { debug("Make sure you have set the account in sending program."); } } break; } } if (_lastimbalance != _imbalance) { _lastimbalance = _imbalance; // register for imbalance data stiQuote.RegisterForAllMdx(true); } } catch (Exception ex) { debug(ex.Message + ex.StackTrace); } if (_symsq.isEmpty && _orderq.isEmpty && _cancelq.isEmpty) { Thread.Sleep(_SLEEP); } } }