Selaa lähdekoodia

QQQ15

- add ATR trailing stop loss
Alexey Kim 2 viikkoa sitten
vanhempi
commit
2c50b52cfc
2 muutettua tiedostoa jossa 83 lisäystä ja 31 poistoa
  1. 38 31
      strategy/alpaca/qqq15/strategy.go
  2. 45 0
      strategy/alpaca/qqq15_nodelay/strategy.go

+ 38 - 31
strategy/alpaca/qqq15/strategy.go

@@ -10,7 +10,7 @@ import (
 const (
 	ATR_MULTIPLIER = 1.5
 	ATR_PERIOD     = 14
-	ATR_HHV        = 10
+	ATR_HHV        = 8
 )
 
 var Strategy = alpacaQQQ{}
@@ -65,6 +65,9 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 	for side, symbol := range s.PositionSymbols() {
 		var (
 			position sentio.Position
+			quote    sentio.Quote
+			bars     []sentio.Bar
+			sl       float64
 			ok       bool
 		)
 
@@ -73,6 +76,26 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			continue
 		}
 
+		if quote, err = market.Quote(symbol); err != nil {
+			return nil, err
+		}
+
+		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
+			}
+
+			trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
+			sl = trailing[len(trailing)-1]
+			fmt.Printf("ATR Trailing StopLoss: [%f]\n", sl)
+		}
+
 		if portfolio != nil {
 			position, ok = portfolio.Get(symbol)
 			ok = ok && position != nil && position.GetSize() != 0
@@ -80,36 +103,6 @@ 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{{
@@ -119,6 +112,15 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			}}, nil
 		}
 
+		// Close position if BidPrice less than StopLoss
+		if ok && quote.BidPrice < sl {
+			return []sentio.StrategyOrder{{
+				Symbol: symbol,
+				Action: sentio.OrderSell,
+				Ratio:  1,
+			}}, nil
+		}
+
 		if proba < 0 {
 			continue
 		}
@@ -151,6 +153,11 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			continue
 		}
 
+		// Prevent BUYs if BID price less than StopLoss
+		if quote.BidPrice < sl {
+			continue
+		}
+
 		if sentio.LONG == side && proba > 1 {
 			size := float64(0)
 

+ 45 - 0
strategy/alpaca/qqq15_nodelay/strategy.go

@@ -1,10 +1,18 @@
 package main
 
 import (
+	"fmt"
 	"git.beejay.kim/Gshopper/sentio"
+	"git.beejay.kim/Gshopper/sentio/indicator"
 	"time"
 )
 
+const (
+	ATR_MULTIPLIER = 1.5
+	ATR_PERIOD     = 14
+	ATR_HHV        = 8
+)
+
 var Strategy = alpacaQQQ{}
 
 type alpacaQQQ struct{}
@@ -57,6 +65,9 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 	for side, symbol := range s.PositionSymbols() {
 		var (
 			position sentio.Position
+			quote    sentio.Quote
+			bars     []sentio.Bar
+			sl       float64
 			ok       bool
 		)
 
@@ -65,6 +76,26 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			continue
 		}
 
+		if quote, err = market.Quote(symbol); err != nil {
+			return nil, err
+		}
+
+		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
+			}
+
+			trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
+			sl = trailing[len(trailing)-1]
+			fmt.Printf("ATR Trailing StopLoss: [%f]\n", sl)
+		}
+
 		if portfolio != nil {
 			position, ok = portfolio.Get(symbol)
 			ok = ok && position != nil && position.GetSize() != 0
@@ -81,6 +112,15 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			}}, nil
 		}
 
+		// Close position if BidPrice less than StopLoss
+		if ok && quote.BidPrice < sl {
+			return []sentio.StrategyOrder{{
+				Symbol: symbol,
+				Action: sentio.OrderSell,
+				Ratio:  1,
+			}}, nil
+		}
+
 		if proba < 0 {
 			continue
 		}
@@ -108,6 +148,11 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			continue
 		}
 
+		// Prevent BUYs if BID price less than StopLoss
+		if quote.BidPrice < sl {
+			continue
+		}
+
 		if sentio.LONG == side && proba > 1 {
 			size := float64(0)