修复 scale-in 后 leg.EntryPrice 不更新 + 重启恢复时加载 scale prices
- trader.go: scale-in 后用 weightedAvgPrice 更新 LongLeg/ShortLeg.EntryPrice - trader.go: restoreOpenPositions 从 orders 表加载 scale prices 重建完整价格切片 - db/trade_repo.go: 新增 GetScalePrices(tradeID) 方法 - README: v1.2.1 版本记录
This commit is contained in:
@@ -547,6 +547,10 @@ func (t *Trader) checkScaleIn(pos *ArbPosition, bgP, hlP, diffPct float64, store
|
||||
pos.LongEntryPrices = append(pos.LongEntryPrices, longPrice)
|
||||
pos.ShortEntryPrices = append(pos.ShortEntryPrices, shortPrice)
|
||||
|
||||
// Update leg EntryPrice to reflect weighted average across all scale levels
|
||||
pos.LongLeg.EntryPrice = weightedAvgPrice(pos.LongEntryPrices, t.cfg.TradeAmountUSD)
|
||||
pos.ShortLeg.EntryPrice = weightedAvgPrice(pos.ShortEntryPrices, t.cfg.TradeAmountUSD)
|
||||
|
||||
// Persist scale orders to DB immediately
|
||||
if t.db != nil && pos.DBTradeID > 0 {
|
||||
now := time.Now()
|
||||
@@ -1172,6 +1176,20 @@ func (t *Trader) restoreOpenPositions() {
|
||||
}
|
||||
pos.ShortEntryPrices = []float64{*tr.ShortEntry}
|
||||
}
|
||||
|
||||
// Restore scale-in prices from orders table for correct weighted average
|
||||
scaleLong, scaleShort, err := t.db.GetScalePrices(tr.ID)
|
||||
if err == nil {
|
||||
pos.LongEntryPrices = append(pos.LongEntryPrices, scaleLong...)
|
||||
pos.ShortEntryPrices = append(pos.ShortEntryPrices, scaleShort...)
|
||||
// Refresh leg EntryPrice to reflect all scale levels
|
||||
if len(pos.LongEntryPrices) > 1 {
|
||||
pos.LongLeg.EntryPrice = weightedAvgPrice(pos.LongEntryPrices, t.cfg.TradeAmountUSD)
|
||||
}
|
||||
if len(pos.ShortEntryPrices) > 1 {
|
||||
pos.ShortLeg.EntryPrice = weightedAvgPrice(pos.ShortEntryPrices, t.cfg.TradeAmountUSD)
|
||||
}
|
||||
}
|
||||
t.positions[tr.Coin] = pos
|
||||
// Prevent immediate re-trading of the same coin
|
||||
t.lastTradeTime[tr.Coin] = tr.OpenedAt
|
||||
|
||||
Reference in New Issue
Block a user