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 Sep 28, 2024
1 parent 9d42d18 commit c649e55
Show file tree
Hide file tree
Showing 26 changed files with 623 additions and 18 deletions.
63 changes: 63 additions & 0 deletions data/static/auto-trade-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"kraken": {
"BTC-EUR": {
"accounts": [
"user1",
"user2"
],
"algorithmName": "example-trader",
"repeatTime": "5s",
"baseStartAmount": "0.5BTC",
"quoteStartAmount": "50%EUR",
"stopCriteria": [
{
"type": "duration",
"value": "4h"
},
{
"type": "protectLoss",
"value": "-30%"
},
{
"type": "secureProfit",
"value": "80%"
}
]
},
"ETH-EUR": {
"accounts": [
"user1"
],
"algorithmName": "example-trader",
"repeatTime": "3s",
"baseStartAmount": "45ETH",
"quoteStartAmount": "50%EUR",
"stopCriteria": [
{
"type": "duration",
"value": "4h"
},
{
"type": "protectLoss",
"value": "-30%"
},
{
"type": "secureProfit",
"value": "80%"
}
]
}
},
"binance": {
"XRP-USDT": {
"accounts": [
"user1"
],
"algorithmName": "example-trader",
"repeatTime": "1s",
"baseStartAmount": "50000.56XRP",
"quoteStartAmount": "100%USDT",
"stopCriteria": []
}
}
}
58 changes: 58 additions & 0 deletions src/engine/include/auto-trade-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <map>

#include "cct_fixedcapacityvector.hpp"
#include "cct_json.hpp"
#include "cct_smallvector.hpp"
#include "cct_vector.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "public-exchange-auto-trade-options.hpp"

namespace cct {

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

struct MarketExchanges {
Market market;
ExchangeNames privateExchangeNames;
const MarketAutoTradeOptions *pMarketAutoTradeOptions{};
};

using MarketStatusVector = vector<MarketExchanges>;

struct MarketExchangeOptions {
MarketStatusVector marketStatusVector;
};

struct PublicExchangeMarketOptions {
ExchangeName publicExchangeName;
MarketExchangeOptions marketExchangeOptions;
};

using PublicExchangeMarketOptionsVector = FixedCapacityVector<PublicExchangeMarketOptions, kNbSupportedExchanges>;

AutoTradeOptions() noexcept = default;

explicit AutoTradeOptions(const json &data);

auto begin() const { return _options.begin(); }
auto end() const { return _options.end(); }

ExchangeNames exchangeNames() const;

PublicExchangeNameVector publicExchanges() const;

AccountAutoTradeOptionsPtrVector accountAutoTradeOptionsPtr(std::string_view publicExchangeName) const;

const PublicExchangeAutoTradeOptions &operator[](const ExchangeName &exchangeName) const;

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

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

#include <compare>
#include <functional>

#include "cct_smallvector.hpp"
#include "cct_vector.hpp"
#include "exchange-names.hpp"
#include "exchange.hpp"
#include "market.hpp"
#include "public-exchange-auto-trade-options.hpp"
#include "timedef.hpp"

namespace cct {
class AutoTradeOptions;

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

struct SelectedMarket {
ExchangeNames privateExchangeNames;
Market market;
};

using SelectedMarketVector = SmallVector<SelectedMarket, kTypicalNbPrivateAccounts>;

SelectedMarketVector computeSelectedMarkets();

private:
struct MarketStatus {
ExchangeNames privateExchangeNames;
Market market;
TimePoint lastQueryTime;
const MarketAutoTradeOptions* pMarketAutoTradeOptions{};
};

using MarketStatusVector = vector<MarketStatus>;

struct ExchangeStatus {
MarketStatusVector marketStatusVector;
const PublicExchangeAutoTradeOptions* pPublicExchangeAutoTradeOptions{};
};

using ExchangeStatusVector = SmallVector<ExchangeStatus, kTypicalNbPrivateAccounts>;

ExchangeStatusVector _exchangeStatusVector;
TimePoint _startTs = Clock::now();
TimePoint _ts{_startTs};
};
} // namespace cct
6 changes: 5 additions & 1 deletion 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 Down Expand Up @@ -55,7 +56,7 @@ class Coincenter {

/// Query market data without returning it.
/// This method is especially useful for serialization and metric exports.
void queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchange);
MarketDataPerExchange queryMarketDataPerExchange(std::span<const Market> marketPerPublicExchangePos);

/// Retrieve the last 24h traded volume for exchanges supporting given market.
MonetaryAmountPerExchange getLast24hTradedVolumePerExchange(Market mk, ExchangeNameSpan exchangeNames);
Expand Down Expand Up @@ -149,6 +150,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
2 changes: 2 additions & 0 deletions src/engine/include/exchangesorchestrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <optional>
#include <span>

#include "auto-trade-options.hpp"
#include "auto-trade-processor.hpp"
#include "exchange-names.hpp"
#include "exchangename.hpp"
#include "exchangeretriever.hpp"
Expand Down
43 changes: 43 additions & 0 deletions src/engine/include/market-auto-trade-options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <span>
#include <string_view>

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

namespace cct {

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

std::span<const string> accounts() const { return _accounts; }

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; }

using trivially_relocatable = is_trivially_relocatable<string>::type;

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

} // namespace cct
12 changes: 12 additions & 0 deletions src/engine/include/public-exchange-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 PublicExchangeAutoTradeOptions = std::map<Market, MarketAutoTradeOptions>;

}
Loading

0 comments on commit c649e55

Please sign in to comment.