Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MPogotsky committed Sep 22, 2024
1 parent 30d46d0 commit 4ceb6eb
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 24 deletions.
26 changes: 14 additions & 12 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
50 changes: 50 additions & 0 deletions examples/GetAllSymbols.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <string>

#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>


boost::asio::awaitable<void> 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;
}
27 changes: 15 additions & 12 deletions examples/main.cpp → examples/GetBalance.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
#include <iostream>
#include <memory>
#include <string>

#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>

boost::asio::awaitable<void> 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<void> run(boost::asio::io_context &context)
{
xapi::Socket socket(context);
Expand All @@ -15,24 +28,14 @@ boost::asio::awaitable<void> 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
{
co_await socket.initSession(accountType);
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();
Expand Down
76 changes: 76 additions & 0 deletions examples/GetCandles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <memory>
#include <string>

#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>

boost::asio::awaitable<void> 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<void> 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;
}
102 changes: 102 additions & 0 deletions examples/TradeTransaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <memory>
#include <string>

#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <xapi/Xapi.hpp>

boost::asio::awaitable<int64_t> 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<boost::json::object>(response.at("returnData"));
auto order = boost::json::value_to<int64_t>(returnData.at("order"));
std::cout << "Order: " << order << std::endl;

co_return order;
}

boost::asio::awaitable<void> 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<boost::json::object>(response.at("returnData"));
auto returnStatus = boost::json::value_to<int>(returnData.at("requestStatus"));
const auto tradeStatus = static_cast<xapi::TradeStatus>(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<void> 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;
}

0 comments on commit 4ceb6eb

Please sign in to comment.