Skip to content

Commit

Permalink
Fix interest computation
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Wu Fei authored and Carlos Wu Fei committed Sep 13, 2023
1 parent c438441 commit 91f432a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
3 changes: 1 addition & 2 deletions api/account/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions api/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
class BinanceApi:
"""
Binance API URLs
To test:
https://binance.github.io/binance-api-swagger/
"""

Expand Down
38 changes: 28 additions & 10 deletions web/src/state/bots/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 91f432a

Please sign in to comment.