all: add lock protected GetBase method for Position

This commit is contained in:
c9s 2022-01-09 00:35:45 +08:00
parent 9b92c8948d
commit 7e2acdc416
6 changed files with 17 additions and 18 deletions

View File

@ -66,7 +66,7 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
}
}
unrealizedProfit := (fixedpoint.NewFromFloat(currentPrice) - position.AverageCost).Mul(position.Base)
unrealizedProfit := (fixedpoint.NewFromFloat(currentPrice) - position.AverageCost).Mul(position.GetBase())
return &AverageCostPnlReport{
Symbol: symbol,
Market: c.Market,
@ -77,7 +77,7 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c
BuyVolume: bidVolume,
SellVolume: askVolume,
Stock: position.Base.Float64(),
Stock: position.GetBase().Float64(),
Profit: totalProfit,
NetProfit: totalNetProfit,
UnrealizedProfit: unrealizedProfit,

View File

@ -131,10 +131,7 @@ func (e *TwapExecution) newBestPriceOrder() (orderForm types.SubmitOrder, err er
}
minQuantity := fixedpoint.NewFromFloat(e.market.MinQuantity)
e.position.Lock()
base := e.position.Base
e.position.Unlock()
base := e.position.GetBase()
restQuantity := e.TargetQuantity - fixedpoint.Abs(base)
@ -341,9 +338,7 @@ func (e *TwapExecution) orderUpdater(ctx context.Context) {
}
func (e *TwapExecution) cancelContextIfTargetQuantityFilled() bool {
e.position.Lock()
base := e.position.Base
e.position.Unlock()
base := e.position.GetBase()
if fixedpoint.Abs(base) >= e.TargetQuantity {
log.Infof("filled target quantity, canceling the order execution context")

View File

@ -203,7 +203,7 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec
one := fixedpoint.NewFromFloat(1.0)
askPrice := midPrice.Mul(one + s.Spread)
bidPrice := midPrice.Mul(one - s.Spread)
base := s.state.Position.Base
base := s.state.Position.GetBase()
balances := s.session.Account.Balances()
log.Infof("mid price:%f spread: %s ask:%f bid: %f",

View File

@ -5,9 +5,10 @@ import (
"fmt"
"sync"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/service"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
@ -44,7 +45,7 @@ type PercentageTargetStop struct {
// GenerateOrders generates the orders from the given targets
func (stop *PercentageTargetStop) GenerateOrders(market types.Market, pos *types.Position) []types.SubmitOrder {
var price = pos.AverageCost
var quantity = pos.Base
var quantity = pos.GetBase()
// submit target orders
var targetOrders []types.SubmitOrder

View File

@ -252,9 +252,7 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
// 1. place bid orders when we already bought too much
// 2. place ask orders when we already sold too much
if s.MaxExposurePosition > 0 {
s.state.Position.Lock()
pos := s.state.Position.Base
s.state.Position.Unlock()
pos := s.state.Position.GetBase()
if pos < -s.MaxExposurePosition {
// stop sell if we over-sell
@ -814,9 +812,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
// uncover position = -5 - -3 (covered position) = -2
s.tradeCollector.Process()
s.state.Position.Lock()
position := s.state.Position.Base
s.state.Position.Unlock()
position := s.state.Position.GetBase()
uncoverPosition := position - s.state.CoveredPosition.AtomicLoad()
absPos := math.Abs(uncoverPosition.Float64())

View File

@ -42,6 +42,13 @@ type Position struct {
sync.Mutex
}
func (p *Position) GetBase() (base fixedpoint.Value) {
p.Lock()
base = p.Base
p.Unlock()
return base
}
type FuturesPosition struct {
Symbol string `json:"symbol"`
BaseCurrency string `json:"baseCurrency"`