mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #797 from c9s/feature/trailingstop
feature: re-implement trailing stop and add mock test
This commit is contained in:
commit
d3f0ca9f7a
3
go.mod
3
go.mod
|
@ -75,6 +75,7 @@ require (
|
||||||
github.com/go-test/deep v1.0.6 // indirect
|
github.com/go-test/deep v1.0.6 // indirect
|
||||||
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
|
||||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||||
|
github.com/golang/mock v1.6.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
|
@ -116,11 +117,13 @@ require (
|
||||||
go.opentelemetry.io/otel/trace v0.19.0 // indirect
|
go.opentelemetry.io/otel/trace v0.19.0 // indirect
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
|
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
|
||||||
|
golang.org/x/mod v0.5.1 // indirect
|
||||||
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
|
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b // indirect
|
||||||
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
|
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
golang.org/x/tools v0.1.9 // indirect
|
golang.org/x/tools v0.1.9 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
|
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
|
||||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
|
7
go.sum
7
go.sum
|
@ -194,7 +194,10 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU
|
||||||
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||||
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||||
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
|
||||||
|
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
|
||||||
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
|
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
|
||||||
|
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
|
||||||
|
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
@ -523,6 +526,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
||||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||||
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
|
github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs=
|
||||||
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
|
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
|
||||||
|
@ -599,6 +603,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
|
||||||
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
|
||||||
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
||||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
@ -780,6 +786,7 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc
|
||||||
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||||
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||||
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
|
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||||
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
||||||
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
|
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
|
||||||
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
||||||
|
|
145
pkg/bbgo/exit_trailing_stop.go
Normal file
145
pkg/bbgo/exit_trailing_stop.go
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
package bbgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TrailingStop2 struct {
|
||||||
|
Symbol string
|
||||||
|
|
||||||
|
// CallbackRate is the callback rate from the previous high price
|
||||||
|
CallbackRate fixedpoint.Value `json:"callbackRate,omitempty"`
|
||||||
|
|
||||||
|
ActivationRatio fixedpoint.Value `json:"activationRatio,omitempty"`
|
||||||
|
|
||||||
|
// ClosePosition is a percentage of the position to be closed
|
||||||
|
ClosePosition fixedpoint.Value `json:"closePosition,omitempty"`
|
||||||
|
|
||||||
|
// MinProfit is the percentage of the minimum profit ratio.
|
||||||
|
// Stop order will be activated only when the price reaches above this threshold.
|
||||||
|
MinProfit fixedpoint.Value `json:"minProfit,omitempty"`
|
||||||
|
|
||||||
|
// Interval is the time resolution to update the stop order
|
||||||
|
// KLine per Interval will be used for updating the stop order
|
||||||
|
Interval types.Interval `json:"interval,omitempty"`
|
||||||
|
|
||||||
|
Side types.SideType `json:"side,omitempty"`
|
||||||
|
|
||||||
|
latestHigh fixedpoint.Value
|
||||||
|
|
||||||
|
// activated: when the price reaches the min profit price, we set the activated to true to enable trailing stop
|
||||||
|
activated bool
|
||||||
|
|
||||||
|
// private fields
|
||||||
|
session *ExchangeSession
|
||||||
|
orderExecutor *GeneralOrderExecutor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TrailingStop2) Subscribe(session *ExchangeSession) {
|
||||||
|
// use 1m kline to handle roi stop
|
||||||
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TrailingStop2) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
||||||
|
s.session = session
|
||||||
|
s.orderExecutor = orderExecutor
|
||||||
|
|
||||||
|
position := orderExecutor.Position()
|
||||||
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
||||||
|
s.checkStopPrice(kline.Close, position)
|
||||||
|
}))
|
||||||
|
|
||||||
|
if !IsBackTesting {
|
||||||
|
session.MarketDataStream.OnMarketTrade(func(trade types.Trade) {
|
||||||
|
if trade.Symbol != position.Symbol {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.checkStopPrice(trade.Price, position)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TrailingStop2) getRatio(price fixedpoint.Value, position *types.Position) (fixedpoint.Value, error) {
|
||||||
|
switch s.Side {
|
||||||
|
case types.SideTypeBuy:
|
||||||
|
// for short position
|
||||||
|
return position.AverageCost.Sub(price).Div(price), nil
|
||||||
|
case types.SideTypeSell:
|
||||||
|
return price.Sub(position.AverageCost).Div(position.AverageCost), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixedpoint.Zero, fmt.Errorf("unexpected side type: %v", s.Side)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.Position) error {
|
||||||
|
if position.IsClosed() || position.IsDust(price) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !s.MinProfit.IsZero() {
|
||||||
|
// check if we have the minimal profit
|
||||||
|
roi := position.ROI(price)
|
||||||
|
if roi.Compare(s.MinProfit) >= 0 {
|
||||||
|
Notify("[trailingStop] activated: ROI %f > minimal profit ratio %f", roi.Float64(), s.MinProfit.Float64())
|
||||||
|
s.activated = true
|
||||||
|
}
|
||||||
|
} else if !s.ActivationRatio.IsZero() {
|
||||||
|
ratio, err := s.getRatio(price, position)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ratio.Compare(s.ActivationRatio) >= 0 {
|
||||||
|
s.activated = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// update the latest high for the sell order, or the latest low for the buy order
|
||||||
|
if s.latestHigh.IsZero() {
|
||||||
|
s.latestHigh = price
|
||||||
|
} else {
|
||||||
|
switch s.Side {
|
||||||
|
case types.SideTypeBuy:
|
||||||
|
s.latestHigh = fixedpoint.Min(price, s.latestHigh)
|
||||||
|
case types.SideTypeSell:
|
||||||
|
s.latestHigh = fixedpoint.Max(price, s.latestHigh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !s.activated {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch s.Side {
|
||||||
|
case types.SideTypeBuy:
|
||||||
|
s.latestHigh = fixedpoint.Min(price, s.latestHigh)
|
||||||
|
|
||||||
|
change := price.Sub(s.latestHigh).Div(s.latestHigh)
|
||||||
|
if change.Compare(s.CallbackRate) >= 0 {
|
||||||
|
// submit order
|
||||||
|
return s.triggerStop(price)
|
||||||
|
}
|
||||||
|
|
||||||
|
case types.SideTypeSell:
|
||||||
|
s.latestHigh = fixedpoint.Max(price, s.latestHigh)
|
||||||
|
|
||||||
|
change := price.Sub(s.latestHigh).Div(s.latestHigh)
|
||||||
|
if change.Compare(s.CallbackRate) >= 0 {
|
||||||
|
// submit order
|
||||||
|
return s.triggerStop(price)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *TrailingStop2) triggerStop(price fixedpoint.Value) error {
|
||||||
|
Notify("[TrailingStop] %s stop loss triggered. price: %f callback rate: %f", s.Symbol, price.Float64(), s.CallbackRate.Float64())
|
||||||
|
ctx := context.Background()
|
||||||
|
return s.orderExecutor.ClosePosition(ctx, fixedpoint.One, "trailingStop")
|
||||||
|
}
|
102
pkg/bbgo/exit_trailing_stop_test.go
Normal file
102
pkg/bbgo/exit_trailing_stop_test.go
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
package bbgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
|
"github.com/c9s/bbgo/pkg/types/mocks"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getTestMarket returns the BTCUSDT market information
|
||||||
|
// for tests, we always use BTCUSDT
|
||||||
|
func getTestMarket() types.Market {
|
||||||
|
market := types.Market{
|
||||||
|
Symbol: "BTCUSDT",
|
||||||
|
PricePrecision: 8,
|
||||||
|
VolumePrecision: 8,
|
||||||
|
QuoteCurrency: "USDT",
|
||||||
|
BaseCurrency: "BTC",
|
||||||
|
MinNotional: fixedpoint.MustNewFromString("0.001"),
|
||||||
|
MinAmount: fixedpoint.MustNewFromString("10.0"),
|
||||||
|
MinQuantity: fixedpoint.MustNewFromString("0.001"),
|
||||||
|
}
|
||||||
|
return market
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrailingStop(t *testing.T) {
|
||||||
|
market := getTestMarket()
|
||||||
|
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
mockEx := mocks.NewMockExchange(mockCtrl)
|
||||||
|
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
|
||||||
|
mockEx.EXPECT().SubmitOrders(gomock.Any(), types.SubmitOrder{
|
||||||
|
Symbol: "BTCUSDT",
|
||||||
|
Side: types.SideTypeBuy,
|
||||||
|
Type: types.OrderTypeMarket,
|
||||||
|
Market: market,
|
||||||
|
Quantity: fixedpoint.NewFromFloat(1.0),
|
||||||
|
Tag: "trailingStop",
|
||||||
|
})
|
||||||
|
|
||||||
|
session := NewExchangeSession("test", mockEx)
|
||||||
|
assert.NotNil(t, session)
|
||||||
|
|
||||||
|
session.markets[market.Symbol] = market
|
||||||
|
|
||||||
|
position := types.NewPositionFromMarket(market)
|
||||||
|
position.AverageCost = fixedpoint.NewFromFloat(20000.0)
|
||||||
|
position.Base = fixedpoint.NewFromFloat(-1.0)
|
||||||
|
|
||||||
|
orderExecutor := NewGeneralOrderExecutor(session, "BTCUSDT", "test", "test-01", position)
|
||||||
|
|
||||||
|
activationRatio := fixedpoint.NewFromFloat(0.01)
|
||||||
|
callbackRatio := fixedpoint.NewFromFloat(0.01)
|
||||||
|
stop := &TrailingStop2{
|
||||||
|
Symbol: "BTCUSDT",
|
||||||
|
Interval: types.Interval1m,
|
||||||
|
Side: types.SideTypeBuy,
|
||||||
|
CallbackRate: callbackRatio,
|
||||||
|
ActivationRatio: activationRatio,
|
||||||
|
}
|
||||||
|
stop.Bind(session, orderExecutor)
|
||||||
|
|
||||||
|
// the same price
|
||||||
|
currentPrice := fixedpoint.NewFromFloat(20000.0)
|
||||||
|
err := stop.checkStopPrice(currentPrice, position)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.False(t, stop.activated)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 20000 - 1% = 19800
|
||||||
|
currentPrice = currentPrice.Mul(one.Sub(activationRatio))
|
||||||
|
err = stop.checkStopPrice(currentPrice, position)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.True(t, stop.activated)
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19800.0), currentPrice)
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19800.0), stop.latestHigh)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 19800 - 1% = 19602
|
||||||
|
currentPrice = currentPrice.Mul(one.Sub(callbackRatio))
|
||||||
|
err = stop.checkStopPrice(currentPrice, position)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19602.0), currentPrice)
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19602.0), stop.latestHigh)
|
||||||
|
assert.True(t, stop.activated)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 19602 + 1% = 19798.02
|
||||||
|
currentPrice = currentPrice.Mul(one.Add(callbackRatio))
|
||||||
|
err = stop.checkStopPrice(currentPrice, position)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19798.02), currentPrice)
|
||||||
|
assert.Equal(t, fixedpoint.NewFromFloat(19602.0), stop.latestHigh)
|
||||||
|
assert.True(t, stop.activated)
|
||||||
|
}
|
||||||
|
}
|
|
@ -160,10 +160,6 @@ func (set *StandardIndicatorSet) VOLATILITY(iw types.IntervalWindow) *indicator.
|
||||||
// ExchangeSession presents the exchange connection Session
|
// ExchangeSession presents the exchange connection Session
|
||||||
// It also maintains and collects the data returned from the stream.
|
// It also maintains and collects the data returned from the stream.
|
||||||
type ExchangeSession struct {
|
type ExchangeSession struct {
|
||||||
// exchange Session based notification system
|
|
||||||
// we make it as a value field so that we can configure it separately
|
|
||||||
Notifiability `json:"-" yaml:"-"`
|
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
// Session config fields
|
// Session config fields
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
|
@ -253,12 +249,6 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
|
||||||
marketDataStream.SetPublicOnly()
|
marketDataStream.SetPublicOnly()
|
||||||
|
|
||||||
session := &ExchangeSession{
|
session := &ExchangeSession{
|
||||||
Notifiability: Notifiability{
|
|
||||||
SymbolChannelRouter: NewPatternChannelRouter(nil),
|
|
||||||
SessionChannelRouter: NewPatternChannelRouter(nil),
|
|
||||||
ObjectChannelRouter: NewObjectChannelRouter(),
|
|
||||||
},
|
|
||||||
|
|
||||||
Name: name,
|
Name: name,
|
||||||
Exchange: exchange,
|
Exchange: exchange,
|
||||||
UserDataStream: userDataStream,
|
UserDataStream: userDataStream,
|
||||||
|
@ -282,8 +272,7 @@ func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
|
||||||
|
|
||||||
session.OrderExecutor = &ExchangeOrderExecutor{
|
session.OrderExecutor = &ExchangeOrderExecutor{
|
||||||
// copy the notification system so that we can route
|
// copy the notification system so that we can route
|
||||||
Notifiability: session.Notifiability,
|
Session: session,
|
||||||
Session: session,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return session
|
return session
|
||||||
|
@ -805,11 +794,6 @@ func (session *ExchangeSession) InitExchange(name string, ex types.Exchange) err
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Name = name
|
session.Name = name
|
||||||
session.Notifiability = Notifiability{
|
|
||||||
SymbolChannelRouter: NewPatternChannelRouter(nil),
|
|
||||||
SessionChannelRouter: NewPatternChannelRouter(nil),
|
|
||||||
ObjectChannelRouter: NewObjectChannelRouter(),
|
|
||||||
}
|
|
||||||
session.Exchange = ex
|
session.Exchange = ex
|
||||||
session.UserDataStream = ex.NewStream()
|
session.UserDataStream = ex.NewStream()
|
||||||
session.MarketDataStream = ex.NewStream()
|
session.MarketDataStream = ex.NewStream()
|
||||||
|
@ -830,8 +814,7 @@ func (session *ExchangeSession) InitExchange(name string, ex types.Exchange) err
|
||||||
session.orderStores = make(map[string]*OrderStore)
|
session.orderStores = make(map[string]*OrderStore)
|
||||||
session.OrderExecutor = &ExchangeOrderExecutor{
|
session.OrderExecutor = &ExchangeOrderExecutor{
|
||||||
// copy the notification system so that we can route
|
// copy the notification system so that we can route
|
||||||
Notifiability: session.Notifiability,
|
Session: session,
|
||||||
Session: session,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session.usedSymbols = make(map[string]struct{})
|
session.usedSymbols = make(map[string]struct{})
|
||||||
|
|
|
@ -46,13 +46,10 @@ type SupportTakeProfit struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SupportTakeProfit) Subscribe(session *bbgo.ExchangeSession) {
|
func (s *SupportTakeProfit) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
log.Infof("[supportTakeProfit] Subscribe(%s, %s)", s.Symbol, s.Interval)
|
|
||||||
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SupportTakeProfit) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
|
func (s *SupportTakeProfit) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
|
||||||
log.Infof("[supportTakeProfit] Bind(%s, %s)", s.Symbol, s.Interval)
|
|
||||||
|
|
||||||
s.session = session
|
s.session = session
|
||||||
s.orderExecutor = orderExecutor
|
s.orderExecutor = orderExecutor
|
||||||
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
s.activeOrders = bbgo.NewActiveOrderBook(s.Symbol)
|
||||||
|
|
|
@ -74,6 +74,7 @@ func ValidExchangeName(a string) (ExchangeName, error) {
|
||||||
return "", fmt.Errorf("invalid exchange name: %s", a)
|
return "", fmt.Errorf("invalid exchange name: %s", a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:generate mockgen -destination=mocks/mock_exchange.go -package=mocks . Exchange
|
||||||
type Exchange interface {
|
type Exchange interface {
|
||||||
Name() ExchangeName
|
Name() ExchangeName
|
||||||
|
|
||||||
|
|
227
pkg/types/mocks/mock_exchange.go
Normal file
227
pkg/types/mocks/mock_exchange.go
Normal file
|
@ -0,0 +1,227 @@
|
||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: github.com/c9s/bbgo/pkg/types (interfaces: Exchange)
|
||||||
|
|
||||||
|
// Package mocks is a generated GoMock package.
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
types "github.com/c9s/bbgo/pkg/types"
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockExchange is a mock of Exchange interface.
|
||||||
|
type MockExchange struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockExchangeMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockExchangeMockRecorder is the mock recorder for MockExchange.
|
||||||
|
type MockExchangeMockRecorder struct {
|
||||||
|
mock *MockExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockExchange creates a new mock instance.
|
||||||
|
func NewMockExchange(ctrl *gomock.Controller) *MockExchange {
|
||||||
|
mock := &MockExchange{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockExchangeMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockExchange) EXPECT() *MockExchangeMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelOrders mocks base method.
|
||||||
|
func (m *MockExchange) CancelOrders(arg0 context.Context, arg1 ...types.Order) error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
varargs := []interface{}{arg0}
|
||||||
|
for _, a := range arg1 {
|
||||||
|
varargs = append(varargs, a)
|
||||||
|
}
|
||||||
|
ret := m.ctrl.Call(m, "CancelOrders", varargs...)
|
||||||
|
ret0, _ := ret[0].(error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// CancelOrders indicates an expected call of CancelOrders.
|
||||||
|
func (mr *MockExchangeMockRecorder) CancelOrders(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
varargs := append([]interface{}{arg0}, arg1...)
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelOrders", reflect.TypeOf((*MockExchange)(nil).CancelOrders), varargs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name mocks base method.
|
||||||
|
func (m *MockExchange) Name() types.ExchangeName {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Name")
|
||||||
|
ret0, _ := ret[0].(types.ExchangeName)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name indicates an expected call of Name.
|
||||||
|
func (mr *MockExchangeMockRecorder) Name() *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Name", reflect.TypeOf((*MockExchange)(nil).Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStream mocks base method.
|
||||||
|
func (m *MockExchange) NewStream() types.Stream {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "NewStream")
|
||||||
|
ret0, _ := ret[0].(types.Stream)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStream indicates an expected call of NewStream.
|
||||||
|
func (mr *MockExchangeMockRecorder) NewStream() *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewStream", reflect.TypeOf((*MockExchange)(nil).NewStream))
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlatformFeeCurrency mocks base method.
|
||||||
|
func (m *MockExchange) PlatformFeeCurrency() string {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "PlatformFeeCurrency")
|
||||||
|
ret0, _ := ret[0].(string)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlatformFeeCurrency indicates an expected call of PlatformFeeCurrency.
|
||||||
|
func (mr *MockExchangeMockRecorder) PlatformFeeCurrency() *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PlatformFeeCurrency", reflect.TypeOf((*MockExchange)(nil).PlatformFeeCurrency))
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryAccount mocks base method.
|
||||||
|
func (m *MockExchange) QueryAccount(arg0 context.Context) (*types.Account, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryAccount", arg0)
|
||||||
|
ret0, _ := ret[0].(*types.Account)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryAccount indicates an expected call of QueryAccount.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryAccount(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryAccount", reflect.TypeOf((*MockExchange)(nil).QueryAccount), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryAccountBalances mocks base method.
|
||||||
|
func (m *MockExchange) QueryAccountBalances(arg0 context.Context) (types.BalanceMap, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryAccountBalances", arg0)
|
||||||
|
ret0, _ := ret[0].(types.BalanceMap)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryAccountBalances indicates an expected call of QueryAccountBalances.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryAccountBalances(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryAccountBalances", reflect.TypeOf((*MockExchange)(nil).QueryAccountBalances), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryKLines mocks base method.
|
||||||
|
func (m *MockExchange) QueryKLines(arg0 context.Context, arg1 string, arg2 types.Interval, arg3 types.KLineQueryOptions) ([]types.KLine, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryKLines", arg0, arg1, arg2, arg3)
|
||||||
|
ret0, _ := ret[0].([]types.KLine)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryKLines indicates an expected call of QueryKLines.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryKLines(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryKLines", reflect.TypeOf((*MockExchange)(nil).QueryKLines), arg0, arg1, arg2, arg3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryMarkets mocks base method.
|
||||||
|
func (m *MockExchange) QueryMarkets(arg0 context.Context) (types.MarketMap, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryMarkets", arg0)
|
||||||
|
ret0, _ := ret[0].(types.MarketMap)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryMarkets indicates an expected call of QueryMarkets.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryMarkets(arg0 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryMarkets", reflect.TypeOf((*MockExchange)(nil).QueryMarkets), arg0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryOpenOrders mocks base method.
|
||||||
|
func (m *MockExchange) QueryOpenOrders(arg0 context.Context, arg1 string) ([]types.Order, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryOpenOrders", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].([]types.Order)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryOpenOrders indicates an expected call of QueryOpenOrders.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryOpenOrders(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryOpenOrders", reflect.TypeOf((*MockExchange)(nil).QueryOpenOrders), arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryTicker mocks base method.
|
||||||
|
func (m *MockExchange) QueryTicker(arg0 context.Context, arg1 string) (*types.Ticker, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "QueryTicker", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].(*types.Ticker)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryTicker indicates an expected call of QueryTicker.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryTicker(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryTicker", reflect.TypeOf((*MockExchange)(nil).QueryTicker), arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryTickers mocks base method.
|
||||||
|
func (m *MockExchange) QueryTickers(arg0 context.Context, arg1 ...string) (map[string]types.Ticker, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
varargs := []interface{}{arg0}
|
||||||
|
for _, a := range arg1 {
|
||||||
|
varargs = append(varargs, a)
|
||||||
|
}
|
||||||
|
ret := m.ctrl.Call(m, "QueryTickers", varargs...)
|
||||||
|
ret0, _ := ret[0].(map[string]types.Ticker)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryTickers indicates an expected call of QueryTickers.
|
||||||
|
func (mr *MockExchangeMockRecorder) QueryTickers(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
varargs := append([]interface{}{arg0}, arg1...)
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "QueryTickers", reflect.TypeOf((*MockExchange)(nil).QueryTickers), varargs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubmitOrders mocks base method.
|
||||||
|
func (m *MockExchange) SubmitOrders(arg0 context.Context, arg1 ...types.SubmitOrder) (types.OrderSlice, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
varargs := []interface{}{arg0}
|
||||||
|
for _, a := range arg1 {
|
||||||
|
varargs = append(varargs, a)
|
||||||
|
}
|
||||||
|
ret := m.ctrl.Call(m, "SubmitOrders", varargs...)
|
||||||
|
ret0, _ := ret[0].(types.OrderSlice)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubmitOrders indicates an expected call of SubmitOrders.
|
||||||
|
func (mr *MockExchangeMockRecorder) SubmitOrders(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
varargs := append([]interface{}{arg0}, arg1...)
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SubmitOrders", reflect.TypeOf((*MockExchange)(nil).SubmitOrders), varargs...)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user