mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
47677e303f
Signed-off-by: c9s <yoanlin93@gmail.com>
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package pivotshort
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
)
|
|
|
|
type RoiStopLoss struct {
|
|
Percentage fixedpoint.Value `json:"percentage"`
|
|
|
|
session *bbgo.ExchangeSession
|
|
orderExecutor *bbgo.GeneralOrderExecutor
|
|
}
|
|
|
|
func (s *RoiStopLoss) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.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.Compare(s.Percentage.Neg()) < 0 {
|
|
// stop loss
|
|
bbgo.Notify("[RoiStopLoss] %s stop loss triggered by ROI %s/%s, price: %f", position.Symbol, roi.Percentage(), s.Percentage.Neg().Percentage(), kline.Close.Float64())
|
|
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
|
|
return
|
|
}
|
|
})
|
|
}
|