forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bind market data store and query avg price before we start
- Loading branch information
Showing
12 changed files
with
324 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package buyandhold | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"syscall" | ||
|
||
"github.com/jmoiron/sqlx" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/c9s/bbgo/cmd/cmdutil" | ||
"github.com/c9s/bbgo/pkg/bbgo" | ||
"github.com/c9s/bbgo/pkg/strategy/buyandhold" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
func init() { | ||
rootCmd.Flags().String("exchange", "", "target exchange") | ||
rootCmd.Flags().String("symbol", "", "trading symbol") | ||
} | ||
|
||
func connectMysql() (*sqlx.DB, error) { | ||
mysqlURL := viper.GetString("mysql-url") | ||
mysqlURL = fmt.Sprintf("%s?parseTime=true", mysqlURL) | ||
return sqlx.Connect("mysql", mysqlURL) | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "buyandhold", | ||
Short: "buy and hold", | ||
Long: "hold trader", | ||
|
||
// SilenceUsage is an option to silence usage when an error occurs. | ||
SilenceUsage: true, | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
exchangeNameStr, err := cmd.Flags().GetString("exchange") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
exchangeName, err := types.ValidExchangeName(exchangeNameStr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
symbol, err := cmd.Flags().GetString("symbol") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
exchange, err := cmdutil.NewExchange(exchangeName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db, err := cmdutil.ConnectMySQL() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sessionID := "main" | ||
environ := bbgo.NewEnvironment(db) | ||
environ.AddExchange(sessionID, exchange).Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{}) | ||
|
||
trader := bbgo.NewTrader(environ) | ||
trader.AttachStrategy(sessionID, buyandhold.New(symbol)) | ||
trader.Run(ctx) | ||
|
||
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM) | ||
return nil | ||
}, | ||
} | ||
|
||
func main() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.WithError(err).Fatalf("cannot execute command") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func ConnectMySQL() (*sqlx.DB, error) { | ||
mysqlURL := viper.GetString("mysql-url") | ||
mysqlURL = fmt.Sprintf("%s?parseTime=true", mysqlURL) | ||
return sqlx.Connect("mysql", mysqlURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/c9s/bbgo/pkg/exchange/binance" | ||
"github.com/c9s/bbgo/pkg/exchange/max" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
func NewExchange(n types.ExchangeName) (types.Exchange, error) { | ||
switch n { | ||
|
||
case types.ExchangeBinance: | ||
key := viper.GetString("binance-api-key") | ||
secret := viper.GetString("binance-api-secret") | ||
if len(key) == 0 || len(secret) == 0 { | ||
return nil, errors.New("empty key or secret") | ||
} | ||
|
||
return binance.New(key, secret), nil | ||
|
||
case types.ExchangeMax: | ||
key := viper.GetString("max-api-key") | ||
secret := viper.GetString("max-api-secret") | ||
if len(key) == 0 || len(secret) == 0 { | ||
return nil, errors.New("empty key or secret") | ||
} | ||
|
||
return max.New(key, secret), nil | ||
|
||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.