Преглед на файлове

QQQ15

- adjust probas
- enhance ATR trailing stoploss policy
Alexey Kim преди 1 седмица
родител
ревизия
212605921f
променени са 2 файла, в които са добавени 47 реда и са изтрити 45 реда
  1. 24 23
      strategy/alpaca/qqq15/strategy.go
  2. 23 22
      strategy/alpaca/qqq15_nodelay/strategy.go

+ 24 - 23
strategy/alpaca/qqq15/strategy.go

@@ -8,9 +8,12 @@ import (
 )
 
 const (
-	ATR_MULTIPLIER = 1.5
-	ATR_PERIOD     = 14
-	ATR_HHV        = 8
+	ATR_MULTIPLIER         = 1.5
+	ATR_PERIOD             = 14
+	ATR_HHV                = 10
+	ATR_STOPLOSS_THRESHOLD = 1
+
+	EXTRA_POSITION_THRESHOLD = 1.002
 )
 
 var Strategy = alpacaQQQ{}
@@ -64,11 +67,12 @@ 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
+			position   sentio.Position
+			quote      *sentio.Quote
+			bars       []sentio.Bar
+			stoploss   float64
+			uptrending bool
+			ok         bool
 		)
 
 		// no need to trade BASE quote
@@ -92,8 +96,10 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			}
 
 			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)
+			stoploss = trailing[len(trailing)-1]
+			uptrending = trailing[len(trailing)-1] > trailing[len(trailing)-2]
+
+			fmt.Printf("ATR Trailing StopLoss: [%s: %f; uptrading: %v]\n", symbol, stoploss, uptrending)
 		}
 
 		if portfolio != nil {
@@ -113,7 +119,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 		}
 
 		// Close position if BidPrice less than StopLoss
-		if ok && quote.BidPrice < sl {
+		if ok && !uptrending && quote.BidPrice/stoploss < ATR_STOPLOSS_THRESHOLD {
 			return []sentio.StrategyOrder{{
 				Symbol: symbol,
 				Action: sentio.OrderSell,
@@ -144,7 +150,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 		}
 
 		// Prevent BUYs for delayed probas
-		if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now) {
+		if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now.Round(time.Minute)) {
 			continue
 		}
 
@@ -153,20 +159,15 @@ 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)
 
-			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
+			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
 				// extra position with cheaper price
-				size = .4
+				size = .5
 			} else if !ok && proba > 1.0008 {
 				// new trade position
-				size = .6
+				size = .7
 			}
 
 			if size > 0 {
@@ -183,12 +184,12 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 		if sentio.SHORT == side && proba < 1 {
 			size := float64(0)
 
-			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
+			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
 				// extra position with cheaper price
-				size = .4
+				size = .5
 			} else if !ok && proba < .9990 {
 				// new trade position
-				size = .6
+				size = .7
 			}
 
 			if size > 0 {

+ 23 - 22
strategy/alpaca/qqq15_nodelay/strategy.go

@@ -8,9 +8,12 @@ import (
 )
 
 const (
-	ATR_MULTIPLIER = 1.5
-	ATR_PERIOD     = 14
-	ATR_HHV        = 8
+	ATR_MULTIPLIER         = 1.5
+	ATR_PERIOD             = 14
+	ATR_HHV                = 10
+	ATR_STOPLOSS_THRESHOLD = 1
+
+	EXTRA_POSITION_THRESHOLD = 1.002
 )
 
 var Strategy = alpacaQQQ{}
@@ -64,11 +67,12 @@ 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
+			position   sentio.Position
+			quote      *sentio.Quote
+			bars       []sentio.Bar
+			stoploss   float64
+			uptrending bool
+			ok         bool
 		)
 
 		// no need to trade BASE quote
@@ -92,8 +96,10 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 			}
 
 			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)
+			stoploss = trailing[len(trailing)-1]
+			uptrending = trailing[len(trailing)-1] > trailing[len(trailing)-2]
+
+			fmt.Printf("ATR Trailing StopLoss: [%s: %f; uptrading: %v]\n", symbol, stoploss, uptrending)
 		}
 
 		if portfolio != nil {
@@ -113,7 +119,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 		}
 
 		// Close position if BidPrice less than StopLoss
-		if ok && quote.BidPrice < sl {
+		if ok && !uptrending && quote.BidPrice/stoploss < ATR_STOPLOSS_THRESHOLD {
 			return []sentio.StrategyOrder{{
 				Symbol: symbol,
 				Action: sentio.OrderSell,
@@ -148,20 +154,15 @@ 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)
 
-			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
+			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
 				// extra position with cheaper price
-				size = .4
+				size = .5
 			} else if !ok && proba > 1.0008 {
 				// new trade position
-				size = .6
+				size = .7
 			}
 
 			if size > 0 {
@@ -178,12 +179,12 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
 		if sentio.SHORT == side && proba < 1 {
 			size := float64(0)
 
-			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
+			if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
 				// extra position with cheaper price
-				size = .4
+				size = .5
 			} else if !ok && proba < .9990 {
 				// new trade position
-				size = .6
+				size = .7
 			}
 
 			if size > 0 {