Skip to content

Commit

Permalink
This commit will be fixed up
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Aug 21, 2024
1 parent 3dc681e commit 594b9d9
Show file tree
Hide file tree
Showing 27 changed files with 438 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ if(CCT_ENABLE_PROTO)
else()
# Check here for a new version: https://protobuf.dev/support/version-support/#cpp
if (NOT PROTOBUF_VERSION)
set(PROTOBUF_VERSION v5.27.2)
set(PROTOBUF_VERSION v5.27.3)
endif()

message(STATUS "Configuring protobuf ${PROTOBUF_VERSION} from sources")
Expand Down
53 changes: 53 additions & 0 deletions data/static/auto-trade-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"kraken": {
"BTC-EUR": {
"algorithmName": "example-trader",
"repeatTime": "3s",
"baseStartAmount": "0.5BTC",
"quoteStartAmount": "50%EUR",
"stopCriteria": [
{
"type": "duration",
"value": "4h"
},
{
"type": "protectLoss",
"value": "-30%"
},
{
"type": "secureProfit",
"value": "80%"
}
]
},
"ETH-EUR": {
"algorithmName": "example-trader",
"repeatTime": "3s",
"baseStartAmount": "45ETH",
"quoteStartAmount": "50%EUR",
"stopCriteria": [
{
"type": "duration",
"value": "4h"
},
{
"type": "protectLoss",
"value": "-30%"
},
{
"type": "secureProfit",
"value": "80%"
}
]
}
},
"binance_user1": {
"XRP-USDT": {
"algorithmName": "example-trader",
"repeatTime": "1s",
"baseStartAmount": "50000.56XRP",
"quoteStartAmount": "100%USDT",
"stopCriteria": []
}
}
}
12 changes: 12 additions & 0 deletions src/engine/include/account-auto-trade-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <map>

#include "market-auto-trade-options.hpp"
#include "market.hpp"

namespace cct {

using AccountAutoTradeOptions = std::map<Market, MarketAutoTradeOptions>;

}
31 changes: 31 additions & 0 deletions src/engine/include/auto-trade-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <map>

#include "account-auto-trade-options.hpp"
#include "cct_json.hpp"
#include "cct_smallvector.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"

namespace cct {

class AutoTradeOptions {
public:
using AccountAutoTradeOptionsPtrVector = SmallVector<const AccountAutoTradeOptions *, kTypicalNbPrivateAccounts>;

AutoTradeOptions() noexcept = default;

explicit AutoTradeOptions(const json &data);

auto size() const noexcept { return _options.size(); }

PublicExchangeNameVector getExchanges() const;

AccountAutoTradeOptionsPtrVector getAccountAutoTradeOptionsPtr(std::string_view publicExchangeName) const;

private:
std::map<ExchangeName, AccountAutoTradeOptions> _options;
};

} // namespace cct
18 changes: 18 additions & 0 deletions src/engine/include/auto-trade-processor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "threadpool.hpp"

namespace cct {
class AutoTradeOptions;

class AutoTradeProcessor {
public:
explicit AutoTradeProcessor(const AutoTradeOptions& autoTradeOptions);

void start();

private:
const AutoTradeOptions& _autoTradeOptions;
ThreadPool _threadPool;
};
} // namespace cct
5 changes: 5 additions & 0 deletions src/engine/include/coincenter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <span>

#include "apikeysprovider.hpp"
#include "auto-trade-options.hpp"
#include "cct_const.hpp"
#include "cct_fixedcapacityvector.hpp"
#include "coincenterinfo.hpp"
Expand All @@ -19,6 +20,7 @@
#include "ordersconstraints.hpp"
#include "queryresulttypes.hpp"
#include "replay-options.hpp"
#include "signal-handler.hpp"
#include "transferablecommandresult.hpp"

namespace cct {
Expand Down Expand Up @@ -149,6 +151,9 @@ class Coincenter {
ReplayResults replay(const AbstractMarketTraderFactory &marketTraderFactory, const ReplayOptions &replayOptions,
Market market, ExchangeNameSpan exchangeNames);

/// Run auto trade.
void autoTrade(const AutoTradeOptions &autoTradeOptions);

/// Dumps the content of all file caches in data directory to save cURL queries.
void updateFileCaches() const;

Expand Down
7 changes: 6 additions & 1 deletion src/engine/include/coincentercommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <optional>
#include <string_view>
#include <type_traits>
#include <variant>

Expand Down Expand Up @@ -47,6 +48,8 @@ class CoincenterCommand {

CoincenterCommand& setReplayOptions(ReplayOptions replayOptions);

CoincenterCommand& setJsonConfigFile(std::string_view jsonConfigFile);

CoincenterCommand& setPercentageAmount(bool value = true);
CoincenterCommand& withBalanceInUse(bool value = true);

Expand Down Expand Up @@ -79,6 +82,8 @@ class CoincenterCommand {

const ReplayOptions& replayOptions() const { return std::get<ReplayOptions>(_specialOptions); }

std::string_view getJsonConfigFile() const { return std::get<std::string_view>(_specialOptions); }

bool operator==(const CoincenterCommand&) const noexcept = default;

using trivially_relocatable =
Expand All @@ -89,7 +94,7 @@ class CoincenterCommand {

private:
using SpecialOptions = std::variant<std::monostate, OrdersConstraints, WithdrawsOrDepositsConstraints, TradeOptions,
WithdrawOptions, ReplayOptions>;
WithdrawOptions, ReplayOptions, std::string_view>;

ExchangeNames _exchangeNames;
SpecialOptions _specialOptions;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/include/coincenteroptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class CoincenterCmdLineOptions {

std::string_view marketData;

std::string_view autoTrade;

std::optional<std::string_view> replay;
std::string_view algorithmNames;
std::string_view market;
Expand Down
16 changes: 16 additions & 0 deletions src/engine/include/coincenteroptionsdef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,22 @@ struct CoincenterAllowedOptions : private CoincenterCmdLineOptionsDefinitions {
"\nNominal replay will not validate input data to optimize performance, use this option to validate data once "
"and for all."},
&OptValueType::validateOnly},
{{{"Automation", 8004},
"auto-trade",
"<path/to/json.conf>",
"Automatic live trading mode. Once you have validated on historical market-data the performance of an "
"algorithm, it's time to try it for real!\n"
"This command has some particularities:\n"
"- next commands will never be executed\n"
"- repeat is ignored (the auto trade will continue until one of terminating signals defined in the "
"configuration file is reached)\n"
"Configuration will be loaded from given json file, with following options (check README to get full "
"configuration schema):\n"
"- 'algorithm' : algorithm name to use\n"
"- 'market' : the market to trade onto\n"
"- 'startAmount' : the starting amount in base currency (can be a percentage of available amount)\n"
"- 'exchange' : exchange with account key (not needed if not ambiguous)"},
&OptValueType::autoTrade},
{{{"Monitoring", 9000},
"--monitoring",
"",
Expand Down
3 changes: 3 additions & 0 deletions src/engine/include/exchangesorchestrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <optional>
#include <span>

#include "auto-trade-options.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "exchangeretriever.hpp"
Expand Down Expand Up @@ -106,6 +107,8 @@ class ExchangesOrchestrator {
std::span<MarketTraderEngine> marketTraderEngines, MarketTradeRangeStatsPerExchange &&tradeRangeStatsPerExchange,
ExchangeNameSpan exchangeNames);

void autoTrade(const AutoTradeOptions &autoTradeOptions);

private:
ExchangeRetriever _exchangeRetriever;
ThreadPool _threadPool;
Expand Down
37 changes: 37 additions & 0 deletions src/engine/include/market-auto-trade-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <span>
#include <string_view>

#include "auto-trade-stop-criterion.hpp"
#include "cct_json.hpp"
#include "cct_string.hpp"
#include "cct_vector.hpp"
#include "monetaryamount.hpp"
#include "timedef.hpp"

namespace cct {

class MarketAutoTradeOptions {
public:
explicit MarketAutoTradeOptions(const json &data);

std::string_view algorithmName() const { return _algorithmName; }

Duration repeatTime() const { return _repeatTime; }

MonetaryAmount baseStartAmount() const { return _baseStartAmount; }

MonetaryAmount quoteStartAmount() const { return _quoteStartAmount; }

std::span<const AutoTradeStopCriterion> stopCriterion() const { return _stopCriteria; }

private:
string _algorithmName;
Duration _repeatTime;
MonetaryAmount _baseStartAmount;
MonetaryAmount _quoteStartAmount;
vector<AutoTradeStopCriterion> _stopCriteria;
};

} // namespace cct
40 changes: 40 additions & 0 deletions src/engine/src/auto-trade-options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "auto-trade-options.hpp"

namespace cct {

AutoTradeOptions::AutoTradeOptions(const json &data) {
for (const auto &[exchangeName, marketAutoTradeOptions] : data.items()) {
AccountAutoTradeOptions accountAutoTradeOptions;
for (const auto &marketJson : marketAutoTradeOptions.items()) {
accountAutoTradeOptions.emplace(marketJson.key(), MarketAutoTradeOptions(marketJson.value()));
}
_options.emplace(exchangeName, std::move(accountAutoTradeOptions));
}
}

PublicExchangeNameVector AutoTradeOptions::getExchanges() const {
PublicExchangeNameVector exchanges;
for (const auto &[exchangeStr, _] : _options) {
ExchangeName exchangeName(exchangeStr);
std::string_view exchangeNameStr = exchangeName.name();
// It's possible because std::map keys are lexicographically ordered
if (exchanges.empty() || exchanges.back().name() != exchangeNameStr) {
exchanges.emplace_back(exchangeNameStr);
}
}
return exchanges;
}

AutoTradeOptions::AccountAutoTradeOptionsPtrVector AutoTradeOptions::getAccountAutoTradeOptionsPtr(
std::string_view publicExchangeName) const {
AccountAutoTradeOptionsPtrVector accountAutoTradeOptionsPtr;
for (const auto &[exchangeStr, accountAutoTradeOptions] : _options) {
ExchangeName exchangeName(exchangeStr);
if (exchangeStr.name() == publicExchangeName) {
accountAutoTradeOptionsPtr.emplace_back(&accountAutoTradeOptions);
}
}
return accountAutoTradeOptionsPtr;
}

} // namespace cct
12 changes: 12 additions & 0 deletions src/engine/src/auto-trade-processor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "auto-trade-processor.hpp"

#include "auto-trade-options.hpp"

namespace cct {

AutoTradeProcessor::AutoTradeProcessor(const AutoTradeOptions& autoTradeOptions)
: _autoTradeOptions(autoTradeOptions), _threadPool(autoTradeOptions.size()) {}

void AutoTradeProcessor::start() {}

} // namespace cct
Loading

0 comments on commit 594b9d9

Please sign in to comment.