Skip to content

Commit

Permalink
Updated bots without useless mutex and thread headers.
Browse files Browse the repository at this point in the history
████ ███  To request new features or in case this commit breaks something for you,
████ ███  please, create a new github issue with all possible information for me,
▓███▀█▄   but never share your API Keys!
▒▓██ ███
░▒▓█ ███  Signed-off-by: Carles Tubio <[email protected]>
 _________________________________________
/ Hello, WORLD!                           \
|                                         |
\ pssst.. 1.00000000 BTC = 56423.56 EUR.  /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  • Loading branch information
ctubio committed Oct 6, 2024
1 parent 88bf0e4 commit 5a9864f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ K ?= K.sh
MAJOR = 0
MINOR = 7
PATCH = 0
BUILD = 27
BUILD = 28

OBLIGATORY = DISCLAIMER: This is strict non-violent software: \n$\
if you hurt other living creatures, please stop; \n$\
Expand Down
4 changes: 3 additions & 1 deletion src/bin/+portfolios/+portfolios.client/Orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ export class OrdersComponent {
pong: o.isPong,
time: o.time
});
if (this.symbols.indexOf(o.symbol) == -1)
if (this.symbols.indexOf(o.symbol) == -1) {
this.symbols.push(o.symbol);
this.symbols.sort();
}
});

this.api.setGridOption('rowData', []);
Expand Down
10 changes: 6 additions & 4 deletions src/bin/trading-bot/trading-bot.test.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ SCENARIO_METHOD(TradingBot, "ANY BTC/EUR") {
REQUIRE_FALSE(engine.levels.diff.empty());
THEN("broadcast") {
REQUIRE_NOTHROW(engine.qp.delayUI = 0);
this_thread::sleep_for(chrono::milliseconds(370));
struct timeval tv = {0, 370000};
::select(0, nullptr, nullptr, nullptr, &tv);
REQUIRE_NOTHROW(engine.levels.diff.read = [&]() {
REQUIRE(engine.levels.diff.blob().dump() == "{"
"\"asks\":[{\"price\":1234.69,\"size\":0.11234566}],"
Expand Down Expand Up @@ -178,7 +179,8 @@ SCENARIO_METHOD(TradingBot, "ANY BTC/EUR") {
REQUIRE_FALSE(engine.wallet.safety.recentTrades.sumSells);
}
THEN("expired") {
this_thread::sleep_for(chrono::milliseconds(1001));
struct timeval tv = {1, 1000};
::select(0, nullptr, nullptr, nullptr, &tv);
REQUIRE_NOTHROW(engine.qp.tradeRateSeconds = 1);
REQUIRE_NOTHROW(engine.wallet.safety.recentTrades.expire());
REQUIRE(engine.wallet.safety.recentTrades.lastBuyPrice == 1234.50);
Expand Down Expand Up @@ -440,7 +442,6 @@ SCENARIO_METHOD(TradingBot, "ANY BTC/EUR") {
REQUIRE(engine.wallet.quote.held == Approx(457.22592546));
}
THEN("to json") {
cout << engine.orders.blob() << endl;
REQUIRE(string::npos == engine.orders.blob().dump().find("\"status\":0"));
REQUIRE(string::npos == engine.orders.blob().dump().find("\"status\":2"));
REQUIRE(string::npos != engine.orders.blob().dump().find("{\"exchangeId\":\"\",\"isPong\":false,\"latency\":69,\"orderId\":\"" + randIds[0] + "\",\"price\":1234.5,\"quantity\":0.12345678,\"side\":0,\"status\":1,\"symbol\":\"BTC-EUR\",\"time\":" + to_string(time) + ",\"timeInForce\":0,\"type\":0}"));
Expand Down Expand Up @@ -620,7 +621,8 @@ SCENARIO_METHOD(TradingBot, "ANY BTC/EUR") {
baseSign = (order.side == Side::Bid) ? 1 : -1;
expectedBaseDelta += baseSign * order.qtyFilled;
expectedQuoteDelta -= baseSign * order.qtyFilled * order.price;
this_thread::sleep_for(chrono::milliseconds(2));
struct timeval tv = {2, 0};
::select(0, nullptr, nullptr, nullptr, &tv);
engine.wallet.safety.trades.insert(order);
Amount actualBaseDelta = 0;
Amount actualQuoteDelta = 0;
Expand Down
63 changes: 27 additions & 36 deletions src/lib/Krypto.ninja-apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ namespace ₿ {
function<void(const Trade&)>
>;
public:
mutex *guard = nullptr;
curl_socket_t loopfd = 0;
struct {
Decimal funds,
Expand Down Expand Up @@ -454,10 +453,6 @@ namespace ₿ {

class GwApiWs: public Gw,
public Curl::WebSocket {
public:
GwApiWs()
: WebSocket(guard)
{};
private:
unsigned int countdown = 1;
bool subscription = false;
Expand Down Expand Up @@ -529,10 +524,6 @@ namespace ₿ {
};
class GwApiWsWs: public GwApiWs,
public Curl::WebSocketTwin {
public:
GwApiWsWs()
: WebSocketTwin(guard)
{};
protected:
bool connected() const override {
return GwApiWs::connected()
Expand Down Expand Up @@ -575,7 +566,7 @@ class GwApiWsFix: public GwApiWs,
public Curl::FixSocket {
public:
GwApiWsFix(const string &t)
: FixSocket(t, apikey, guard)
: FixSocket(t, apikey)
{};
private:
string fix;
Expand Down Expand Up @@ -637,7 +628,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp) + "&recvWindow=21000";
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/api/v3/exchangeInfo");
const json reply = Curl::Web::xfer(http + "/api/v3/exchangeInfo");
if (!reply.is_object()
or !reply.contains("symbols")
or !reply.at("symbols").is_array()
Expand All @@ -651,7 +642,7 @@ class GwApiWsFix: public GwApiWs,
) report += it.value("baseAsset", "") + "/" + it.value("quoteAsset", "") + ANSI_NEW_LINE;
};
json handshake() const override {
json reply1 = Curl::Web::xfer(*guard, http + "/api/v3/exchangeInfo?symbol="+ base + quote);
json reply1 = Curl::Web::xfer(http + "/api/v3/exchangeInfo?symbol="+ base + quote);
if (reply1.contains("symbols") and reply1.at("symbols").is_array())
for (const json &it : reply1.at("symbols"))
if (it.value("symbol", "") == base + quote) {
Expand Down Expand Up @@ -684,7 +675,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &h1, const string &crud) const {
return Curl::Web::xfer(*guard, url, crud, "", {
return Curl::Web::xfer(url, crud, "", {
"X-MBX-APIKEY: " + h1
});
};
Expand Down Expand Up @@ -728,7 +719,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/instrument?filter="+((json){
const json reply = Curl::Web::xfer(http + "/instrument?filter="+((json){
{"typ", "IFXXXP"}, {"state", "Open"}
}).dump());
if (!reply.is_array()
Expand All @@ -742,7 +733,7 @@ class GwApiWsFix: public GwApiWs,
};
json handshake() const override {
json reply = {
{"object", Curl::Web::xfer(*guard, http + "/instrument?symbol=" + base + "_" + quote)}
{"object", Curl::Web::xfer(http + "/instrument?symbol=" + base + "_" + quote)}
};
if (reply.at("object").is_array() and !reply.at("object").empty())
reply = reply.at("object").at(0);
Expand All @@ -759,7 +750,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &h1, const string &h2, const string &h3, const string &post, const string &crud) const {
return Curl::Web::xfer(*guard, url, crud, post, {
return Curl::Web::xfer(url, crud, post, {
"api-expires: " + h1,
"api-key: " + h2,
"api-signature: " + h3
Expand All @@ -784,7 +775,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp / 1e+3);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/spot/currency_pairs");
const json reply = Curl::Web::xfer(http + "/spot/currency_pairs");
if (!reply.is_array()
or reply.empty()
or !reply.at(0).is_object()
Expand All @@ -796,7 +787,7 @@ class GwApiWsFix: public GwApiWs,
};
json handshake() const override {
json reply = {
{"object", Curl::Web::xfer(*guard, http + "/spot/currency_pairs")}
{"object", Curl::Web::xfer(http + "/spot/currency_pairs")}
};
if (reply.at("object").is_array() and !reply.at("object").empty())
for (const json &it : reply.at("object"))
Expand All @@ -820,7 +811,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &h1, const string &h2, const string &h3, const string &post, const string &crud) const {
return Curl::Web::xfer(*guard, url, crud, post, {
return Curl::Web::xfer(url, crud, post, {
"Content-Type: application/json",
"KEY: " + h1,
"Timestamp: " + h2,
Expand All @@ -846,7 +837,7 @@ class GwApiWsFix: public GwApiWs,
return randId() + randId();
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/public/symbol");
const json reply = Curl::Web::xfer(http + "/public/symbol");
if (!reply.is_array()
or reply.empty()
or !reply.at(0).is_object()
Expand All @@ -857,7 +848,7 @@ class GwApiWsFix: public GwApiWs,
report += it.value("baseCurrency", "") + "/" + it.value("quoteCurrency", "") + ANSI_NEW_LINE;
};
json handshake() const override {
const json reply = Curl::Web::xfer(*guard, http + "/public/symbol/" + base + quote);
const json reply = Curl::Web::xfer(http + "/public/symbol/" + base + quote);
return {
{ "base", base == "USDT" ? "USD" : base },
{ "quote", quote == "USDT" ? "USD" : quote },
Expand All @@ -874,7 +865,7 @@ class GwApiWsFix: public GwApiWs,
return ws.substr(0, ws.length() - 6) + "trading";
};
json xfer(const string &url, const string &auth, const string &post) const {
return Curl::Web::xfer(*guard, url, "DELETE", post, {}, auth);
return Curl::Web::xfer(url, "DELETE", post, {}, auth);
};
};
class GwBequant: virtual public GwHitBtc {
Expand Down Expand Up @@ -938,7 +929,7 @@ class GwApiWsFix: public GwApiWs,
return string(ws).insert(ws.find("ws.") + 2, "-user");
};
json xfer(const string &url, const string &post = "", const string &crud = "GET") const {
return Curl::Web::xfer(*guard, url, crud, post, {
return Curl::Web::xfer(url, crud, post, {
"Content-Type: application/json",
"Authorization: Bearer " + token(crud, url)
});
Expand All @@ -965,7 +956,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp * 1e+3);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/conf/pub:list:pair:" + trading);
const json reply = Curl::Web::xfer(http + "/conf/pub:list:pair:" + trading);
if (!reply.is_array()
or reply.empty()
or !reply.at(0).is_array()
Expand All @@ -982,7 +973,7 @@ class GwApiWsFix: public GwApiWs,
};
json handshake() const override {
json reply1 = {
{"object", Curl::Web::xfer(*guard, http + "/ticker/t" + base + quote)}
{"object", Curl::Web::xfer(http + "/ticker/t" + base + quote)}
};
if (reply1.at("object").is_array()
and reply1.at("object").size() > 6
Expand All @@ -991,7 +982,7 @@ class GwApiWsFix: public GwApiWs,
reply1.at("object").at(6).get<double>()
), -4) -4);
json reply2 = {
{"object", Curl::Web::xfer(*guard, http + "/conf/pub:info:pair")}
{"object", Curl::Web::xfer(http + "/conf/pub:info:pair")}
};
if (reply2.at("object").is_array() and !reply2.at("object").empty())
for (const json &it : reply2.at("object").at(0)) {
Expand Down Expand Up @@ -1019,7 +1010,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &post, const string &h1, const string &h2, const string &h3) const {
return Curl::Web::xfer(*guard, url, "GET", post, {
return Curl::Web::xfer(url, "GET", post, {
"Content-Type: application/json",
"bfx-apikey: " + h1,
"bfx-nonce: " + h2,
Expand Down Expand Up @@ -1055,7 +1046,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/api/v1/symbols");
const json reply = Curl::Web::xfer(http + "/api/v1/symbols");
if (!reply.is_object()
or !reply.contains("data")
or !reply.at("data").is_array()
Expand All @@ -1068,7 +1059,7 @@ class GwApiWsFix: public GwApiWs,
report += it.value("baseCurrency", "") + "/" + it.value("quoteCurrency", "") + ANSI_NEW_LINE;
};
json handshake() const override {
json reply1 = Curl::Web::xfer(*guard, http + "/api/v1/symbols");
json reply1 = Curl::Web::xfer(http + "/api/v1/symbols");
if (reply1.contains("data") and reply1.at("data").is_array())
for (const json &it : reply1.at("data"))
if (it.value("symbol", "") == base + "-" + quote) {
Expand All @@ -1089,7 +1080,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &h1, const string &h2, const string &h3, const string &h4, const string &crud, const string &post = "") const {
return Curl::Web::xfer(*guard, url, crud, post, {
return Curl::Web::xfer(url, crud, post, {
"Content-Type: application/json",
"KC-API-KEY: " + h1,
"KC-API-SIGN: " + h2,
Expand Down Expand Up @@ -1137,7 +1128,7 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/0/public/AssetPairs");
const json reply = Curl::Web::xfer(http + "/0/public/AssetPairs");
if (!reply.is_object()
or !reply.contains("result")
or !reply.at("result").is_object()
Expand All @@ -1147,7 +1138,7 @@ class GwApiWsFix: public GwApiWs,
report += it.value("wsname", "") + ANSI_NEW_LINE;
};
json handshake() const override {
json reply = Curl::Web::xfer(*guard, http + "/0/public/AssetPairs?pair=" + base + quote);
json reply = Curl::Web::xfer(http + "/0/public/AssetPairs?pair=" + base + quote);
if (reply.contains("result"))
for (const json &it : reply.at("result"))
if (it.contains("pair_decimals")) {
Expand All @@ -1168,7 +1159,7 @@ class GwApiWsFix: public GwApiWs,
return string(ws).insert(ws.find("ws.") + 2, "-auth");
};
json xfer(const string &url, const string &h1, const string &h2, const string &post) const {
return Curl::Web::xfer(*guard, url, "GET", post, {
return Curl::Web::xfer(url, "GET", post, {
"API-Key: " + h1,
"API-Sign: " + h2
});
Expand All @@ -1192,15 +1183,15 @@ class GwApiWsFix: public GwApiWs,
return to_string(Tstamp);
};
void pairs(string &report) const override {
const json reply = Curl::Web::xfer(*guard, http + "/markets");
const json reply = Curl::Web::xfer(http + "/markets");
if (!reply.is_array())
print("Error while reading pairs: " + reply.dump());
else for (auto &it : reply)
if (it.value("state", "") == "NORMAL")
report += it.value("displayName", "") + ANSI_NEW_LINE;
};
json handshake() const override {
json reply = Curl::Web::xfer(*guard, http + "/markets/" + base + "_" + quote);
json reply = Curl::Web::xfer(http + "/markets/" + base + "_" + quote);
if (reply.is_array() and !reply.empty() and reply.at(0).value("state", "") == "NORMAL" and reply.at(0).at("symbolTradeLimit").is_object())
reply = reply.at(0).at("symbolTradeLimit");
return {
Expand All @@ -1214,7 +1205,7 @@ class GwApiWsFix: public GwApiWs,
};
};
json xfer(const string &url, const string &post, const string &h1, const string &h2, const string &h3) const {
return Curl::Web::xfer(*guard, url, "GET", post, {
return Curl::Web::xfer(url, "GET", post, {
"Content-Type: application/json",
"key: " + h1,
"signature: " + h2,
Expand Down
13 changes: 5 additions & 8 deletions src/lib/Krypto.ninja-bots.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace ₿ {
exit(prefix + ANSI_PUKE_RED + " Errrror: " + ANSI_HIGH_RED + reason);
};

static mutex lock;

class Rollout {
public:
Rollout() {
Expand All @@ -33,7 +31,7 @@ namespace ₿ {
#ifndef NDEBUG
json::object();
#else
Curl::Web::xfer(lock, "https://api.github.com/repos/ctubio/"
Curl::Web::xfer("https://api.github.com/repos/ctubio/"
"Krypto-trading-bot/compare/" K_HEAD "...HEAD");
#endif
if (diff.value("ahead_by", 0)
Expand Down Expand Up @@ -103,7 +101,7 @@ namespace ₿ {
static void die(const int) {
if (epilogue.empty())
epilogue = "Excellent decision! "
+ Curl::Web::xfer(lock, "https://api.chucknorris.io/jokes/random?category=dev")
+ Curl::Web::xfer("https://api.chucknorris.io/jokes/random?category=dev")
.value("value", "let's plant a tree instead..");
halt(
epilogue.find("Errrror") == string::npos
Expand Down Expand Up @@ -1502,7 +1500,7 @@ namespace ₿ {
#endif
};
protected:
void required_setup(const Option *const K, mutex &lock, const curl_socket_t &loopfd) {
void required_setup(const Option *const K, const curl_socket_t &loopfd) {
if (!(gateway = Gw::new_Gw(K->arg<string>("exchange"))))
error("CF",
"Unable to configure a valid gateway using --exchange="
Expand All @@ -1526,7 +1524,6 @@ namespace ₿ {
gateway->apikeyid = K->arg<string>("apikeyid");
gateway->maxLevel = K->arg<int>("market-limit");
gateway->debug = K->arg<int>("debug-secret");
gateway->guard = &lock;
gateway->loopfd = loopfd;
gateway->printer = [K](const string &prefix, const string &reason, const string &highlight) {
if (reason.find("Error") != string::npos)
Expand Down Expand Up @@ -1594,13 +1591,13 @@ namespace ₿ {
{
ending([&]() { with_goodbye(); });
optional_setup(argc, argv, proactive(), blackhole(), unmounted());
required_setup(this, lock, poll());
required_setup(this, poll());
} {
if (windowed())
wait_for_keylog(this);
} {
log("CF", "Outbound IP address is",
wtfismyip = Curl::Web::xfer(lock, "https://wtfismyip.com/json")
wtfismyip = Curl::Web::xfer("https://wtfismyip.com/json")
.value("YourFuckingIPAddress", wtfismyip)
);
} {
Expand Down
Loading

0 comments on commit 5a9864f

Please sign in to comment.