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; }
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 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; }
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; }
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; }