2022-06-28 17:58:15 +00:00
package bbgo
2022-06-26 08:13:58 +00:00
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
2022-06-28 17:58:15 +00:00
// RoiTakeProfit force takes the profit by the given ROI percentage.
2022-06-26 08:13:58 +00:00
type RoiTakeProfit struct {
Percentage fixedpoint . Value ` json:"percentage" `
2022-06-28 17:58:15 +00:00
session * ExchangeSession
orderExecutor * GeneralOrderExecutor
2022-06-26 08:13:58 +00:00
}
2022-06-28 17:58:15 +00:00
func ( s * RoiTakeProfit ) Bind ( session * ExchangeSession , orderExecutor * GeneralOrderExecutor ) {
2022-06-26 08:13:58 +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 . Compare ( s . Percentage ) > 0 {
// stop loss
2022-06-28 17:58:15 +00:00
Notify ( "[RoiTakeProfit] %s take profit is triggered by ROI %s/%s, price: %f" , position . Symbol , roi . Percentage ( ) , s . Percentage . Percentage ( ) , kline . Close . Float64 ( ) )
2022-06-27 10:17:57 +00:00
_ = orderExecutor . ClosePosition ( context . Background ( ) , fixedpoint . One , "roiTakeProfit" )
2022-06-26 08:13:58 +00:00
return
}
} )
}