@@ -76,10 +76,16 @@ func (s *TrailingStop2) getRatio(price fixedpoint.Value, position *types.Positio
76
76
switch s .Side {
77
77
case types .SideTypeBuy :
78
78
// for short position, it's:
79
- // (avg_cost - price) / price
80
- return position .AverageCost .Sub (price ).Div (price ), nil
79
+ // (avg_cost - price) / avg_cost
80
+ return position .AverageCost .Sub (price ).Div (position . AverageCost ), nil
81
81
case types .SideTypeSell :
82
82
return price .Sub (position .AverageCost ).Div (position .AverageCost ), nil
83
+ default :
84
+ if position .IsLong () {
85
+ return price .Sub (position .AverageCost ).Div (position .AverageCost ), nil
86
+ } else if position .IsShort () {
87
+ return position .AverageCost .Sub (price ).Div (position .AverageCost ), nil
88
+ }
83
89
}
84
90
85
91
return fixedpoint .Zero , fmt .Errorf ("unexpected side type: %v" , s .Side )
@@ -117,6 +123,12 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
117
123
s .latestHigh = fixedpoint .Min (price , s .latestHigh )
118
124
case types .SideTypeSell :
119
125
s .latestHigh = fixedpoint .Max (price , s .latestHigh )
126
+ default :
127
+ if position .IsLong () {
128
+ s .latestHigh = fixedpoint .Max (price , s .latestHigh )
129
+ } else if position .IsShort () {
130
+ s .latestHigh = fixedpoint .Min (price , s .latestHigh )
131
+ }
120
132
}
121
133
}
122
134
@@ -126,22 +138,31 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
126
138
127
139
switch s .Side {
128
140
case types .SideTypeBuy :
129
- s .latestHigh = fixedpoint .Min (price , s .latestHigh )
130
-
131
141
change := price .Sub (s .latestHigh ).Div (s .latestHigh )
132
142
if change .Compare (s .CallbackRate ) >= 0 {
133
143
// submit order
134
144
return s .triggerStop (price )
135
145
}
136
-
137
146
case types .SideTypeSell :
138
- s .latestHigh = fixedpoint .Max (price , s .latestHigh )
139
-
140
- change := s .latestHigh .Sub (price ).Div (price )
147
+ change := s .latestHigh .Sub (price ).Div (s .latestHigh )
141
148
if change .Compare (s .CallbackRate ) >= 0 {
142
149
// submit order
143
150
return s .triggerStop (price )
144
151
}
152
+ default :
153
+ if position .IsLong () {
154
+ change := s .latestHigh .Sub (price ).Div (s .latestHigh )
155
+ if change .Compare (s .CallbackRate ) >= 0 {
156
+ // submit order
157
+ return s .triggerStop (price )
158
+ }
159
+ } else if position .IsShort () {
160
+ change := price .Sub (s .latestHigh ).Div (s .latestHigh )
161
+ if change .Compare (s .CallbackRate ) >= 0 {
162
+ // submit order
163
+ return s .triggerStop (price )
164
+ }
165
+ }
145
166
}
146
167
147
168
return nil
0 commit comments