/** * Request from fornt end to modify params */ void TestContext::onCMDModifyStrategy(API2::AbstractUserParams* newParams) { DEBUG_METHOD(reqQryDebugLog()); _modify = true; API2::UserParams* modParams = (API2::UserParams*) newParams; /** * setting new params in a new data structure of params */ if(!setModifiedInternalParameters(modParams)) { DEBUG_MESSAGE(reqQryDebugLog(),"Front End Parameters Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_VERSION_DIFFERS); return; } /** * If no order pending , we can modifyStartegy */ if(_modify && !isAnyOrderPending()) modifyStrategy(); /** need to send this response to front end that strategy has successfully recieved modify command */ reqSendStrategyResponse(API2::CONSTANTS::RSP_ResponseType_STRATEGY_RUNNING, API2::CONSTANTS::RSP_RiskStatus_SUCCESS, API2::CONSTANTS::RSP_StrategyComment_USER_INPUT); }
bool API2::Test2::Context::setTestParameters() { _userParams._symbolId = reqQrySymbolID("ESMNSE ESMNSE RELIANCE"); DEBUG_VARSHOW(reqQryDebugLog(), "====SymbolID: ", _userParams._symbolId); //_userParams._symbolId = GET_SYMBOL_ID_NEW(268084,API2::CONSTANTS::CMD_ExchangeId_SMART,API2::CONSTANTS::CMD_SourceId_IB); // _userParams._symbolId = GET_SYMBOL_ID_NEW(2885,CMD::ExchangeId_ESMNSE,CMD::SourceId_ESMNSE); _userParams._priceType=1; _userParams._qty=1000; _userParams._side = API2::CONSTANTS::CMD_OrderMode_BUY; std::stringstream ss; _userParams.dump(ss, "Frontend Params:"); DEBUG_MESSAGE(reqQryDebugLog(),ss.str().c_str()); try{ _Instrument = createNewInstrument(_userParams._symbolId,true,true); _orderWrapper = API2::COMMON::OrderWrapper(_Instrument,API2::DATA_TYPES::OrderMode(_userParams._side),API2::CONSTANTS::CMD_OrderType_LIMIT, this); return true; } catch(API2::MarketDataSubscriptionFailedException e) { DEBUG_VARSHOW(reqQryDebugLog(), "Exception occured", e.what()); return false; } }
void API2::Test2::Context::onProcessOrderConfirmation(API2::OrderConfirmation &confirmation) { DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__); DEBUG_MESSAGE(reqQryDebugLog(), getStrConfirmation(confirmation).c_str()); _orderWrapper.processConfirmation(confirmation); }
void API2::Test2::Context::onFilled(API2::OrderConfirmation &confirmation, API2::COMMON::OrderId *orderId) { DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__ ); DEBUG_MESSAGE(reqQryDebugLog(), "Order Filled, Terminating"); reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_COMPLETED_SUCCESSFULLY); reqTerminateStrategy(false); }
void TestContext::onFrozen(API2::OrderConfirmation &confirmation, API2::COMMON::OrderId *orderId){ if(orderId == _buyOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Buy Order Frozen-----"); _buyOrderWrapper.processConfirmation(confirmation); } else if(orderId == _sellOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Sell Order Frozen-----"); _sellOrderWrapper.processConfirmation(confirmation); } }
void TestContext::onReplaceRejected(API2::OrderConfirmation &confirmation, API2::COMMON::OrderId* orderId){ if(orderId == _buyOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Buy Order Replace Rejected-----"); _buyOrderWrapper.processConfirmation(confirmation); } else if(orderId == _sellOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Sell Order Replace Rejected-----"); _sellOrderWrapper.processConfirmation(confirmation); } processStrategyState(); }
void TestContext::onPartialFill(API2::OrderConfirmation &confirmation, API2::COMMON::OrderId* orderId){ if(orderId == _buyOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Buy Order Partial Filled-----"); _buyOrderWrapper.processConfirmation(confirmation); _buyTotalTradedLots += confirmation.getLastFillQuantity() / _biddingInstrument->getStaticData()->marketLot; } else if(orderId == _sellOrderWrapper._orderId) { DEBUG_MESSAGE(reqQryDebugLog(),"-----Sell Order Partial Filled-----"); _sellOrderWrapper.processConfirmation(confirmation); _sellTotalTradedLots += confirmation.getLastFillQuantity() / _biddingInstrument->getStaticData()->marketLot; } }
void TestContext::terminateStrategy() { DEBUG_MESSAGE(reqQryDebugLog(),"Terminated From Front End"); reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_COMPLETED_SUCCESSFULLY); reqTerminateStrategy(); return; }
/** * Request from front end to terminate strategy */ void TestContext::onCMDTerminateStartegy() { DEBUG_METHOD(reqQryDebugLog()); _terminate = true; if(_terminate && !isAnyOrderPending()) terminateStrategy(); return; }
void TestContext::placeCancelOrder(API2::COMMON::OrderWrapper &wrapper) { // if order stands , then only we should go for its cancelation if( wrapper.getLastQuantity() ) { // canceling order if( !wrapper.cancelOrder( _riskStatus ) ) { // if risk failure comes DEBUG_MESSAGE( reqQryDebugLog(),"Order Cancel Failed" ); wrapper._isPendingCancel = false; terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_RMS_FAILURE); return; } DEBUG_MESSAGE( reqQryDebugLog(),"Order Canceled" ); } }
void TestContext::dump(){ /** * Params Dump */ DEBUG_VARSHOW(reqQryDebugLog(),"Symbol Id : ",_userParams._symbolIdBidding); DEBUG_VARSHOW(reqQryDebugLog(),"Buy Order Lots : ",_userParams._buyOrderLots); DEBUG_VARSHOW(reqQryDebugLog(),"Buy Total Lots : ",_userParams._buyTotalLots); DEBUG_VARSHOW(reqQryDebugLog(),"Sell Order Lots : ",_userParams._sellOrderLots); DEBUG_VARSHOW(reqQryDebugLog(),"Sell Total Lots : ",_userParams._sellTotalLots); DEBUG_VARSHOW(reqQryDebugLog(),"Buy Price : ",_userParams._buyPrice); DEBUG_VARSHOW(reqQryDebugLog(),"Sell Price : ",_userParams._sellPrice); DEBUG_VARSHOW(reqQryDebugLog(),"Max Diff : ",_userParams._maxDiff); }
void TestContext::mapFrontEndValues(){ DEBUG_METHOD(reqQryDebugLog()); /** * Copying the modifiedParams structure to original structure */ _userParams._buyOrderLots = _modUserParams._buyOrderLots; _userParams._sellOrderLots = _modUserParams._sellOrderLots; _userParams._buyTotalLots = _modUserParams._buyTotalLots; _userParams._sellTotalLots = _modUserParams._sellTotalLots; _userParams._buyPrice = _modUserParams._buyPrice; _userParams._sellPrice = _modUserParams._sellPrice; _userParams._maxDiff = _modUserParams._maxDiff; }
void API2::Test2::Context::onMarketDataEvent(UNSIGNED_LONG symbolId) { API2::DATA_TYPES::RiskStatus risk; // DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__ ); // DEBUG_FLUSH(reqQryDebugLog()); // reqQryMarketData(symbolId)->dump(); // return; if(_orderWrapper._isReset) { DEBUG_MESSAGE(reqQryDebugLog(), "Sending New Order"); if(!_orderWrapper.newOrder(risk,getPrice(),_userParams._qty)) { DEBUG_VARSHOW(reqQryDebugLog(), "Failed Sending New Order, Reason:", risk); DEBUG_FLUSH(reqQryDebugLog()); reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_RMS_FAILURE); reqTerminateStrategy(); } } else { switch(reqQryOrderStatus(_orderWrapper._orderId)) { case API2::CONSTANTS::RSP_OrderStatus_PENDING: return; case API2::CONSTANTS::RSP_OrderStatus_FILLED: case API2::CONSTANTS::RSP_OrderStatus_CANCELED: _orderWrapper.reset(); onMarketDataEvent(symbolId); return; case API2::CONSTANTS::RSP_OrderStatus_CONFIRMED: case API2::CONSTANTS::RSP_OrderStatus_PARTIALLY_FILLED: case API2::CONSTANTS::RSP_OrderStatus_REPLACED: if(_orderWrapper.replaceOrder(risk,getPrice(),_userParams._qty)){ DEBUG_MESSAGE(reqQryDebugLog(), "Sent Replace Order"); } else{ DEBUG_VARSHOW(reqQryDebugLog(), "Failed Replace Order, reason",risk); } } } DEBUG_FLUSH(reqQryDebugLog()); }
void SampleStrategy::sendReplaceOrder(DATA_TYPES::PRICE price, DATA_TYPES::QTY qty) { // DEBUG_METHOD(reqQryDebugLog()); HFT_Start_Timer_Define; API2::DATA_TYPES::RiskStatus risk; if(_orderWrapper->replaceOrder(risk,price,qty)) { HFT_Record_Timer(_LNReplaceOrder); } else { HFT_Record_Timer(_LNReplaceOrderReject); DEBUG_VARSHOW(reqQryDebugLog(), "Replace Order Rejected Internally, Exiting Strategy. Reject Reason:", risk); // reqTerminateStrategy(false); } }
API2::Test2::Context::Context(API2::StrategyParameters *params): API2::SGContext(params, "CustomTest2"), _Instrument(NULL), _userParams() { API2::UserParams *customOMMParams = (API2::UserParams *)params->getInfo(); DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__ ); customOMMParams->dump(); if(!setInternalParameters(customOMMParams)) { reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); reqTerminateStrategy(); } }
bool API2::Test2::Context::setInternalParameters(API2::UserParams *params) { #ifndef TESTING if(params->getValue("SYMBOL LEG1",_userParams._symbolId) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "Issue in SYMBOL LEG1"); return false; } if(params->getValue("Order Mode 1",_userParams._side) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "Order Mode 1"); return false; } if(params->getValue("Price Type",_userParams._priceType) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "issue in Price Type"); return false; } if(params->getValue("Order Qty",_userParams._qty) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "issue in Qty"); return false; } #else setTestParameters(); #endif std::stringstream ss; _userParams.dump(ss, "Frontend Params:"); DEBUG_MESSAGE(reqQryDebugLog(),ss.str().c_str()); try{ _Instrument = createNewInstrument(_userParams._symbolId,true,true); _orderWrapper = API2::COMMON::OrderWrapper(_Instrument,API2::DATA_TYPES::OrderMode(_userParams._side),API2::CONSTANTS::CMD_OrderType_LIMIT, this); return true; } catch(API2::MarketDataSubscriptionFailedException e) { DEBUG_VARSHOW(reqQryDebugLog(), "Exception occured", e.what()); return false; } DEBUG_FLUSH(reqQryDebugLog()); }
void SampleStrategy::sendNewOrder(DATA_TYPES::PRICE price, DATA_TYPES::QTY qty) { API2::DATA_TYPES::RiskStatus risk; HFT_Start_Timer_Define; if(_orderWrapper->newOrder(risk,price,qty)) { HFT_Record_Timer(_LNNewOrder); } else { HFT_Record_Timer(_LNNewOrderReject); DEBUG_VARSHOW(reqQryDebugLog(), "New Order Rejected Internally, Exiting Strategy. Reject Reason:", risk); // _LNNewOrderReject.dumpDataSet(); HFT_Start_Timer _orderWrapper->reset(); HFT_Record_Timer(_LNResetOrder); // reqTerminateStrategy(false); } }
void TestContext::setOrderWrapper() { /* * Creating instances of OrderWrapper here , to place or cancel orders through this instance */ DEBUG_METHOD(reqQryDebugLog()); API2::AccountDetail account; _buyOrderWrapper = API2::COMMON::OrderWrapper( _biddingInstrument, _buyOrder, this, account ); _sellOrderWrapper = API2::COMMON::OrderWrapper( _biddingInstrument, _sellOrder, this, account ); }
void TestContext::registerSymbols() { /* * Creating Instrument for Leg */ DEBUG_METHOD(reqQryDebugLog()); if((GET_EXCHANGE_ID(_userParams._symbolIdBidding) != API2::CONSTANTS::CMD_ExchangeId_NSECM ) && (GET_EXCHANGE_ID(_userParams._symbolIdBidding) != API2::CONSTANTS::CMD_ExchangeId_NSEFO ) && ( GET_EXCHANGE_ID(_userParams._symbolIdBidding)!= API2::CONSTANTS::CMD_ExchangeId_NSECDS)) _biddingInstrument = createNewInstrument(_userParams._symbolIdBidding,true,true,false); else { /** * If Snapshot feed required for this instrument , pass third argument as true * false in case of TBT */ _biddingInstrument = createNewInstrument(_userParams._symbolIdBidding,true,!_isTbtEnabled); } }
/** * Map the modified front end params to front end params */ void TestContext::modifyStrategy(){ DEBUG_METHOD(reqQryDebugLog()); _modify = false; mapFrontEndValues(); dump(); }
/** * Places Orders */ void TestContext::placeOrder(const API2::DATA_TYPES::OrderMode &orderMode) { DEBUG_METHOD(reqQryDebugLog()); /** * If strategy has been requested for termination or modification, * need to return from here */ if(_terminate || _modify) return; /** * If any order is pending * we need to return from here */ if(isAnyOrderPending()) return; /** * Buy Case */ if(orderMode == API2::CONSTANTS::CMD_OrderMode_BUY) { API2::DATA_TYPES::QTY qty = std::min(_userParams._buyOrderLots,_userParams._buyTotalLots - _buyTotalTradedLots); API2::DATA_TYPES::PRICE price = _userParams._buyPrice; // price in terms of tick size // e.g if price is 200.07 // converting it to 200.05 price = price - (price % _buyOrderWrapper._instrument->getStaticData()->tickSize); qty *= _biddingInstrument->getStaticData()->marketLot; if( qty != 0 ) qty += _buyOrderWrapper.getLastFilledQuantity(); if(qty <= 0 || price <= 0) { placeCancelOrder( _buyOrderWrapper ); return; } /** * getLastQuantity() returns the last quantity of which order was placed * * getLastQuotedPrice() returns the last price on which order was placed */ if(qty == _buyOrderWrapper.getLastQuantity() && price == _buyOrderWrapper.getLastQuotedPrice()) return; /** * If getLastQuantity() returns a non zero , * that means an order is already standing * we can replace it */ if(_buyOrderWrapper.getLastQuantity()) { /** *replaces the existing order */ if(!_buyOrderWrapper.replaceOrder(_riskStatus,price,qty)) { /** * If replaceOrder returns false * RMS checks have been failed * Terminating strategy here */ _buyOrderWrapper._isPendingReplace = false; DEBUG_MESSAGE(reqQryDebugLog()," Buy Could not be Replaced"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } DEBUG_MESSAGE(reqQryDebugLog()," Buy Replaced"); } else { /** *If getLastQuantity() returns 0 * we need to reset orderWrapper(generate an new order structure and new common orderId) * */ _buyOrderWrapper.reset(); /** * places a new order */ if(!_buyOrderWrapper.newOrder(_riskStatus,price,qty)) { /** * If placeOrder returns false * RMS checks have been failed * Terminating strategy here */ _buyOrderWrapper._isPendingNew = false; DEBUG_MESSAGE(reqQryDebugLog()," Buy Could not be Placed") terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } DEBUG_MESSAGE(reqQryDebugLog()," Buy Placed"); } } /** * Sell Case */ else if(orderMode == API2::CONSTANTS::CMD_OrderMode_SELL) { API2::DATA_TYPES::QTY qty = std::min(_userParams._sellOrderLots,_userParams._sellTotalLots - _sellTotalTradedLots); API2::DATA_TYPES::PRICE price = _userParams._sellPrice; // price in terms of tick size // e.g. if price is 300.02 , then converting it to 300.05 SIGNED_LONG rem = price % _sellOrderWrapper._instrument->getStaticData()->tickSize; if(rem) { price = price + (_sellOrderWrapper._instrument->getStaticData()->tickSize - rem); } qty *= _biddingInstrument->getStaticData()->marketLot; if( qty != 0 ) qty += _sellOrderWrapper.getLastFilledQuantity(); if(qty <= 0 || price <= 0) { placeCancelOrder( _sellOrderWrapper ); return; } if(qty == _sellOrderWrapper.getLastQuantity() && price == _sellOrderWrapper.getLastQuotedPrice()) return; if(_sellOrderWrapper.getLastQuantity()) { if(!_sellOrderWrapper.replaceOrder(_riskStatus,price,qty)) { _sellOrderWrapper._isPendingReplace = false; DEBUG_MESSAGE(reqQryDebugLog()," Sell Could not be Replaced"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } DEBUG_MESSAGE(reqQryDebugLog()," Sell Replaced"); } else { _sellOrderWrapper.reset(); if(!_sellOrderWrapper.newOrder(_riskStatus,price,qty)) { _buyOrderWrapper._isPendingNew = false; DEBUG_MESSAGE(reqQryDebugLog()," Sell Could not be Placed") terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } DEBUG_MESSAGE(reqQryDebugLog()," Sell Placed"); } } }
void API2::Test2::Context::onReplaced(API2::OrderConfirmation &confirmation, API2::COMMON::OrderId *orderId) { DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__ ); }
TestContext::TestContext(API2::StrategyParameters *params): API2::SGContext(params,"Test_Strategy"), _riskStatus(API2::CONSTANTS::RSP_RiskStatus_MAX), _buyOrder(API2::CONSTANTS::CMD_OrderMode_BUY), _sellOrder(API2::CONSTANTS::CMD_OrderMode_SELL), _modify(false), _terminate(false), _isTbtEnabled(false), _buyTotalTradedLots(0), _sellTotalTradedLots(0) { /** * convert api paramaters into your own data structure */ API2::UserParams* customParams = (API2::UserParams*) params->getInfo(); if(!setInternalParameters(customParams)) { DEBUG_MESSAGE(reqQryDebugLog(), "Front End Parameters Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } /* * * register symbolids * */ try { registerSymbols(); } catch(API2::MarketDataSubscriptionFailedException &e) { std::cout<<e.what()<< std::endl; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } catch(API2::InstrumentNotFoundException& e) { std::cout<<e.what() <<std::endl; std::cout << "TBT Subscription Failed, terminating strategy\n"; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } catch(std::exception &e) { std::cout<<e.what() <<std::endl; std::cout << "TBT Subscription Failed, terminating strategy\n"; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } if(_terminate) return; setOrderWrapper(); dump(); reqTimerEvent(10000); DEBUG_FLUSH(reqQryDebugLog()); }