audacity: finalize strategy

This commit is contained in:
austin362667 2022-09-07 15:46:41 +08:00 committed by Austin Liu
parent 713ead669e
commit 8b97e4c4e8
3 changed files with 32 additions and 28 deletions

View File

@ -16,6 +16,6 @@ exchangeStrategies:
- on: binance - on: binance
audacitymaker: audacitymaker:
symbol: ETHBUSD symbol: ETHBUSD
perTrade: orderFlow:
interval: 1m interval: 1m
quantity: 0.01 quantity: 0.01

View File

@ -2,8 +2,6 @@ package audacitymaker
import ( import (
"context" "context"
"math"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/datatype/floats" "github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/fixedpoint" "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 orderFlowSize floats.Slice
var orderFlowNumber floats.Slice var orderFlowNumber floats.Slice
var orderFlowSizeMinMaxArcCos floats.Slice var orderFlowSizeMinMax floats.Slice
var orderFlowNumberMinMaxArcCos floats.Slice var orderFlowNumberMinMax floats.Slice
threshold := 3.
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) { 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() ctx := context.Background()
@ -80,43 +80,47 @@ func (s *PerTrade) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
sellTradesNumber.Update(1) sellTradesNumber.Update(1)
} }
canceled := s.orderExecutor.GracefulCancel(ctx) //canceled := s.orderExecutor.GracefulCancel(ctx)
if canceled != nil { //if canceled != nil {
_ = s.orderExecutor.GracefulCancel(ctx) // _ = s.orderExecutor.GracefulCancel(ctx)
} //}
sizeFraction := buyTradeSize.Sum() / sellTradeSize.Sum() sizeFraction := buyTradeSize.Sum() / sellTradeSize.Sum()
numberFraction := buyTradesNumber.Sum() / sellTradesNumber.Sum() numberFraction := buyTradesNumber.Sum() / sellTradesNumber.Sum()
orderFlowSize.Push(sizeFraction) orderFlowSize.Push(sizeFraction)
if orderFlowSize.Length() > 100 { if orderFlowSize.Length() > 100 {
// min-max scaling // min-max scaling
oaMax := orderFlowSize.Tail(100).Max() ofsMax := orderFlowSize.Tail(100).Max()
oaMin := orderFlowSize.Tail(100).Min() ofsMin := orderFlowSize.Tail(100).Min()
oaMinMax := (orderFlowSize.Last() - oaMin) / (oaMax - oaMin) ofsMinMax := (orderFlowSize.Last() - ofsMin) / (ofsMax - ofsMin)
// preserves temporal dependency via polar encoded angles // preserves temporal dependency via polar encoded angles
orderFlowSizeMinMaxArcCos.Push(math.Cosh(oaMinMax)) orderFlowSizeMinMax.Push(ofsMinMax)
} }
orderFlowNumber.Push(numberFraction) orderFlowNumber.Push(numberFraction)
if orderFlowNumber.Length() > 100 { if orderFlowNumber.Length() > 100 {
// min-max scaling // min-max scaling
ofMax := orderFlowNumber.Tail(100).Max() ofnMax := orderFlowNumber.Tail(100).Max()
ofMin := orderFlowNumber.Tail(100).Min() ofnMin := orderFlowNumber.Tail(100).Min()
ofMinMax := (orderFlowNumber.Last() - ofMin) / (ofMax - ofMin) ofnMinMax := (orderFlowNumber.Last() - ofnMin) / (ofnMax - ofnMin)
// preserves temporal dependency via polar encoded angles // 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() 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!!") 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.SideTypeBuy, s.Quantity, bid.Price, symbol)
_ = s.placeOrder(ctx, types.SideTypeSell, s.Quantity, trade.Price.Mul(fixedpoint.NewFromFloat(1.0005)), symbol) //_ = s.placeOrder(ctx, types.SideTypeSell, s.Quantity, ask.Price.Mul(fixedpoint.NewFromFloat(1.0005)), symbol)
} else if outlier(orderFlowSizeMinMaxArcCos.Tail(100), 2) < 0 && outlier(orderFlowNumberMinMaxArcCos.Tail(100), 2) < 0 { } else if outlier(orderFlowSizeMinMax.Tail(100), threshold) < 0 && outlier(orderFlowNumberMinMax.Tail(100), threshold) < 0 {
_ = s.orderExecutor.GracefulCancel(ctx)
log.Infof("short!!") 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.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)
} }
} }

View File

@ -41,7 +41,7 @@ type Strategy struct {
activeOrders *bbgo.ActiveOrderBook activeOrders *bbgo.ActiveOrderBook
PerTrade *PerTrade `json:"perTrade"` OrderFlow *PerTrade `json:"orderFlow"`
ExitMethods bbgo.ExitMethodSet `json:"exits"` ExitMethods bbgo.ExitMethodSet `json:"exits"`
@ -55,7 +55,7 @@ type Strategy struct {
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{}) session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
session.Subscribe(types.MarketTradeChannel, 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 { if !bbgo.IsBackTesting {
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{}) 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) method.Bind(session, s.orderExecutor)
} }
if s.PerTrade != nil { if s.OrderFlow != nil {
s.PerTrade.Bind(session, s.orderExecutor) s.OrderFlow.Bind(session, s.orderExecutor)
} }
bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) { bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {