2022-06-28 17:58:15 +00:00
|
|
|
package bbgo
|
2022-06-26 08:31:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LowerShadowTakeProfit struct {
|
|
|
|
Ratio fixedpoint.Value `json:"ratio"`
|
|
|
|
|
2022-06-28 17:58:15 +00:00
|
|
|
session *ExchangeSession
|
|
|
|
orderExecutor *GeneralOrderExecutor
|
2022-06-26 08:31:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 17:58:15 +00:00
|
|
|
func (s *LowerShadowTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
2022-06-26 08:31:48 +00:00
|
|
|
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 {
|
2022-06-28 17:58:15 +00:00
|
|
|
Notify("%s TakeProfit triggered by shadow ratio %f, price = %f",
|
2022-06-26 08:31:48 +00:00
|
|
|
position.Symbol,
|
|
|
|
kline.GetLowerShadowRatio().Float64(),
|
|
|
|
kline.Close.Float64(),
|
|
|
|
kline)
|
|
|
|
|
|
|
|
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|