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; }
int main(int argc, char *argv[]) { std::string procName = "CreateEntry"; if (argc == 1) { printHelp(procName); return -1; } bool bWasError = false; 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, true, loginParams->getSessionID(), loginParams->getPin()); session->subscribeSessionStatus(sessionListener); bool bConnected = login(session, sessionListener, loginParams); 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) { O2G2Ptr<IO2GLoginRules> loginRules = session->getLoginRules(); if (loginRules) { O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = loginRules->getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider->getBaseUnitSize(sampleParams->getInstrument(), account); int iAmount = iBaseUnitSize * sampleParams->getLots(); int iCondDistEntryLimit = tradingSettingsProvider->getCondDistEntryLimit(sampleParams->getInstrument()); int iCondDistEntryStop = tradingSettingsProvider->getCondDistEntryStop(sampleParams->getInstrument()); std::string sOrderType = getEntryOrderType(offer->getBid(), offer->getAsk(), sampleParams->getRate(), sampleParams->getBuySell(), offer->getPointSize(), iCondDistEntryLimit, iCondDistEntryStop); O2G2Ptr<IO2GRequest> request = createEntryOrderRequest(session, offer->getOfferID(), account->getAccountID(), iAmount, sampleParams->getRate(), sampleParams->getBuySell(), sOrderType.c_str(), sampleParams->getExpDate()); if (request) { responseListener->setRequestID(request->getRequestID()); session->sendRequest(request); if (responseListener->waitEvents()) { 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 << "Cannot get login rules" << std::endl; bWasError = true; } } 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; }
int main(int argc, char *argv[]) { std::string procName = "GetLastOrderUpdate"; if (argc == 1) { printHelp(procName); return -1; } bool bWasError = false; LoginParams *loginParams = new LoginParams(argc, argv); SampleParams *sampleParams = new SampleParams(argc, argv); printSampleParams(procName, loginParams, sampleParams); if (!checkObligatoryParams(loginParams, sampleParams)) { delete loginParams; delete sampleParams; return -1; } IO2GSession *session = CO2GTransport::createSession(); SessionStatusListener *sessionListener = new SessionStatusListener(session, true, loginParams->getSessionID(), loginParams->getPin()); session->subscribeSessionStatus(sessionListener); bool bConnected = login(session, sessionListener, loginParams); 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) { O2G2Ptr<IO2GLoginRules> loginRules = session->getLoginRules(); if (loginRules) { O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = loginRules->getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider->getBaseUnitSize(sampleParams->getInstrument(), account); int iAmount = iBaseUnitSize * sampleParams->getLots(); O2G2Ptr<IO2GRequest> request = createTrueMarketOrderRequest(session, offer->getOfferID(), account->getAccountID(), iAmount, sampleParams->getBuySell()); if (request) { responseListener->setRequestID(request->getRequestID()); session->sendRequest(request); if (responseListener->waitEvents()) { std::string sOrderID = responseListener->getOrderID(); if (!sOrderID.empty()) { O2G2Ptr<IO2GRequest> updateRequest = getLastOrderUpdateRequest(session, sOrderID.c_str(), account->getAccountName()); if (updateRequest) { responseListener->setRequestID(updateRequest->getRequestID()); session->sendRequest(updateRequest); if (responseListener->waitEvents()) { O2G2Ptr<IO2GResponse> response = responseListener->getResponse(); if (response && response->getType() == GetLastOrderUpdate) { O2G2Ptr<IO2GResponseReaderFactory> readerFactory = session->getResponseReaderFactory(); if (readerFactory) { O2G2Ptr<IO2GLastOrderUpdateResponseReader> reader = readerFactory->createLastOrderUpdateResponseReader(response); char sTime[20]; formatDate(reader->getOrder()->getStatusTime(), sTime); std::cout << "Last order update: UpdateType='" << reader->getUpdateType() << "', " << "OrderID='" << reader->getOrder()->getOrderID() << "', " << "Status='" << reader->getOrder()->getStatus() << "', " << "StatusTime='" << sTime << "'" << std::endl; } } 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 << "Response waiting timeout expired" << std::endl; bWasError = true; } } else { std::cout << "Cannot create request" << std::endl; bWasError = true; } } else { std::cout << "Cannot get login rules" << std::endl; bWasError = true; } } 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(); delete loginParams; delete sampleParams; if (bWasError) return -1; return 0; }
int main(int argc, char *argv[]) { std::string procName = "CreateOTO"; 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(); session->useTableManager(Yes, 0); 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; ResponseListener *responseListener = new ResponseListener(); TableListener *tableListener = new TableListener(responseListener); session->subscribeResponse(responseListener); O2G2Ptr<IO2GTableManager> tableManager = session->getTableManager(); O2GTableManagerStatus managerStatus = tableManager->getStatus(); while (managerStatus == TablesLoading) { Sleep(50); managerStatus = tableManager->getStatus(); } if (managerStatus == TablesLoadFailed) { std::cout << "Cannot refresh all tables of table manager" << std::endl; } O2G2Ptr<IO2GAccountRow> account = getAccount(tableManager, sampleParams->getAccount()); if (account) { if (bIsAccountEmpty) { sampleParams->setAccount(account->getAccountID()); std::cout << "Account: " << sampleParams->getAccount() << std::endl; } O2G2Ptr<IO2GOfferRow> offer = getOffer(tableManager, sampleParams->getInstrument()); if (offer) { O2G2Ptr<IO2GLoginRules> loginRules = session->getLoginRules(); if (loginRules) { O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = loginRules->getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider->getBaseUnitSize(sampleParams->getInstrument(), account); int iAmount = iBaseUnitSize * sampleParams->getLots(); // For the purpose of this example we will place primary order 30 pips below the current market price // and our secondary order 15 pips below the current market price double dRatePrimary = offer->getAsk() - 30.0 * offer->getPointSize(); double dRateSecondary = offer->getAsk() - 15.0 * offer->getPointSize(); O2G2Ptr<IO2GRequest> request = createOTORequest(session, offer->getOfferID(), account->getAccountID(), iAmount, dRatePrimary, dRateSecondary); if (request) { tableListener->subscribeEvents(tableManager); std::vector<std::string> requestIDs(request->getChildrenCount()); for (int i=0; i<request->getChildrenCount(); ++i) { requestIDs[i] = request->getChildRequest(i)->getRequestID(); } responseListener->setRequestIDs(requestIDs); tableListener->setRequestIDs(requestIDs); session->sendRequest(request); if (responseListener->waitEvents()) { std::cout << "Done!" << std::endl; } else { std::cout << "Response waiting timeout expired" << std::endl; bWasError = true; } tableListener->unsubscribeEvents(tableManager); tableListener->release(); } else { std::cout << "Cannot create request" << std::endl; bWasError = true; } } else { std::cout << "Cannot get login rules" << std::endl; bWasError = true; } } 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; }
int main(int argc, char *argv[]) { std::string procName = "OpenPosition"; 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(); session->useTableManager(Yes, 0); 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; ResponseListener *responseListener = new ResponseListener(); TableListener *tableListener = new TableListener(responseListener); session->subscribeResponse(responseListener); O2G2Ptr<IO2GTableManager> tableManager = session->getTableManager(); O2GTableManagerStatus managerStatus = tableManager->getStatus(); while (managerStatus == TablesLoading) { Sleep(50); managerStatus = tableManager->getStatus(); } if (managerStatus == TablesLoadFailed) { std::cout << "Cannot refresh all tables of table manager" << std::endl; } O2G2Ptr<IO2GAccountRow> account = getAccount(tableManager, sampleParams->getAccount()); if (account && strcmp(account->getMaintenanceType(), "0") != 0) // not netting account { if (bIsAccountEmpty) { sampleParams->setAccount(account->getAccountID()); std::cout << "Account: " << sampleParams->getAccount() << std::endl; } O2G2Ptr<IO2GOfferRow> offer = getOffer(tableManager, sampleParams->getInstrument()); if (offer) { O2G2Ptr<IO2GLoginRules> loginRules = session->getLoginRules(); if (loginRules) { O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = loginRules->getTradingSettingsProvider(); int iBaseUnitSize = tradingSettingsProvider->getBaseUnitSize(sampleParams->getInstrument(), account); int iAmount = iBaseUnitSize * sampleParams->getLots(); O2G2Ptr<IO2GRequest> request = createTrueMarketOrderRequest(session, offer->getOfferID(), account->getAccountID(), iAmount, sampleParams->getBuySell()); if (request) { tableListener->subscribeEvents(tableManager); responseListener->setRequestID(request->getRequestID()); tableListener->setRequestID(request->getRequestID()); 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; } tableListener->unsubscribeEvents(tableManager); tableListener->release(); } else { std::cout << "Cannot create request" << std::endl; bWasError = true; } } else { std::cout << "Cannot get login rules" << std::endl; bWasError = true; } } 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; }
// Print trading settings of the first account bool printTradingSettings(IO2GSession *session) { O2G2Ptr<IO2GLoginRules> loginRules = session->getLoginRules(); if (!loginRules) { std::cout << "Cannot get login rules" << std::endl; return false; } O2G2Ptr<IO2GResponse> accountsResponse = loginRules->getTableRefreshResponse(Accounts); if (!accountsResponse) { std::cout << "Cannot get response" << std::endl; return false; } O2G2Ptr<IO2GResponse> offersResponse = loginRules->getTableRefreshResponse(Offers); if (!offersResponse) { std::cout << "Cannot get response" << std::endl; return false; } O2G2Ptr<IO2GTradingSettingsProvider> tradingSettingsProvider = loginRules->getTradingSettingsProvider(); O2G2Ptr<IO2GResponseReaderFactory> factory = session->getResponseReaderFactory(); if (!factory) { std::cout << "Cannot create response reader factory" << std::endl; return false; } O2G2Ptr<IO2GAccountsTableResponseReader> accountsReader = factory->createAccountsTableReader(accountsResponse); O2G2Ptr<IO2GOffersTableResponseReader> instrumentsReader = factory->createOffersTableReader(offersResponse); O2G2Ptr<IO2GAccountRow> account = accountsReader->getRow(0); for (int i = 0; i < instrumentsReader->size(); ++i) { O2G2Ptr<IO2GOfferRow> instrumentRow = instrumentsReader->getRow(i); const char *sInstrument = instrumentRow->getInstrument(); int condDistStopForTrade = tradingSettingsProvider->getCondDistStopForTrade(sInstrument); int condDistLimitForTrade = tradingSettingsProvider->getCondDistLimitForTrade(sInstrument); int condDistEntryStop = tradingSettingsProvider->getCondDistEntryStop(sInstrument); int condDistEntryLimit = tradingSettingsProvider->getCondDistEntryLimit(sInstrument); int minQuantity = tradingSettingsProvider->getMinQuantity(sInstrument, account); int maxQuantity = tradingSettingsProvider->getMaxQuantity(sInstrument, account); int baseUnitSize = tradingSettingsProvider->getBaseUnitSize(sInstrument, account); O2GMarketStatus marketStatus = tradingSettingsProvider->getMarketStatus(sInstrument); int minTrailingStep = tradingSettingsProvider->getMinTrailingStep(); int maxTrailingStep = tradingSettingsProvider->getMaxTrailingStep(); double mmr = tradingSettingsProvider->getMMR(sInstrument, account); std::string sMarketStatus = "unknown"; switch (marketStatus) { case MarketStatusOpen: sMarketStatus = "Market Open"; break; case MarketStatusClosed: sMarketStatus = "Market Close"; break; } std::cout << "Instrument: " << sInstrument << ", Status: " << sMarketStatus << std::endl; std::cout << "Cond.Dist: ST=" << condDistStopForTrade << "; LT=" << condDistLimitForTrade << std::endl; std::cout << "Cond.Dist entry stop=" << condDistEntryStop << "; entry limit=" << condDistEntryLimit << std::endl; std::cout << "Quantity: Min=" << minQuantity << "; Max=" << maxQuantity << "; Base unit size=" << baseUnitSize << "; MMR=" << mmr << std::endl; double mmr2=0, emr=0, lmr=0; if (tradingSettingsProvider->getMargins(sInstrument, account, mmr2, emr, lmr)) { std::cout << "Three level margin: MMR=" << mmr2 << "; EMR=" << emr << "; LMR=" << lmr << std::endl; } else { std::cout << "Single level margin: MMR=" << mmr2 << "; EMR=" << emr << "; LMR=" << lmr << std::endl; } std::cout << "Trailing step: " << minTrailingStep << "-" << maxTrailingStep << std::endl; } return true; }