trailingstop: add side both

This commit is contained in:
Andy Cheng 2022-08-11 16:39:16 +08:00
parent df0e527e1e
commit 8d3dfd17c7

View File

@ -76,10 +76,16 @@ func (s *TrailingStop2) getRatio(price fixedpoint.Value, position *types.Positio
switch s.Side { switch s.Side {
case types.SideTypeBuy: case types.SideTypeBuy:
// for short position, it's: // for short position, it's:
// (avg_cost - price) / price // (avg_cost - price) / avg_cost
return position.AverageCost.Sub(price).Div(price), nil return position.AverageCost.Sub(price).Div(position.AverageCost), nil
case types.SideTypeSell: case types.SideTypeSell:
return price.Sub(position.AverageCost).Div(position.AverageCost), nil return price.Sub(position.AverageCost).Div(position.AverageCost), nil
case types.SideTypeBoth:
if position.IsLong() {
return price.Sub(position.AverageCost).Div(position.AverageCost), nil
} else if position.IsShort() {
return position.AverageCost.Sub(price).Div(position.AverageCost), nil
}
} }
return fixedpoint.Zero, fmt.Errorf("unexpected side type: %v", s.Side) return fixedpoint.Zero, fmt.Errorf("unexpected side type: %v", s.Side)
@ -90,7 +96,6 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
return nil return nil
} }
if (position.IsLong() && s.Side == types.SideTypeSell) || (position.IsShort() && s.Side == types.SideTypeBuy) {
if !s.MinProfit.IsZero() { if !s.MinProfit.IsZero() {
// check if we have the minimal profit // check if we have the minimal profit
roi := position.ROI(price) roi := position.ROI(price)
@ -108,7 +113,6 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
s.activated = true s.activated = true
} }
} }
}
// update the latest high for the sell order, or the latest low for the buy order // update the latest high for the sell order, or the latest low for the buy order
if s.latestHigh.IsZero() { if s.latestHigh.IsZero() {
@ -119,6 +123,12 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
s.latestHigh = fixedpoint.Min(price, s.latestHigh) s.latestHigh = fixedpoint.Min(price, s.latestHigh)
case types.SideTypeSell: case types.SideTypeSell:
s.latestHigh = fixedpoint.Max(price, s.latestHigh) s.latestHigh = fixedpoint.Max(price, s.latestHigh)
case types.SideTypeBoth:
if position.IsLong() {
s.latestHigh = fixedpoint.Max(price, s.latestHigh)
} else if position.IsShort() {
s.latestHigh = fixedpoint.Min(price, s.latestHigh)
}
} }
} }
@ -128,16 +138,26 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
switch s.Side { switch s.Side {
case types.SideTypeBuy: case types.SideTypeBuy:
if position.IsShort() {
change := price.Sub(s.latestHigh).Div(s.latestHigh) change := price.Sub(s.latestHigh).Div(s.latestHigh)
if change.Compare(s.CallbackRate) >= 0 { if change.Compare(s.CallbackRate) >= 0 {
// submit order // submit order
return s.triggerStop(price) return s.triggerStop(price)
} }
}
case types.SideTypeSell: case types.SideTypeSell:
change := s.latestHigh.Sub(price).Div(s.latestHigh)
if change.Compare(s.CallbackRate) >= 0 {
// submit order
return s.triggerStop(price)
}
case types.SideTypeBoth:
if position.IsLong() { if position.IsLong() {
change := s.latestHigh.Sub(price).Div(price) change := s.latestHigh.Sub(price).Div(s.latestHigh)
if change.Compare(s.CallbackRate) >= 0 {
// submit order
return s.triggerStop(price)
}
} else if position.IsShort() {
change := price.Sub(s.latestHigh).Div(s.latestHigh)
if change.Compare(s.CallbackRate) >= 0 { if change.Compare(s.CallbackRate) >= 0 {
// submit order // submit order
return s.triggerStop(price) return s.triggerStop(price)