-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathrule_stop.go
More file actions
27 lines (22 loc) · 767 Bytes
/
rule_stop.go
File metadata and controls
27 lines (22 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package techan
import "github.com/sdcoffey/big"
type stopLossRule struct {
Indicator
tolerance big.Decimal
}
// NewStopLossRule returns a new rule that is satisfied when the given loss tolerance (a percentage) is met or exceeded.
// Loss tolerance should be a value between -1 and 1.
func NewStopLossRule(series *TimeSeries, lossTolerance float64) Rule {
return stopLossRule{
Indicator: NewClosePriceIndicator(series),
tolerance: big.NewDecimal(lossTolerance),
}
}
func (slr stopLossRule) IsSatisfied(index int, record *TradingRecord) bool {
if !record.CurrentPosition().IsOpen() {
return false
}
openPrice := record.CurrentPosition().CostBasis()
loss := slr.Indicator.Calculate(index).Div(openPrice).Sub(big.ONE)
return loss.LTE(slr.tolerance)
}