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"`
Sessions []string `json:"sessions"`
Symbols []string `json:"symbols"`
Intervals []types.Interval `json:"intervals"`
InitialTotalBalances types.BalanceMap `json:"initialTotalBalances"`
FinalTotalBalances types.BalanceMap `json:"finalTotalBalances"`
@ -48,6 +49,8 @@ type SummaryReport struct {
type SessionSymbolReport struct {
Exchange types.ExchangeName `json:"exchange"`
Symbol string `json:"symbol,omitempty"`
Intervals []types.Interval `json:"intervals,omitempty"`
Subscriptions []types.Subscription `json:"subscriptions"`
Market types.Market `json:"market"`
LastPrice fixedpoint.Value `json:"lastPrice,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) {
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
}
exchangeSources, err := toExchangeSources(environ.Sessions())
backTestIntervals := []types.Interval{types.Interval1h, types.Interval1d}
exchangeSources, err := toExchangeSources(environ.Sessions(), backTestIntervals...)
if err != nil {
return err
}
@ -468,7 +469,21 @@ var BacktestCmd = &cobra.Command{
Symbols: nil,
}
allKLineIntervals := map[types.Interval]struct{}{}
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 {
symbolReport, err := createSymbolReport(userConfig, session, symbol, trades.Trades)
if err != nil {
@ -551,6 +566,22 @@ func createSymbolReport(userConfig *bbgo.Config, session *bbgo.ExchangeSession,
FinalBalances: finalBalances,
// 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
}
@ -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 {
exchange := session.Exchange.(*backtest.Exchange)
exchange.InitMarketData()
c, err := exchange.SubscribeMarketData(types.Interval1h, types.Interval1d)
c, err := exchange.SubscribeMarketData(extraIntervals...)
if err != nil {
return exchangeSources, err
}

View File

@ -56,7 +56,7 @@ var klineCmd = &cobra.Command{
s := session.Exchange.NewStream()
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) {
log.Infof("kline closed: %s", kline.String())

View File

@ -92,7 +92,7 @@ func (s *Stream) handleConnect() {
Channel: string(sub.Channel),
Market: toLocalSymbol(sub.Symbol),
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, "-", "")
}
////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
func toLocalSymbol(symbol string) string {
if s, ok := spotSymbolMap[symbol]; ok {
@ -68,18 +68,19 @@ var CandleChannels = []string{
"candle30m", "candle15m", "candle5m", "candle3m", "candle1m",
}
func convertIntervalToCandle(interval string) string {
switch interval {
func convertIntervalToCandle(interval types.Interval) string {
s := interval.String()
switch s {
case "1h", "2h", "4h", "6h", "12h", "1d", "3d":
return "candle" + strings.ToUpper(interval)
return "candle" + strings.ToUpper(s)
case "1m", "5m", "15m", "30m":
return "candle" + interval
return "candle" + s
}
return "candle" + interval
return "candle" + s
}
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)
}
func toLocalInterval(src string, ) string {
func toLocalInterval(src string) string {
var re = regexp.MustCompile("\\d+[hdw]")
return re.ReplaceAllStringFunc(src, func(w string) string {
return strings.ToUpper(w)

View File

@ -34,7 +34,7 @@ func toSubscriptions(sub *pb.Subscription) (types.Subscription, error) {
Symbol: sub.Symbol,
Channel: types.KLineChannel,
Options: types.SubscribeOptions{
Interval: sub.Interval,
Interval: types.Interval(sub.Interval),
},
}, 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
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 {
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) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(s.Interval),
Interval: s.Interval,
})
if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(s.DefaultBollinger.Interval),
Interval: s.DefaultBollinger.Interval,
})
}
if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" {
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) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval.String()})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverageInterval.String()})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverageInterval})
}
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
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) {

View File

@ -81,8 +81,8 @@ func (s *Strategy) Initialize() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
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: s.Interval.String()})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
session.Subscribe(types.BookTickerChannel, s.Symbol, types.SubscribeOptions{})

View File

@ -3,11 +3,13 @@ package factorzoo
import (
"context"
"fmt"
"github.com/sajari/regression"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/sajari/regression"
"github.com/sirupsen/logrus"
)
const ID = "factorzoo"
@ -57,7 +59,7 @@ func (s *Strategy) ID() string {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
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 {
@ -85,7 +87,7 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
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)
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) {
//if s.prevER.Sign() < 0 && er.Sign() > 0 {
// if s.prevER.Sign() < 0 && er.Sign() > 0 {
if er.Sign() >= 0 {
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
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)
if err != nil {
@ -113,13 +115,13 @@ func (s *Strategy) placeOrders(ctx context.Context, orderExecutor bbgo.OrderExec
}
s.orderStore.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 {
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
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)
if err != nil {
@ -144,13 +146,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.pvDivergence = &Correlation{IntervalWindow: iw}
// bind indicator to the data store, so that our callback could be triggered
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)
//})
// })
s.Alpha = [][]float64{{}, {}, {}, {}, {}}
s.Ret = []float64{}
//thetas := []float64{0, 0, 0, 0}
// thetas := []float64{0, 0, 0, 0}
preCompute := 0
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[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()
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.Run()
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()})
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) {
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 {

View File

@ -77,10 +77,10 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
for _, detection := range s.SupportDetection {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.Interval),
Interval: detection.Interval,
})
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 (
"context"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
@ -26,7 +27,7 @@ func (s *Strategy) ID() string {
}
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 {

View File

@ -3,6 +3,7 @@ package pivotshort
import (
"context"
"fmt"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
@ -65,9 +66,8 @@ func (s *Strategy) ID() string {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
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: types.Interval1d.String()})
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
//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) {

View File

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

View File

@ -35,7 +35,7 @@ func (s *Strategy) ID() string {
}
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 {

View File

@ -74,7 +74,7 @@ func (s *Strategy) Validate() error {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
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) {
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 {
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 {
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) {
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 {
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 {
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,
// 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
// in specific events.

View File

@ -7,9 +7,10 @@ import (
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"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{})
for _, detection := range s.SupportDetection {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.Interval),
Interval: detection.Interval,
})
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
type SubscribeOptions struct {
// TODO: change to Interval type later
Interval string `json:"interval,omitempty"`
Depth Depth `json:"depth,omitempty"`
Speed Speed `json:"speed,omitempty"`
Interval Interval `json:"interval,omitempty"`
Depth Depth `json:"depth,omitempty"`
Speed Speed `json:"speed,omitempty"`
}
func (o SubscribeOptions) String() string {
if len(o.Interval) > 0 {
return o.Interval
return string(o.Interval)
}
return string(o.Depth)