Selaa lähdekoodia

QQQ15

- draft indicator.AtrTrailingStopLoss impl
Alexey Kim 2 viikkoa sitten
vanhempi
commit
3d08a4589d
2 muutettua tiedostoa jossa 39 lisäystä ja 0 poistoa
  1. 7 0
      strategy/alpaca/qqq15/constants.go
  2. 32 0
      strategy/alpaca/qqq15/strategy.go

+ 7 - 0
strategy/alpaca/qqq15/constants.go

@@ -0,0 +1,7 @@
+package main
+
+const (
+	ATR_MULTIPLIER = 1.5
+	ATR_PERIOD     = 14
+	ATR_HHV        = 10
+)

+ 32 - 0
strategy/alpaca/qqq15/strategy.go

@@ -1,7 +1,9 @@
 package main
 
 import (
+	"fmt"
 	"git.beejay.kim/Gshopper/sentio"
+	"git.beejay.kim/Gshopper/sentio/indicator"
 	"time"
 )
 
@@ -72,6 +74,36 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			ok = false
 		}
 
+		if ok {
+			var (
+				sl   []float64
+				bars []sentio.Bar
+			)
+
+			if bars, err = market.HistoricalBars(symbol, time.Minute, nil); err == nil && len(bars) > 0 {
+				h := make([]float64, len(bars))
+				l := make([]float64, len(bars))
+				c := make([]float64, len(bars))
+
+				for i := range bars {
+					h[i] = bars[i].High
+					l[i] = bars[i].Low
+					c[i] = bars[i].Close
+				}
+
+				sl = indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
+				fmt.Printf("ATR Trailing StopLoss: [%f]\n", sl[len(sl)-1])
+
+				if position.GetCurrentPrice() < sl[len(sl)-1] {
+					return []sentio.StrategyOrder{{
+						Symbol: symbol,
+						Action: sentio.OrderSell,
+						Ratio:  1,
+					}}, nil
+				}
+			}
+		}
+
 		// Close positions before market closed
 		if ok && now.Hour() == 15 && now.Minute() > 55 {
 			return []sentio.StrategyOrder{{