Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit cdeea54

Browse files
committed
Add README for examples
1 parent b0c15ca commit cdeea54

File tree

8 files changed

+155
-5
lines changed

8 files changed

+155
-5
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ Step-by-step guide to build the project using CMake.
9797
cmake --install .
9898
```
9999

100-
### Runing Tests
100+
## Usage
101+
To use xAPI, an active account on the xStation5 trading platform is required. It could be a real account, or demo account. To create one, you can visit [xtb.com](https://www.xtb.com)
102+
103+
Once your account is set up, you can leverage the Xapi library to connect to the platform and start trading.
104+
105+
For a detailed overview and practical usage of the library, refer to the [**Examples README**](examples/README.md). It contains step-by-step guides and illustrative examples to help you understand how to effectively use the library.
106+
107+
## Runing Tests
101108
To build the tests, follow these steps:
102109

103110
1. Navigate to the `build` directory.

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ project(ExampleApp)
33

44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
67

78
find_package(Xapi REQUIRED)
89

@@ -22,3 +23,4 @@ add_example(GetAllSymbols)
2223
add_example(GetBalance)
2324
add_example(GetCandles)
2425
add_example(TradeTransaction)
26+
add_example(RetrieveStreamSessionId)

examples/GetAllSymbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, char const *argv[])
4343
{
4444
boost::asio::io_context context;
4545

46-
boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached);
46+
boost::asio::co_spawn(context, run(context), boost::asio::detached);
4747

4848
context.run();
4949
return 0;

examples/GetBalance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int main(int argc, char const *argv[])
5858
{
5959
boost::asio::io_context context;
6060

61-
boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached);
61+
boost::asio::co_spawn(context, run(context), boost::asio::detached);
6262

6363
context.run();
6464
return 0;

examples/GetCandles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main(int argc, char const *argv[])
6969
{
7070
boost::asio::io_context context;
7171

72-
boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached);
72+
boost::asio::co_spawn(context, run(context), boost::asio::detached);
7373

7474
context.run();
7575
return 0;

examples/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Introduction
2+
3+
Xapi-cpp is a C++ library that provides a simple and easy-to-use API for interacting with the xStation5 trading platform.
4+
Built on top of the Boost libraries, it leverages Boost’s powerful and widely adopted capabilities, particularly for network-based applications. Boost provides robust, high-performance abstractions that simplify the development of complex asynchronous operations, making it an ideal choice for ensuring a reliable and efficient connection to the xStation5 platform.
5+
6+
## Key Concepts
7+
8+
### 1. Asynchronous Programming with Coroutines
9+
The library makes heavy use of C++20 coroutines and Boost.Asio to handle asynchronous operations. Each API call is suspended until the operation completes, allowing the program to remain responsive and efficient.
10+
11+
While C++20 provides the coroutine syntax (``co_await``, ``co_return``, ``co_yield``), it doesn’t define a complete framework for asynchronous execution or I/O. Libraries like Boost.Asio provide the necessary abstractions and asynchronous I/O handling that C++20 coroutines on their own do not.
12+
13+
### 2. Secure WebSocket Communication
14+
The xapi uses secure WebSockets (WebSockets over SSL) for communication, ensuring that the data transmitted between the client and server is encrypted. This provides a secure channel for sensitive information, such as account credentials and balance data.
15+
16+
### 3. Separate WebSocket Connections
17+
The xapi establishes separate WebSocket connections for **xapi::Socket** and **xapi::Stream**. This separation aims to minimize congestion on a single connection especially if one connection is handling high-frequency data (like streaming).
18+
19+
20+
# Getting Started
21+
Let`s break down some important steps to start using xStation in your project
22+
23+
## Awailable operations
24+
### 1. Socket Operations
25+
The **xapi::Socket** object is used to Request-Reply commands. Full list of commands is in [xApi Retrieving Trading Data](http://developers.xstore.pro/documentation/2.5.0#retrieving-trading-data)
26+
27+
### 2. Stream Operations
28+
The **xapi::Stream** object is used to manage real-time streaming data, such as balance updates, tick prises and so on. Full list of streaming operations is in [xApi Available Streaming Commands](http://developers.xstore.pro/documentation/2.5.0#available-streaming-commands)
29+
30+
Please note, that to use streaming operations, you first should get streaming id from **xapi::Socket** login() command.
31+
32+
## Cmake configuration
33+
Xapi supports ``find_package``, simplifying the process of linking the library to your project. A typical CMake configuration might look like this:
34+
35+
```cmake
36+
cmake_minimum_required(VERSION 3.10)
37+
project(ExampleApp)
38+
39+
set(CMAKE_CXX_STANDARD 20)
40+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
41+
42+
find_package(Xapi REQUIRED)
43+
44+
add_executable(ExampleApp main.cpp)
45+
46+
target_link_libraries(ExampleApp
47+
PRIVATE
48+
Boost::system
49+
Boost::json
50+
Xapi::Xapi
51+
)
52+
```
53+
54+
55+
## Simple example
56+
The simplest example is [RetrieveStreamSessionId](RetrieveStreamSessionId.cpp), which demonstrates how to connect to an xStation server using Xapi. The program initializes a session, logs in, and retrieves a stream session ID.
57+
58+
Additional examples can be found in the .cpp files within the examples folder.
59+
60+
## Compile examples
61+
You can compile all examples by following these steps:
62+
63+
1. Fix your credentials in the example you want to use. In ``run`` function:
64+
65+
```cpp
66+
const std::string accountId = "<your accountId>";
67+
const std::string password = "<your password>";
68+
69+
// If you are using real account, replace with "real"
70+
const std::string accountType = "demo";
71+
```
72+
73+
1. Create a `build` directory and navigate into it:
74+
75+
```bash
76+
mkdir build
77+
cd build
78+
```
79+
80+
2. Run CMake to configure and build the project:
81+
82+
```bash
83+
cmake ..
84+
cmake --build .
85+
```
86+
87+
3. Binaries will be stored in ``bin`` directory. Run example app that you want (f.e. RetrieveStreamSessionId)
88+
```bash
89+
./bin/RetrieveStreamSessionId
90+
```

examples/RetrieveStreamSessionId.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include <boost/asio.hpp>
5+
#include <boost/json.hpp>
6+
#include <xapi/Xapi.hpp>
7+
8+
9+
boost::asio::awaitable<void> run(boost::asio::io_context &context)
10+
{
11+
xapi::Socket socket(context);
12+
13+
// Replace with your actual credentials
14+
const std::string accountId = "accountId";
15+
const std::string password = "password";
16+
17+
// If you are using real account, replace with "real"
18+
const std::string accountType = "demo";
19+
20+
try
21+
{
22+
co_await socket.initSession(accountType);
23+
auto streamsessionId = co_await socket.login(accountId, password);
24+
25+
std::cout << "Stream session id: " << streamsessionId << std::endl;
26+
27+
co_await socket.closeSession();
28+
}
29+
catch (xapi::exception::ConnectionClosed &e)
30+
{
31+
std::cout << "Connection failed: " << e.what() << std::endl;
32+
}
33+
catch (xapi::exception::LoginFailed &e)
34+
{
35+
std::cout << "Logging failed: " << e.what() << std::endl;
36+
}
37+
catch (std::exception &e)
38+
{
39+
std::cout << e.what() << std::endl;
40+
}
41+
}
42+
43+
int main(int argc, char const *argv[])
44+
{
45+
boost::asio::io_context context;
46+
47+
boost::asio::co_spawn(context, run(context), boost::asio::detached);
48+
49+
context.run();
50+
return 0;
51+
}

examples/TradeTransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int main(int argc, char const *argv[])
9595
{
9696
boost::asio::io_context context;
9797

98-
boost::asio::co_spawn(context, run(std::ref(context)), boost::asio::detached);
98+
boost::asio::co_spawn(context, run(context), boost::asio::detached);
9999

100100
context.run();
101101
return 0;

0 commit comments

Comments
 (0)