void TLServer_WM::SrvGotOrder(TLOrder order) { if (!order.isValid()) return; for (size_t i = 0; i<client.size(); i++) if (client[i]!="") TLSend(ORDERNOTIFY,order.Serialize(),client[i]); }
char getPI(TLOrder o) { if (o.isLimit() && o.isStop()) return PRICE_INDICATOR_STOPLIMIT; else if (o.isLimit()) return PRICE_INDICATOR_LIMIT; else if (o.isStop()) return PRICE_INDICATOR_STOP; return PRICE_INDICATOR_MARKET; }
void TLServer_IP::SrvGotOrder(TLOrder order) { if (order.symbol=="") return; for (size_t i = 0; i<client.size(); i++) if (client[i]!="") TLSend(ORDERNOTIFY,order.Serialize(),client[i]); }
int TWS_TLServer::SendOrder(TLOrder o) { // check our order if (o.symbol=="") return UNKNOWN_SYMBOL; if (!o.isValid()) return INVALID_ORDERSIZE; // create broker-specific objects here Order* order(new Order); order->auxPrice = o.isTrail() ? o.trail : o.stop; order->lmtPrice = o.price; order->orderType = (o.isStop()) ? "STP" : (o.isLimit() ? "LMT" : (o.isTrail() ? "TRAIL" : "MKT")); order->totalQuantity = (long)o.size; order->action = (o.side) ? "BUY" : "SELL"; order->account = o.account; order->tif = o.TIF; order->outsideRth = true; order->orderId = newOrder(o.id,o.account); order->transmit = true; Contract* contract(new Contract); contract->symbol = o.symbol; contract->localSymbol = o.localsymbol!="" ? o.localsymbol : o.symbol; if (o.exchange=="") o.exchange = "SMART"; contract->exchange = o.exchange; contract->secType = o.security; contract->currency = o.currency; // get the TWS session associated with our account EClient* client; if (o.account=="") // if no account specified, get default client = m_link[this->validlinkids[0]]; else // otherwise get the session our account is logged into client = GetOrderSink(o.account); // place our order if (client!=NULL) client->placeOrder(order->orderId,*contract,*order); delete order; delete contract; return OK; }
UINT __cdecl DoOrderResend(LPVOID param) { // we need a queue object LS_TLWM* tl = (LS_TLWM*)param; // ensure it's present if (tl==NULL) { return OK; } while (tl->_go) { if (tl->_resends!=0) { // process every valid order in queue for (uint i = 0; i<tl->resend.size(); i++) { // get tradelink order TLOrder ord = tl->resend[i]; // skip if invalid if (!ord.isValid()) continue; // mark as invalid TLOrder blank; tl->resend[i] = blank; tl->_resends--; // resend it tl->SendOrder(ord); // notify CString tmp; tmp.Format("%s Resending order: %s",ord.symbol,ord.Serialize()); tl->D(tmp); // wait briefly Sleep(25); } } Sleep(100); } return OK; }
long LS_TLWM::gettype(TLOrder o) { long type = o.isStop() ? L_OrderType::STOP : ( o.isLimit() ? L_OrderType::LIMIT : L_OrderType::MARKET); if ((o.TIF=="DAY") || (o.TIF=="IOC") || (o.TIF=="GTC") || (o.TIF=="")) return type; if (o.TIF=="MOC") return L_OrderType::MOC; else if ((o.TIF=="LOO") || (o.TIF=="OPG")) return L_OrderType::LOO; else if (o.TIF=="LOC") return L_OrderType::LOC; else if (o.TIF=="MOO") return L_OrderType::MOO; return type; }
TLOrder LS_TLWM::ProcessOrder(L_Order* order) { TLOrder o; long lsid = order->L_ReferenceId(); // ensure it exists if (order) { // udpate order information CTime ct = CTime(order->L_CreateTime()); o.symbol = CString(order->L_Symbol()); o.time = (ct.GetHour()*10000)+(ct.GetMinute()*100)+ct.GetSecond(); o.date = (ct.GetYear()*10000)+(ct.GetMonth()*100)+ct.GetDay(); o.exchange = CString(order->L_OriginalMarket()); o.price = order->L_AveragePrice(); o.side = order->L_TradeSide()=='B'; o.stop = order->L_SecondaryPrice(); o.size = (o.side ? 1 : -1) * abs(order->L_ActiveShares()); o.account = CString(accounts[0]->L_TraderId()); // try to save this order bool isnew = saveOrder(order,0); // if it fails, we already have it so get the id // if it succeeds, we should be able to get the id anyways int64 tlid = fetchOrderId(order); o.id = tlid; if (!_noverb) { if (isnew) { CString tmp; tmp.Format("%s new order tlid: %lld lsid: %i ord: %s",o.symbol,o.id,lsid,o.Serialize()); v(tmp); } else { CString tmp; tmp.Format("%s processed order tlid: %lld lsid: %i ord: %s",o.symbol,tlid,lsid,o.Serialize()); v(tmp); } } } else { CString tmp; tmp.Format("%i order id not found.",lsid); D(tmp); } return o; }
int TWS_TLWM::SendOrder(TLOrder o) { // check our order if (!o.isValid()) return GOTNULLORDER; if (o.symbol=="") return UNKNOWNSYM; // create broker-specific objects here Order* order(new Order); order->auxPrice = o.stop; order->lmtPrice = o.price; order->orderType = (o.stop!=0) ? "STP" : (o.price!=0 ? "LMT" : "MKT"); order->totalQuantity = (long)o.size; order->action = (o.side) ? "BUY" : "SELL"; order->account = o.account; order->tif = o.TIF; order->outsideRth = true; if (o.id!=0) // if ID is provided, keep it order->orderId = o.id; else // otherwise just get the next id order->orderId = getNextOrderId(o.account); order->transmit = true; Contract* contract(new Contract); contract->symbol = o.symbol; contract->localSymbol = o.localsymbol; if (o.exchange=="") o.exchange = "SMART"; contract->exchange = o.exchange; contract->secType = o.security; contract->currency = o.currency; // get the TWS session associated with our account EClient* client; if (o.account=="") // if no account specified, get default client = m_link[this->validlinkids[0]]; else // otherwise get the session our account is logged into client = GetOrderSink(o.account); // place our order if (client!=NULL) client->placeOrder(order->orderId,*contract,*order); delete order; delete contract; return OK; }
static void __stdcall Basics() { const CString sym = "LVS"; const double x = 10; const int s = 200; TLOrder o; CFIX_ASSERT(!o.isFilled()); CFIX_ASSERT(!o.isValid()); o.symbol = sym; o.size = s; CFIX_ASSERT(o.isMarket()); o.price = x; CFIX_ASSERT(o.isLimit()); o.stop = x; CFIX_ASSERT(o.isStop()); }
int TLClient_WM::SendOrder(TLOrder o) { return TLSend(SENDORDER,o.Serialize(),_him); }
int LS_TLWM::SendOrder(TLOrder o) { if (accounts.size()==0) return INVALID_ACCOUNT; // if order id is set and not-unique, reject order if ((o.id!=0) && (!IdIsUnique(o.id))) return DUPLICATE_ORDERID; L_Summary* summary = preload(o.symbol); L_Account* account = NULL; for (uint i = 0; i<accounts.size(); i++) { if (CString(accounts[i]->L_TraderId())==o.account) account = accounts[i]; } if (account==NULL) account = accounts[0]; // do maximum size account check if (_hasmaxaccountpospct) { double totalbp = account->L_BuyingPower(); L_Position* pos = account->L_FindPosition(o.symbol); if (pos) { double poscost = pos->L_DollarValue(); double pospcttotal = totalbp==0 ? 1 : poscost/totalbp; if (pospcttotal>_maxaccountpospct) { CString tmp; tmp.Format("%s rejecting for position size exceeded: %s",o.symbol,o.Serialize()); D(tmp); return REJECTEDACCOUNTSAFETY; } } } if (_hasmaxaccount) { double bpinuse = account->L_BuyingPowerInUse(); if (abs(bpinuse)>=_sendordermaxaccount) { CString tmp; tmp.Format("%s rejecting for account BP safety: %s",o.symbol,o.Serialize()); D(tmp); return REJECTEDACCOUNTSAFETY; } } if (_hasmaxconnectorshares) { if (_curconnectorshares >= _maxconnectorshares) { CString tmp; tmp.Format("%s rejecting for max api/connector shares: %s",o.symbol,o.Serialize()); D(tmp); return REJECTEDACCOUNTSAFETY; } } if (_hasmaxpositionsize) { L_Position* pos = account->L_FindPosition(o.symbol); // only run check if we have a position if (pos) { long size = pos->L_Shares(); if (abs(size)>_maxpositionsize) { CString tmp; tmp.Format("%s rejecting for max position size: %s",o.symbol,o.Serialize()); D(tmp); return REJECTEDACCOUNTSAFETY; } } } CString ex = o.exchange; CString ex2 = NULL; if (o.exchange.FindOneOf("+")!=-1) { std::vector<CString> exr; gsplit(o.exchange,CString("+"),exr); ex = exr[0]; ex2 = exr[1]; } // check for loading if (!summary || !summary->L_IsValid() || !summary->L_IsInit()) { if (!_resendsummarynotloaded) return SYMBOL_NOT_LOADED; CString tmp; tmp.Format("%s Not loaded yet, Queing for resend: %s",o.symbol,o.Serialize()); D(tmp); resend.push_back(o); _resends++; return 0; } //convert the arguments char side = o.side ? L_Side::BUY : L_Side::SELL; double price = o.isStop() ? o.stop : o.price; // get correlation id long corid = 0; // associate order id and correlation id uint coridx = lscorrelationid.size(); lscorrelationid.push_back(corid); tlcorrelationid.push_back(o.id); // associate blank ids for orders lsorderid1.push_back(0); lsorderid2.push_back(0); lsorderidfinal.push_back(0); // save order tlorders.push_back(o); if (!_noverb) { CString tmp; tmp.Format("%s received api order tlid: %lld lscorid: %i ord: %s",o.symbol,o.id,corid,o.Serialize()); v(tmp); } // get appropriate tif long tif = gettif(o); long type = gettype(o); // prepare to receive the result L_Order** orderSent = NULL; L_Order** orderSent2 = NULL; // send the order account->L_SendOrder( summary, type, side, abs(o.size), price, ex, tif, false, abs(o.size), 0,ex2,&corid); lscorrelationid[coridx] = corid; // return result return 0; }