From 343434685ba74b73c35240a68a669fd7feb71498 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 17 May 2022 01:32:51 +0800 Subject: [PATCH] rollback to go1.17 and make try lock backward compatible --- Dockerfile | 2 +- pkg/strategy/ewoDgtrd/strategy.go | 17 ++++++++++------- pkg/strategy/ewoDgtrd/trylock.go | 11 +++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 pkg/strategy/ewoDgtrd/trylock.go diff --git a/Dockerfile b/Dockerfile index f6f75208d..f054e4faa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # First stage container -FROM golang:1.18.2-alpine3.15 AS builder +FROM golang:1.17.6-alpine3.15 AS builder RUN apk add --no-cache git ca-certificates gcc libc-dev pkgconfig # gcc is for github.com/mattn/go-sqlite3 # ADD . $GOPATH/src/github.com/c9s/bbgo diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 09b3da4e7..25d3a5888 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -796,7 +796,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se bottomBack := s.bottomPrice spBack := s.sellPrice if !quoteBalance.IsZero() && !s.sellPrice.IsZero() && !s.DisableShortStop { - //longSignal := types.CrossOver(s.ewo, s.ewoSignal) + // longSignal := types.CrossOver(s.ewo, s.ewoSignal) // TP /*if lastPrice.Compare(s.sellPrice) < 0 && (s.ccis.BuySignal() || longSignal.Last()) { buyall = true @@ -910,7 +910,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se bestBid := ticker.Buy bestAsk := ticker.Sell var midPrice fixedpoint.Value - if s.lock.TryLock() { + + // TODO: for go1.18 we can use TryLock, use build flag to support this + if tryLock(&s.lock) { if !bestAsk.IsZero() && !bestBid.IsZero() { s.midPrice = bestAsk.Add(bestBid).Div(types.Two) } else if !bestAsk.IsZero() { @@ -921,10 +923,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se midPrice = s.midPrice s.lock.Unlock() } + if !midPrice.IsZero() { buyOrderTPSL(midPrice) sellOrderTPSL(midPrice) - //log.Debugf("best bid %v, best ask %v, mid %v", bestBid, bestAsk, midPrice) + // log.Debugf("best bid %v, best ask %v, mid %v", bestBid, bestAsk, midPrice) } }) @@ -974,8 +977,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.CancelAll(ctx) // To get the threshold for ewo - //mean := types.Mean(s.ewo, 10) - //std := types.Stdev(s.ewo, 10) + // mean := types.Mean(s.ewo, 10) + // std := types.Stdev(s.ewo, 10) longSignal := types.CrossOver(s.ewo, s.ewoSignal) shortSignal := types.CrossUnder(s.ewo, s.ewoSignal) @@ -991,9 +994,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se breakDown = kline.Close.Float64() < s.ma5.Last() } // kline breakthrough ma5, ma50 trend up, and ewo > threshold - IsBull := bull && breakThrough && s.ccis.BuySignal() //&& s.ewo.Last() > mean + 2 * std + IsBull := bull && breakThrough && s.ccis.BuySignal() // && s.ewo.Last() > mean + 2 * std // kline downthrough ma5, ma50 trend down, and ewo < threshold - IsBear := !bull && breakDown && s.ccis.SellSignal() //.ewo.Last() < mean - 2 * std + IsBear := !bull && breakDown && s.ccis.SellSignal() // .ewo.Last() < mean - 2 * std if !s.Environment.IsBackTesting() { log.Infof("IsBull: %v, bull: %v, longSignal[1]: %v, shortSignal: %v", diff --git a/pkg/strategy/ewoDgtrd/trylock.go b/pkg/strategy/ewoDgtrd/trylock.go new file mode 100644 index 000000000..ec3e875a9 --- /dev/null +++ b/pkg/strategy/ewoDgtrd/trylock.go @@ -0,0 +1,11 @@ +//go:build !go1.18 +// +build !go1.18 + +package ewoDgtrd + +import "sync" + +func tryLock(lock *sync.RWMutex) bool { + lock.Lock() + return true +}