bbgo_origin/pkg/bbgo/exit_lower_shadow_take_profit.go
c9s 16f2a06b1f
all: move exit methods to the bbgo core
Signed-off-by: c9s <yoanlin93@gmail.com>
2022-06-29 01:58:15 +08:00

53 lines
1.1 KiB
Go

package bbgo
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type LowerShadowTakeProfit struct {
Ratio fixedpoint.Value `json:"ratio"`
session *ExchangeSession
orderExecutor *GeneralOrderExecutor
}
func (s *LowerShadowTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
s.session = session
s.orderExecutor = orderExecutor
position := orderExecutor.Position()
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
if kline.Symbol != position.Symbol || kline.Interval != types.Interval1m {
return
}
closePrice := kline.Close
if position.IsClosed() || position.IsDust(closePrice) {
return
}
roi := position.ROI(closePrice)
if roi.Sign() < 0 {
return
}
if s.Ratio.IsZero() {
return
}
if kline.GetLowerShadowHeight().Div(kline.Close).Compare(s.Ratio) > 0 {
Notify("%s TakeProfit triggered by shadow ratio %f, price = %f",
position.Symbol,
kline.GetLowerShadowRatio().Float64(),
kline.Close.Float64(),
kline)
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
return
}
})
}