From 8b97e4c4e840baa41a3ab70d6c84818e2963ae06 Mon Sep 17 00:00:00 2001 From: austin362667 Date: Wed, 7 Sep 2022 15:46:41 +0800 Subject: [PATCH] audacity: finalize strategy --- config/audacitymaker.yaml | 4 +- .../{pertrade.go => orderflow.go} | 48 ++++++++++--------- pkg/strategy/audacitymaker/strategy.go | 8 ++-- 3 files changed, 32 insertions(+), 28 deletions(-) rename pkg/strategy/audacitymaker/{pertrade.go => orderflow.go} (75%) diff --git a/config/audacitymaker.yaml b/config/audacitymaker.yaml index 3e5ba102b..36ecaa887 100644 --- a/config/audacitymaker.yaml +++ b/config/audacitymaker.yaml @@ -16,6 +16,6 @@ exchangeStrategies: - on: binance audacitymaker: symbol: ETHBUSD - perTrade: + orderFlow: interval: 1m - quantity: 0.01 \ No newline at end of file + quantity: 0.01 diff --git a/pkg/strategy/audacitymaker/pertrade.go b/pkg/strategy/audacitymaker/orderflow.go similarity index 75% rename from pkg/strategy/audacitymaker/pertrade.go rename to pkg/strategy/audacitymaker/orderflow.go index cd78c1b9e..b8b0d80c7 100644 --- a/pkg/strategy/audacitymaker/pertrade.go +++ b/pkg/strategy/audacitymaker/orderflow.go @@ -2,8 +2,6 @@ package audacitymaker import ( "context" - "math" - "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/datatype/floats" "github.com/c9s/bbgo/pkg/fixedpoint" @@ -54,12 +52,14 @@ func (s *PerTrade) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener var orderFlowSize floats.Slice var orderFlowNumber floats.Slice - var orderFlowSizeMinMaxArcCos floats.Slice - var orderFlowNumberMinMaxArcCos floats.Slice + var orderFlowSizeMinMax floats.Slice + var orderFlowNumberMinMax floats.Slice + + threshold := 3. session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { - log.Infof("%s trade @ %f", trade.Side, trade.Price.Float64()) + //log.Infof("%s trade @ %f", trade.Side, trade.Price.Float64()) ctx := context.Background() @@ -80,43 +80,47 @@ func (s *PerTrade) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener sellTradesNumber.Update(1) } - canceled := s.orderExecutor.GracefulCancel(ctx) - if canceled != nil { - _ = s.orderExecutor.GracefulCancel(ctx) - } + //canceled := s.orderExecutor.GracefulCancel(ctx) + //if canceled != nil { + // _ = s.orderExecutor.GracefulCancel(ctx) + //} sizeFraction := buyTradeSize.Sum() / sellTradeSize.Sum() numberFraction := buyTradesNumber.Sum() / sellTradesNumber.Sum() orderFlowSize.Push(sizeFraction) if orderFlowSize.Length() > 100 { // min-max scaling - oaMax := orderFlowSize.Tail(100).Max() - oaMin := orderFlowSize.Tail(100).Min() - oaMinMax := (orderFlowSize.Last() - oaMin) / (oaMax - oaMin) + ofsMax := orderFlowSize.Tail(100).Max() + ofsMin := orderFlowSize.Tail(100).Min() + ofsMinMax := (orderFlowSize.Last() - ofsMin) / (ofsMax - ofsMin) // preserves temporal dependency via polar encoded angles - orderFlowSizeMinMaxArcCos.Push(math.Cosh(oaMinMax)) + orderFlowSizeMinMax.Push(ofsMinMax) } orderFlowNumber.Push(numberFraction) if orderFlowNumber.Length() > 100 { // min-max scaling - ofMax := orderFlowNumber.Tail(100).Max() - ofMin := orderFlowNumber.Tail(100).Min() - ofMinMax := (orderFlowNumber.Last() - ofMin) / (ofMax - ofMin) + ofnMax := orderFlowNumber.Tail(100).Max() + ofnMin := orderFlowNumber.Tail(100).Min() + ofnMinMax := (orderFlowNumber.Last() - ofnMin) / (ofnMax - ofnMin) // preserves temporal dependency via polar encoded angles - orderFlowNumberMinMaxArcCos.Push(math.Cosh(ofMinMax)) + orderFlowNumberMinMax.Push(ofnMinMax) } - if orderFlowSizeMinMaxArcCos.Length() > 100 && orderFlowNumberMinMaxArcCos.Length() > 100 { + if orderFlowSizeMinMax.Length() > 100 && orderFlowNumberMinMax.Length() > 100 { bid, ask, _ := s.StreamBook.BestBidAndAsk() - if outlier(orderFlowSizeMinMaxArcCos.Tail(100), 2) > 0 && outlier(orderFlowNumberMinMaxArcCos.Tail(100), 2) > 0 { + if outlier(orderFlowSizeMinMax.Tail(100), threshold) > 0 && outlier(orderFlowNumberMinMax.Tail(100), threshold) > 0 { + _ = s.orderExecutor.GracefulCancel(ctx) log.Infof("long!!") + //_ = s.placeTrade(ctx, types.SideTypeBuy, s.Quantity, symbol) _ = s.placeOrder(ctx, types.SideTypeBuy, s.Quantity, bid.Price, symbol) - _ = s.placeOrder(ctx, types.SideTypeSell, s.Quantity, trade.Price.Mul(fixedpoint.NewFromFloat(1.0005)), symbol) - } else if outlier(orderFlowSizeMinMaxArcCos.Tail(100), 2) < 0 && outlier(orderFlowNumberMinMaxArcCos.Tail(100), 2) < 0 { + //_ = s.placeOrder(ctx, types.SideTypeSell, s.Quantity, ask.Price.Mul(fixedpoint.NewFromFloat(1.0005)), symbol) + } else if outlier(orderFlowSizeMinMax.Tail(100), threshold) < 0 && outlier(orderFlowNumberMinMax.Tail(100), threshold) < 0 { + _ = s.orderExecutor.GracefulCancel(ctx) log.Infof("short!!") + //_ = s.placeTrade(ctx, types.SideTypeSell, s.Quantity, symbol) _ = s.placeOrder(ctx, types.SideTypeSell, s.Quantity, ask.Price, symbol) - _ = s.placeOrder(ctx, types.SideTypeBuy, s.Quantity, trade.Price.Mul(fixedpoint.NewFromFloat(0.9995)), symbol) + //_ = s.placeOrder(ctx, types.SideTypeBuy, s.Quantity, bid.Price.Mul(fixedpoint.NewFromFloat(0.9995)), symbol) } } diff --git a/pkg/strategy/audacitymaker/strategy.go b/pkg/strategy/audacitymaker/strategy.go index 99be2f8ea..249603e1d 100644 --- a/pkg/strategy/audacitymaker/strategy.go +++ b/pkg/strategy/audacitymaker/strategy.go @@ -41,7 +41,7 @@ type Strategy struct { activeOrders *bbgo.ActiveOrderBook - PerTrade *PerTrade `json:"perTrade"` + OrderFlow *PerTrade `json:"orderFlow"` ExitMethods bbgo.ExitMethodSet `json:"exits"` @@ -55,7 +55,7 @@ type Strategy struct { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{}) session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{}) - session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.PerTrade.Interval}) + session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.OrderFlow.Interval}) if !bbgo.IsBackTesting { session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{}) @@ -119,8 +119,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se method.Bind(session, s.orderExecutor) } - if s.PerTrade != nil { - s.PerTrade.Bind(session, s.orderExecutor) + if s.OrderFlow != nil { + s.OrderFlow.Bind(session, s.orderExecutor) } bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {