mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add context parameter to the strategy method
This commit is contained in:
parent
fe3ae14fc8
commit
d1b618850d
|
@ -6,7 +6,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/accounting"
|
"github.com/c9s/bbgo/pkg/accounting"
|
||||||
|
@ -18,11 +17,11 @@ import (
|
||||||
|
|
||||||
// SingleExchangeStrategy represents the single Exchange strategy
|
// SingleExchangeStrategy represents the single Exchange strategy
|
||||||
type SingleExchangeStrategy interface {
|
type SingleExchangeStrategy interface {
|
||||||
Run(trader types.Trader, session *ExchangeSession) error
|
Run(ctx context.Context, trader types.Trader, session *ExchangeSession) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type CrossExchangeStrategy interface {
|
type CrossExchangeStrategy interface {
|
||||||
Run(trader types.Trader, sessions map[string]*ExchangeSession) error
|
Run(ctx context.Context, trader types.Trader, sessions map[string]*ExchangeSession) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExchangeSession presents the exchange connection session
|
// ExchangeSession presents the exchange connection session
|
||||||
|
@ -199,13 +198,12 @@ func (trader *Trader) AddNotifier(notifier Notifier) {
|
||||||
|
|
||||||
// AttachStrategy attaches the single exchange strategy on an exchange session.
|
// AttachStrategy attaches the single exchange strategy on an exchange session.
|
||||||
// Single exchange strategy is the default behavior.
|
// Single exchange strategy is the default behavior.
|
||||||
func (trader *Trader) AttachStrategy(session string, strategy SingleExchangeStrategy) error {
|
func (trader *Trader) AttachStrategy(session string, strategy SingleExchangeStrategy) {
|
||||||
if _, ok := trader.environment.sessions[session]; !ok {
|
if _, ok := trader.environment.sessions[session]; !ok {
|
||||||
return errors.New("session not defined")
|
log.Panicf("session %s is not defined", session)
|
||||||
}
|
}
|
||||||
|
|
||||||
trader.exchangeStrategies[session] = append(trader.exchangeStrategies[session], strategy)
|
trader.exchangeStrategies[session] = append(trader.exchangeStrategies[session], strategy)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AttachCrossExchangeStrategy attaches the cross exchange strategy
|
// AttachCrossExchangeStrategy attaches the cross exchange strategy
|
||||||
|
@ -222,7 +220,7 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
// load and run session strategies
|
// load and run session strategies
|
||||||
for session, strategies := range trader.exchangeStrategies {
|
for session, strategies := range trader.exchangeStrategies {
|
||||||
for _, strategy := range strategies {
|
for _, strategy := range strategies {
|
||||||
err := strategy.Run(trader, trader.environment.sessions[session])
|
err := strategy.Run(ctx, trader, trader.environment.sessions[session])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -230,7 +228,7 @@ func (trader *Trader) Run(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, strategy := range trader.crossExchangeStrategies {
|
for _, strategy := range trader.crossExchangeStrategies {
|
||||||
if err := strategy.Run(trader, trader.environment.sessions); err != nil {
|
if err := strategy.Run(ctx, trader, trader.environment.sessions); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package buyandhold
|
package buyandhold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/c9s/bbgo/pkg/bbgo"
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
@ -15,10 +17,21 @@ func New(symbol string) *Strategy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Run(trader types.Trader, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, trader types.Trader, session *bbgo.ExchangeSession) error {
|
||||||
session.Subscribe(types.KLineChannel, s.symbol, types.SubscribeOptions{})
|
session.Subscribe(types.KLineChannel, s.symbol, types.SubscribeOptions{ Interval: "1h" })
|
||||||
|
|
||||||
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
||||||
// trader.SubmitOrder(ctx, ....)
|
changePercentage := kline.GetChange() / kline.Open
|
||||||
|
|
||||||
|
// buy when price drops -10%
|
||||||
|
if changePercentage < -0.1 {
|
||||||
|
trader.SubmitOrder(ctx, &types.SubmitOrder{
|
||||||
|
Symbol: kline.Symbol,
|
||||||
|
Side: types.SideTypeBuy,
|
||||||
|
Type: types.OrderTypeMarket,
|
||||||
|
Quantity: 1.0,
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -153,6 +153,7 @@ func (k KLine) GetBody() float64 {
|
||||||
return k.GetChange()
|
return k.GetChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetChange returns Close price - Open price.
|
||||||
func (k KLine) GetChange() float64 {
|
func (k KLine) GetChange() float64 {
|
||||||
return k.Close - k.Open
|
return k.Close - k.Open
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user