Skip to content

Commit f97c786

Browse files
Merge pull request #526 from crypto-chassis/misc
binance spot filter out zero balances
2 parents 1559da7 + 87147c5 commit f97c786

3 files changed

Lines changed: 65 additions & 32 deletions

File tree

include/ccapi_cpp/ccapi_util_private.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,16 @@ class Decimal {
948948
public:
949949
Decimal() {}
950950

951+
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
952+
Decimal(T value) {
953+
if (value < 0) {
954+
this->sign = false;
955+
this->before = -value;
956+
} else if (value > 0) {
957+
this->before = value;
958+
}
959+
}
960+
951961
explicit Decimal(std::string_view originalValue) {
952962
if (originalValue.empty()) {
953963
throw std::invalid_argument("Decimal constructor input value cannot be empty");
@@ -1340,6 +1350,8 @@ class Decimal {
13401350

13411351
return result;
13421352
}
1353+
1354+
static const Decimal zero;
13431355
#ifndef CCAPI_EXPOSE_INTERNAL
13441356

13451357
private:
@@ -1354,6 +1366,8 @@ class Decimal {
13541366
mutable std::optional<double> cachedToDouble;
13551367
};
13561368

1369+
inline const Decimal Decimal::zero = Decimal(0); // define after full class body
1370+
13571371
inline std::string ConvertDecimalToString(const Decimal& input, bool normalize = true) {
13581372
// constexpr unsigned precision = GetCppDecFloatDigits<Decimal::backend_type>::value;
13591373

include/ccapi_cpp/service/ccapi_execution_management_service_binance_base.h

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -374,56 +374,73 @@ class ExecutionManagementServiceBinanceBase : public ExecutionManagementService
374374
const auto& marginType = request.getMarginType();
375375
if (this->isDerivatives) {
376376
for (const auto& x : document["assets"].GetArray()) {
377-
Element element;
378-
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
379-
element.insert(CCAPI_EM_QUANTITY_TOTAL, x["walletBalance"].GetString());
380-
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["availableBalance"].GetString());
381-
if (this->isDerivatives) {
382-
element.insert(CCAPI_LAST_UPDATED_TIME_SECONDS, UtilTime::convertMillisecondsStrToSecondsStr(x["updateTime"].GetString()));
377+
const auto& quantityTotalDecimal = Decimal(x["walletBalance"].GetString());
378+
if (quantityTotalDecimal != Decimal::zero) {
379+
Element element;
380+
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
381+
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(quantityTotalDecimal));
382+
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["availableBalance"].GetString());
383+
if (this->isDerivatives) {
384+
element.insert(CCAPI_LAST_UPDATED_TIME_SECONDS, UtilTime::convertMillisecondsStrToSecondsStr(x["updateTime"].GetString()));
385+
}
386+
elementList.emplace_back(std::move(element));
383387
}
384-
elementList.emplace_back(std::move(element));
385388
}
386389
} else {
387390
if (marginType == CCAPI_EM_MARGIN_TYPE_CROSS_MARGIN) {
388391
for (const auto& x : document["userAssets"].GetArray()) {
389-
Element element;
390-
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
391-
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(Decimal(x["free"].GetString()) + (Decimal(x["locked"].GetString()))));
392-
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["free"].GetString());
393-
element.insert(CCAPI_EM_QUANTITY_LIABILITY, ConvertDecimalToString(Decimal(x["borrowed"].GetString()) + (Decimal(x["interest"].GetString()))));
394-
elementList.emplace_back(std::move(element));
392+
const auto& quantityTotalDecimal = Decimal(x["free"].GetString()) + Decimal(x["locked"].GetString());
393+
if (quantityTotalDecimal != Decimal::zero) {
394+
Element element;
395+
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
396+
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(quantityTotalDecimal));
397+
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["free"].GetString());
398+
element.insert(CCAPI_EM_QUANTITY_LIABILITY, ConvertDecimalToString(Decimal(x["borrowed"].GetString()) + (Decimal(x["interest"].GetString()))));
399+
elementList.emplace_back(std::move(element));
400+
}
395401
}
396402
} else if (marginType == CCAPI_EM_MARGIN_TYPE_ISOLATED_MARGIN) {
397403
for (const auto& x : document["assets"].GetArray()) {
398404
std::string symbol = x["symbol"].GetString();
399405
{
400406
const auto& y = x["baseAsset"];
401-
Element element;
402-
element.insert(CCAPI_EM_INSTRUMENT, symbol);
403-
element.insert(CCAPI_EM_ASSET, y["asset"].GetString());
404-
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(Decimal(y["free"].GetString()) + (Decimal(y["locked"].GetString()))));
405-
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, y["free"].GetString());
406-
element.insert(CCAPI_EM_QUANTITY_LIABILITY, ConvertDecimalToString(Decimal(y["borrowed"].GetString()) + (Decimal(y["interest"].GetString()))));
407-
elementList.emplace_back(std::move(element));
407+
const auto& quantityTotalDecimal = Decimal(y["free"].GetString()) + Decimal(y["locked"].GetString());
408+
if (quantityTotalDecimal != Decimal::zero) {
409+
Element element;
410+
element.insert(CCAPI_EM_INSTRUMENT, symbol);
411+
element.insert(CCAPI_EM_ASSET, y["asset"].GetString());
412+
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(quantityTotalDecimal));
413+
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, y["free"].GetString());
414+
element.insert(CCAPI_EM_QUANTITY_LIABILITY,
415+
ConvertDecimalToString(Decimal(y["borrowed"].GetString()) + (Decimal(y["interest"].GetString()))));
416+
elementList.emplace_back(std::move(element));
417+
}
408418
}
409419
{
410420
const auto& y = x["quoteAsset"];
411-
Element element;
412-
element.insert(CCAPI_EM_INSTRUMENT, symbol);
413-
element.insert(CCAPI_EM_ASSET, y["asset"].GetString());
414-
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(Decimal(y["free"].GetString()) + (Decimal(y["locked"].GetString()))));
415-
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, y["free"].GetString());
416-
element.insert(CCAPI_EM_QUANTITY_LIABILITY, ConvertDecimalToString(Decimal(y["borrowed"].GetString()) + (Decimal(y["interest"].GetString()))));
417-
elementList.emplace_back(std::move(element));
421+
const auto& quantityTotalDecimal = Decimal(y["free"].GetString()) + Decimal(y["locked"].GetString());
422+
if (quantityTotalDecimal != Decimal::zero) {
423+
Element element;
424+
element.insert(CCAPI_EM_INSTRUMENT, symbol);
425+
element.insert(CCAPI_EM_ASSET, y["asset"].GetString());
426+
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(quantityTotalDecimal));
427+
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, y["free"].GetString());
428+
element.insert(CCAPI_EM_QUANTITY_LIABILITY,
429+
ConvertDecimalToString(Decimal(y["borrowed"].GetString()) + (Decimal(y["interest"].GetString()))));
430+
elementList.emplace_back(std::move(element));
431+
}
418432
}
419433
}
420434
} else {
421435
for (const auto& x : document["balances"].GetArray()) {
422-
Element element;
423-
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
424-
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(Decimal(x["free"].GetString()) + (Decimal(x["locked"].GetString()))));
425-
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["free"].GetString());
426-
elementList.emplace_back(std::move(element));
436+
const auto& quantityTotalDecimal = Decimal(x["free"].GetString()) + Decimal(x["locked"].GetString());
437+
if (quantityTotalDecimal != Decimal::zero) {
438+
Element element;
439+
element.insert(CCAPI_EM_ASSET, x["asset"].GetString());
440+
element.insert(CCAPI_EM_QUANTITY_TOTAL, ConvertDecimalToString(quantityTotalDecimal));
441+
element.insert(CCAPI_EM_QUANTITY_AVAILABLE_FOR_TRADING, x["free"].GetString());
442+
elementList.emplace_back(std::move(element));
443+
}
427444
}
428445
}
429446
}

include/ccapi_cpp/service/ccapi_market_data_service_bybit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class MarketDataServiceBybit : public MarketDataService {
3232

3333
protected:
3434
#endif
35+
void pingOnApplicationLevel(std::shared_ptr<WsConnection> wsConnectionPtr, ErrorCode& ec) override { this->send(wsConnectionPtr, R"({"op":"ping"})", ec); }
36+
3537
std::string getInstrumentGroup(const Subscription& subscription) override {
3638
const auto& instrumentTypeSubstitute = subscription.getInstrumentType();
3739
std::string url = MarketDataService::getInstrumentGroup(subscription);

0 commit comments

Comments
 (0)