add error check and logger

This commit is contained in:
c9s 2020-10-15 23:38:00 +08:00
parent 300609e3db
commit 7482fa52d6
3 changed files with 11 additions and 7 deletions

View File

@ -11,12 +11,12 @@ import (
type Account struct {
sync.Mutex
Balances map[string]types.Balance
balances map[string]types.Balance
}
func (a *Account) Balance(currency string) (balance types.Balance, ok bool) {
a.Lock()
balance, ok = a.Balances[currency]
balance, ok = a.balances[currency]
a.Unlock()
return balance, ok
}
@ -26,7 +26,7 @@ func (a *Account) handleBalanceUpdates(balances map[string]types.Balance) {
defer a.Unlock()
for _, balance := range balances {
a.Balances[balance.Currency] = balance
a.balances[balance.Currency] = balance
}
}
@ -39,7 +39,7 @@ func (a *Account) Print() {
a.Lock()
defer a.Unlock()
for _, balance := range a.Balances {
for _, balance := range a.balances {
if util.NotZero(balance.Available) {
log.Infof("account balance %s %f", balance.Currency, balance.Available)
}

View File

@ -149,7 +149,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
session.Stream = stream
session.Account = &Account{Balances: balances}
session.Account = &Account{balances: balances}
session.Account.BindStream(session.Stream)
marketDataStore := NewMarketDataStore()
@ -175,7 +175,6 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
func (environ *Environment) Connect(ctx context.Context) error {
for _, session := range environ.sessions {
for _, s := range session.Subscriptions {
log.Infof("subscribing %s %s %v", s.Symbol, s.Channel, s.Options)
session.Stream.Subscribe(s.Channel, s.Symbol, s.Options)

View File

@ -6,6 +6,8 @@ import (
"io/ioutil"
"math"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
@ -70,12 +72,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor types.OrderExecutor, s
}
}
orderExecutor.SubmitOrder(ctx, types.SubmitOrder{
err := orderExecutor.SubmitOrder(ctx, types.SubmitOrder{
Symbol: kline.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: s.BaseQuantity * math.Abs(changePercentage),
})
if err != nil {
log.WithError(err).Error("submit order error")
}
}
})