Skip to content

Commit

Permalink
Merge pull request c9s#1393 from c9s/strategy/emacross
Browse files Browse the repository at this point in the history
STRATEGY: add emacross strategy
  • Loading branch information
c9s authored Dec 20, 2023
2 parents f047e75 + 4894a59 commit a4f996c
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
37 changes: 37 additions & 0 deletions config/emacross.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
persistence:
json:
directory: var/data
redis:
host: 127.0.0.1
port: 6379
db: 0

sessions:
binance:
exchange: binance
envVarPrefix: binance

exchangeStrategies:
- on: binance
emacross:
symbol: BTCUSDT
# interval: 5m
# fastWindow: 6
# slowWindow: 18
# quantity: 0.01
leverage: 2

backtest:
startTime: "2022-01-01"
endTime: "2022-03-01"
symbols:
- BTCUSDT
sessions: [max,binance]
# syncSecKLines: true
accounts:
binance:
makerFeeRate: 0.0%
takerFeeRate: 0.075%
balances:
BTC: 0.0
USDT: 10_000.0
1 change: 1 addition & 0 deletions pkg/cmd/strategy/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
_ "github.com/c9s/bbgo/pkg/strategy/deposit2transfer"
_ "github.com/c9s/bbgo/pkg/strategy/drift"
_ "github.com/c9s/bbgo/pkg/strategy/elliottwave"
_ "github.com/c9s/bbgo/pkg/strategy/emacross"
_ "github.com/c9s/bbgo/pkg/strategy/emastop"
_ "github.com/c9s/bbgo/pkg/strategy/etf"
_ "github.com/c9s/bbgo/pkg/strategy/ewoDgtrd"
Expand Down
118 changes: 118 additions & 0 deletions pkg/strategy/emacross/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package emacross

import (
"context"
"fmt"
"sync"

log "github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/strategy/common"
"github.com/c9s/bbgo/pkg/types"
)

const ID = "emacross"

func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}

type Strategy struct {
*common.Strategy

Environment *bbgo.Environment
Market types.Market

Symbol string `json:"symbol"`
Interval types.Interval `json:"interval"`
SlowWindow int `json:"slowWindow"`
FastWindow int `json:"fastWindow"`
OpenBelow fixedpoint.Value `json:"openBelow"`
CloseAbove fixedpoint.Value `json:"closeAbove"`

lastKLine types.KLine

bbgo.OpenPositionOptions
}

func (s *Strategy) ID() string {
return ID
}

func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s:%s:%d-%d", ID, s.Symbol, s.Interval, s.FastWindow, s.SlowWindow)
}

func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}

func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())

session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval5m, func(k types.KLine) {
s.lastKLine = k
}))

fastEMA := session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.FastWindow})
slowEMA := session.Indicators(s.Symbol).EWMA(types.IntervalWindow{Interval: s.Interval, Window: s.SlowWindow})

cross := indicatorv2.Cross(fastEMA, slowEMA)
cross.OnUpdate(func(v float64) {
switch indicatorv2.CrossType(v) {

case indicatorv2.CrossOver:
if err := s.Strategy.OrderExecutor.GracefulCancel(ctx); err != nil {
log.WithError(err).Errorf("unable to cancel order")
}

opts := s.OpenPositionOptions
opts.Long = true
if price, ok := session.LastPrice(s.Symbol); ok {
opts.Price = price
}

opts.Tags = []string{"emaCrossOver"}

_, err := s.Strategy.OrderExecutor.OpenPosition(ctx, opts)
logErr(err, "unable to open position")
case indicatorv2.CrossUnder:
err := s.Strategy.OrderExecutor.ClosePosition(ctx, fixedpoint.One)
logErr(err, "unable to submit close position order")
}
})

bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
})

return nil
}

func logErr(err error, msgAndArgs ...interface{}) bool {
if err == nil {
return false
}

if len(msgAndArgs) == 0 {
log.WithError(err).Error(err.Error())
} else if len(msgAndArgs) == 1 {
msg := msgAndArgs[0].(string)
log.WithError(err).Error(msg)
} else if len(msgAndArgs) > 1 {
msg := msgAndArgs[0].(string)
log.WithError(err).Errorf(msg, msgAndArgs[1:]...)
}

return true
}
5 changes: 4 additions & 1 deletion pkg/strategy/fixedmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (s *Strategy) Defaults() error {
}

func (s *Strategy) Initialize() error {
s.Strategy = &common.Strategy{}
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}

return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/strategy/liquiditymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ type Strategy struct {
}

func (s *Strategy) Initialize() error {
s.Strategy = &common.Strategy{}
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/strategy/scmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ type Strategy struct {
}

func (s *Strategy) Initialize() error {
s.Strategy = &common.Strategy{}
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}
return nil
}

Expand Down

0 comments on commit a4f996c

Please sign in to comment.