From 5277098f701bf99810a10e65d62d35364a8c3d70 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 2 Jun 2022 18:05:35 +0800 Subject: [PATCH] add api .UnrealizedProfit and .IsDust method on Position --- pkg/strategy/bollmaker/strategy.go | 4 ++++ pkg/types/position.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index 14d5c8409..edce5c980 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -346,6 +346,10 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec log.Infof("calculated %s max exposure position: %v", s.Symbol, maxExposurePosition) + if !s.Position.IsClosed() && !s.Position.IsDust(midPrice) { + log.Infof("current %s unrealized profit: %f %s", s.Symbol, s.Position.UnrealizedProfit(midPrice).Float64(), s.Market.QuoteCurrency) + } + canSell := true canBuy := true diff --git a/pkg/types/position.go b/pkg/types/position.go index 195f6b1c3..6075d730e 100644 --- a/pkg/types/position.go +++ b/pkg/types/position.go @@ -144,6 +144,13 @@ func (p *Position) NewClosePositionOrder(percentage fixedpoint.Value) *SubmitOrd } } +func (p *Position) IsDust(price fixedpoint.Value) bool { + base := p.GetBase() + return p.Market.IsDustQuantity(base, price) +} + +// GetBase locks the mutex and return the base quantity +// The base quantity can be negative func (p *Position) GetBase() (base fixedpoint.Value) { p.Lock() base = p.Base @@ -151,6 +158,18 @@ func (p *Position) GetBase() (base fixedpoint.Value) { return base } +func (p *Position) UnrealizedProfit(price fixedpoint.Value) fixedpoint.Value { + base := p.GetBase() + + if p.IsLong() { + return price.Sub(p.AverageCost).Mul(base) + } else if p.IsShort() { + return p.AverageCost.Sub(price).Mul(base) + } + + return fixedpoint.Zero +} + type FuturesPosition struct { Symbol string `json:"symbol"` BaseCurrency string `json:"baseCurrency"` @@ -229,6 +248,10 @@ func (p *Position) IsLong() bool { return p.Base.Sign() > 0 } +func (p *Position) IsClosed() bool { + return p.Base.Sign() == 0 +} + func (p *Position) Type() PositionType { if p.Base.Sign() > 0 { return PositionLong