diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 19ad2d5..0bd938f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -6,17 +6,19 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Xapi REQUIRED) -set( SOURCES - main.cpp -) +function(add_example name) -add_executable(ExampleApp - ${SOURCES} -) + add_executable(${name} ${name}.cpp) + target_link_libraries(${name} + PRIVATE + Boost::system + Boost::json + Xapi::Xapi + ) -target_link_libraries(ExampleApp PRIVATE - Boost::system - Boost::url - Boost::json - Xapi::Xapi -) +endfunction() + +add_example(GetAllSymbols) +add_example(GetBalance) +add_example(GetCandles) +add_example(TradeTransaction) diff --git a/examples/GetAllSymbols.cpp b/examples/GetAllSymbols.cpp new file mode 100644 index 0000000..bd45fe1 --- /dev/null +++ b/examples/GetAllSymbols.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include +#include +#include + + +boost::asio::awaitable run(boost::asio::io_context &context) +{ + xapi::Socket socket(context); + + // Add your credentials here + const std::string accountId = "accountId"; + const std::string password = "password"; + const std::string accountType = "demo"; + + try + { + co_await socket.initSession(accountType); + auto streamsessionId = co_await socket.login(accountId, password); + + auto result = co_await socket.getAllSymbols(); + std::cout << boost::json::serialize(result) << std::endl; + + co_await socket.closeSession(); + } + catch (xapi::exception::ConnectionClosed &e) + { + std::cout << "Connection failed: " << e.what() << std::endl; + } + catch (xapi::exception::LoginFailed &e) + { + std::cout << "Logging failed: " << e.what() << std::endl; + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } +} + +int main(int argc, char const *argv[]) +{ + boost::asio::io_context context; + + boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached); + + context.run(); + return 0; +} diff --git a/examples/main.cpp b/examples/GetBalance.cpp similarity index 74% rename from examples/main.cpp rename to examples/GetBalance.cpp index 58077b4..cf60d36 100644 --- a/examples/main.cpp +++ b/examples/GetBalance.cpp @@ -1,11 +1,24 @@ #include -#include #include #include #include #include +boost::asio::awaitable printBalance(xapi::Stream &stream) +{ + co_await stream.getBalance(); + int counter = 0; + while (counter < 5) + { + std::cout << "Processing " << counter << std::endl; + auto result = co_await stream.listen(); + std::cout << boost::json::serialize(result) << std::endl; + counter += 1; + } + co_await stream.stopBalance(); +} + boost::asio::awaitable run(boost::asio::io_context &context) { xapi::Socket socket(context); @@ -15,7 +28,6 @@ boost::asio::awaitable run(boost::asio::io_context &context) const std::string accountId = "accountId"; const std::string password = "password"; const std::string accountType = "demo"; - bool safe = true; try { @@ -23,16 +35,7 @@ boost::asio::awaitable run(boost::asio::io_context &context) auto streamsessionId = co_await socket.login(accountId, password); co_await stream.initSession(accountType, streamsessionId); - co_await stream.getBalance(); - int counter = 0; - while (counter < 5) - { - std::cout << "Processing " << counter << std::endl; - auto result = co_await stream.listen(); - std::cout << boost::json::serialize(result) << std::endl; - - counter += 1; - } + co_await printBalance(stream); co_await stream.closeSession(); co_await socket.closeSession(); diff --git a/examples/GetCandles.cpp b/examples/GetCandles.cpp new file mode 100644 index 0000000..5bd1cf9 --- /dev/null +++ b/examples/GetCandles.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +#include +#include +#include + +boost::asio::awaitable printCandles(xapi::Stream &stream) +{ + co_await stream.getCandles("US100"); + // Use getKeepAlive, to keep the connection alive + co_await stream.getKeepAlive(); + + // Print 4 candle results from the stream + int counter = 0; + while (counter < 4) + { + auto result = co_await stream.listen(); + + // Check for result type, as you`ll get keep alive messages + // every 3 seconds and candle messages every minute + if (result["command"] == "candle") + { + std::cout << boost::json::serialize(result) << std::endl; + counter += 1; + } + } + + co_await stream.stopCandles("US100"); +} + +boost::asio::awaitable run(boost::asio::io_context &context) +{ + xapi::Socket socket(context); + xapi::Stream stream(context); + + // Add your credentials here + const std::string accountId = "accountId"; + const std::string password = "password"; + const std::string accountType = "demo"; + + try + { + co_await socket.initSession(accountType); + auto streamsessionId = co_await socket.login(accountId, password); + co_await stream.initSession(accountType, streamsessionId); + + co_await printCandles(stream); + + co_await stream.closeSession(); + co_await socket.closeSession(); + } + catch (xapi::exception::ConnectionClosed &e) + { + std::cout << "Connection failed: " << e.what() << std::endl; + } + catch (xapi::exception::LoginFailed &e) + { + std::cout << "Logging failed: " << e.what() << std::endl; + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } +} + +int main(int argc, char const *argv[]) +{ + boost::asio::io_context context; + + boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached); + + context.run(); + return 0; +} diff --git a/examples/TradeTransaction.cpp b/examples/TradeTransaction.cpp new file mode 100644 index 0000000..4e70882 --- /dev/null +++ b/examples/TradeTransaction.cpp @@ -0,0 +1,102 @@ +#include +#include +#include + +#include +#include +#include + +boost::asio::awaitable executeTransaction(xapi::Socket &socket) +{ + auto response = co_await socket.tradeTransaction("US100", xapi::TradeCmd::BUY_LIMIT, xapi::TradeType::OPEN, 18000.00f, + 0.5f, 0.0f, 0.0f, 0.0f, 0, 0, ""); + + if (response["status"].as_bool() != true) + { + std::cout << "Failed to execute a transaction: " << boost::json::serialize(response) << std::endl; + co_return -1; + } + auto returnData = boost::json::value_to(response.at("returnData")); + auto order = boost::json::value_to(returnData.at("order")); + std::cout << "Order: " << order << std::endl; + + co_return order; +} + +boost::asio::awaitable checkTransactionStatus(xapi::Socket &socket, int64_t order) +{ + auto response = co_await socket.tradeTransactionStatus(order); + if (response["status"].as_bool() != true) + { + std::cout << "Failed to trade a transaction: " << response << std::endl; + co_return; + } + + auto returnData = boost::json::value_to(response.at("returnData")); + auto returnStatus = boost::json::value_to(returnData.at("requestStatus")); + const auto tradeStatus = static_cast(returnStatus); + + switch (tradeStatus) + { + case xapi::TradeStatus::ERROR: + std::cout << "The transaction finished with error: " << returnData["message"].as_string() << std::endl; + break; + case xapi::TradeStatus::PENDING: + std::cout << "The transaction is pending" << std::endl; + break; + case xapi::TradeStatus::ACCEPTED: + std::cout << "The transaction has been executed successfully" << std::endl; + break; + case xapi::TradeStatus::REJECTED: + std::cout << "The transaction has been rejected: " << returnData["message"].as_string() << std::endl; + break; + default: + std::cout << "Unknown transaction status" << std::endl; + break; + } +} + +boost::asio::awaitable run(boost::asio::io_context &context) +{ + xapi::Socket socket(context); + + // Add your credentials here + const std::string accountId = "accountId"; + const std::string password = "password"; + const std::string accountType = "demo"; + + socket.safeMode = false; + + try + { + co_await socket.initSession(type); + auto streamsessionId = co_await socket.login(accountId, password); + + const auto order_id = co_await executeTransaction(socket); + co_await checkTransactionStatus(socket, order_id); + + co_await socket.closeSession(); + } + catch (xapi::exception::ConnectionClosed &e) + { + std::cout << "Connection failed: " << e.what() << std::endl; + } + catch (xapi::exception::LoginFailed &e) + { + std::cout << "Logging failed: " << e.what() << std::endl; + } + catch (std::exception &e) + { + std::cout << e.what() << std::endl; + } +} + +int main(int argc, char const *argv[]) +{ + boost::asio::io_context context; + + boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached); + + context.run(); + return 0; +}