use types.Interval instead of string

This commit is contained in:
c9s 2022-05-19 09:48:36 +08:00
parent 038781a094
commit 13bf5d69a3
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
24 changed files with 100 additions and 61 deletions

View File

@ -35,6 +35,7 @@ type SummaryReport struct {
EndTime time.Time `json:"endTime"` EndTime time.Time `json:"endTime"`
Sessions []string `json:"sessions"` Sessions []string `json:"sessions"`
Symbols []string `json:"symbols"` Symbols []string `json:"symbols"`
Intervals []types.Interval `json:"intervals"`
InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"` InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"`
FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"` FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"`
@ -48,6 +49,8 @@ type SummaryReport struct {
type SessionSymbolReport struct { type SessionSymbolReport struct {
Exchange types.ExchangeName `json:"exchange"` Exchange types.ExchangeName `json:"exchange"`
Symbol string `json:"symbol,omitempty"` Symbol string `json:"symbol,omitempty"`
Intervals []types.Interval `json:"intervals,omitempty"`
Subscriptions []types.Subscription `json:"subscriptions"`
Market types.Market `json:"market"` Market types.Market `json:"market"`
LastPrice fixedpoint.Value `json:"lastPrice,omitempty"` LastPrice fixedpoint.Value `json:"lastPrice,omitempty"`
StartPrice fixedpoint.Value `json:"startPrice,omitempty"` StartPrice fixedpoint.Value `json:"startPrice,omitempty"`

View File

@ -52,7 +52,7 @@ func NewTrailingStopController(symbol string, config *TrailingStop) *TrailingSto
func (c *TrailingStopController) Subscribe(session *ExchangeSession) { func (c *TrailingStopController) Subscribe(session *ExchangeSession) {
session.Subscribe(types.KLineChannel, c.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, c.Symbol, types.SubscribeOptions{
Interval: c.Interval.String(), Interval: c.Interval,
}) })
} }

View File

@ -273,7 +273,8 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
exchangeSources, err := toExchangeSources(environ.Sessions()) backTestIntervals := []types.Interval{types.Interval1h, types.Interval1d}
exchangeSources, err := toExchangeSources(environ.Sessions(), backTestIntervals...)
if err != nil { if err != nil {
return err return err
} }
@ -468,7 +469,21 @@ var BacktestCmd = &cobra.Command{
Symbols: nil, Symbols: nil,
} }
allKLineIntervals := map[types.Interval]struct{}{}
for _, session := range environ.Sessions() { for _, session := range environ.Sessions() {
for _, sub := range session.Subscriptions {
if sub.Channel == types.KLineChannel {
allKLineIntervals[types.Interval(sub.Options.Interval)] = struct{}{}
}
}
}
for interval := range allKLineIntervals {
summaryReport.Intervals = append(summaryReport.Intervals, interval)
}
for _, session := range environ.Sessions() {
for symbol, trades := range session.Trades { for symbol, trades := range session.Trades {
symbolReport, err := createSymbolReport(userConfig, session, symbol, trades.Trades) symbolReport, err := createSymbolReport(userConfig, session, symbol, trades.Trades)
if err != nil { if err != nil {
@ -551,6 +566,22 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
FinalBalances: finalBalances, FinalBalances: finalBalances,
// Manifests: manifests, // Manifests: manifests,
} }
for _, s := range session.Subscriptions {
symbolReport.Subscriptions = append(symbolReport.Subscriptions, s)
}
sessionKLineIntervals := map[types.Interval]struct{}{}
for _, sub := range session.Subscriptions {
if sub.Channel == types.KLineChannel {
sessionKLineIntervals[types.Interval(sub.Options.Interval)] = struct{}{}
}
}
for interval := range sessionKLineIntervals {
symbolReport.Intervals = append(symbolReport.Intervals, interval)
}
return &symbolReport, nil return &symbolReport, nil
} }
@ -586,12 +617,12 @@ func confirmation(s string) bool {
} }
} }
func toExchangeSources(sessions map[string]*bbgo.ExchangeSession) (exchangeSources []backtest.ExchangeDataSource, err error) { func toExchangeSources(sessions map[string]*bbgo.ExchangeSession, extraIntervals ...types.Interval) (exchangeSources []backtest.ExchangeDataSource, err error) {
for _, session := range sessions { for _, session := range sessions {
exchange := session.Exchange.(*backtest.Exchange) exchange := session.Exchange.(*backtest.Exchange)
exchange.InitMarketData() exchange.InitMarketData()
c, err := exchange.SubscribeMarketData(types.Interval1h, types.Interval1d) c, err := exchange.SubscribeMarketData(extraIntervals...)
if err != nil { if err != nil {
return exchangeSources, err return exchangeSources, err
} }

View File

@ -56,7 +56,7 @@ var klineCmd = &cobra.Command{
s := session.Exchange.NewStream() s := session.Exchange.NewStream()
s.SetPublicOnly() s.SetPublicOnly()
s.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: interval}) s.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: types.Interval(interval)})
s.OnKLineClosed(func(kline types.KLine) { s.OnKLineClosed(func(kline types.KLine) {
log.Infof("kline closed: %s", kline.String()) log.Infof("kline closed: %s", kline.String())

View File

@ -92,7 +92,7 @@ func (s *Stream) handleConnect() {
Channel: string(sub.Channel), Channel: string(sub.Channel),
Market: toLocalSymbol(sub.Symbol), Market: toLocalSymbol(sub.Symbol),
Depth: depth, Depth: depth,
Resolution: sub.Options.Interval, Resolution: sub.Options.Interval.String(),
}) })
} }

View File

@ -17,7 +17,7 @@ func toGlobalSymbol(symbol string) string {
return strings.ReplaceAll(symbol, "-", "") return strings.ReplaceAll(symbol, "-", "")
} }
////go:generate sh -c "echo \"package okex\nvar spotSymbolMap = map[string]string{\n\" $(curl -s -L 'https://okex.com/api/v5/public/instruments?instType=SPOT' | jq -r '.data[] | \"\\(.instId | sub(\"-\" ; \"\") | tojson ): \\( .instId | tojson),\n\"') \"\n}\" > symbols.go" // //go:generate sh -c "echo \"package okex\nvar spotSymbolMap = map[string]string{\n\" $(curl -s -L 'https://okex.com/api/v5/public/instruments?instType=SPOT' | jq -r '.data[] | \"\\(.instId | sub(\"-\" ; \"\") | tojson ): \\( .instId | tojson),\n\"') \"\n}\" > symbols.go"
//go:generate go run gensymbols.go //go:generate go run gensymbols.go
func toLocalSymbol(symbol string) string { func toLocalSymbol(symbol string) string {
if s, ok := spotSymbolMap[symbol]; ok { if s, ok := spotSymbolMap[symbol]; ok {
@ -68,18 +68,19 @@ var CandleChannels = []string{
"candle30m", "candle15m", "candle5m", "candle3m", "candle1m", "candle30m", "candle15m", "candle5m", "candle3m", "candle1m",
} }
func convertIntervalToCandle(interval string) string { func convertIntervalToCandle(interval types.Interval) string {
switch interval { s := interval.String()
switch s {
case "1h", "2h", "4h", "6h", "12h", "1d", "3d": case "1h", "2h", "4h", "6h", "12h", "1d", "3d":
return "candle" + strings.ToUpper(interval) return "candle" + strings.ToUpper(s)
case "1m", "5m", "15m", "30m": case "1m", "5m", "15m", "30m":
return "candle" + interval return "candle" + s
} }
return "candle" + interval return "candle" + s
} }
func convertSubscription(s types.Subscription) (WebsocketSubscription, error) { func convertSubscription(s types.Subscription) (WebsocketSubscription, error) {
@ -270,7 +271,7 @@ func toGlobalOrderType(orderType okexapi.OrderType) (types.OrderType, error) {
return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType) return "", fmt.Errorf("unknown or unsupported okex order type: %s", orderType)
} }
func toLocalInterval(src string, ) string { func toLocalInterval(src string) string {
var re = regexp.MustCompile("\\d+[hdw]") var re = regexp.MustCompile("\\d+[hdw]")
return re.ReplaceAllStringFunc(src, func(w string) string { return re.ReplaceAllStringFunc(src, func(w string) string {
return strings.ToUpper(w) return strings.ToUpper(w)

View File

@ -34,7 +34,7 @@ func toSubscriptions(sub *pb.Subscription) (types.Subscription, error) {
Symbol: sub.Symbol, Symbol: sub.Symbol,
Channel: types.KLineChannel, Channel: types.KLineChannel,
Options: types.SubscribeOptions{ Options: types.SubscribeOptions{
Interval: sub.Interval, Interval: types.Interval(sub.Interval),
}, },
}, nil }, nil
} }

View File

@ -108,10 +108,10 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
} }
// currently we need the 1m kline to update the last close price and indicators // currently we need the 1m kline to update the last close price and indicators
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
if len(s.RepostInterval) > 0 && s.Interval != s.RepostInterval { if len(s.RepostInterval) > 0 && s.Interval != s.RepostInterval {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.RepostInterval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.RepostInterval})
} }
} }

View File

@ -185,18 +185,18 @@ func (s *Strategy) Initialize() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(s.Interval), Interval: s.Interval,
}) })
if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" { if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(s.DefaultBollinger.Interval), Interval: s.DefaultBollinger.Interval,
}) })
} }
if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" { if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(s.NeutralBollinger.Interval), Interval: s.NeutralBollinger.Interval,
}) })
} }

View File

@ -73,8 +73,8 @@ func (s *Strategy) ID() string {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverageInterval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverageInterval})
} }
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) { func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
@ -83,7 +83,7 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
// make sure we have the connection alive // make sure we have the connection alive
targetSession := sessions[s.TargetExchangeName] targetSession := sessions[s.TargetExchangeName]
targetSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) targetSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
} }
func (s *Strategy) clear(ctx context.Context, orderExecutor bbgo.OrderExecutor) { func (s *Strategy) clear(ctx context.Context, orderExecutor bbgo.OrderExecutor) {

View File

@ -81,8 +81,8 @@ func (s *Strategy) Initialize() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
log.Infof("subscribe %s", s.Symbol) log.Infof("subscribe %s", s.Symbol)
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.BookTickerChannel, s.Symbol, types.SubscribeOptions{}) session.Subscribe(types.BookTickerChannel, s.Symbol, types.SubscribeOptions{})

View File

@ -3,11 +3,13 @@ package factorzoo
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/sajari/regression"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/sajari/regression"
"github.com/sirupsen/logrus"
) )
const ID = "factorzoo" const ID = "factorzoo"
@ -57,7 +59,7 @@ func (s *Strategy) ID() string {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
log.Infof("subscribe %s", s.Symbol) log.Infof("subscribe %s", s.Symbol)
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
} }
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
@ -85,7 +87,7 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
Market: s.Market, Market: s.Market,
} }
//s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder) // s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder) createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder)
if err != nil { if err != nil {
@ -99,13 +101,13 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, er fixedpoint.Value) { func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, er fixedpoint.Value) {
//if s.prevER.Sign() < 0 && er.Sign() > 0 { // if s.prevER.Sign() < 0 && er.Sign() > 0 {
if er.Sign() >= 0 { if er.Sign() >= 0 {
submitOrder := types.SubmitOrder{ submitOrder := types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: types.OrderTypeMarket, Type: types.OrderTypeMarket,
Quantity: s.Quantity, //er.Abs().Mul(fixedpoint.NewFromInt(20)), Quantity: s.Quantity, // er.Abs().Mul(fixedpoint.NewFromInt(20)),
} }
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder) createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil { if err != nil {
@ -113,13 +115,13 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec
} }
s.orderStore.Add(createdOrders...) s.orderStore.Add(createdOrders...)
s.activeMakerOrders.Add(createdOrders...) s.activeMakerOrders.Add(createdOrders...)
//} else if s.prevER.Sign() > 0 && er.Sign() < 0 { // } else if s.prevER.Sign() > 0 && er.Sign() < 0 {
} else { } else {
submitOrder := types.SubmitOrder{ submitOrder := types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Type: types.OrderTypeMarket, Type: types.OrderTypeMarket,
Quantity: s.Quantity, //er.Abs().Mul(fixedpoint.NewFromInt(20)), Quantity: s.Quantity, // er.Abs().Mul(fixedpoint.NewFromInt(20)),
} }
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder) createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil { if err != nil {
@ -144,13 +146,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.pvDivergence = &Correlation{IntervalWindow: iw} s.pvDivergence = &Correlation{IntervalWindow: iw}
// bind indicator to the data store, so that our callback could be triggered // bind indicator to the data store, so that our callback could be triggered
s.pvDivergence.Bind(st) s.pvDivergence.Bind(st)
//s.pvDivergence.OnUpdate(func(corr float64) { // s.pvDivergence.OnUpdate(func(corr float64) {
// //fmt.Printf("now we've got corr: %f\n", corr) // //fmt.Printf("now we've got corr: %f\n", corr)
//}) // })
s.Alpha = [][]float64{{}, {}, {}, {}, {}} s.Alpha = [][]float64{{}, {}, {}, {}, {}}
s.Ret = []float64{} s.Ret = []float64{}
//thetas := []float64{0, 0, 0, 0} // thetas := []float64{0, 0, 0, 0}
preCompute := 0 preCompute := 0
s.activeMakerOrders = bbgo.NewLocalActiveOrderBook(s.Symbol) s.activeMakerOrders = bbgo.NewLocalActiveOrderBook(s.Symbol)
@ -198,7 +200,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.Alpha[3] = append(s.Alpha[3], mom.Float64()) s.Alpha[3] = append(s.Alpha[3], mom.Float64())
s.Alpha[4] = append(s.Alpha[4], ogap.Float64()) s.Alpha[4] = append(s.Alpha[4], ogap.Float64())
//s.Alpha[5] = append(s.Alpha[4], 1.0) // constant // s.Alpha[5] = append(s.Alpha[4], 1.0) // constant
ret := kline.Close.Sub(s.prevClose).Div(s.prevClose).Float64() ret := kline.Close.Sub(s.prevClose).Div(s.prevClose).Float64()
s.Ret = append(s.Ret, ret) s.Ret = append(s.Ret, ret)
@ -231,7 +233,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
r.Train(rdp...) r.Train(rdp...)
r.Run() r.Run()
fmt.Printf("Regression formula:\n%v\n", r.Formula) fmt.Printf("Regression formula:\n%v\n", r.Formula)
//prediction := r.Coeff(0)*corr.Float64() + r.Coeff(1)*rev.Float64() + r.Coeff(2)*factorzoo.Float64() + r.Coeff(3)*mom.Float64() + r.Coeff(4) // prediction := r.Coeff(0)*corr.Float64() + r.Coeff(1)*rev.Float64() + r.Coeff(2)*factorzoo.Float64() + r.Coeff(3)*mom.Float64() + r.Coeff(4)
prediction, _ := r.Predict([]float64{corr.Float64(), rev.Float64(), a150.Float64(), mom.Float64(), ogap.Float64()}) prediction, _ := r.Predict([]float64{corr.Float64(), rev.Float64(), a150.Float64(), mom.Float64(), ogap.Float64()})
log.Infof("Predicted Return: %f", prediction) log.Infof("Predicted Return: %f", prediction)

View File

@ -106,7 +106,7 @@ func (s *Strategy) updateBidOrders(orderExecutor bbgo.OrderExecutor, session *bb
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) session.Subscribe(types.KLineChannel, s.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 {

View File

@ -77,10 +77,10 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
for _, detection := range s.SupportDetection { for _, detection := range s.SupportDetection {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.Interval), Interval: detection.Interval,
}) })
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.MovingAverageIntervalWindow.Interval), Interval: detection.MovingAverageIntervalWindow.Interval,
}) })
} }
} }

View File

@ -2,6 +2,7 @@ package kline
import ( import (
"context" "context"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
@ -26,7 +27,7 @@ func (s *Strategy) ID() string {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.MovingAverage.Interval)}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverage.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 {

View File

@ -3,6 +3,7 @@ package pivotshort
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
@ -65,9 +66,8 @@ func (s *Strategy) ID() string {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
log.Infof("subscribe %s", s.Symbol) log.Infof("subscribe %s", s.Symbol)
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
//session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1d.String()}) //session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1d})
} }
func (s *Strategy) placeOrder(ctx context.Context, price fixedpoint.Value, qty fixedpoint.Value, orderExecutor bbgo.OrderExecutor) { func (s *Strategy) placeOrder(ctx context.Context, price fixedpoint.Value, qty fixedpoint.Value, orderExecutor bbgo.OrderExecutor) {

View File

@ -20,7 +20,7 @@ type Strategy struct {
// These fields will be filled from the config file (it translates YAML to JSON) // These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
Interval string `json:"interval"` Interval types.Interval `json:"interval"`
MinChange fixedpoint.Value `json:"minChange"` MinChange fixedpoint.Value `json:"minChange"`
} }

View File

@ -35,7 +35,7 @@ func (s *Strategy) ID() string {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) session.Subscribe(types.KLineChannel, s.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 {

View File

@ -74,7 +74,7 @@ func (s *Strategy) Validate() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
for _, symbol := range s.getSymbols() { for _, symbol := range s.getSymbols() {
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval})
} }
} }

View File

@ -46,12 +46,12 @@ func (s *Strategy) ID() string {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
if s.BelowMovingAverage != nil { if s.BelowMovingAverage != nil {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.BelowMovingAverage.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.BelowMovingAverage.Interval})
} }
if s.AboveMovingAverage != nil { if s.AboveMovingAverage != nil {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.AboveMovingAverage.Interval.String()}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.AboveMovingAverage.Interval})
} }
} }

View File

@ -202,14 +202,14 @@ func (s *Strategy) Validate() error {
} }
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.Interval)}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
if s.TriggerMovingAverage != zeroiw { if s.TriggerMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.TriggerMovingAverage.Interval)}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.TriggerMovingAverage.Interval})
} }
if s.LongTermMovingAverage != zeroiw { if s.LongTermMovingAverage != zeroiw {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: string(s.LongTermMovingAverage.Interval)}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LongTermMovingAverage.Interval})
} }
} }

View File

@ -55,7 +55,7 @@ type Strategy struct {
// Interval is the interval of the kline channel we want to subscribe, // Interval is the interval of the kline channel we want to subscribe,
// the kline event will trigger the strategy to check if we need to submit order. // the kline event will trigger the strategy to check if we need to submit order.
Interval string `json:"interval"` Interval types.Interval `json:"interval"`
// MinChange filters out the k-lines with small changes. so that our strategy will only be triggered // MinChange filters out the k-lines with small changes. so that our strategy will only be triggered
// in specific events. // in specific events.

View File

@ -7,9 +7,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/binance" "github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -69,11 +70,11 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{}) // session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
for _, detection := range s.SupportDetection { for _, detection := range s.SupportDetection {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.Interval), Interval: detection.Interval,
}) })
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.MovingAverageInterval), Interval: detection.MovingAverageInterval,
}) })
} }
} }

View File

@ -410,14 +410,14 @@ const (
// SubscribeOptions provides the standard stream options // SubscribeOptions provides the standard stream options
type SubscribeOptions struct { type SubscribeOptions struct {
// TODO: change to Interval type later // TODO: change to Interval type later
Interval string `json:"interval,omitempty"` Interval Interval `json:"interval,omitempty"`
Depth Depth `json:"depth,omitempty"` Depth Depth `json:"depth,omitempty"`
Speed Speed `json:"speed,omitempty"` Speed Speed `json:"speed,omitempty"`
} }
func (o SubscribeOptions) String() string { func (o SubscribeOptions) String() string {
if len(o.Interval) > 0 { if len(o.Interval) > 0 {
return o.Interval return string(o.Interval)
} }
return string(o.Depth) return string(o.Depth)