Skip to content

Commit

Permalink
fix: update dca formula (#1392)
Browse files Browse the repository at this point in the history
  • Loading branch information
phucledien authored May 13, 2024
1 parent ed05e21 commit 08a3988
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/model/binance_asset_average_cost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package model
type BinanceAssetAverageCost struct {
ProfileId string `json:"profile_id"`
Symbol string `json:"symbol"`
TotalAmount string `json:"total_amount"`
AverageCost string `json:"average_cost"`
}
41 changes: 36 additions & 5 deletions pkg/repo/binance_spot_transaction/pg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package binancespottransaction

import (
"strconv"

"gorm.io/gorm"

"github.com/defipod/mochi/pkg/model"
Expand Down Expand Up @@ -41,26 +43,55 @@ func (pg *pg) GetUserAverageCost(profileId string) ([]model.BinanceAssetAverageC
db := pg.db
rows, err := db.Raw(`
SELECT
profile_id, symbol, sum(executed_qty::decimal * price_in_usd::decimal) / sum(executed_qty::decimal) as average_cost
profile_id,
symbol,
sum(
CASE WHEN side = 'BUY' THEN
executed_qty::decimal
ELSE
- executed_qty::decimal
END) AS total_amount,
sum(
CASE WHEN side = 'BUY' THEN
executed_qty::decimal * price::decimal
ELSE
- executed_qty::decimal * price::decimal
END) /
sum(
CASE WHEN side = 'BUY' THEN
executed_qty::decimal
ELSE
- executed_qty::decimal
END) AS avg_cost
FROM
binance_spot_transactions
WHERE
profile_id = ?
AND TYPE = 'LIMIT'
AND side = 'BUY'
AND status = 'FILLED'
GROUP BY symbol, profile_id;
GROUP BY
symbol, profile_id;
`, profileId).Rows()
if err != nil {
return nil, err
}

for rows.Next() {
var avgCostItem model.BinanceAssetAverageCost
if err := rows.Scan(&avgCostItem.ProfileId, &avgCostItem.Symbol, &avgCostItem.AverageCost); err != nil {
if err := rows.Scan(&avgCostItem.ProfileId, &avgCostItem.Symbol, &avgCostItem.TotalAmount, &avgCostItem.AverageCost); err != nil {
return nil, err
}
avgCost = append(avgCost, avgCostItem)
// Incase of invalid total amount, we just skip the pair
// there is some case leading to invalid total amount
// tx is not enough
// user deposit or withdraw asset from cex
amount, err := strconv.ParseFloat(avgCostItem.TotalAmount, 64)
if err != nil {
continue
}
if amount > 0 {
avgCost = append(avgCost, avgCostItem)
}
}

return avgCost, nil
Expand Down

0 comments on commit 08a3988

Please sign in to comment.