rename protectionStopLoss to protectiveStopLoss

This commit is contained in:
c9s 2022-06-29 01:31:56 +08:00
parent 44d0bbc3fa
commit 3d4f765678
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 22 additions and 22 deletions

View File

@ -32,7 +32,7 @@ exchangeStrategies:
- roiTakeProfit: - roiTakeProfit:
percentage: 30% percentage: 30%
- protectionStopLoss: - protectiveStopLoss:
activationRatio: 1% activationRatio: 1%
stopLossRatio: 0.2% stopLossRatio: 0.2%
placeStopOrder: true placeStopOrder: true

View File

@ -71,13 +71,13 @@ exchangeStrategies:
percentage: 35% percentage: 35%
# (2) protective stop loss -- short term # (2) protective stop loss -- short term
- protectionStopLoss: - protectiveStopLoss:
activationRatio: 0.6% activationRatio: 0.6%
stopLossRatio: 0.1% stopLossRatio: 0.1%
placeStopOrder: false placeStopOrder: false
# (3) protective stop loss -- long term # (3) protective stop loss -- long term
- protectionStopLoss: - protectiveStopLoss:
activationRatio: 5% activationRatio: 5%
stopLossRatio: 1% stopLossRatio: 1%
placeStopOrder: false placeStopOrder: false

View File

@ -44,8 +44,8 @@ matrix:
step: 5% step: 5%
- type: range - type: range
path: '/exchangeStrategies/0/pivotshort/exits/2/protectionStopLoss/activationRatio' path: '/exchangeStrategies/0/pivotshort/exits/2/protectiveStopLoss/activationRatio'
label: protectionStopLoss_activationRatio label: protectiveStopLoss_activationRatio
min: 0.5% min: 0.5%
max: 3% max: 3%
step: 0.1% step: 0.1%
@ -60,6 +60,6 @@ matrix:
- type: range - type: range
path: '/exchangeStrategies/0/pivotshort/exits/5/cumulatedVolumeTakeProfit/minQuoteVolume' path: '/exchangeStrategies/0/pivotshort/exits/5/cumulatedVolumeTakeProfit/minQuoteVolume'
label: cumulatedVolumeTakeProfit_minQuoteVolume label: cumulatedVolumeTakeProfit_minQuoteVolume
min: 30_000_000 min: 3_000_000
max: 200_000_000 max: 20_000_000
step: 10_000_000 step: 100_000

View File

@ -4,15 +4,15 @@ import "github.com/c9s/bbgo/pkg/bbgo"
type ExitMethod struct { type ExitMethod struct {
RoiStopLoss *RoiStopLoss `json:"roiStopLoss"` RoiStopLoss *RoiStopLoss `json:"roiStopLoss"`
ProtectionStopLoss *ProtectionStopLoss `json:"protectionStopLoss"` ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"`
RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"` RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"`
LowerShadowTakeProfit *LowerShadowTakeProfit `json:"lowerShadowTakeProfit"` LowerShadowTakeProfit *LowerShadowTakeProfit `json:"lowerShadowTakeProfit"`
CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"` CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"`
} }
func (m *ExitMethod) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) { func (m *ExitMethod) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
if m.ProtectionStopLoss != nil { if m.ProtectiveStopLoss != nil {
m.ProtectionStopLoss.Bind(session, orderExecutor) m.ProtectiveStopLoss.Bind(session, orderExecutor)
} else if m.RoiStopLoss != nil { } else if m.RoiStopLoss != nil {
m.RoiStopLoss.Bind(session, orderExecutor) m.RoiStopLoss.Bind(session, orderExecutor)
} else if m.RoiTakeProfit != nil { } else if m.RoiTakeProfit != nil {

View File

@ -8,7 +8,7 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
type ProtectionStopLoss struct { type ProtectiveStopLoss struct {
// ActivationRatio is the trigger condition of this ROI protection stop loss // ActivationRatio is the trigger condition of this ROI protection stop loss
// When the price goes lower (for short position) with the ratio, the protection stop will be activated. // When the price goes lower (for short position) with the ratio, the protection stop will be activated.
// This number should be positive to protect the profit // This number should be positive to protect the profit
@ -27,7 +27,7 @@ type ProtectionStopLoss struct {
stopLossOrder *types.Order stopLossOrder *types.Order
} }
func (s *ProtectionStopLoss) shouldActivate(position *types.Position, closePrice fixedpoint.Value) bool { func (s *ProtectiveStopLoss) shouldActivate(position *types.Position, closePrice fixedpoint.Value) bool {
if position.IsLong() { if position.IsLong() {
r := one.Add(s.ActivationRatio) r := one.Add(s.ActivationRatio)
activationPrice := position.AverageCost.Mul(r) activationPrice := position.AverageCost.Mul(r)
@ -42,7 +42,7 @@ func (s *ProtectionStopLoss) shouldActivate(position *types.Position, closePrice
return false return false
} }
func (s *ProtectionStopLoss) placeStopOrder(ctx context.Context, position *types.Position, orderExecutor bbgo.OrderExecutor) error { func (s *ProtectiveStopLoss) placeStopOrder(ctx context.Context, position *types.Position, orderExecutor bbgo.OrderExecutor) error {
if s.stopLossOrder != nil { if s.stopLossOrder != nil {
if err := orderExecutor.CancelOrders(ctx, *s.stopLossOrder); err != nil { if err := orderExecutor.CancelOrders(ctx, *s.stopLossOrder); err != nil {
log.WithError(err).Errorf("failed to cancel stop limit order: %+v", s.stopLossOrder) log.WithError(err).Errorf("failed to cancel stop limit order: %+v", s.stopLossOrder)
@ -58,7 +58,7 @@ func (s *ProtectionStopLoss) placeStopOrder(ctx context.Context, position *types
Price: s.stopLossPrice.Mul(one.Add(fixedpoint.NewFromFloat(0.005))), // +0.5% from the trigger price, slippage protection Price: s.stopLossPrice.Mul(one.Add(fixedpoint.NewFromFloat(0.005))), // +0.5% from the trigger price, slippage protection
StopPrice: s.stopLossPrice, StopPrice: s.stopLossPrice,
Market: position.Market, Market: position.Market,
Tag: "protectionStopLoss", Tag: "protectiveStopLoss",
}) })
if len(createdOrders) > 0 { if len(createdOrders) > 0 {
@ -67,7 +67,7 @@ func (s *ProtectionStopLoss) placeStopOrder(ctx context.Context, position *types
return err return err
} }
func (s *ProtectionStopLoss) shouldStop(closePrice fixedpoint.Value) bool { func (s *ProtectiveStopLoss) shouldStop(closePrice fixedpoint.Value) bool {
if s.stopLossPrice.IsZero() { if s.stopLossPrice.IsZero() {
return false return false
} }
@ -75,7 +75,7 @@ func (s *ProtectionStopLoss) shouldStop(closePrice fixedpoint.Value) bool {
return closePrice.Compare(s.stopLossPrice) >= 0 return closePrice.Compare(s.stopLossPrice) >= 0
} }
func (s *ProtectionStopLoss) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) { func (s *ProtectiveStopLoss) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
s.session = session s.session = session
s.orderExecutor = orderExecutor s.orderExecutor = orderExecutor
@ -127,7 +127,7 @@ func (s *ProtectionStopLoss) Bind(session *bbgo.ExchangeSession, orderExecutor *
} }
} }
func (s *ProtectionStopLoss) handleChange(ctx context.Context, position *types.Position, closePrice fixedpoint.Value, orderExecutor *bbgo.GeneralOrderExecutor) { func (s *ProtectiveStopLoss) handleChange(ctx context.Context, position *types.Position, closePrice fixedpoint.Value, orderExecutor *bbgo.GeneralOrderExecutor) {
if s.stopLossOrder != nil { if s.stopLossOrder != nil {
// use RESTful to query the order status // use RESTful to query the order status
// orderQuery := orderExecutor.Session().Exchange.(types.ExchangeOrderQueryService) // orderQuery := orderExecutor.Session().Exchange.(types.ExchangeOrderQueryService)
@ -149,7 +149,7 @@ func (s *ProtectionStopLoss) handleChange(ctx context.Context, position *types.P
s.stopLossPrice = position.AverageCost.Mul(one.Add(s.StopLossRatio)) s.stopLossPrice = position.AverageCost.Mul(one.Add(s.StopLossRatio))
} }
log.Infof("[ProtectionStopLoss] %s protection stop loss activated, current price = %f, average cost = %f, stop loss price = %f", log.Infof("[ProtectiveStopLoss] %s protection stop loss activated, current price = %f, average cost = %f, stop loss price = %f",
position.Symbol, closePrice.Float64(), position.AverageCost.Float64(), s.stopLossPrice.Float64()) position.Symbol, closePrice.Float64(), position.AverageCost.Float64(), s.stopLossPrice.Float64())
if s.PlaceStopOrder { if s.PlaceStopOrder {
@ -168,14 +168,14 @@ func (s *ProtectionStopLoss) handleChange(ctx context.Context, position *types.P
s.checkStopPrice(closePrice, position) s.checkStopPrice(closePrice, position)
} }
func (s *ProtectionStopLoss) checkStopPrice(closePrice fixedpoint.Value, position *types.Position) { func (s *ProtectiveStopLoss) checkStopPrice(closePrice fixedpoint.Value, position *types.Position) {
if s.stopLossPrice.IsZero() { if s.stopLossPrice.IsZero() {
return return
} }
if s.shouldStop(closePrice) { if s.shouldStop(closePrice) {
log.Infof("[ProtectionStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position) log.Infof("[ProtectiveStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position)
if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectionStopLoss"); err != nil { if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectiveStopLoss"); err != nil {
log.WithError(err).Errorf("failed to close position") log.WithError(err).Errorf("failed to close position")
} }
} }