mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-06-06 05:51:19 +08:00
fix: adaptive price precision for meme coins
- Add adaptivePriceRound() in store/position.go for database storage - Update position_builder.go to use adaptive precision for entry/exit prices - Add Gate to OrderSync skip list in auto_trader.go - Add debug logging in gate/order_sync.go for price parsing issues - Create web/src/utils/format.ts with formatPrice() for frontend display - Update TraderDashboardPage.tsx and PositionHistory.tsx to use adaptive formatting Fixes issue where meme coin prices (e.g. 0.000000166) displayed as 0.0000
This commit is contained in:
@@ -3,12 +3,63 @@ package store
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// adaptivePriceRound rounds a price based on its magnitude to preserve meaningful precision.
|
||||
// For small prices (like meme coins), it preserves more decimal places.
|
||||
// It detects the number of decimal places needed from the reference price(s).
|
||||
func adaptivePriceRound(price float64, referencePrices ...float64) float64 {
|
||||
if price == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Find the minimum magnitude among all prices (including the price itself)
|
||||
minMagnitude := math.Abs(price)
|
||||
for _, ref := range referencePrices {
|
||||
if ref > 0 && ref < minMagnitude {
|
||||
minMagnitude = ref
|
||||
}
|
||||
}
|
||||
|
||||
// Determine decimal places needed based on price magnitude
|
||||
// For price 0.000000541, we need ~15 decimal places
|
||||
// For price 0.0001, we need ~8 decimal places
|
||||
// For price 1.0, we need ~4 decimal places
|
||||
var multiplier float64
|
||||
switch {
|
||||
case minMagnitude < 0.000001: // Ultra small (meme coins like CHEEMS, SHIB)
|
||||
multiplier = 1e15 // 15 decimal places
|
||||
case minMagnitude < 0.0001: // Very small (PEPE, FLOKI)
|
||||
multiplier = 1e12 // 12 decimal places
|
||||
case minMagnitude < 0.01: // Small
|
||||
multiplier = 1e10 // 10 decimal places
|
||||
case minMagnitude < 1: // Medium
|
||||
multiplier = 1e8 // 8 decimal places
|
||||
default: // Large
|
||||
multiplier = 1e6 // 6 decimal places
|
||||
}
|
||||
|
||||
return math.Round(price*multiplier) / multiplier
|
||||
}
|
||||
|
||||
// getPriceDecimalPlaces returns the number of decimal places in a price string
|
||||
func getPriceDecimalPlaces(price float64) int {
|
||||
if price == 0 {
|
||||
return 0
|
||||
}
|
||||
s := strconv.FormatFloat(price, 'f', -1, 64)
|
||||
idx := strings.Index(s, ".")
|
||||
if idx == -1 {
|
||||
return 0
|
||||
}
|
||||
return len(s) - idx - 1
|
||||
}
|
||||
|
||||
// TraderStats trading statistics metrics
|
||||
type TraderStats struct {
|
||||
TotalTrades int `json:"total_trades"`
|
||||
@@ -156,8 +207,8 @@ func (s *PositionStore) UpdatePositionQuantityAndPrice(id int64, addQty float64,
|
||||
newQty := math.Round((pos.Quantity+addQty)*10000) / 10000
|
||||
newEntryQty := math.Round((currentEntryQty+addQty)*10000) / 10000
|
||||
newEntryPrice := (pos.EntryPrice*pos.Quantity + addPrice*addQty) / newQty
|
||||
// Use 8 decimal places for price precision (crypto standard)
|
||||
newEntryPrice = math.Round(newEntryPrice*100000000) / 100000000
|
||||
// Use adaptive precision based on price magnitude (for meme coins with very small prices)
|
||||
newEntryPrice = adaptivePriceRound(newEntryPrice, pos.EntryPrice, addPrice)
|
||||
newFee := pos.Fee + addFee
|
||||
nowMs := time.Now().UTC().UnixMilli()
|
||||
|
||||
@@ -188,8 +239,8 @@ func (s *PositionStore) ReducePositionQuantity(id int64, reduceQty float64, exit
|
||||
var newExitPrice float64
|
||||
if newClosedQty > 0 {
|
||||
newExitPrice = (pos.ExitPrice*closedQty + exitPrice*reduceQty) / newClosedQty
|
||||
// Use 8 decimal places for price precision (crypto standard)
|
||||
newExitPrice = math.Round(newExitPrice*100000000) / 100000000
|
||||
// Use adaptive precision based on price magnitude (for meme coins with very small prices)
|
||||
newExitPrice = adaptivePriceRound(newExitPrice, pos.ExitPrice, exitPrice, pos.EntryPrice)
|
||||
}
|
||||
|
||||
nowMs := time.Now().UTC().UnixMilli()
|
||||
|
||||
@@ -147,8 +147,8 @@ func (pb *PositionBuilder) handleClose(
|
||||
var finalExitPrice float64
|
||||
if totalClosed > 0 {
|
||||
finalExitPrice = (position.ExitPrice*closedBefore + price*closeQty) / totalClosed
|
||||
// Use 8 decimal places for price precision (crypto standard)
|
||||
finalExitPrice = math.Round(finalExitPrice*100000000) / 100000000
|
||||
// Use adaptive precision based on price magnitude (for meme coins with very small prices)
|
||||
finalExitPrice = adaptivePriceRound(finalExitPrice, position.ExitPrice, price, position.EntryPrice)
|
||||
} else {
|
||||
finalExitPrice = price
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user