mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
add ExitMethodSet.Bind method
This commit is contained in:
parent
d2637ce261
commit
74593720a7
|
@ -100,6 +100,21 @@ exchangeStrategies:
|
|||
window: 2
|
||||
minQuoteVolume: 200_000_000
|
||||
|
||||
- trailingStop:
|
||||
callbackRate: 2%
|
||||
|
||||
# activationRatio is relative to the average cost,
|
||||
# when side is buy, 1% means lower 1% than the average cost.
|
||||
# when side is sell, 1% means higher 1% than the average cost.
|
||||
activationRatio: 10%
|
||||
|
||||
# minProfit uses the position ROI to calculate the profit ratio
|
||||
# minProfit: 1%
|
||||
|
||||
interval: 1m
|
||||
side: buy
|
||||
closePosition: 100%
|
||||
|
||||
backtest:
|
||||
sessions:
|
||||
- binance
|
||||
|
|
|
@ -501,3 +501,65 @@ In the Run method of your strategy, you need to check if these fields are nil, a
|
|||
|
||||
That's it. Hit Ctrl-C and you should see BBGO saving your strategy states.
|
||||
|
||||
|
||||
## Exit Method Set
|
||||
|
||||
To integrate the built-in exit methods into your strategy, simply add a field with type bbgo.ExitMethodSet:
|
||||
|
||||
```go
|
||||
type Strategy struct {
|
||||
ExitMethods bbgo.ExitMethodSet `json:"exits"`
|
||||
}
|
||||
|
||||
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
||||
s.ExitMethods.SetAndSubscribe(session, s)
|
||||
}
|
||||
|
||||
func (s *Strategy) Run() {
|
||||
s.ExitMethods.Bind(session, s.orderExecutor)
|
||||
}
|
||||
```
|
||||
|
||||
And then you can use the following config structure to configure your exit settings like this:
|
||||
|
||||
```yaml
|
||||
- on: binance
|
||||
pivotshort:
|
||||
exits:
|
||||
# (0) roiStopLoss is the stop loss percentage of the position ROI (currently the price change)
|
||||
- roiStopLoss:
|
||||
percentage: 0.8%
|
||||
|
||||
# (1) roiTakeProfit is used to force taking profit by percentage of the position ROI (currently the price change)
|
||||
# force to take the profit ROI exceeded the percentage.
|
||||
- roiTakeProfit:
|
||||
percentage: 35%
|
||||
|
||||
# (2) protective stop loss -- short term
|
||||
- protectiveStopLoss:
|
||||
activationRatio: 0.6%
|
||||
stopLossRatio: 0.1%
|
||||
placeStopOrder: false
|
||||
|
||||
# (3) protective stop loss -- long term
|
||||
- protectiveStopLoss:
|
||||
activationRatio: 5%
|
||||
stopLossRatio: 1%
|
||||
placeStopOrder: false
|
||||
|
||||
# (4) lowerShadowTakeProfit is used to taking profit when the (lower shadow height / low price) > lowerShadowRatio
|
||||
# you can grab a simple stats by the following SQL:
|
||||
# SELECT ((close - low) / close) AS shadow_ratio FROM binance_klines WHERE symbol = 'ETHUSDT' AND `interval` = '5m' AND start_time > '2022-01-01' ORDER BY shadow_ratio DESC LIMIT 20;
|
||||
- lowerShadowTakeProfit:
|
||||
interval: 30m
|
||||
window: 99
|
||||
ratio: 3%
|
||||
|
||||
# (5) cumulatedVolumeTakeProfit is used to take profit when the cumulated quote volume from the klines exceeded a threshold
|
||||
- cumulatedVolumeTakeProfit:
|
||||
interval: 5m
|
||||
window: 2
|
||||
minQuoteVolume: 200_000_000
|
||||
|
||||
|
||||
```
|
||||
|
|
|
@ -20,6 +20,12 @@ func (s *ExitMethodSet) SetAndSubscribe(session *ExchangeSession, parent interfa
|
|||
}
|
||||
}
|
||||
|
||||
func (s *ExitMethodSet) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
||||
for _, method := range *s {
|
||||
method.Bind(session, orderExecutor)
|
||||
}
|
||||
}
|
||||
|
||||
type ExitMethod struct {
|
||||
RoiStopLoss *RoiStopLoss `json:"roiStopLoss"`
|
||||
ProtectiveStopLoss *ProtectiveStopLoss `json:"protectiveStopLoss"`
|
||||
|
|
|
@ -261,9 +261,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
})
|
||||
s.orderExecutor.Bind()
|
||||
|
||||
for _, method := range s.ExitMethods {
|
||||
method.Bind(session, s.orderExecutor)
|
||||
}
|
||||
s.ExitMethods.Bind(session, s.orderExecutor)
|
||||
|
||||
if s.ResistanceShort != nil && s.ResistanceShort.Enabled {
|
||||
s.ResistanceShort.Bind(session, s.orderExecutor)
|
||||
|
|
Loading…
Reference in New Issue
Block a user