Exemplo n.º 1
0
// Find the first opened position by AccountID and OfferID
IO2GTradeRow *getTrade(IO2GTableManager *tableManager, const char *sAccountID, const char *sOfferID)
{
    O2G2Ptr<IO2GTradesTable> tradesTable = (IO2GTradesTable *)tableManager->getTable(Trades);
    for (int i = 0; i < tradesTable->size(); ++i)
    {
        O2G2Ptr<IO2GTradeRow> trade = tradesTable->getRow(i);
        if (strcmp(sAccountID, trade->getAccountID()) == 0 && 
                strcmp(sOfferID, trade->getOfferID()) == 0)
            return trade.Detach();
    }
    return NULL;
}
Exemplo n.º 2
0
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;
}
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;
}
Exemplo n.º 5
0
/** Request update data received data handler. */
void ResponseListener::onTablesUpdates(IO2GResponse *data)
{
    if (data)
    {
        O2G2Ptr<IO2GResponseReaderFactory> factory = mSession->getResponseReaderFactory();
        if (factory)
        {
            O2G2Ptr<IO2GTablesUpdatesReader> reader = factory->createTablesUpdatesReader(data);
            if (reader)
            {
                for (int i = 0; i < reader->size(); ++i)
                {
                    switch (reader->getUpdateTable(i))
                    {
                    case Orders:
                    {
                        O2G2Ptr<IO2GOrderRow> order = reader->getOrderRow(i);
                        if (mRequestID != order->getRequestID())
                            break;
                        switch (reader->getUpdateType(i))
                        {
                        case Insert:
                            if ((isClosingOrder(order) || isOpeningOrder(order)) &&
                                    mOrderMonitorNetting == NULL)
                            {
                                std::cout << "The order has been added. OrderID='" << order->getOrderID() << "', "
                                          << "Rate='" << order->getRate() << "', "
                                          << "TimeInForce='" << order->getTimeInForce() << "'"
                                          << std::endl;
                                O2G2Ptr<IO2GTradeRow> trade = NULL;
                                std::string sTradeID = std::string(order->getTradeID());
                                if (mTradesTable)
                                {
                                    for (int j = 0; j < mTradesTable->size(); ++j)
                                    {
                                        if (sTradeID == mTradesTable->getRow(j)->getTradeID())
                                        {
                                            trade = mTradesTable->getRow(j);
                                            break;
                                        }
                                    }
                                }
                                if (trade)
                                    mOrderMonitorNetting = new OrderMonitorNetting(order, trade->getAmount());
                                else
                                    mOrderMonitorNetting = new OrderMonitorNetting(order, 0);
                            }
                            break;
                        case Delete:
                            if (mOrderMonitorNetting)
                            {
                                std::cout << "The order has been deleted. OrderID='" << order->getOrderID() << "'"
                                          << std::endl;
                                mOrderMonitorNetting->onOrderDeleted(order);
                                if (mOrderMonitorNetting->isOrderCompleted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                            break;
                        }
                    }
                    break;
                    case Trades:
                    {
                        O2G2Ptr<IO2GTradeRow> trade = reader->getTradeRow(i);
                        switch (reader->getUpdateType(i))
                        {
                        case Insert:
                        {
                            if (mOrderMonitorNetting)
                            {
                                mOrderMonitorNetting->onTradeAdded(trade);
                                if (mOrderMonitorNetting->isOrderCompleted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                        break;
                        case Update:
                        {
                            if (mOrderMonitorNetting)
                            {
                                mOrderMonitorNetting->onTradeUpdated(trade);
                                if (mOrderMonitorNetting->isOrderCompleted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                        break;
                        }
                    }
                    break;
                    case ClosedTrades:
                    {
                        if (reader->getUpdateType(i) == Insert)
                        {
                            O2G2Ptr<IO2GClosedTradeRow> closedTrade = reader->getClosedTradeRow(i);
                            if (mOrderMonitorNetting)
                            {
                                mOrderMonitorNetting->onClosedTradeAdded(closedTrade);
                                if (mOrderMonitorNetting->isOrderCompleted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case Messages:
                    {
                        if (reader->getUpdateType(i) == Insert)
                        {
                            O2G2Ptr<IO2GMessageRow> message = reader->getMessageRow(i);
                            if (mOrderMonitorNetting)
                            {
                                mOrderMonitorNetting->onMessageAdded(message);
                                if (mOrderMonitorNetting->isOrderCompleted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case Accounts:
                    {
                        if (reader->getUpdateType(i) == Update && reader->getUpdateTable(i) == Accounts)
                        {
                            O2G2Ptr<IO2GAccountRow> account = reader->getAccountRow(i);
                            std::cout << "The balance has been changed. AccountID=" << account->getAccountID() << ", "
                                      << "Balance=" << std::fixed << account->getBalance() << std::endl;
                        }
                    }
                    break;
                    }
                }
            }
        }
    }
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
    std::string procName = "PrintTable";
    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();
    session->useTableManager(Yes, 0);

    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<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;
            }
        }
        else
        {
            std::cout << "No valid accounts" << std::endl;
            bWasError = true;
        }

        printOrders(tableManager, sampleParams->getAccount());
        std::cout << "Done!" << std::endl;

        logout(session, sessionListener);
    }
    else
    {
        bWasError = true;
    }

    session->unsubscribeSessionStatus(sessionListener);
    sessionListener->release();
    session->release();

    if (bWasError)
        return -1;
    return 0;
}
Exemplo n.º 8
0
int main(int argc, char *argv[])
{
    std::string procName = "NetStopLimit";
    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<IO2GTradeRow> trade = getTrade(tableManager, sampleParams->getAccount(), offer->getOfferID());
                if (trade)
                {
                    const char *sBuySell = strcmp(trade->getBuySell(), O2G2::Buy) == 0 ? O2G2::Sell : O2G2::Buy;
                    O2G2Ptr<IO2GRequest> requestStop = createNetEntryOrderRequest(session, offer->getOfferID(),
                            account->getAccountID(), sampleParams->getRateStop(), sBuySell, O2G2::Orders::StopEntry);
                    if (requestStop)
                    {
                        tableListener->subscribeEvents(tableManager);

                        responseListener->setRequestID(requestStop->getRequestID());
                        tableListener->setRequestID(requestStop->getRequestID());
                        session->sendRequest(requestStop);
                        if (responseListener->waitEvents())
                        {
                            O2G2Ptr<IO2GRequest> requestLimit = createNetEntryOrderRequest(session, offer->getOfferID(),
                                    account->getAccountID(), sampleParams->getRateLimit(), sBuySell, O2G2::Orders::LimitEntry);
                            if (requestLimit)
                            {
                                responseListener->setRequestID(requestLimit->getRequestID());
                                tableListener->setRequestID(requestLimit->getRequestID());
                                session->sendRequest(requestLimit);
                                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 << "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 << "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;
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
    std::string procName = "JoinNewGroup";
    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();
    int iContingencyGroupType = 1; // OCO group

    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;
            }

            std::vector<std::string> orderIDs(2);
            orderIDs[0] = sampleParams->getPrimaryID();
            orderIDs[1] = sampleParams->getSecondaryID();

            for (size_t i=0; i < orderIDs.size(); ++i)
            {
                if (!isOrderExists(session, sampleParams->getAccount(), orderIDs[i].c_str(), responseListener))
                {
                    std::cout << "Order '" << orderIDs[i] << "' does not exist" << std::endl;
                    bWasError = true;
                }
            }

            if (!bWasError)
            {
                O2G2Ptr<IO2GRequest> request = joinToNewGroupRequest(session, sampleParams->getAccount(),
                        orderIDs, iContingencyGroupType);
                if (request)
                {
                    responseListener->setRequestID(request->getRequestID());
                    responseListener->setOrderIDs(orderIDs);
                    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 << "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;
}
Exemplo n.º 11
0
/** Request update data received data handler. */
void ResponseListener::onTablesUpdates(IO2GResponse *data)
{
    if (data)
    {
        O2G2Ptr<IO2GResponseReaderFactory> factory = mSession->getResponseReaderFactory();
        if (factory)
        {
            O2G2Ptr<IO2GTablesUpdatesReader> reader = factory->createTablesUpdatesReader(data);
            if (reader)
            {
                for (int i = 0; i < reader->size(); ++i)
                {
                    switch (reader->getUpdateTable(i))
                    {
                    case Orders:
                    {
                        O2G2Ptr<IO2GOrderRow> order = reader->getOrderRow(i);
                        if (reader->getUpdateType(i) == Insert)
                        {
                            if (mBatchOrderMonitor)
                            {
                                std::cout << "The order has been added. OrderID='" << order->getOrderID() << "', "
                                        << "Rate='" << order->getRate() << "', "
                                        << "TimeInForce='" << order->getTimeInForce() << "'"
                                        << std::endl;
                                mBatchOrderMonitor->onOrderAdded(order);
                            }
                        }
                        else if (reader->getUpdateType(i) == Delete)
                        {
                            if (mBatchOrderMonitor)
                            {
                                std::cout << "The order has been deleted. OrderID='" << order->getOrderID() << "'"
                                        << std::endl;
                                mBatchOrderMonitor->onOrderDeleted(order);
                                if (mBatchOrderMonitor->isBatchExecuted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case Trades:
                    {
                        if (reader->getUpdateType(i) == Insert)
                        {
                            O2G2Ptr<IO2GTradeRow> trade = reader->getTradeRow(i);
                            if (mBatchOrderMonitor)
                            {
                                mBatchOrderMonitor->onTradeAdded(trade);
                                if (mBatchOrderMonitor->isBatchExecuted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case ClosedTrades:
                    {
                        if (reader->getUpdateType(i) == Insert)
                        {
                            O2G2Ptr<IO2GClosedTradeRow> closedTrade = reader->getClosedTradeRow(i);
                            if (mBatchOrderMonitor)
                            {
                                mBatchOrderMonitor->onClosedTradeAdded(closedTrade);
                                if (mBatchOrderMonitor->isBatchExecuted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case Messages:
                    {
                        if (reader->getUpdateType(i) == Insert)
                        {
                            O2G2Ptr<IO2GMessageRow> message = reader->getMessageRow(i);
                            if (mBatchOrderMonitor)
                            {
                                mBatchOrderMonitor->onMessageAdded(message);
                                if (mBatchOrderMonitor->isBatchExecuted())
                                {
                                    printResult();
                                    SetEvent(mResponseEvent);
                                }
                            }
                        }
                    }
                    break;
                    case Accounts:
                    {
                        if (reader->getUpdateType(i) == Update && reader->getUpdateTable(i) == Accounts)
                        {
                            O2G2Ptr<IO2GAccountRow> account = reader->getAccountRow(i);
                            std::cout << "The balance has been changed. AccountID=" << account->getAccountID() << ", "
                                    << "Balance=" << std::fixed << account->getBalance() << std::endl;
                        }
                    }
                    break;
                    }
                }
            }
        }
    }
}
Exemplo n.º 12
0
int main(int argc, char *argv[])
{
    std::string procName = "SubscriptionStatus";
    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 = getOfferAndPrint(session, sampleParams->getInstrument());
            if (offer)
            {
                O2G2Ptr<IO2GRequest> request = createSetSubscriptionStatusRequest(session, offer->getOfferID(), sampleParams->getStatus(), responseListener);
                if (request)
                {
                    responseListener->setRequestID(request->getRequestID());
                    session->sendRequest(request);
                    if (responseListener->waitEvents())
                    {
                        O2G2Ptr<IO2GResponse> response = responseListener->getResponse();
                        if (response && response->getType() == CommandResponse)
                        {
                            printf("Subscription status for '%s' is set to '%s'\n",
                                    sampleParams->getInstrument(), sampleParams->getStatus());
                        }
                        printMargins(session, account, offer);
                        updateMargins(session, responseListener);
                        printMargins(session, account, offer);
                        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 << "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;
}