Bläddra i källkod

QQQ10f

- refactoring
Alexey Kim 1 månad sedan
förälder
incheckning
6e7fec33a9
1 ändrade filer med 50 tillägg och 45 borttagningar
  1. 50 45
      strategy/alpaca/qqq10f/strategy.go

+ 50 - 45
strategy/alpaca/qqq10f/strategy.go

@@ -36,7 +36,6 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 	var (
 		portfolio sentio.Portfolio
 		orders    []sentio.StrategyOrder
-		ok        bool
 		err       error
 	)
 
@@ -45,29 +44,37 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 	}
 
 	for side, symbol := range s.PositionSymbols() {
-		if sentio.BASE == side {
-			continue
-		}
-
 		var (
 			position sentio.Position
+			ok       bool
 			t        = market.Time().Now()
 		)
 
+		// no need to trade BASE quote
+		if sentio.BASE == side {
+			continue
+		}
+
+		// skip to early trades
+		if t.Hour() == 13 && t.Minute() < 44 {
+			continue
+		}
+
 		if portfolio != nil {
 			position, ok = portfolio.Get(symbol)
-			ok = ok && position.GetSize() != 0
+			ok = ok && position != nil && position.GetSize() != 0
 		} else {
 			ok = false
 		}
 
 		// Close positions before market closed
-		if ok && t.Hour() == 19 && t.Minute() > 30 {
+		if ok && t.Hour() == 19 && t.Minute() > 55 {
 			orders = append(orders, sentio.StrategyOrder{
 				Symbol: symbol,
 				Action: sentio.OrderSell,
 				Ratio:  1,
 			})
+
 			continue
 		}
 
@@ -84,6 +91,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 			})
 		}
 
+		// Close SHORT position
 		if ok && sentio.SHORT == side && proba > .9995 {
 			orders = append(orders, sentio.StrategyOrder{
 				Symbol: symbol,
@@ -92,54 +100,51 @@ func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.Strateg
 			})
 		}
 
-		if t.Hour() == 19 && t.Minute() > 29 {
+		if t.Hour() == 19 && t.Minute() > 40 {
 			continue
 		}
 
-		if sentio.LONG == side && proba > 1.0009 {
-			if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
-				continue
+		if sentio.LONG == side && proba > 1 {
+			size := float64(0)
+
+			if ok && position.GetAvgPrice() > position.GetCurrentPrice() {
+				// extra position with cheaper price
+				size = .4
+			} else if !ok && proba > 1.0009 {
+				// new trade position
+				size = .6
+			}
+
+			if size > 0 {
+				orders = append(orders, sentio.StrategyOrder{
+					Symbol: symbol,
+					Action: sentio.OrderBuy,
+					Ratio:  size,
+				})
 			}
 
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderBuy,
-				Ratio: func() float64 {
-					if t.Hour() >= 18 {
-						return .3
-					}
-
-					if ok {
-						return .4
-					} else {
-						return .6
-					}
-				}(),
-			})
 			continue
 		}
 
-		if sentio.SHORT == side && proba < .9991 {
-			// prevent extra orders if our position is cheaper
-			if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
-				continue
+		if sentio.SHORT == side && proba < 1 {
+			size := float64(0)
+
+			if ok && position.GetAvgPrice() > position.GetCurrentPrice() {
+				// extra position with cheaper price
+				size = .4
+			} else if !ok && proba < .9991 {
+				// new trade position
+				size = .6
+			}
+
+			if size > 0 {
+				orders = append(orders, sentio.StrategyOrder{
+					Symbol: symbol,
+					Action: sentio.OrderBuy,
+					Ratio:  size,
+				})
 			}
 
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderBuy,
-				Ratio: func() float64 {
-					if t.Hour() >= 18 {
-						return .3
-					}
-
-					if ok {
-						return .4
-					} else {
-						return .6
-					}
-				}(),
-			})
 			continue
 		}
 	}