strategy/supertrend: use CalculateQuoteQuantity() in strategy

This commit is contained in:
Andy Cheng 2022-08-05 16:28:42 +08:00
parent b564e69f82
commit 737f6e99ba
2 changed files with 15 additions and 27 deletions

View File

@ -283,7 +283,7 @@ func CalculateQuoteQuantity(session *bbgo.ExchangeSession, ctx context.Context,
usingLeverage := session.Margin || session.IsolatedMargin || session.Futures || session.IsolatedFutures
if !usingLeverage {
// For spot, we simply return the quote balance
return quoteBalance.Available, nil
return quoteBalance.Available.Mul(fixedpoint.Min(leverage, fixedpoint.One)), nil
}
// using leverage -- starts from here
@ -294,5 +294,5 @@ func CalculateQuoteQuantity(session *bbgo.ExchangeSession, ctx context.Context,
}
logrus.Infof("calculating available leveraged quote quantity: account available quote = %+v", availableQuote)
return availableQuote, nil
return availableQuote.Mul(leverage), nil
}

View File

@ -282,35 +282,23 @@ func (s *Strategy) calculateQuantity(ctx context.Context, currentPrice fixedpoin
}
return balance.Available.Mul(fixedpoint.Min(s.Leverage, fixedpoint.One)).Div(currentPrice)
} else if !usingLeverage { // Spot
if side == types.SideTypeBuy {
balance, ok := s.session.GetAccount().Balance(s.Market.QuoteCurrency)
if !ok {
log.Errorf("can not update %s quote balance from exchange", s.Symbol)
return fixedpoint.Zero
}
return balance.Available.Mul(fixedpoint.Min(s.Leverage, fixedpoint.One)).Div(currentPrice)
} else if side == types.SideTypeSell {
balance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency)
if !ok {
log.Errorf("can not update %s base balance from exchange", s.Symbol)
return fixedpoint.Zero
}
return balance.Available
}
} else { // Using leverage
netValue, err := s.AccountValueCalculator.NetValue(ctx)
if err != nil {
log.WithError(err).Errorf("%s can not get net account value from exchange", s.Symbol)
} else if !usingLeverage && side == types.SideTypeSell { // Spot sell
balance, ok := s.session.GetAccount().Balance(s.Market.BaseCurrency)
if !ok {
log.Errorf("can not update %s base balance from exchange", s.Symbol)
return fixedpoint.Zero
}
return netValue.Mul(s.Leverage).Div(currentPrice)
}
return balance.Available.Mul(fixedpoint.Min(s.Leverage, fixedpoint.One))
} else { // Using leverage or spot buy
quoteQty, err := risk.CalculateQuoteQuantity(s.session, ctx, s.Market.QuoteCurrency, s.Leverage)
if err != nil {
log.WithError(err).Errorf("can not update %s quote balance from exchange", s.Symbol)
return fixedpoint.Zero
}
return fixedpoint.Zero
return quoteQty.Div(currentPrice)
}
}
// PrintResult prints accumulated profit status