1
+ #include " auto-trade-options.hpp"
2
+
3
+ #include < utility>
4
+
5
+ #include " auto-trade-config.hpp"
6
+ #include " cct_invalid_argument_exception.hpp"
7
+
8
+ namespace cct {
9
+
10
+ AutoTradeOptions::AutoTradeOptions (schema::AutoTradeConfig &&autoTradeConfig)
11
+ : _autoTradeConfig(std::move(autoTradeConfig)) {}
12
+
13
+ ExchangeNames AutoTradeOptions::exchangeNames () const {
14
+ ExchangeNames exchangeNames;
15
+ for (const auto &[exchangeNameEnum, publicExchangeAutoTradeOptions] : _autoTradeConfig) {
16
+ const int posPublicExchangeName = exchangeNames.size ();
17
+ for (const auto &[market, autoTradeMarketConfig] : publicExchangeAutoTradeOptions) {
18
+ const int posMarket = exchangeNames.size ();
19
+ for (std::string_view account : autoTradeMarketConfig.accounts ) {
20
+ ExchangeName exchangeName (exchangeNameEnum, account);
21
+ const auto it = std::find (exchangeNames.begin () + posPublicExchangeName, exchangeNames.end (), exchangeName);
22
+ if (it == exchangeNames.end ()) {
23
+ exchangeNames.push_back (std::move (exchangeName));
24
+ } else if (it >= exchangeNames.begin () + posMarket) {
25
+ throw invalid_argument (" Duplicated account {} for exchange {}" , account, exchangeName.name ());
26
+ }
27
+ }
28
+ }
29
+ }
30
+ return exchangeNames;
31
+ }
32
+
33
+ ExchangeNameEnumVector AutoTradeOptions::publicExchanges () const {
34
+ ExchangeNameEnumVector exchanges;
35
+ for (const auto &[publicExchangeName, _] : _autoTradeConfig) {
36
+ exchanges.emplace_back (publicExchangeName);
37
+ }
38
+ std::ranges::sort (exchanges);
39
+ return exchanges;
40
+ }
41
+
42
+ AutoTradeOptions::AccountAutoTradeOptionsPtrVector AutoTradeOptions::accountAutoTradeOptionsPtr (
43
+ std::string_view publicExchangeName) const {
44
+ AccountAutoTradeOptionsPtrVector accountAutoTradeOptionsPtr;
45
+ for (const auto &[exchangeNameEnum, publicExchangeAutoTradeOptions] : _autoTradeConfig) {
46
+ if (kSupportedExchanges [static_cast <int >(exchangeNameEnum)] == publicExchangeName) {
47
+ accountAutoTradeOptionsPtr.emplace_back (&publicExchangeAutoTradeOptions);
48
+ }
49
+ }
50
+ return accountAutoTradeOptionsPtr;
51
+ }
52
+
53
+ const schema::AutoTradeExchangeConfig &AutoTradeOptions::operator [](ExchangeNameEnum exchangeNameEnum) const {
54
+ const auto it = std::ranges::find_if (_autoTradeConfig, [exchangeNameEnum](const auto &exchangeConfig) {
55
+ return exchangeConfig.first == exchangeNameEnum;
56
+ });
57
+ if (it == _autoTradeConfig.end ()) {
58
+ throw exception (" No auto trade options for exchange {}" , kSupportedExchanges [static_cast <int >(exchangeNameEnum)]);
59
+ }
60
+ return it->second ;
61
+ }
62
+
63
+ } // namespace cct
0 commit comments