int SmallOrder::onResponseMsg(ResponseMessage &rsp) { if (rsp.type() == TYPE_NEW_ORDER_CONFIRM) { state = Sent; } else if (rsp.type() == TYPE_CANCEL_ORDER_CONFIRM) { if (state == TerminateCancel) { setRecycle(); return 0; } state = Ready; } else if (rsp.type() == TYPE_TRADE) { placeQty -= rsp.trade_quantity(); leftQty -= rsp.trade_quantity(); LOG(INFO) << "before trade, the place qty is: " << placeQty + rsp.trade_quantity(); LOG(INFO) << "after trade, the place qty is: " << placeQty; LOG(INFO) << "after trade, the left qty is: " << leftQty; if (leftQty == 0) { setRecycle(); } else if (leftQty < stockMinimumQty) { cancelOrder(true); } else if (placeQty == 0) { updateLeg(SwitchSignal::FinishPhase); state = Ready; } else if (placeQty < stockMinimumQty) { updateLeg(SwitchSignal::FinishPhase); cancelOrder(); } } else if (rsp.type() == TYPE_ERROR) { setRecycle(); } return 0; }
int SmallOrder::onMktUpdate(MarketUpdate &mkt) { switch (state) { case OrderState::Init: { halfSpread = calHalfSpread(mkt); arrivalOrderPT = CedarTimeHelper::getCurPTime(); activeLeg = Starting; updateLeg(SwitchSignal::Initial); placeOrder(mkt); LOG(INFO) << "snap half spread: " << halfSpread; logStatusInfo(mkt); break; } case Ready: { placeOrder(mkt); break; } //TODO: move time expiration checking to a separate timer event case Sent: { double currentMid = (mkt.bid_price(0) + mkt.ask_price(0)) * 0.5; double currentPrice = calPlacePrice(mkt); if (isMaxTimeExpired()) { LOG(INFO) << "max order life reached, cancelling..."; cancelOrder(true); } else if (isTimeExpired() || isMktMoved(currentMid)) { LOG(INFO) << "time expired or market moved, switching legs..."; LOG(INFO) << "current mid: " << currentMid; LOG(INFO) << "last mid: " << lastLegMid; updateLeg(SwitchSignal::TimeExpired); cancelOrder(); } else if (placePrice != currentPrice) { LOG(INFO) << "price expired, recalculating limit price..."; cancelOrder(); } break; } case TerminateCancel: default: return 0; } return 0; }
void Exchange_BTCe::setupApi(QtBitcoinTrader *mainClass, bool tickOnly) { tickerOnly=tickOnly; if(!tickerOnly) { connect(mainClass,SIGNAL(reloadOrders()),this,SLOT(reloadOrders())); connect(mainClass,SIGNAL(apiBuy(double, double)),this,SLOT(buy(double, double))); connect(mainClass,SIGNAL(apiSell(double, double)),this,SLOT(sell(double, double))); connect(mainClass,SIGNAL(cancelOrderByOid(QByteArray)),this,SLOT(cancelOrder(QByteArray))); connect(this,SIGNAL(ordersChanged(QString)),mainClass,SLOT(ordersChanged(QString))); connect(mainClass,SIGNAL(cancelOrderByOid(QByteArray)),this,SLOT(cancelOrder(QByteArray))); connect(mainClass,SIGNAL(getHistory(bool)),this,SLOT(getHistory(bool))); connect(this,SIGNAL(ordersLogChanged(QString)),mainClass,SLOT(ordersLogChanged(QString))); connect(this,SIGNAL(orderCanceled(QByteArray)),mainClass,SLOT(orderCanceled(QByteArray))); connect(this,SIGNAL(ordersIsEmpty()),mainClass,SLOT(ordersIsEmpty())); }
void Application::processCancel( const std::string& id, const std::string& symbol, Order::Side side ) { Order & order = m_orderMatcher.find( symbol, side, id ); order.cancel(); cancelOrder( order ); m_orderMatcher.erase( order ); }
void Exchange::setupApi(QtBitcoinTrader *mainClass, bool tickOnly)//Execute only once { QSettings settingsParams(":/Resources/Exchanges/"+currencyMapFile+".ini",QSettings::IniFormat); QStringList symbolList=settingsParams.childGroups(); QList<CurrencyPairItem> *newCurrPairs=new QList<CurrencyPairItem>; for(int n=0;n<symbolList.count();n++) { CurrencyPairItem currentPair=defaultCurrencyParams; currentPair.name=settingsParams.value(symbolList.at(n)+"/Symbol","").toByteArray(); if(currentPair.name.length()!=6)continue; currentPair.setSymbol(currentPair.name.toLatin1()); currentPair.name.insert(3,"/"); currentPair.currRequestSecond=settingsParams.value(symbolList.at(n)+"/RequestSecond","").toByteArray(); if(!currentPair.currRequestSecond.isEmpty()) currentPair.name.append(" ["+currentPair.currRequestSecond+"]"); currentPair.currRequestPair=settingsParams.value(symbolList.at(n)+"/Request","").toByteArray(); if(currentPair.currRequestPair.isEmpty())continue; currentPair.priceDecimals=settingsParams.value(symbolList.at(n)+"/PriceDecimals","").toInt(); currentPair.priceMin=settingsParams.value(symbolList.at(n)+"/PriceMin","").toDouble(); currentPair.tradeVolumeMin=settingsParams.value(symbolList.at(n)+"/TradeVolumeMin","").toDouble(); currentPair.tradePriceMin=settingsParams.value(symbolList.at(n)+"/TradePriceMin","").toDouble(); currentPair.currADecimals=settingsParams.value(symbolList.at(n)+"/ItemDecimals").toInt(); currentPair.currBDecimals=settingsParams.value(symbolList.at(n)+"/ValueDecimals").toInt(); (*newCurrPairs)<<currentPair; } connect(this,SIGNAL(setCurrencyPairsList(QList<CurrencyPairItem>*)),mainClass,SLOT(setCurrencyPairsList(QList<CurrencyPairItem>*))); emit setCurrencyPairsList(newCurrPairs); tickerOnly=tickOnly; if(!tickerOnly) { connect(mainClass,SIGNAL(apiBuy(QString, double, double)),this,SLOT(buy(QString, double, double))); connect(mainClass,SIGNAL(apiSell(QString, double, double)),this,SLOT(sell(QString, double, double))); connect(mainClass,SIGNAL(cancelOrderByOid(QString, QByteArray)),this,SLOT(cancelOrder(QString, QByteArray))); connect(mainClass,SIGNAL(getHistory(bool)),this,SLOT(getHistory(bool))); connect(this,SIGNAL(orderBookChanged(QString, QList<OrderItem> *)),mainClass,SLOT(orderBookChanged(QString, QList<OrderItem> *))); connect(this,SIGNAL(historyChanged(QList<HistoryItem>*)),mainClass,SLOT(historyChanged(QList<HistoryItem>*))); connect(this,SIGNAL(orderCanceled(QString, QByteArray)),mainClass,SLOT(orderCanceled(QString, QByteArray))); connect(this,SIGNAL(ordersIsEmpty()),mainClass,SLOT(ordersIsEmpty())); }
// MAIN PROCESSING LOOP void PosixTestClient::processMessages() { fd_set readSet, writeSet, errorSet; struct timeval tval; tval.tv_usec = 0; tval.tv_sec = 0; time_t now = time(NULL); switch (m_state) { case ST_STARTMONITOR: startMonitor(); break; case ST_PLACEORDER: placeOrder(); break; case ST_PLACEORDER_ACK: break; case ST_CANCELORDER: cancelOrder(); break; case ST_CANCELORDER_ACK: break; case ST_CHECKALGO: algoSweep(); break; case ST_CHECKALGO_ACK: break; case ST_PING: reqCurrentTime(); break; case ST_PING_ACK: if( m_sleepDeadline < now) { disconnect(); return; } break; case ST_IDLE: if (m_algoSweepTrigger < now) { m_state = ST_CHECKALGO; return; } if( m_sleepDeadline < now) { m_state = ST_PING; return; } break; } if( m_sleepDeadline > 0) { // initialize timeout with m_sleepDeadline - now tval.tv_sec = m_sleepDeadline - now; } if( m_pClient->fd() >= 0 ) { FD_ZERO( &readSet); errorSet = writeSet = readSet; FD_SET( m_pClient->fd(), &readSet); if( !m_pClient->isOutBufferEmpty()) FD_SET( m_pClient->fd(), &writeSet); FD_CLR( m_pClient->fd(), &errorSet); int ret = select( m_pClient->fd() + 1, &readSet, &writeSet, &errorSet, &tval); if( ret == 0) { // timeout return; } if( ret < 0) { // error disconnect(); return; } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &errorSet)) { // error on socket m_pClient->onError(); } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &writeSet)) { // socket is ready for writing m_pClient->onSend(); } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &readSet)) { // socket is ready for reading m_pClient->onReceive(); } } }
void TwsClient::processMessages() { fd_set readSet, writeSet, errorSet; struct timeval tval; tval.tv_usec = 0; tval.tv_sec = 0; time_t now = time(NULL); //std::cout<<"m_state "<<m_state<<std::endl; switch (m_state) { case ST_PLACEORDER: placeOrder(); break; case ST_PLACEORDER_ACK: break; case ST_CANCELORDER: cancelOrder(); break; case ST_CANCELORDER_ACK: break; case ST_PING: reqCurrentTime(); break; case ST_PING_ACK: if( m_sleepDeadline < now) { disconnect(); return; } break; case ST_IDLE: if( m_sleepDeadline < now) { m_state = ST_PING; return; } break; case ST_MKT_DATA: reqMktData(); } if( m_sleepDeadline > 0) { // initialize timeout with m_sleepDeadline - now tval.tv_sec = m_sleepDeadline - now; } if( m_pClient->fd() >= 0 ) { FD_ZERO( &readSet); errorSet = writeSet = readSet; FD_SET( m_pClient->fd(), &readSet); if( !m_pClient->isOutBufferEmpty()) FD_SET( m_pClient->fd(), &writeSet); FD_CLR( m_pClient->fd(), &errorSet); int ret = select( m_pClient->fd() + 1, &readSet, &writeSet, &errorSet, &tval); if( ret == 0) { // timeout return; } if( ret < 0) { // error disconnect(); return; } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &errorSet)) { // error on socket m_pClient->onError(); } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &writeSet)) { // socket is ready for writing m_pClient->onSend(); } if( m_pClient->fd() < 0) return; if( FD_ISSET( m_pClient->fd(), &readSet)) { // socket is ready for reading m_pClient->onReceive(); } } }