pivotshort: add leverage settings

This commit is contained in:
c9s 2022-07-14 17:44:25 +08:00
parent adb96cac39
commit c4332fcac2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 15 additions and 3 deletions

View File

@ -27,6 +27,7 @@ type BreakLow struct {
// limit sell price = breakLowPrice * (1 + BounceRatio)
BounceRatio fixedpoint.Value `json:"bounceRatio"`
Leverage fixedpoint.Value `json:"leverage"`
Quantity fixedpoint.Value `json:"quantity"`
StopEMARange fixedpoint.Value `json:"stopEMARange"`
StopEMA *types.IntervalWindow `json:"stopEMA"`
@ -170,8 +171,7 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
// graceful cancel all active orders
_ = orderExecutor.GracefulCancel(ctx)
leverage := fixedpoint.NewFromInt(5)
quantity, err := useQuantityOrBaseBalance(s.session, s.Market, closePrice, s.Quantity, leverage)
quantity, err := useQuantityOrBaseBalance(s.session, s.Market, closePrice, s.Quantity, s.Leverage)
if err != nil {
log.WithError(err).Errorf("quantity calculation error")
}
@ -222,6 +222,10 @@ func useQuantityOrBaseBalance(session *bbgo.ExchangeSession, market types.Market
return quantity, nil
}
if leverage.IsZero() {
leverage = fixedpoint.NewFromInt(3)
}
// quantity is zero, we need to calculate the quantity
baseBalance, _ := session.Account.Balance(market.BaseCurrency)
quoteBalance, _ := session.Account.Balance(market.QuoteCurrency)

View File

@ -155,7 +155,11 @@ type Strategy struct {
// pivot interval and window
types.IntervalWindow
Leverage fixedpoint.Value `json:"leverage"`
Quantity fixedpoint.Value `json:"quantity"`
// persistence fields
Position *types.Position `persistence:"position"`
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
@ -177,7 +181,6 @@ type Strategy struct {
bbgo.StrategyController
}
func (s *Strategy) ID() string {
return ID
}
@ -236,6 +239,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.TradeStats = types.NewTradeStats(s.Symbol)
}
if s.Leverage.IsZero() {
// the default leverage is 3x
s.Leverage = fixedpoint.NewFromInt(3)
}
// StrategyController
s.Status = types.StrategyStatusRunning