art_trailing_stop_loss.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package indicator
  2. import (
  3. "errors"
  4. "git.beejay.kim/Gshopper/sentio"
  5. "git.beejay.kim/Gshopper/sentio/talib"
  6. "time"
  7. )
  8. func AtrTrailingStopLoss(m sentio.Market,
  9. period uint,
  10. length uint,
  11. multiplier float64,
  12. hhv int,
  13. symbols ...string,
  14. ) (map[string]float64, error) {
  15. var (
  16. stoplosses map[string]float64
  17. bars map[string][]sentio.Bar
  18. err error
  19. )
  20. if bars, err = m.HistoricalBars(symbols, time.Minute*time.Duration(period), nil); err != nil {
  21. return nil, err
  22. }
  23. stoplosses = make(map[string]float64)
  24. for s := range bars {
  25. if bars == nil || len(bars[s]) < int(length) {
  26. return nil, errors.New("AtrStopLoss: could not calculate stoploss for too short timeseries")
  27. }
  28. h := make([]float64, len(bars[s]))
  29. l := make([]float64, len(bars[s]))
  30. c := make([]float64, len(bars[s]))
  31. for i := range bars[s] {
  32. h[i] = bars[s][i].High
  33. l[i] = bars[s][i].Low
  34. c[i] = bars[s][i].Close
  35. }
  36. atr := talib.Atr(h, l, c, int(length))
  37. trailing := make([]float64, len(atr))
  38. for i := range atr {
  39. trailing[i] = h[i] - multiplier*atr[i]
  40. }
  41. if hhv > 1 {
  42. trailing = talib.Max(trailing, hhv)
  43. }
  44. stoplosses[s] = trailing[len(trailing)-1]
  45. }
  46. return stoplosses, nil
  47. }