IO2GRequest *createNetEntryOrderRequest(IO2GSession *session, const char *sOfferID, const char *sAccountID, double dRate, const char *sBuySell, const char *sOrderType) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, sOrderType); valuemap->setString(AccountID, sAccountID); valuemap->setString(OfferID, sOfferID); valuemap->setString(BuySell, sBuySell); valuemap->setString(NetQuantity, "Y"); valuemap->setDouble(Rate, dRate); valuemap->setString(CustomID, "EntryOrder"); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
IO2GRequest *joinToNewGroupRequest(IO2GSession *session, const char *sAccountID, std::vector<std::string> &orderIDs, int iContingencyType) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::JoinToNewContingencyGroup); valuemap->setInt(ContingencyGroupType, iContingencyType); for (size_t i=0; i < orderIDs.size(); ++i) { O2G2Ptr<IO2GValueMap> valuemapChild = requestFactory->createValueMap(); valuemapChild->setString(OrderID, orderIDs[i].c_str()); valuemapChild->setString(AccountID, sAccountID); valuemap->appendChild(valuemapChild); } O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
// Update margin requirements void updateMargins(IO2GSession *session, ResponseListener *responseListener) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::UpdateMarginRequirements); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return; } responseListener->setRequestID(request->getRequestID()); session->sendRequest(request); if (!responseListener->waitEvents()) { std::cout << "Response waiting timeout expired" << std::endl; return; } O2G2Ptr<IO2GResponse> response = responseListener->getResponse(); if (response && response->getType() == MarginRequirementsResponse) { O2G2Ptr<IO2GResponseReaderFactory> responseFactory = session->getResponseReaderFactory(); if (responseFactory) { responseFactory->processMarginRequirementsResponse(response); std::cout << "Margin requirements have been updated" << std::endl; } } }
bool ForexConnectClient::openPosition(const std::string& instrument, const std::string& buysell, int amount) { if (buysell != O2G2::Sell && buysell != O2G2::Buy) { return false; } std::map<std::string, std::string> offers = getOffers(); std::string offerID; std::map<std::string, std::string>::const_iterator offer_itr = offers.find(instrument); if (offer_itr != offers.end()) { offerID = offer_itr->second; } else { BOOST_LOG_TRIVIAL(error) << "Could not find offer row for instrument " << instrument; return false; } O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = mpLoginRules->getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider->getBaseUnitSize(instrument.c_str(), mpAccountRow); O2G2Ptr<IO2GValueMap> valuemap = mpRequestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, O2G2::Orders::TrueMarketOpen); valuemap->setString(AccountID, mAccountID.c_str()); valuemap->setString(OfferID, offerID.c_str()); valuemap->setString(BuySell, buysell.c_str()); valuemap->setInt(Amount, amount * iBaseUnitSize); valuemap->setString(TimeInForce, O2G2::TIF::IOC); valuemap->setString(CustomID, "TrueMarketOrder"); O2G2Ptr<IO2GRequest> request = mpRequestFactory->createOrderRequest(valuemap); if (!request) { BOOST_LOG_TRIVIAL(error) << mpRequestFactory->getLastError(); return false; } mpResponseListener->setRequestID(request->getRequestID()); mpSession->sendRequest(request); if (mpResponseListener->waitEvents()) { Sleep(1000); // Wait for the balance update BOOST_LOG_TRIVIAL(info) << "Done!"; return true; } BOOST_LOG_TRIVIAL(error) << "Response waiting timeout expired"; return false; }
// Subscribe or unsubscribe an instrument IO2GRequest *createSetSubscriptionStatusRequest(IO2GSession *session, const char *sOfferID, const char *sStatus, ResponseListener *responseListener) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::SetSubscriptionStatus); valuemap->setString(SubscriptionStatus, sStatus); valuemap->setString(OfferID, sOfferID); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
// Create GetLastOrderUpdate request IO2GRequest *getLastOrderUpdateRequest(IO2GSession *session, const char *sOrderID, const char *sAccountName) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::GetLastOrderUpdate); valuemap->setString(Key, O2G2::KeyType::OrderID); valuemap->setString(Id, sOrderID); // value of Key valuemap->setString(AccountName, sAccountName); // Account name, not Account ID O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
IO2GRequest *createEntryOrderRequest(IO2GSession *session, const char *sOfferID, const char *sAccountID, int iAmount, double dRate, const char *sBuySell, const char *sOrderType, const char *expDate) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, sOrderType); valuemap->setString(AccountID, sAccountID); valuemap->setString(OfferID, sOfferID); valuemap->setString(BuySell, sBuySell); valuemap->setInt(Amount, iAmount); valuemap->setDouble(Rate, dRate); valuemap->setString(CustomID, "EntryOrder"); if(strcmp(expDate, "") != 0) { valuemap->setString(TimeInForce, "GTD"); // 'Good till Date' valuemap->setString(ExpireDateTime, expDate); // FIX UTCTimestamp format: "yyyyMMdd- HH:mm:ss.SSS" (milliseconds are optional) } O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
IO2GRequest *createTrueMarketOrderRequest(IO2GSession *session, const char *sOfferID, const char *sAccountID, int iAmount, const char *sBuySell) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, O2G2::Orders::TrueMarketOpen); valuemap->setString(AccountID, sAccountID); valuemap->setString(OfferID, sOfferID); valuemap->setString(BuySell, sBuySell); valuemap->setInt(Amount, iAmount); valuemap->setString(CustomID, "TrueMarketOrder"); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
bool ForexConnectClient::closePosition(const std::string& tradeID) { TableHandler<Trades, IO2GTradesTable, IO2GTradeTableRow> handler(mpSession); IO2GTradeTableRow *tradeRow = NULL; IO2GTableIterator tableIterator; while (true) { tradeRow = handler.getNextRow(); if (!tradeRow) { BOOST_LOG_TRIVIAL(error) << "Could not find trade with ID = " << tradeID; return false; } if (tradeID == tradeRow->getTradeID()) { break; } } O2G2Ptr<IO2GValueMap> valuemap = mpRequestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, O2G2::Orders::TrueMarketClose); valuemap->setString(AccountID, mAccountID.c_str()); valuemap->setString(OfferID, tradeRow->getOfferID()); valuemap->setString(TradeID, tradeID.c_str()); valuemap->setString(BuySell, (strcmp(tradeRow->getBuySell(), O2G2::Buy) == 0) ? O2G2::Sell : O2G2::Buy); valuemap->setInt(Amount, tradeRow->getAmount()); valuemap->setString(CustomID, "CloseMarketOrder"); O2G2Ptr<IO2GRequest> request = mpRequestFactory->createOrderRequest(valuemap); if (!request) { BOOST_LOG_TRIVIAL(error) << mpRequestFactory->getLastError(); return false; } mpResponseListener->setRequestID(request->getRequestID()); mpSession->sendRequest(request); if (mpResponseListener->waitEvents()) { Sleep(1000); // Wait for the balance update BOOST_LOG_TRIVIAL(info) << "Done!"; return true; } BOOST_LOG_TRIVIAL(error) << "Response waiting timeout expired"; return false; }
IO2GRequest *createCloseMarketNettingOrderRequest(IO2GSession *session, CloseOrderData *closeOrdersData) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); O2G2Ptr<IO2GValueMap> batchValuemap = requestFactory->createValueMap(); batchValuemap->setString(Command, O2G2::Commands::CreateOrder); std::string sOfferID = closeOrdersData->offerID; std::string sAccountID = closeOrdersData->account; OrderSide side = closeOrdersData->side; std::string sOrderType = O2G2::Orders::TrueMarketClose; switch (side) { case Buy: { O2G2Ptr<IO2GValueMap> childValuemap = requestFactory->createValueMap(); childValuemap->setString(Command, O2G2::Commands::CreateOrder); childValuemap->setString(NetQuantity, "Y"); childValuemap->setString(OrderType, sOrderType.c_str()); childValuemap->setString(AccountID, sAccountID.c_str()); childValuemap->setString(OfferID, sOfferID.c_str()); childValuemap->setString(BuySell, O2G2::Buy); batchValuemap->appendChild(childValuemap); } break; case Sell: { O2G2Ptr<IO2GValueMap> childValuemap = requestFactory->createValueMap(); childValuemap->setString(Command, O2G2::Commands::CreateOrder); childValuemap->setString(NetQuantity, "Y"); childValuemap->setString(OrderType, sOrderType.c_str()); childValuemap->setString(AccountID, sAccountID.c_str()); childValuemap->setString(OfferID, sOfferID.c_str()); childValuemap->setString(BuySell, O2G2::Sell); batchValuemap->appendChild(childValuemap); } break; case Both: { O2G2Ptr<IO2GValueMap> buyValuemap = requestFactory->createValueMap(); buyValuemap->setString(Command, O2G2::Commands::CreateOrder); buyValuemap->setString(NetQuantity, "Y"); buyValuemap->setString(OrderType, sOrderType.c_str()); buyValuemap->setString(AccountID, sAccountID.c_str()); buyValuemap->setString(OfferID, sOfferID.c_str()); buyValuemap->setString(BuySell, O2G2::Buy); batchValuemap->appendChild(buyValuemap); O2G2Ptr<IO2GValueMap> sellValuemap = requestFactory->createValueMap(); sellValuemap->setString(Command, O2G2::Commands::CreateOrder); sellValuemap->setString(NetQuantity, "Y"); sellValuemap->setString(OrderType, sOrderType.c_str()); sellValuemap->setString(AccountID, sAccountID.c_str()); sellValuemap->setString(OfferID, sOfferID.c_str()); sellValuemap->setString(BuySell, O2G2::Sell); batchValuemap->appendChild(sellValuemap); } break; } IO2GRequest *request = requestFactory->createOrderRequest(batchValuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request; }
IO2GRequest *createOTORequest(IO2GSession *session, const char *sOfferID, const char *sAccountID, int iAmount, double dRatePrimary, double dRateSecondary) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (!requestFactory) { std::cout << "Cannot create request factory" << std::endl; return NULL; } O2G2Ptr<IO2GValueMap> valuemap = requestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOTO); O2G2Ptr<IO2GValueMap> valuemapPrimary = requestFactory->createValueMap(); valuemapPrimary->setString(Command, O2G2::Commands::CreateOrder); valuemapPrimary->setString(OrderType, O2G2::Orders::StopEntry); valuemapPrimary->setString(AccountID, sAccountID); valuemapPrimary->setString(OfferID, sOfferID); valuemapPrimary->setString(BuySell, O2G2::Sell); valuemapPrimary->setInt(Amount, iAmount); valuemapPrimary->setDouble(Rate, dRatePrimary); valuemap->appendChild(valuemapPrimary); O2G2Ptr<IO2GValueMap> valuemapSecondary = requestFactory->createValueMap(); valuemapSecondary->setString(Command, O2G2::Commands::CreateOrder); valuemapSecondary->setString(OrderType, O2G2::Orders::StopEntry); valuemapSecondary->setString(AccountID, sAccountID); valuemapSecondary->setString(OfferID, sOfferID); valuemapSecondary->setString(BuySell, O2G2::Buy); valuemapSecondary->setInt(Amount, iAmount); valuemapSecondary->setDouble(Rate, dRateSecondary); valuemap->appendChild(valuemapSecondary); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemap); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); }
IO2GRequest *createOTOCORequest(IO2GSession *session, const char *sOfferID, const char *sAccountID, int iAmount, double dRatePrimary, double dRateOcoFirst, double dRateOcoSecond) { O2G2Ptr<IO2GRequestFactory> requestFactory = session->getRequestFactory(); if (requestFactory) { // Create OTO command O2G2Ptr<IO2GValueMap> valuemapMain = requestFactory->createValueMap(); valuemapMain->setString(Command, O2G2::Commands::CreateOTO); // Create Entry order O2G2Ptr<IO2GValueMap> valuemapPrimary = requestFactory->createValueMap(); valuemapPrimary->setString(Command, O2G2::Commands::CreateOrder); valuemapPrimary->setString(OrderType, O2G2::Orders::StopEntry); valuemapPrimary->setString(AccountID, sAccountID); valuemapPrimary->setString(OfferID, sOfferID); valuemapPrimary->setString(BuySell, O2G2::Sell); valuemapPrimary->setInt(Amount, iAmount); valuemapPrimary->setDouble(Rate, dRatePrimary); // Create OCO group of orders O2G2Ptr<IO2GValueMap> valuemapOCO = requestFactory->createValueMap(); valuemapOCO->setString(Command, O2G2::Commands::CreateOCO); // Create Entry orders to OCO O2G2Ptr<IO2GValueMap> valuemapOCOFirst = requestFactory->createValueMap(); valuemapOCOFirst->setString(Command, O2G2::Commands::CreateOrder); valuemapOCOFirst->setString(OrderType, O2G2::Orders::StopEntry); valuemapOCOFirst->setString(AccountID, sAccountID); valuemapOCOFirst->setString(OfferID, sOfferID); valuemapOCOFirst->setString(BuySell, O2G2::Buy); valuemapOCOFirst->setInt(Amount, iAmount); valuemapOCOFirst->setDouble(Rate, dRateOcoFirst); O2G2Ptr<IO2GValueMap> valuemapOCOSecond = requestFactory->createValueMap(); valuemapOCOSecond->setString(Command, O2G2::Commands::CreateOrder); valuemapOCOSecond->setString(OrderType, O2G2::Orders::StopEntry); valuemapOCOSecond->setString(AccountID, sAccountID); valuemapOCOSecond->setString(OfferID, sOfferID); valuemapOCOSecond->setString(BuySell, O2G2::Buy); valuemapOCOSecond->setInt(Amount, iAmount); valuemapOCOSecond->setDouble(Rate, dRateOcoSecond); // Fill the created groups. Please note, first you should add an entry order to OTO order and then OCO group of orders valuemapMain->appendChild(valuemapPrimary);; valuemapOCO->appendChild(valuemapOCOFirst); valuemapOCO->appendChild(valuemapOCOSecond); valuemapMain->appendChild(valuemapOCO); O2G2Ptr<IO2GRequest> request = requestFactory->createOrderRequest(valuemapMain); if (!request) { std::cout << requestFactory->getLastError() << std::endl; return NULL; } return request.Detach(); } else { std::cout << "Cannot create request factory" << std::endl; return NULL; } }