mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #865 from andycheng123/fix/protective-stoploss
fix: protectivestoploss not working on long position
This commit is contained in:
commit
ef18791c6a
|
@ -79,12 +79,18 @@ func (s *ProtectiveStopLoss) placeStopOrder(ctx context.Context, position *types
|
|||
return err
|
||||
}
|
||||
|
||||
func (s *ProtectiveStopLoss) shouldStop(closePrice fixedpoint.Value) bool {
|
||||
func (s *ProtectiveStopLoss) shouldStop(closePrice fixedpoint.Value, position *types.Position) bool {
|
||||
if s.stopLossPrice.IsZero() {
|
||||
return false
|
||||
}
|
||||
|
||||
return closePrice.Compare(s.stopLossPrice) >= 0
|
||||
if position.IsShort() {
|
||||
return closePrice.Compare(s.stopLossPrice) >= 0
|
||||
} else if position.IsLong() {
|
||||
return closePrice.Compare(s.stopLossPrice) <= 0
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
||||
|
@ -119,8 +125,10 @@ func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *Gener
|
|||
}
|
||||
|
||||
isPositionOpened := !position.IsClosed() && !position.IsDust(kline.Close)
|
||||
if isPositionOpened && position.IsShort() {
|
||||
if isPositionOpened {
|
||||
s.handleChange(context.Background(), position, kline.Close, s.orderExecutor)
|
||||
} else {
|
||||
s.stopLossPrice = fixedpoint.Zero
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -185,7 +193,7 @@ func (s *ProtectiveStopLoss) checkStopPrice(closePrice fixedpoint.Value, positio
|
|||
return
|
||||
}
|
||||
|
||||
if s.shouldStop(closePrice) {
|
||||
if s.shouldStop(closePrice, position) {
|
||||
log.Infof("[ProtectiveStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position)
|
||||
if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectiveStopLoss"); err != nil {
|
||||
log.WithError(err).Errorf("failed to close position")
|
||||
|
|
|
@ -334,10 +334,14 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
if s.TradeStats == nil {
|
||||
s.TradeStats = types.NewTradeStats(s.Symbol)
|
||||
}
|
||||
startTime := s.Environment.StartTime()
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1d, startTime))
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1w, startTime))
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1mo, startTime))
|
||||
|
||||
// Interval profit report
|
||||
if bbgo.IsBackTesting {
|
||||
startTime := s.Environment.StartTime()
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1d, startTime))
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1w, startTime))
|
||||
s.TradeStats.SetIntervalProfitCollector(types.NewIntervalProfitCollector(types.Interval1mo, startTime))
|
||||
}
|
||||
|
||||
// Set fee rate
|
||||
if s.session.MakerFeeRate.Sign() > 0 || s.session.TakerFeeRate.Sign() > 0 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user