Skip to content

Commit 98248b2

Browse files
committed
Add example43 and refactor infrastructure usage in examples
- Added example43 to CMakeLists.txt and dsc.md with description for Bybit mainnet spot infrastructure - Updated example38 to use a more configurable strategy setup with Binance BTC/USDT hash key - Refactored example41 to separate Binance and Bybit infrastructure instances - Renamed variables for clarity and fixed registration inconsistency
1 parent 18b60c4 commit 98248b2

File tree

6 files changed

+145
-10
lines changed

6 files changed

+145
-10
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ add_subdirectory(example38)
3535
add_subdirectory(example39)
3636
add_subdirectory(example40)
3737
add_subdirectory(example41)
38-
add_subdirectory(example42)
38+
add_subdirectory(example42)
39+
add_subdirectory(example43)

examples/dsc.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| examples | Description |
22
|:-----------|:------------:|
3-
| example42 | infra bybit mainnet linear |
3+
| example43 | bybit mainnet spot infrastructure |
4+
| example42 | bybit mainnet linear infrastructure |
45
| example40 | run deviation and mutual information strategy on random data |
56
| example39 | generate and decompose crypto hash keys |
67
| example38 | run a strategy on random data |

examples/example38/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "aos/sliding_window_storage/c_sliding_window_storage.h"
55
#include "aos/mutual_information/c_mutual_information_calculator.h"
66
#include "aos/strategies/deviation_and_mutual_info/c_strategy.h"
7+
#include "aoe/binance/hash_utils/hash_utils.h"
78
#include "fmt/format.h"
89
#include "fmt/ranges.h"
910
#include "fmtlog.h"
@@ -18,8 +19,15 @@ int main() {
1819
market_triplet_manager.Connect(1, 2);
1920

2021
boost::asio::thread_pool thread_pool;
21-
22-
aos::strategies::deviation_and_mutual_information::Strategy<HashT, Price> strategy(5, market_triplet_manager);
22+
const auto binance_btc_usdt = aoe::binance::HashKey(
23+
aoe::binance::Category::kFutures, aos::NetworkEnvironment::kMainNet,
24+
aos::TradingPair::kBTCUSDT);
25+
constexpr aos::strategies::deviation_and_mutual_information::Config<
26+
HashT>
27+
config_strategy{5, 10, binance_btc_usdt};
28+
29+
30+
aos::strategies::deviation_and_mutual_information::Strategy<HashT, Price> strategy(market_triplet_manager, config_strategy);
2331

2432
StrategyEngineDefault<HashT, Price> strategy_engine(
2533
thread_pool, strategy);

examples/example41/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "aoe/aoe.h"
44
#include "aoe/binance/hash_utils/hash_utils.h"
5-
#include "aoe/binance/infrastructure/infrastructure.h"
5+
#include "aoe/binance/binance_futures_main_net_infrastructure/binance_futures_main_net_infrastructure.h"
66
#include "aoe/binance/order_book_sync/order_book_sync.h"
77
#include "aoe/binance/parser/json/ws/diff_response/parser.h"
88
#include "aoe/bybit/hash_utils/hash_utils.h"
@@ -84,14 +84,14 @@ int main(int argc, char** argv) {
8484
aoe::binance::impl::main_net::futures::Infrastructure<
8585
Price, Qty, common::MemoryPoolThreadSafety,
8686
common::MemoryPoolNotThreadSafety>
87-
infrastructure(thread_pool);
88-
infrastructure.Register(aos::TradingPair::kBTCUSDT);
87+
binance_futures_main_net_infrastructure(thread_pool);
88+
binance_futures_main_net_infrastructure.Register(aos::TradingPair::kBTCUSDT);
8989

9090
aoe::bybit::impl::main_net::linear::Infrastructure<
9191
Price, Qty, common::MemoryPoolThreadSafety,
9292
common::MemoryPoolNotThreadSafety>
93-
infrastructure(thread_pool);
94-
infrastructure.Register(aos::TradingPair::kBTCUSDT);
93+
bybit_linear_mainnet_infrastructure(thread_pool);
94+
binance_futures_main_net_infrastructure.Register(aos::TradingPair::kBTCUSDT);
9595
//init strategy
9696

9797
aos::impl::MarketTripletManagerDefault<HashT> market_triplet_manager;
@@ -114,7 +114,7 @@ int main(int argc, char** argv) {
114114
strategy);
115115

116116
str::Config config;
117-
str::StrategyWrapper<Price, Qty, HashT> strategy_wrapper(config, infrastructure, strategy_engine);
117+
str::StrategyWrapper<Price, Qty, HashT> strategy_wrapper(config, binance_futures_main_net_infrastructure, strategy_engine);
118118
auto status = strategy_wrapper.Run();
119119
if (!status) {
120120
logi("Strategy not init successful");

examples/example43/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
set (PROJECT_NAME example43)
3+
project(${PROJECT_NAME})
4+
5+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
6+
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
7+
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
8+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
9+
10+
file(GLOB SRC
11+
"*.cpp"
12+
)
13+
14+
15+
add_executable (${PROJECT_NAME}
16+
${SRC}
17+
)
18+
19+
target_link_libraries(${PROJECT_NAME}
20+
aos
21+
magic_enum::magic_enum
22+
tomlplusplus::tomlplusplus
23+
sodium
24+
concurrentqueue
25+
nlohmann_json::nlohmann_json
26+
simdjson::simdjson
27+
fmt
28+
fmtlog-static
29+
)
30+
31+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 23)
32+
33+
option(ENABLE_SANITIZERS "Enable AddressSanitizer and LeakSanitizer" ON)
34+
35+
if (ENABLE_SANITIZERS)
36+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,leak -fno-omit-frame-pointer -g")
37+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,leak")
38+
endif()

examples/example43/main.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <thread>
2+
3+
#include "aoe/aoe.h"
4+
#include "aoe/bybit/infrastructure/infrastructure.h"
5+
#include "aoe/bybit/order_book_sync/order_book_sync.h"
6+
#include "aoe/bybit/parser/json/ws/order_book_response/parser.h"
7+
#include "aos/aos.h"
8+
#include "aos/common/exchange_id.h"
9+
#include "aos/trading_pair/trading_pair.h"
10+
#include "fmtlog.h"
11+
#include "aos/logger/logger.h"
12+
#include "aos/common/mem_pool.h"
13+
14+
namespace str {
15+
struct Config {
16+
public:
17+
aos::TradingPair trading_pair = aos::TradingPair::kBTCUSDT;
18+
};
19+
template <typename Price, typename Qty>
20+
class Strategy {
21+
Config config_;
22+
aoe::bybit::spot::main_net::InfrastructureNotifierInterface<
23+
Price, Qty>& infra_;
24+
bool strategy_init_success_ = false;
25+
26+
public:
27+
Strategy(Config config,
28+
aoe::bybit::spot::main_net::InfrastructureNotifierInterface<
29+
Price, Qty>& infra)
30+
: config_(config), infra_(infra) {
31+
SetStrategy();
32+
}
33+
bool Run() {
34+
logi("Strategy run");
35+
return strategy_init_success_;
36+
}
37+
38+
private:
39+
void SetStrategy() {
40+
bool status_set_cb_on_best_bid_change =
41+
infra_.SetCallbackOnBestBidChange(
42+
config_.trading_pair,
43+
[](const aos::BestBid<Price, Qty>& new_bid) {
44+
logi(
45+
"[MY_ULTIMATE_SPOT_STRATEGY] invoke callback "
46+
"on best bid "
47+
"change");
48+
});
49+
bool status_set_cb_on_best_ask_change = infra_.SetCallbackOnBestAskChange(
50+
config_.trading_pair, [](const aos::BestAsk<Price, Qty>& new_bid) {
51+
logi(
52+
"[MY_ULTIMATE_SPOT_STRATEGY] invoke callback on "
53+
"best ask "
54+
"change");
55+
});
56+
strategy_init_success_ =
57+
status_set_cb_on_best_bid_change & status_set_cb_on_best_ask_change;
58+
}
59+
};
60+
}; // namespace str
61+
62+
int main(int argc, char** argv) {
63+
{
64+
fmtlog::setLogLevel(fmtlog::LogLevel::DBG);
65+
logd("{}", aos::TradingPair::kBTCUSDT);
66+
67+
boost::asio::thread_pool thread_pool;
68+
LogPolling log_polling(thread_pool, std::chrono::microseconds(1));
69+
using Price = double;
70+
using Qty = double;
71+
aoe::bybit::impl::main_net::spot::Infrastructure<
72+
Price, Qty, common::MemoryPoolThreadSafety,
73+
common::MemoryPoolNotThreadSafety>
74+
infrastructure(thread_pool);
75+
infrastructure.Register(aos::TradingPair::kBTCUSDT);
76+
str::Config config;
77+
str::Strategy<Price, Qty> strategy(config, infrastructure);
78+
auto status = strategy.Run();
79+
if (!status) {
80+
logi("Strategy not init successful");
81+
return 0;
82+
}
83+
}
84+
logi("FINISHHHHHHHHHHHHHHHHHHHHHHHHHHH");
85+
fmtlog::poll();
86+
return 0;
87+
}

0 commit comments

Comments
 (0)