From 91f432abb87aa0ad7233891801f85f3c03d55d50 Mon Sep 17 00:00:00 2001 From: Carlos Wu Fei Date: Wed, 13 Sep 2023 02:49:21 +0100 Subject: [PATCH] Fix interest computation --- api/account/assets.py | 3 +-- api/apis.py | 2 -- web/src/state/bots/actions.js | 38 ++++++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/api/account/assets.py b/api/account/assets.py index 5eeddd657..6914411f4 100644 --- a/api/account/assets.py +++ b/api/account/assets.py @@ -298,7 +298,7 @@ async def get_balance_series(self, end_date, start_date): lte_tp_id = ObjectId.from_datetime(obj_end_date) params["_id"]["$lte"] = lte_tp_id - balance_series = list(self.db.balances.find(params).sort([("_id", -1)])) + balance_series = list(self.db.balances.find(params).sort([("time", -1)])) # btc candlestick data series params = CandlestickParams( @@ -314,7 +314,6 @@ async def get_balance_series(self, end_date, start_date): balances_series_diff = [] balances_series_dates = [] balance_btc_diff = [] - balance_series.sort(key=lambda item: item["_id"], reverse=False) for index, item in enumerate(balance_series): btc_index = self.match_series_dates(dates, item["time"], index) diff --git a/api/apis.py b/api/apis.py index dda4418d8..a99433613 100644 --- a/api/apis.py +++ b/api/apis.py @@ -10,8 +10,6 @@ class BinanceApi: """ Binance API URLs - - To test: https://binance.github.io/binance-api-swagger/ """ diff --git a/web/src/state/bots/actions.js b/web/src/state/bots/actions.js index 2a6777686..006fa0211 100644 --- a/web/src/state/bots/actions.js +++ b/web/src/state/bots/actions.js @@ -80,6 +80,24 @@ export function getProfit(base_price, current_price, strategy = "long") { return 0; } +/** + * Calculate interests based on hourly interest rate + * @param {bot} bot object + * @returns {float} + */ +function getInterestsShortMargin(bot) { + const timeDelta = bot.deal.margin_short_sell_timestamp - bot.deal.margin_short_buy_back_timestamp; + const durationHours = (timeDelta / 1000) / 3600 + const interests = parseFloat(bot.deal.hourly_interest_rate) * durationHours; + const closeTotal = bot.deal.margin_short_buy_back_price; + const openTotal = bot.deal.margin_short_sell_price; + return { + interests: interests, + openTotal: openTotal, + closeTotal: closeTotal, + } +} + /** * This function calculates the profit (not including commissions/fees) * for a single bot, namely the BotForm and TestBotForm components @@ -105,27 +123,27 @@ export function computeSingleBotProfit(bot, realTimeCurrPrice = null) { } else if (bot.deal.margin_short_sell_price > 0) { // Completed margin short if (bot.deal.margin_short_buy_back_price > 0) { - const currentPrice = bot.deal.margin_short_buy_back_price; - const marginSellPrice = bot.deal.margin_short_sell_price; - const interests = (+bot.deal.hourly_interest_rate) * (+bot.deal.margin_short_loan_principal) + + const { interests, openTotal, closeTotal} = getInterestsShortMargin(bot); let profitChange = parseFloat( - ((currentPrice - marginSellPrice - interests) / marginSellPrice) * 100 - ) * -1; + (((openTotal - closeTotal) / openTotal) - interests) * 100 + ); return +profitChange.toFixed(2); } else { - const currentPrice = + // Not completed margin_sho + const closePrice = bot.deal.margin_short_buy_back_price > 0 ? bot.deal.margin_short_buy_back_price : realTimeCurrPrice || bot.deal.current_price; - if (currentPrice === 0) { + if (closePrice === 0) { return 0; } - const marginSellPrice = bot.deal.margin_short_sell_price; + const { interests, openTotal } = getInterestsShortMargin(bot); let profitChange = parseFloat( - ((currentPrice - marginSellPrice) / marginSellPrice) * 100 - ) * -1; + (((openTotal - closePrice) / openTotal) - interests) * 100 + ); return +profitChange.toFixed(2); } } else {