diff --git a/go.mod b/go.mod index 910022d..5aea054 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/sdcoffey/techan go 1.21 require ( - github.com/sdcoffey/big v0.7.0 - github.com/stretchr/testify v1.7.0 + github.com/sdcoffey/big v0.8.0 + github.com/stretchr/testify v1.11.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d195378..768cc6e 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,12 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sdcoffey/big v0.7.0 h1:OnE7fcHq/C59WxWrMegftFa1nftCjsZLVf7PLXsxj2Y= -github.com/sdcoffey/big v0.7.0/go.mod h1:2T05Q7Mt6F1kHHb+PFa0odPFwU67YnSAFYgiYy7krPU= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/sdcoffey/big v0.8.0 h1:njCAnzEHdWUfYBK6Jj9NowXpIaE3p+s2S4qjlh/wn5I= +github.com/sdcoffey/big v0.8.0/go.mod h1:o0blP3VmRolJhiftTQnhwH+gUzqFPSXDDshA6Ddc854= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/rule_stop.go b/rule_stop.go index 66d0bf1..2ea19c3 100644 --- a/rule_stop.go +++ b/rule_stop.go @@ -1,6 +1,10 @@ package techan -import "github.com/sdcoffey/big" +import ( + "strconv" + + "github.com/sdcoffey/big" +) type stopLossRule struct { Indicator @@ -12,7 +16,7 @@ type stopLossRule struct { func NewStopLossRule(series *TimeSeries, lossTolerance float64) Rule { return stopLossRule{ Indicator: NewClosePriceIndicator(series), - tolerance: big.NewDecimal(lossTolerance), + tolerance: big.NewFromString(strconv.FormatFloat(lossTolerance, 'f', -1, 64)), } } @@ -25,9 +29,9 @@ func (slr stopLossRule) IsSatisfied(index int, record *TradingRecord) bool { currentPrice := slr.Indicator.Calculate(index) entryPrice := entryOrder.Price - loss := currentPrice.Div(entryPrice).Sub(big.ONE) + loss := currentPrice.Sub(entryPrice).Div(entryPrice) if entryOrder.Side == SELL { - loss = big.ONE.Sub(currentPrice.Div(entryPrice)) + loss = entryPrice.Sub(currentPrice).Div(entryPrice) } return loss.LTE(slr.tolerance)