void ForexConnectWrapper::closeMarket(const std::string tradeID, int amount) { IO2GTradeTableRow *trade = getTradeTableRow(tradeID); if (!trade) { std::stringstream ss; ss << "Could not find trade with ID = " << tradeID; log(ss.str()); throw ss.str().c_str(); } IO2GValueMap *valuemap = mRequestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, O2G2::Orders::TrueMarketClose); valuemap->setString(AccountID, sAccountID.c_str()); valuemap->setString(OfferID, trade->getOfferID()); valuemap->setString(TradeID, tradeID.c_str()); valuemap->setString(BuySell, ( strncmp(trade->getBuySell(), "B", 1) == 0 ? O2G2::Sell : O2G2::Buy ) ); trade->release(); valuemap->setInt(Amount, amount); //valuemap->setString(CustomID, "Custom"); IO2GRequest *request = mRequestFactory->createOrderRequest(valuemap); valuemap->release(); TableListener *tableListener = new TableListener(); tableListener->setRequestID(request->getRequestID()); IO2GTableManager *tableManager = getLoadedTableManager(); subscribeTableListener(tableManager, tableListener); Listener *ll = new Listener(session); IO2GResponse *response = ll->sendRequest(request); request->release(); ll->release(); if (!response) { std::stringstream ss; ss << "Failed to get response to closeMarket request " << tradeID << " " << amount; log(ss.str()); throw ss.str().c_str(); } tableListener->waitForTableUpdate(); response->release(); unsubscribeTableListener(tableManager, tableListener); tableListener->release(); tableManager->release(); }
void ForexConnectWrapper::openMarket(const std::string symbol, const std::string direction, int amount) { if (direction != O2G2::Sell && direction != O2G2::Buy) { log("Direction must be 'B' or 'S'"); throw "Direction must be 'B' or 'S'"; } std::string sOfferID = getOfferID(symbol); IO2GValueMap *valuemap = mRequestFactory->createValueMap(); valuemap->setString(Command, O2G2::Commands::CreateOrder); valuemap->setString(OrderType, O2G2::Orders::TrueMarketOpen); valuemap->setString(AccountID, sAccountID.c_str()); valuemap->setString(OfferID, sOfferID.c_str()); valuemap->setString(BuySell, direction.c_str()); valuemap->setInt(Amount, amount); valuemap->setString(TimeInForce, O2G2::TIF::IOC); IO2GRequest *orderRequest = mRequestFactory->createOrderRequest(valuemap); valuemap->release(); TableListener *tableListener = new TableListener(); tableListener->setRequestID(orderRequest->getRequestID()); IO2GTableManager *tableManager = getLoadedTableManager(); subscribeTableListener(tableManager, tableListener); Listener *ll = new Listener(session); IO2GResponse *response = ll->sendRequest(orderRequest); orderRequest->release(); ll->release(); if (!response) { std::stringstream ss; ss << "Failed to send openMarket request " << symbol << " " << direction << " " << amount; log(ss.str()); throw ss.str().c_str(); } tableListener->waitForTableUpdate(); response->release(); unsubscribeTableListener(tableManager, tableListener); tableListener->release(); tableManager->release(); }
int main(int argc, char* argv[]) { std::string procName = "CloseAllPositionsByInstrument"; if (argc == 1) { printHelp(procName); return -1; } LoginParams *loginParams = new LoginParams(argc, argv); SampleParams *sampleParams = new SampleParams(argc, argv); printSampleParams(procName, loginParams, sampleParams); if (!checkObligatoryParams(loginParams, sampleParams)) return -1; IO2GSession *session = CO2GTransport::createSession(); SessionStatusListener *sessionListener = new SessionStatusListener(session, false, loginParams->getSessionID(), loginParams->getPin()); session->subscribeSessionStatus(sessionListener); bool bConnected = login(session, sessionListener, loginParams); bool bWasError = false; if (bConnected) { bool bIsAccountEmpty = !sampleParams->getAccount() || strlen(sampleParams->getAccount()) == 0; O2G2Ptr<IO2GAccountRow> account = getAccount(session, sampleParams->getAccount()); ResponseListener *responseListener = new ResponseListener(session); session->subscribeResponse(responseListener); if (account) { if (bIsAccountEmpty) { sampleParams->setAccount(account->getAccountID()); std::cout << "Account: " << sampleParams->getAccount() << std::endl; } O2G2Ptr<IO2GOfferRow> offer = getOffer(session, sampleParams->getInstrument()); if (offer) { CloseOrderData closeOrdersData; if (getCloseOrdersData(session, responseListener, sampleParams->getAccount(), offer->getOfferID(), closeOrdersData)) { O2G2Ptr<IO2GRequest> request = createCloseMarketNettingOrderRequest(session, &closeOrdersData); if (request) { std::vector<std::string> requestIDs(request->getChildrenCount()); for (int i = 0; i < request->getChildrenCount(); ++i) { IO2GRequest *requestChild = request->getChildRequest(i); requestIDs[i] = requestChild->getRequestID(); requestChild->release(); } responseListener->setRequestIDs(requestIDs); session->sendRequest(request); if (responseListener->waitEvents()) { Sleep(1000); // Wait for the balance update std::cout << "Done!" << std::endl; } else { std::cout << "Response waiting timeout expired" << std::endl; bWasError = true; } } else { std::cout << "Cannot create request" << std::endl; bWasError = true; } } else { std::cout << "There are no opened positions for instrument '" << sampleParams->getInstrument() << "'" << std::endl; } } else { std::cout << "The instrument '" << sampleParams->getInstrument() << "' is not valid" << std::endl; bWasError = true; } } else { std::cout << "No valid accounts" << std::endl; bWasError = true; } session->unsubscribeResponse(responseListener); responseListener->release(); logout(session, sessionListener); } else { bWasError = true; } session->unsubscribeSessionStatus(sessionListener); sessionListener->release(); session->release(); if (bWasError) return -1; return 0; }