Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
Preparing for next version
Browse files Browse the repository at this point in the history
  • Loading branch information
Arne Wouters committed Aug 25, 2020
1 parent b114e13 commit 8223a37
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
35 changes: 23 additions & 12 deletions bytra/source/Bybit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -519,9 +525,9 @@ void Bybit::placeLimitOrder(const Order &ord) {

position->activeOrder = std::make_shared<Order>(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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
}
Expand Down
10 changes: 7 additions & 3 deletions bytra/source/Position.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion bytra/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ int main(int argc, char **argv) {
}

std::map<std::string, std::shared_ptr<Strategy>> validStrategies
= {{"rsi", std::make_shared<Rsi>()}, {"ema", std::make_shared<Ema>()}};
= {{"rsi", std::make_shared<Rsi>()},
{"ema", std::make_shared<Ema>()}};

if (validStrategies[strategy].get() == nullptr) {
spdlog::error("Invalid strategy: " + strategy);
Expand Down

0 comments on commit 8223a37

Please sign in to comment.