exits/hhllstop: add hhllstop to exits

This commit is contained in:
Andy Cheng 2023-03-16 18:39:27 +08:00
parent eb5479ffdf
commit 2e00e58442
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28
2 changed files with 22 additions and 12 deletions

View File

@ -29,16 +29,17 @@ func (s *ExitMethodSet) Bind(session *ExchangeSession, orderExecutor *GeneralOrd
}
type ExitMethod struct {
RoiStopLoss *RoiStopLoss `json:"roiStopLoss"`
ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"`
RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"`
TrailingStop *TrailingStop2 `json:"trailingStop"`
RoiStopLoss *RoiStopLoss `json:"roiStopLoss"`
ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"`
RoiTakeProfit *RoiTakeProfit `json:"roiTakeProfit"`
TrailingStop *TrailingStop2 `json:"trailingStop"`
HigherHighLowerLowStop *HigherHighLowerLowStop `json:"higherHighLowerLowStopLoss"`
// Exit methods for short positions
// =================================================
LowerShadowTakeProfit *LowerShadowTakeProfit `json:"lowerShadowTakeProfit"`
CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"`
SupportTakeProfit *SupportTakeProfit `json:"supportTakeProfit"`
SupportTakeProfit *SupportTakeProfit `json:"supportTakeProfit"`
}
func (e ExitMethod) String() string {
@ -78,6 +79,11 @@ func (e ExitMethod) String() string {
buf.WriteString("supportTakeProfit: " + string(b) + ", ")
}
if e.HigherHighLowerLowStop != nil {
b, _ := json.Marshal(e.HigherHighLowerLowStop)
buf.WriteString("hhllStop: " + string(b) + ", ")
}
return buf.String()
}
@ -135,4 +141,8 @@ func (m *ExitMethod) Bind(session *ExchangeSession, orderExecutor *GeneralOrderE
if m.TrailingStop != nil {
m.TrailingStop.Bind(session, orderExecutor)
}
if m.HigherHighLowerLowStop != nil {
m.HigherHighLowerLowStop.Bind(session, orderExecutor)
}
}

View File

@ -8,7 +8,7 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
type HigherHighLowerLowStopLoss struct {
type HigherHighLowerLowStop struct {
Symbol string `json:"symbol"`
Side types.SideType `json:"side"`
@ -45,14 +45,14 @@ type HigherHighLowerLowStopLoss struct {
}
// Subscribe required k-line stream
func (s *HigherHighLowerLowStopLoss) Subscribe(session *ExchangeSession) {
func (s *HigherHighLowerLowStop) Subscribe(session *ExchangeSession) {
// use 1m kline to handle roi stop
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}
// updateActivated checks the position cost against the close price, activation ratio, and deactivation ratio to
// determine whether this stop should be activated
func (s *HigherHighLowerLowStopLoss) updateActivated(position *types.Position, closePrice fixedpoint.Value) {
func (s *HigherHighLowerLowStop) updateActivated(position *types.Position, closePrice fixedpoint.Value) {
if position.IsClosed() || position.IsDust(closePrice) {
s.activated = false
} else if s.activated {
@ -88,7 +88,7 @@ func (s *HigherHighLowerLowStopLoss) updateActivated(position *types.Position, c
}
}
func (s *HigherHighLowerLowStopLoss) updateHighLowNumber(kline types.KLine) {
func (s *HigherHighLowerLowStop) updateHighLowNumber(kline types.KLine) {
if !s.activated {
s.reset()
return
@ -116,7 +116,7 @@ func (s *HigherHighLowerLowStopLoss) updateHighLowNumber(kline types.KLine) {
s.klines.Truncate(s.Window - 1)
}
func (s *HigherHighLowerLowStopLoss) shouldStop(position *types.Position) bool {
func (s *HigherHighLowerLowStop) shouldStop(position *types.Position) bool {
if s.activated {
highs := 0
lows := 0
@ -146,12 +146,12 @@ func (s *HigherHighLowerLowStopLoss) shouldStop(position *types.Position) bool {
return false
}
func (s *HigherHighLowerLowStopLoss) reset() {
func (s *HigherHighLowerLowStop) reset() {
s.highLows = []types.Direction{}
s.klines.Truncate(0)
}
func (s *HigherHighLowerLowStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
func (s *HigherHighLowerLowStop) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
s.session = session
s.orderExecutor = orderExecutor