Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/strategy/supertrend/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ func (s *Strategy) Validate() error {

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LinearRegression.Interval})
// LinearRegression is optional: setupIndicators() nils it for window==0 or
// empty-interval configs, and session.Subscribe panics on an empty kline
// interval — guard both panic paths here. Degenerate configs (window==0
// with an interval set) keep their historical unused subscription so
// existing backtest replay inputs are unchanged.
if s.LinearRegression != nil && s.LinearRegression.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LinearRegression.Interval})
}

s.ExitMethods.SetAndSubscribe(session, s)

Expand Down
66 changes: 66 additions & 0 deletions pkg/strategy/supertrend/strategy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package supertrend

import (
"testing"

"go.uber.org/mock/gomock"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/mocks"
)

func newTestSession(t *testing.T) *bbgo.ExchangeSession {
t.Helper()

mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).AnyTimes()
mockEx.EXPECT().Name().Return(types.ExchangeName("backtest")).AnyTimes()

return bbgo.NewExchangeSession("test", mockEx)
}

// A linearRegression block with window: 0 (or no interval) is nil-ed by
// setupIndicators, but Subscribe used to dereference it unconditionally and
// session.Subscribe panics on an empty kline interval.
func TestStrategySubscribe_LinearRegressionNilSafe(t *testing.T) {
cases := []struct {
name string
linReg *LinReg
subs int
}{
{name: "nil block", linReg: nil, subs: 1},
{
name: "window zero without interval",
linReg: &LinReg{IntervalWindow: types.IntervalWindow{Window: 0}},
subs: 1,
},
{
name: "valid block",
linReg: &LinReg{
IntervalWindow: types.IntervalWindow{Window: 20, Interval: types.Interval4h},
},
subs: 2,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
session := newTestSession(t)
s := &Strategy{
Symbol: "BTCUSDT",
IntervalWindow: types.IntervalWindow{Interval: types.Interval1h, Window: 5},
LinearRegression: tc.linReg,
}

s.Subscribe(session)

if got := len(session.Subscriptions); got != tc.subs {
t.Fatalf("subscriptions = %d, want %d", got, tc.subs)
}
})
}
}
Loading