123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package indicator
- import (
- "errors"
- "git.beejay.kim/Gshopper/sentio"
- "git.beejay.kim/Gshopper/sentio/talib"
- "time"
- )
- func AtrTrailingStopLoss(m sentio.Market,
- period uint,
- length uint,
- multiplier float64,
- hhv int,
- symbols ...string,
- ) (map[string]float64, error) {
- var (
- stoplosses map[string]float64
- bars map[string][]sentio.Bar
- err error
- )
- if bars, err = m.HistoricalBars(symbols, time.Minute*time.Duration(period), nil); err != nil {
- return nil, err
- }
- stoplosses = make(map[string]float64)
- for s := range bars {
- if bars == nil || len(bars[s]) < int(length) {
- return nil, errors.New("AtrStopLoss: could not calculate stoploss for too short timeseries")
- }
- h := make([]float64, len(bars[s]))
- l := make([]float64, len(bars[s]))
- c := make([]float64, len(bars[s]))
- for i := range bars[s] {
- h[i] = bars[s][i].High
- l[i] = bars[s][i].Low
- c[i] = bars[s][i].Close
- }
- atr := talib.Atr(h, l, c, int(length))
- trailing := make([]float64, len(atr))
- for i := range atr {
- trailing[i] = h[i] - multiplier*atr[i]
- }
- if hhv > 1 {
- trailing = talib.Max(trailing, hhv)
- }
- stoplosses[s] = trailing[len(trailing)-1]
- }
- return stoplosses, nil
- }
|