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::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());

}
Esempio n. 3
0
    bool OrderWrapper::processConfirmation(OrderConfirmation &confirmation)
    {
      LegDetail *legOrder = getLegOrder(confirmation.getSymbolId());
      if(!legOrder)
      {
        DEBUG_VARSHOW(_context->reqQryDebugLog(),"leg Order Found Null SymbolID:",confirmation.getSymbolId());
        DEBUG_FLUSH(_context->reqQryDebugLog());
      }
      if(confirmation.getOrderStatus() == API2::CONSTANTS::RSP_OrderStatus_CONFIRMED)
        _exchangeOrderId = _context->reqQryExchangeOrderId(legOrder->orderId);

      switch(confirmation.getOrderStatus())
      {
        case API2::CONSTANTS::RSP_OrderStatus_CONFIRMED:
          legOrder->updateOrderWrapper(confirmation.getOrderQuantity(),confirmation.getOrderPrice(),0);
        case API2::CONSTANTS::RSP_OrderStatus_NEW_REJECTED:
          _isPendingNew = false;break;

        case API2::CONSTANTS::RSP_OrderStatus_REPLACED:
          legOrder->updateOrderWrapper(confirmation.getOrderQuantity(),confirmation.getOrderPrice(),0);
        case API2::CONSTANTS::RSP_OrderStatus_REPLACE_REJECTED:
          _isPendingReplace = false;break;

        case API2::CONSTANTS::RSP_OrderStatus_CANCELED:
        case API2::CONSTANTS::RSP_OrderStatus_CANCELED_OF_IOC:
          legOrder->resetOrderWrapper();
        case API2::CONSTANTS::RSP_OrderStatus_CANCEL_REJECTED:
          _isPendingCancel = false;break;

        case API2::CONSTANTS::RSP_OrderStatus_FILLED:
          if(confirmation.getLastFillQuantity() + getLastFilledQuantity() != legOrder->_lastQuantity)//PartiallyFILLed
          {
            legOrder->updateOrderWrapper(getLastQuantity(),legOrder->_lastQuotedPrice,confirmation.getLastFillQuantity());
            break;
          }
          legOrder->resetOrderWrapper();
          break;

      }

      if(strcmp(confirmation.getExchangeOrderId().c_str(),_exchangeOrderId.c_str()))
      {
        DEBUG_MESSAGE(_context->reqQryDebugLog(),"Mismatch in order id");
        DEBUG_VARSHOW(_context->reqQryDebugLog(),"confirmation ExchangeOrderId",confirmation.getExchangeOrderId().c_str());
        DEBUG_VARSHOW(_context->reqQryDebugLog(),"_exchangeOrderId",_exchangeOrderId.c_str());
        return false;
      }
      return true;
    }
Esempio n. 4
0
    bool OrderWrapper::replaceOrder(DATA_TYPES::RiskStatus &risk, const DATA_TYPES::PRICE &price, 
        const DATA_TYPES::QTY &qty, DATA_TYPES::PRICE stopPrice)
    {
      if(!_orderId){
        DEBUG_MESSAGE(_context->reqQryDebugLog(), "OrderWrapper replace fail OrderId null");
        return false;
      }
      _isReset = false;
      API2::SingleOrder *order = _context->createNewOrder(_instrument,0,0,_mode,_orderType,_orderValidity);
      if(order)
      {
        API2::SingleOrder *origOrder = _context->reqQryOrder(_context->reqQryClientOrderId(_orderId));
        if(origOrder)
        {
          order->setPrice(price);
          order->setStopPrice(stopPrice);

          if(qty !=0)
            order->setQuantity(qty);
          else
            order->setQuantity(origOrder->getQuantity());

          order->setSelfTradeCancelFlag( _selfTradeOrderFlag );

          if(_context->reqQryOrderStatus(_orderId) != API2::CONSTANTS::RSP_OrderStatus_PENDING){

            _replaceOrder = order;
            if(_context->reqReplaceOrder(risk,_instrument,order,_orderId,_isSpread))
            {
              _isPendingReplace = true;
              return true;
            }
            else
            {

              DEBUG_MESSAGE(_context->reqQryDebugLog(), "_context->reqReplaceOrder fail");
              DEBUG_VARSHOW(_context->reqQryDebugLog(), "Replace Order Rejected. Reason:", risk);
              return false;
            }
          }
          else{
            DEBUG_MESSAGE(_context->reqQryDebugLog(),"Current state of Original Order is Pending");
            return false;
          }
        }
        DEBUG_MESSAGE(_context->reqQryDebugLog(),"Original Order NULL");
        return false;
      }
      else
      {
        DEBUG_MESSAGE(_context->reqQryDebugLog(), "Order Wrapper Not Set. Terminating strategy");
        _context->reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE);
        _context->reqTerminateStrategy();
        return false;
      }
      return false;

    }
Esempio n. 5
0
 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 LegDetail::reset()
 {
   if(!instrument){
     DEBUG_MESSAGE(_context->reqQryDebugLog(),"Instrument Null for Leg");
     _context->reqTerminateStrategy(true);
   }
   order = _context->createNewOrder(instrument,0,0,_mode,_orderType, _orderValidity, _productType);
   API2::AccountDetail account;
   orderId = _context->createNewOrderId(instrument,account,_mode);
   _isReset = true;
   DEBUG_VARSHOW(_context->reqQryDebugLog(),"Set LegData for SYmbolID: ",instrument->getSymbolId());
 }
Esempio n. 7
0
    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);
        }
    }
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());

}
Esempio n. 9
0
    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);
        }

    }
Esempio n. 10
0
    bool OrderWrapper::replaceOrder(DATA_TYPES::RiskStatus &risk, const DATA_TYPES::PRICE &price, const DATA_TYPES::QTY &qty)
    {
      if(!_instrument)
        return false;
      if(_orderLegData.size()>1){
        std::cout << "Cannot Modify Multileg Order" << std::endl;
        return false;
      }
      LegDetail &leg1 = _orderLegData[0];

      if(!leg1.orderId){
        DEBUG_MESSAGE(_context->reqQryDebugLog(), "OrderWrapper replace fail OrderId null");
        return false;
      }
      _isReset = false;

      API2::SingleOrder *order = _context->createNewOrder(leg1.instrument,0,0,leg1._mode,
          leg1._orderType, leg1._orderValidity, leg1._productType);
      if(order)
      {
        API2::SingleOrder *origOrder = _context->reqQryOrder(_context->reqQryClientOrderId(leg1.orderId));
        if(origOrder)
        {
          if(price !=0)
            order->setPrice(price);
          else
            order->setPrice(origOrder->getPrice());
          if(qty !=0)
            order->setQuantity(qty);
          else
            order->setQuantity(origOrder->getQuantity());

          if(_context->reqQryOrderStatus(leg1.orderId) != API2::CONSTANTS::RSP_OrderStatus_PENDING){

            if(_context->reqReplaceOrder(risk,leg1.instrument,order,leg1.orderId,_orderPriority))
            {
              _isPendingReplace = true;
              return true;
            }
            else
            {

              DEBUG_MESSAGE(_context->reqQryDebugLog(), "_context->reqReplaceOrder fail");
              DEBUG_VARSHOW(_context->reqQryDebugLog(), "Replace Order Rejected. Reason:", risk);
              return false;
            }
          }
          else{
            DEBUG_MESSAGE(_context->reqQryDebugLog(),"Current state of Original Order is Pending");
            return false;
          }
        }
        DEBUG_MESSAGE(_context->reqQryDebugLog(),"Original Order NULL");
        return false;
      }
      else
      {
        DEBUG_MESSAGE(_context->reqQryDebugLog(), "Order Wrapper Not Set. Terminating strategy");
        _context->reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE);
        _context->reqTerminateStrategy();
        return false;
      }
      return false;

    }