xfunding: pull out newState constructor

This commit is contained in:
c9s 2023-03-26 14:44:18 +08:00
parent 36836c7c79
commit a3f96871e2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -82,6 +82,15 @@ type State struct {
UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"` UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"`
} }
func newState() *State {
return &State{
PositionState: PositionClosed,
PendingBaseTransfer: fixedpoint.Zero,
TotalBaseTransfer: fixedpoint.Zero,
UsedQuoteInvestment: fixedpoint.Zero,
}
}
func (s *State) Reset() { func (s *State) Reset() {
s.PositionState = PositionClosed s.PositionState = PositionClosed
s.PendingBaseTransfer = fixedpoint.Zero s.PendingBaseTransfer = fixedpoint.Zero
@ -246,6 +255,15 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
s.spotMarket, _ = s.spotSession.Market(s.Symbol) s.spotMarket, _ = s.spotSession.Market(s.Symbol)
s.futuresMarket, _ = s.futuresSession.Market(s.Symbol) s.futuresMarket, _ = s.futuresSession.Market(s.Symbol)
binanceFutures, ok := s.futuresSession.Exchange.(*binance.Exchange)
if !ok {
return errNotBinanceExchange
}
binanceSpot, ok := s.spotSession.Exchange.(*binance.Exchange)
if !ok {
return errNotBinanceExchange
}
// adjust QuoteInvestment // adjust QuoteInvestment
if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok { if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok {
originalQuoteInvestment := s.QuoteInvestment originalQuoteInvestment := s.QuoteInvestment
@ -284,26 +302,12 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
} }
if s.State == nil || s.Reset { if s.State == nil || s.Reset {
s.State = &State{ s.State = newState()
PositionState: PositionClosed,
PendingBaseTransfer: fixedpoint.Zero,
TotalBaseTransfer: fixedpoint.Zero,
UsedQuoteInvestment: fixedpoint.Zero,
}
} }
log.Infof("loaded spot position: %s", s.SpotPosition.String()) log.Infof("loaded spot position: %s", s.SpotPosition.String())
log.Infof("loaded futures position: %s", s.FuturesPosition.String()) log.Infof("loaded futures position: %s", s.FuturesPosition.String())
binanceFutures, ok := s.futuresSession.Exchange.(*binance.Exchange)
if !ok {
return errNotBinanceExchange
}
binanceSpot, ok := s.spotSession.Exchange.(*binance.Exchange)
if !ok {
return errNotBinanceExchange
}
s.spotOrderExecutor = s.allocateOrderExecutor(ctx, s.spotSession, instanceID, s.SpotPosition) s.spotOrderExecutor = s.allocateOrderExecutor(ctx, s.spotSession, instanceID, s.SpotPosition)
s.spotOrderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) { s.spotOrderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
// we act differently on the spot account // we act differently on the spot account