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(); }
RowType* ForexConnectWrapper::getTableRow(O2GTable table, std::string key, bool (*finderFunc)(RowType *, std::string), ReaderType* (*readerCreateFunc)(IO2GResponseReaderFactory* , IO2GResponse *)) { IO2GResponse *response; if( !loginRules->isTableLoadedByDefault(table) ) { IO2GRequest *request = mRequestFactory->createRefreshTableRequestByAccount(Trades, sAccountID.c_str()); Listener *ll = new Listener(session); response = ll->sendRequest(request); request->release(); ll->release(); if (!response) { log("No response to manual table refresh request"); throw "No response to manual table refresh request"; } } else { response = loginRules->getTableRefreshResponse(table); if (!response) { log("No response to automatic table refresh request"); throw "No response to automatic table refresh request"; } } ReaderType *reader = readerCreateFunc(mResponseReaderFactory, response); response->release(); RowType *row = NULL; for ( int i = 0; i < reader->size(); ++i ) { row = reader->getRow(i); if ( finderFunc(row, key) ) { break; } row->release(); row = NULL; } reader->release(); if (row == NULL) { std::stringstream ss; ss << "Could not find row for key " << key; log(ss.str()); throw ss.str().c_str(); } return row; }
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(); }
void ForexConnectWrapper::setSubscriptionStatus(std::string instrument, std::string status) { IO2GTableManager *tableManager = getLoadedTableManager(); IO2GOffersTable *offersTable = (IO2GOffersTable *)tableManager->getTable(::Offers); tableManager->release(); IO2GOfferTableRow *offerRow = NULL; IO2GTableIterator tableIterator; bool instrumentFound = false; while (offersTable->getNextRow(tableIterator, offerRow)) { if ( instrument == offerRow->getInstrument() ) { instrumentFound = true; IO2GRequestFactory *factory = session->getRequestFactory(); IO2GValueMap *valueMap = factory->createValueMap(); valueMap->setString(::Command, O2G2::Commands::SetSubscriptionStatus); valueMap->setString(::SubscriptionStatus, status.c_str()); valueMap->setString(::OfferID, offerRow->getOfferID()); IO2GRequest *request = factory->createOrderRequest(valueMap); valueMap->release(); Listener *ll = new Listener(session); IO2GResponse *response = ll->sendRequest(request); response->release(); request->release(); ll->release(); factory->release(); break; } } offerRow->release(); offersTable->release(); if (!instrumentFound) { std::stringstream ss; ss << "Could not find offer row for instrument " << instrument; log(ss.str()); throw ss.str().c_str(); } }
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; }