dca: clean up

This commit is contained in:
c9s 2022-06-22 13:37:32 +08:00
parent a5cb8355d4
commit 929ffc3e5e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -48,7 +48,6 @@ func (b BudgetPeriod) Duration() time.Duration {
// Strategy is the Dollar-Cost-Average strategy // Strategy is the Dollar-Cost-Average strategy
type Strategy struct { type Strategy struct {
*bbgo.Graceful *bbgo.Graceful
*bbgo.Persistence
Environment *bbgo.Environment Environment *bbgo.Environment
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
@ -74,10 +73,6 @@ type Strategy struct {
session *bbgo.ExchangeSession session *bbgo.ExchangeSession
orderExecutor *bbgo.GeneralOrderExecutor orderExecutor *bbgo.GeneralOrderExecutor
activeMakerOrders *bbgo.ActiveOrderBook
orderStore *bbgo.OrderStore
tradeCollector *bbgo.TradeCollector
bbgo.StrategyController bbgo.StrategyController
} }
@ -89,17 +84,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.InvestmentInterval}) session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.InvestmentInterval})
} }
func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExecutor, submitOrders ...types.SubmitOrder) {
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil {
log.WithError(err).Errorf("can not place orders")
}
s.orderStore.Add(createdOrders...)
s.activeMakerOrders.Add(createdOrders...)
s.tradeCollector.Process()
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
base := s.Position.GetBase() base := s.Position.GetBase()
if base.IsZero() { if base.IsZero() {
@ -125,16 +109,10 @@ 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) _, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
createdOrders, err := s.session.Exchange.SubmitOrders(ctx, submitOrder)
if err != nil { if err != nil {
log.WithError(err).Errorf("can not place position close order") log.WithError(err).Errorf("can not place position close order")
} }
s.orderStore.Add(createdOrders...)
s.activeMakerOrders.Add(createdOrders...)
s.tradeCollector.Process()
return err return err
} }
@ -142,7 +120,7 @@ func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol) return fmt.Sprintf("%s:%s", ID, s.Symbol)
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, session *bbgo.ExchangeSession) error {
if s.BudgetQuota.IsZero() { if s.BudgetQuota.IsZero() {
s.BudgetQuota = s.Budget s.BudgetQuota = s.Budget
} }
@ -193,13 +171,16 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
price := kline.Close price := kline.Close
quantity := s.budgetPerInvestment.Div(price) quantity := s.budgetPerInvestment.Div(price)
s.submitOrders(ctx, orderExecutor, types.SubmitOrder{ _, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: s.Symbol, Symbol: s.Symbol,
Side: types.SideTypeBuy, Side: types.SideTypeBuy,
Type: types.OrderTypeMarket, Type: types.OrderTypeMarket,
Quantity: quantity, Quantity: quantity,
Market: s.Market, Market: s.Market,
}) })
if err != nil {
log.WithError(err).Errorf("submit order failed")
}
}) })
return nil return nil