Skip to content

Commit 9664264

Browse files
committed
Here's a concise Git commit message summarizing the changes in the provided diff.txt file:
--- **Commit message:** ``` Refactor strategy logic and improve configurability - Updated `Strategy` class to accept external config (window size, bin count, allowed asset hash) - Replaced `size_t` with `HashT` in action callbacks for better type consistency - Enhanced logging with decomposed hash details for buy/sell operations - Modified MI calculator to use `HashT` for asset identifiers - Corrected `TradingPairToSmallStringView` to use fully qualified enum type and underlying type - Added example40 to CMake build system ``` --- Let me know if you want to tailor it to a specific style (e.g. conventional commits, Jira ticket references, etc.).
1 parent 674bed8 commit 9664264

File tree

7 files changed

+153
-30
lines changed

7 files changed

+153
-30
lines changed

aos/converters/trading_pair_to_small_string_view/trading_pair_to_small_string_view.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace aos {
88
namespace impl {
99
// Хэш-таблица для конвертации торговой пары в строку
1010
class TradingPairToSmallStringView {
11-
constexpr static std::array<std::pair<TradingPair, std::string_view>,
12-
static_cast<std::size_t>(TradingPair::kCount)>
11+
constexpr static std::array<std::pair<aos::TradingPair, std::string_view>,
12+
static_cast<std::underlying_type_t<aos::TradingPair>>(aos::TradingPair::kCount)>
1313
dictionary_ = {{
1414
{TradingPair::kBTCUSDT, "btcusdt"},
1515
{TradingPair::kETHUSDT, "ethusdt"},

aos/mutual_information/c_mutual_information_calculator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class MutualInformationCalculator
7575

7676
std::pair<bool, T> ComputeMutualInformation(
7777
SlidingWindowStorageAvgDevMinMaxInterface<HashT, T>& window_storage,
78-
size_t hash_asset, size_t paired_asset, int bins) const override {
78+
HashT hash_asset, HashT paired_asset, int bins) const override {
7979
// Проверяем, есть ли достаточно данных для обоих активов
8080
if (!window_storage.HasEnoughData(hash_asset) ||
8181
!window_storage.HasEnoughData(paired_asset)) {

aos/strategies/deviation_and_mutual_info/c_strategy.h

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "aos/sliding_window_storage/c_sliding_window_storage.h"
99
#include "aos/strategy/i_strategy.h"
1010
#include "aos/strategy/strategy.h"
11+
#include "aos/strategies/deviation_and_mutual_info/config.h"
12+
#include "aos/hash_utils/decompose_hash.h"
13+
#include "fmtlog.h"
1114

1215
namespace aos {
1316
namespace strategies {
@@ -18,34 +21,33 @@ class Strategy : public StrategyInterface<HashT, T>{
1821
impl::SlidingWindowStorageAvgDevMinMax<HashT, T> sliding_window_;
1922
impl::MutualInformationCalculator<HashT, T> mi_calculator_;
2023
MarketTripletManagerInterface<HashT>& market_triplet_manager_;
21-
24+
const Config<HashT>& config_;
2225
private:
2326
void InitBuyActions() {
2427
core_strategy_.AddActionsToBuy(
25-
[](std::queue<size_t>& queue, size_t hash, const double& value) {
26-
if (hash == 1) {
28+
[this](std::queue<HashT>& queue, HashT hash, const T& value) {
29+
if (hash == config_.allowed_asset_hash) {
2730
logi("start analise hash:{} value:{}", hash, value);
2831
queue.push(hash);
2932
}
3033
});
3134

32-
core_strategy_.AddActionsToBuy([this](std::queue<size_t>& queue, size_t hash,
33-
const double& value) {
35+
core_strategy_.AddActionsToBuy([this](std::queue<HashT>& queue, HashT hash,
36+
const T& value) {
3437
auto [_, ratio] = sliding_window_.GetDeviationRatio(hash, value);
35-
auto dev = sliding_window_.GetDeviation(hash, value);
36-
logi("Buy: hash:{} value:{} dev:{} ratio:{}", hash, value, dev,
37-
ratio);
38+
auto [status_deviation, dev] = sliding_window_.GetDeviation(hash, value);
39+
logi("Buy: hash:{} value:{} dev:{} ratio:{}", hash, value, dev, ratio);
3840
if (ratio > 0.001) {
3941
queue.push(hash);
4042
}
4143
});
4244

43-
core_strategy_.AddActionsToBuy([this](std::queue<size_t>& queue, size_t hash,
44-
const double& value) {
45+
core_strategy_.AddActionsToBuy([this](std::queue<HashT>& queue, HashT hash,
46+
const T& value) {
4547
if (!market_triplet_manager_.HasPair(hash)) return;
4648
for (const auto& pair : market_triplet_manager_.GetPairs(hash)) {
4749
auto [status, mi] = mi_calculator_.ComputeMutualInformation(
48-
sliding_window_, hash, pair, 10);
50+
sliding_window_, hash, pair, config_.number_bins);
4951
if (status && mi > 2) {
5052
logi("Buy MI ({} <-> {}): {}", hash, pair, mi);
5153
queue.push(pair);
@@ -54,36 +56,44 @@ class Strategy : public StrategyInterface<HashT, T>{
5456
});
5557

5658
core_strategy_.AddActionsToBuy(
57-
[](std::queue<size_t>& queue, size_t hash, const double& value) {
58-
logi("Need buy hash:{} value:{}", hash, value);
59+
[](std::queue<HashT>& queue, HashT hash, const T& value) {
60+
common::ExchangeId exchange_id_1 = common::ExchangeId::kInvalid;
61+
aos::NetworkEnvironment network_environment_1;
62+
aos::CategoryRaw category_market_1;
63+
aos::TradingPair trading_pair_1;
64+
aos::DecomposeHash(hash, exchange_id_1, category_market_1,
65+
network_environment_1, trading_pair_1);
66+
logi("need buy with hash={} with value={} on {} category={} {} {}",
67+
hash, value, exchange_id_1, category_market_1, network_environment_1,
68+
trading_pair_1);
5969
});
6070
};
6171
void InitSellActions() {
6272
core_strategy_.AddActionsToSell(
63-
[](std::queue<size_t>& queue, size_t hash, const double& value) {
64-
if (hash == 1) {
73+
[this](std::queue<HashT>& queue, HashT hash, const T& value) {
74+
if (hash == config_.allowed_asset_hash) {
6575
logi("start analise hash:{} value:{}", hash, value);
6676
queue.push(hash);
6777
}
6878
});
6979

70-
core_strategy_.AddActionsToSell([this](std::queue<size_t>& queue, size_t hash,
71-
const double& value) {
80+
core_strategy_.AddActionsToSell([this](std::queue<HashT>& queue, HashT hash,
81+
const T& value) {
7282
auto [_, ratio] = sliding_window_.GetDeviationRatio(hash, value);
73-
auto dev = sliding_window_.GetDeviation(hash, value);
83+
auto [status_deviation, dev] = sliding_window_.GetDeviation(hash, value);
7484
logi("Sell: hash:{} value:{} dev:{} ratio:{}", hash, value, dev,
7585
ratio);
7686
if (ratio < 0.001) {
7787
queue.push(hash);
7888
}
7989
});
8090

81-
core_strategy_.AddActionsToSell([this](std::queue<size_t>& queue, size_t hash,
82-
const double& value) {
91+
core_strategy_.AddActionsToSell([this](std::queue<HashT>& queue, HashT hash,
92+
const T& value) {
8393
if (!market_triplet_manager_.HasPair(hash)) return;
8494
for (const auto& pair : market_triplet_manager_.GetPairs(hash)) {
8595
auto [status, mi] = mi_calculator_.ComputeMutualInformation(
86-
sliding_window_, hash, pair, 10);
96+
sliding_window_, hash, pair, config_.number_bins);
8797
if (status && mi > 2) {
8898
logi("Sell MI ({} <-> {}): {}", hash, pair, mi);
8999
queue.push(pair);
@@ -92,15 +102,24 @@ class Strategy : public StrategyInterface<HashT, T>{
92102
});
93103

94104
core_strategy_.AddActionsToSell(
95-
[](std::queue<size_t>& queue, size_t hash, const double& value) {
96-
logi("Need sell hash:{} value:{}", hash, value);
105+
[](std::queue<HashT>& queue, HashT hash, const T& value) {
106+
common::ExchangeId exchange_id_1 = common::ExchangeId::kInvalid;
107+
aos::NetworkEnvironment network_environment_1;
108+
aos::CategoryRaw category_market_1;
109+
aos::TradingPair trading_pair_1;
110+
aos::DecomposeHash(hash, exchange_id_1, category_market_1,
111+
network_environment_1, trading_pair_1);
112+
logi("need sell with hash={} with value={} on {} category={} {} {}",
113+
hash, value, exchange_id_1, category_market_1, network_environment_1,
114+
trading_pair_1);
97115
});
98116
}
99117

100118
public:
101-
Strategy(int window_size, MarketTripletManagerInterface<HashT>& market_triplet_manager) :
102-
sliding_window_(window_size),
103-
market_triplet_manager_(market_triplet_manager) {
119+
Strategy(MarketTripletManagerInterface<HashT>& market_triplet_manager, const Config<HashT>& config) :
120+
sliding_window_(config.window_size),
121+
market_triplet_manager_(market_triplet_manager),
122+
config_(config) {
104123
InitBuyActions();
105124
InitSellActions();
106125
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include <cstdint>
3+
4+
5+
namespace aos {
6+
namespace strategies {
7+
namespace deviation_and_mutual_information {
8+
template <typename HashT>
9+
struct Config{
10+
const uint32_t window_size;
11+
const uint32_t number_bins;
12+
const HashT allowed_asset_hash;
13+
// constexpr-конструктор
14+
constexpr Config(uint32_t new_window_size, uint32_t new_number_bins, HashT new_allowed_asset_hash)
15+
: window_size(new_window_size), number_bins(new_number_bins), allowed_asset_hash(new_allowed_asset_hash) {}
16+
17+
};
18+
};
19+
};
20+
};

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ add_subdirectory(example35)
3232
add_subdirectory(example36)
3333
add_subdirectory(example37)
3434
add_subdirectory(example38)
35-
add_subdirectory(example39)
35+
add_subdirectory(example39)
36+
add_subdirectory(example40)

examples/example40/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
set (PROJECT_NAME example40)
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+
fmt
27+
fmtlog-static
28+
)
29+
30+
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 23)
31+
32+
option(ENABLE_SANITIZERS "Enable AddressSanitizer and LeakSanitizer" ON)
33+
34+
if (ENABLE_SANITIZERS)
35+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,leak -fno-omit-frame-pointer -g")
36+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,leak")
37+
endif()

examples/example40/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <boost/asio.hpp>
2+
3+
#include "aos/aos.h"
4+
#include "aos/sliding_window_storage/c_sliding_window_storage.h"
5+
#include "aos/mutual_information/c_mutual_information_calculator.h"
6+
#include "aos/strategies/deviation_and_mutual_info/c_strategy.h"
7+
#include "aoe/binance/hash_utils/hash_utils.h"
8+
#include "aoe/bybit/hash_utils/hash_utils.h"
9+
#include "fmtlog.h"
10+
11+
int main() {
12+
{
13+
using HashT = uint64_t;
14+
using Price = double;
15+
using Qty = double;
16+
using namespace aos::impl;
17+
MarketTripletManagerDefault<HashT> market_triplet_manager;
18+
const auto binance_btc_usdt = aoe::binance::HashKey(aoe::binance::Category::kSpot, aos::NetworkEnvironment::kMainNet, aos::TradingPair::kBTCUSDT);
19+
const auto bybit_btc_usdt = aoe::bybit::HashKey(aoe::bybit::Category::kSpot, aos::NetworkEnvironment::kMainNet, aos::TradingPair::kBTCUSDT);
20+
market_triplet_manager.Connect(binance_btc_usdt, bybit_btc_usdt);
21+
22+
boost::asio::thread_pool thread_pool;
23+
24+
constexpr aos::strategies::deviation_and_mutual_information::Config<HashT> config{
25+
5,
26+
10,
27+
binance_btc_usdt
28+
};
29+
aos::strategies::deviation_and_mutual_information::Strategy<HashT, Price> strategy(market_triplet_manager, config);
30+
31+
StrategyEngineDefault<HashT, Price> strategy_engine(
32+
thread_pool, strategy);
33+
std::vector<std::pair<double, double>> incoming_data = {
34+
{29300.25, 29290.10}, {29350.50, 29340.35}, {29400.75, 29380.20},
35+
{29450.00, 29430.15}, {29500.25, 29488.05}, {29550.50, 29520.40},
36+
{29602.75, 29560.60}, {29000.00, 29600.50}, {49700.25, 29650.40},
37+
{29750.50, 59700.35}};
38+
for (uint64_t i = 0; i < incoming_data.size(); ++i) {
39+
strategy_engine.AddData(binance_btc_usdt, incoming_data[i].first);
40+
strategy_engine.AddData(bybit_btc_usdt, incoming_data[i].second);
41+
}
42+
strategy_engine.Wait();
43+
}
44+
fmtlog::poll();
45+
return 0;
46+
}

0 commit comments

Comments
 (0)