From 1cc88c26a66bf66c7222ef51580dfdafc2fda8ce Mon Sep 17 00:00:00 2001 From: Rodrigo Brito Date: Sun, 19 Mar 2023 23:19:53 -0300 Subject: [PATCH] fix(backtesting): change portfolio calc for shorting --- exchange/paperwallet.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/exchange/paperwallet.go b/exchange/paperwallet.go index 94ff381c..30ddc72a 100644 --- a/exchange/paperwallet.go +++ b/exchange/paperwallet.go @@ -448,7 +448,14 @@ func (p *PaperWallet) OnCandle(candle model.Candle) { for asset, info := range p.assets { amount := info.Free + info.Lock pair := strings.ToUpper(asset + p.baseCoin) - total += amount * p.lastCandle[pair].Close + if amount < 0 { + v := math.Abs(amount) + liquid := 2*v*p.avgShortPrice[pair] - v*p.lastCandle[pair].Close + total += liquid + } else { + total += amount * p.lastCandle[pair].Close + } + p.assetValues[asset] = append(p.assetValues[asset], AssetValue{ Time: candle.Time, Value: amount * p.lastCandle[pair].Close,