Merge pull request #1000 from c9s/narumi/rebalance/backtest

fix: rebalance: fix backtest
This commit is contained in:
なるみ 2022-11-01 21:02:54 +08:00 committed by GitHub
commit ba7985690f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 18 deletions

View File

@ -8,18 +8,34 @@ notifications:
orderUpdate: true orderUpdate: true
submitOrder: true submitOrder: true
backtest:
startTime: "2022-01-01"
endTime: "2022-10-01"
symbols:
- BTCUSDT
- ETHUSDT
- MAXUSDT
account:
max:
makerFeeRate: 0.075%
takerFeeRate: 0.075%
balances:
BTC: 0.0
ETH: 0.0
MAX: 0.0
USDT: 10000.0
exchangeStrategies: exchangeStrategies:
- on: max - on: max
rebalance: rebalance:
interval: 1d interval: 1d
quoteCurrency: TWD quoteCurrency: USDT
targetWeights: targetWeights:
BTC: 40% BTC: 50%
ETH: 20% ETH: 25%
MAX: 10% MAX: 15%
USDT: 15% USDT: 10%
TWD: 15% threshold: 1%
threshold: 2%
# max amount to buy or sell per order # max amount to buy or sell per order
maxAmount: 10_000 maxAmount: 1_000
dryRun: false dryRun: false

View File

@ -66,7 +66,9 @@ func (s *Strategy) Validate() error {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.symbols()[0], types.SubscribeOptions{Interval: s.Interval}) for _, symbol := range s.symbols() {
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval})
}
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
@ -113,21 +115,20 @@ func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecut
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap { func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap {
m := make(types.ValueMap) m := make(types.ValueMap)
tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...)
if err != nil {
log.WithError(err).Error("failed to query tickers")
return nil
}
for currency := range s.TargetWeights { for currency := range s.TargetWeights {
if currency == s.QuoteCurrency { if currency == s.QuoteCurrency {
m[s.QuoteCurrency] = fixedpoint.One m[s.QuoteCurrency] = fixedpoint.One
continue continue
} }
m[currency] = tickers[currency+s.QuoteCurrency].Last
}
ticker, err := session.Exchange.QueryTicker(ctx, currency+s.QuoteCurrency)
if err != nil {
log.WithError(err).Error("failed to query tickers")
return nil
}
m[currency] = ticker.Last
}
return m return m
} }