From a91f851ac726b65d8b31ced9abd730c5f6274c4f Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 13 Oct 2020 18:08:02 +0800 Subject: [PATCH] pass types.SubmitOrder by value --- pkg/bbgo/backtest.go | 6 +++--- pkg/bbgo/order_processor.go | 4 ++-- pkg/bbgo/trader.go | 10 +++++----- pkg/exchange/binance/exchange.go | 2 +- pkg/exchange/max/exchange.go | 2 +- pkg/strategy/buyandhold/main.go | 4 ++-- pkg/types/exchange.go | 2 +- pkg/types/trader.go | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/bbgo/backtest.go b/pkg/bbgo/backtest.go index 40c0fcfc4..5d6448aeb 100644 --- a/pkg/bbgo/backtest.go +++ b/pkg/bbgo/backtest.go @@ -25,11 +25,11 @@ type BackTestTrader struct { SourceKLines []types.KLine ProfitAndLossCalculator *accounting.ProfitAndLossCalculator - doneOrders []*types.SubmitOrder - pendingOrders []*types.SubmitOrder + doneOrders []types.SubmitOrder + pendingOrders []types.SubmitOrder } -func (trader *BackTestTrader) SubmitOrder(cxt context.Context, order *types.SubmitOrder) { +func (trader *BackTestTrader) SubmitOrder(ctx context.Context, order types.SubmitOrder) { trader.pendingOrders = append(trader.pendingOrders, order) } diff --git a/pkg/bbgo/order_processor.go b/pkg/bbgo/order_processor.go index 0cdd906ad..753f2821a 100644 --- a/pkg/bbgo/order_processor.go +++ b/pkg/bbgo/order_processor.go @@ -37,9 +37,9 @@ type OrderProcessor struct { Trader *Trader `json:"-"` } -func (p *OrderProcessor) Submit(ctx context.Context, order *types.SubmitOrder) error { +func (p *OrderProcessor) Submit(ctx context.Context, order types.SubmitOrder) error { /* - tradingCtx := p.Trader.Context + tradingCtx := p.OrderExecutor.Context currentPrice := tradingCtx.CurrentPrice market := order.Market quantity := order.Quantity diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 11854bc87..7daed71f4 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -17,11 +17,11 @@ import ( // SingleExchangeStrategy represents the single Exchange strategy type SingleExchangeStrategy interface { - Run(ctx context.Context, trader types.Trader, session *ExchangeSession) error + Run(ctx context.Context, trader types.OrderExecutor, session *ExchangeSession) error } type CrossExchangeStrategy interface { - Run(ctx context.Context, trader types.Trader, sessions map[string]*ExchangeSession) error + Run(ctx context.Context, trader types.OrderExecutor, sessions map[string]*ExchangeSession) error } // ExchangeSession presents the exchange connection session @@ -237,7 +237,7 @@ func (trader *Trader) Run(ctx context.Context) error { } /* -func (trader *Trader) RunStrategyWithHotReload(ctx context.Context, strategy SingleExchangeStrategy, configFile string) (chan struct{}, error) { +func (trader *OrderExecutor) RunStrategyWithHotReload(ctx context.Context, strategy SingleExchangeStrategy, configFile string) (chan struct{}, error) { var done = make(chan struct{}) var configWatcherDone = make(chan struct{}) @@ -315,7 +315,7 @@ func (trader *Trader) RunStrategyWithHotReload(ctx context.Context, strategy Sin */ /* -func (trader *Trader) RunStrategy(ctx context.Context, strategy SingleExchangeStrategy) (chan struct{}, error) { +func (trader *OrderExecutor) RunStrategy(ctx context.Context, strategy SingleExchangeStrategy) (chan struct{}, error) { trader.reportTimer = time.AfterFunc(1*time.Second, func() { trader.reportPnL() }) @@ -363,7 +363,7 @@ func (trader *Trader) Notify(msg string, args ...interface{}) { } } -func (trader *Trader) SubmitOrder(ctx context.Context, order *types.SubmitOrder) { +func (trader *Trader) SubmitOrder(ctx context.Context, order types.SubmitOrder) { trader.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) orderProcessor := &OrderProcessor{ diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index acf65dbe1..9640ef49b 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -220,7 +220,7 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) { }, nil } -func (e *Exchange) SubmitOrder(ctx context.Context, order *types.SubmitOrder) error { +func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) error { /* limit order example diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index 8d3d9eb7a..2bf593bd2 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -38,7 +38,7 @@ func (e *Exchange) NewStream() types.Stream { return NewStream(e.key, e.secret) } -func (e *Exchange) SubmitOrder(ctx context.Context, order *types.SubmitOrder) error { +func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) error { orderType, err := toLocalOrderType(order.Type) if err != nil { return err diff --git a/pkg/strategy/buyandhold/main.go b/pkg/strategy/buyandhold/main.go index 5b217e057..0a336bcca 100644 --- a/pkg/strategy/buyandhold/main.go +++ b/pkg/strategy/buyandhold/main.go @@ -49,7 +49,7 @@ func (s *Strategy) SetMaxAssetQuantity(q float64) *Strategy { return s } -func (s *Strategy) Run(ctx context.Context, trader types.Trader, session *bbgo.ExchangeSession) error { +func (s *Strategy) Run(ctx context.Context, orderExecutor types.OrderExecutor, session *bbgo.ExchangeSession) error { session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval}) session.Stream.OnKLineClosed(func(kline types.KLine) { @@ -70,7 +70,7 @@ func (s *Strategy) Run(ctx context.Context, trader types.Trader, session *bbgo.E } } - trader.SubmitOrder(ctx, &types.SubmitOrder{ + orderExecutor.SubmitOrder(ctx, types.SubmitOrder{ Symbol: kline.Symbol, Side: types.SideTypeBuy, Type: types.OrderTypeMarket, diff --git a/pkg/types/exchange.go b/pkg/types/exchange.go index 55cd12b20..23fb788bc 100644 --- a/pkg/types/exchange.go +++ b/pkg/types/exchange.go @@ -48,7 +48,7 @@ type Exchange interface { QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []Withdraw, err error) - SubmitOrder(ctx context.Context, order *SubmitOrder) error + SubmitOrder(ctx context.Context, order SubmitOrder) error } type TradeQueryOptions struct { diff --git a/pkg/types/trader.go b/pkg/types/trader.go index bf4ebcc4a..1b5491562 100644 --- a/pkg/types/trader.go +++ b/pkg/types/trader.go @@ -2,6 +2,6 @@ package types import "context" -type Trader interface { - SubmitOrder(ctx context.Context, order *SubmitOrder) +type OrderExecutor interface { + SubmitOrder(ctx context.Context, order SubmitOrder) }