From 8223a3720e129de1a2adb645c930338248850eac Mon Sep 17 00:00:00 2001 From: Arne Wouters Date: Tue, 25 Aug 2020 23:41:00 +0200 Subject: [PATCH] Preparing for next version --- bytra/source/Bybit.cpp | 35 +++++++++++++++++++++++------------ bytra/source/Position.h | 10 +++++++--- bytra/source/main.cpp | 3 ++- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/bytra/source/Bybit.cpp b/bytra/source/Bybit.cpp index 1d9f75b..fdd9f5f 100644 --- a/bytra/source/Bybit.cpp +++ b/bytra/source/Bybit.cpp @@ -130,10 +130,12 @@ void Bybit::getPositionApi() { throw std::runtime_error("Bad API response."); } - double entryPrice = std::stod((std::string) response["result"]["entry_price"]); - std::string side = (std::string) response["result"]["side"]; + double entryPrice = std::stod((std::string)response["result"]["entry_price"]); + std::string side = (std::string)response["result"]["side"]; long qty = (long)response["result"]["size"]; - if (side == "Sell") { qty = -qty; } + if (side == "Sell") { + qty = -qty; + } position->update(qty, entryPrice); } @@ -285,10 +287,12 @@ void Bybit::parseWebsocketMsg(const std::string &msg) { if (topic == "position") { for (dom::object item : response["data"]) { - double entryPrice = std::stod((std::string) item["entry_price"]); + double entryPrice = std::stod((std::string)item["entry_price"]); std::string side = (std::string)item["side"]; long qty = (long)item["size"]; - if (side == "Sell") { qty = -qty; } + if (side == "Sell") { + qty = -qty; + } position->update(qty, entryPrice); } @@ -472,7 +476,9 @@ void Bybit::placeMarketOrder(const Order &ord) { throw std::runtime_error("Bad API response."); } - if (ord.reduce) { position->activeOrder = nullptr; } + if (ord.reduce) { + position->activeOrder = nullptr; + } } void Bybit::placeLimitOrder(const Order &ord) { @@ -519,9 +525,9 @@ void Bybit::placeLimitOrder(const Order &ord) { position->activeOrder = std::make_shared(ord); position->activeOrder->id = (std::string)response["result"]["order_id"]; - spdlog::debug("Set activeOrder with price={}, interval=({}, {}), reduce={}", - position->activeOrder->price, position->activeOrder->priceInterval.first, - position->activeOrder->priceInterval.second, position->activeOrder->reduce); + spdlog::debug("Set activeOrder with price={}, interval=({}, {}), reduce={}", position->activeOrder->price, + position->activeOrder->priceInterval.first, position->activeOrder->priceInterval.second, + position->activeOrder->reduce); } void Bybit::amendLimitOrder(const Order &ord) { @@ -558,7 +564,9 @@ void Bybit::amendLimitOrder(const Order &ord) { throw std::runtime_error("Bad API response."); } - if (retCode == 30032 || retCode == 30037 || retCode == 20001) { position->activeOrder = nullptr; } + if (retCode == 30032 || retCode == 30037 || retCode == 20001) { + position->activeOrder = nullptr; + } } void Bybit::cancelActiveLimitOrder() { @@ -683,8 +691,11 @@ void Bybit::doAutomatedTrading() { // Stop Loss if (position->qty != 0) { double midPrice = (orderBook->askPrice() + orderBook->bidPrice()) / 2; - if ((position->isLong() && midPrice < position->stopLossPrice) || (position->isShort() && midPrice > position->stopLossPrice)) { - if (position->activeOrder) { cancelActiveLimitOrder(); } + if ((position->isLong() && midPrice < position->stopLossPrice) + || (position->isShort() && midPrice > position->stopLossPrice)) { + if (position->activeOrder) { + cancelActiveLimitOrder(); + } placeMarketOrder(Order(-position->qty, true)); } } diff --git a/bytra/source/Position.h b/bytra/source/Position.h index 59e85f4..338515f 100644 --- a/bytra/source/Position.h +++ b/bytra/source/Position.h @@ -28,9 +28,13 @@ class Position { qty = newQty; entryPrice = newPrice; - if (qty == 0) { return; } - else if (isLong()) { stopLossPrice = entryPrice - entryPrice*stopLossPercentage; } - else {stopLossPrice = entryPrice + entryPrice*stopLossPercentage;} + if (qty == 0) { + return; + } else if (isLong()) { + stopLossPrice = entryPrice - entryPrice * stopLossPercentage; + } else { + stopLossPrice = entryPrice + entryPrice * stopLossPercentage; + } } }; diff --git a/bytra/source/main.cpp b/bytra/source/main.cpp index b238a85..d7ac9ef 100644 --- a/bytra/source/main.cpp +++ b/bytra/source/main.cpp @@ -88,7 +88,8 @@ int main(int argc, char **argv) { } std::map> validStrategies - = {{"rsi", std::make_shared()}, {"ema", std::make_shared()}}; + = {{"rsi", std::make_shared()}, + {"ema", std::make_shared()}}; if (validStrategies[strategy].get() == nullptr) { spdlog::error("Invalid strategy: " + strategy);