Browse Source

Strategy

- qqq001: revert back
- qqq002: adjust proba and time
- qqq400: remove ibkr related legacy
Alexey Kim 5 months ago
parent
commit
81c6866bee
3 changed files with 137 additions and 141 deletions
  1. 136 0
      strategy/alpaca/qqq15/strategy.go
  2. 1 1
      strategy/alpaca/qqq30/strategy.go
  3. 0 140
      strategy/ibkr/qqq/qqq.go

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

@@ -0,0 +1,136 @@
+package main
+
+import (
+	"git.beejay.kim/Gshopper/sentio"
+)
+
+var Strategy = alpacaQQQ{}
+
+type alpacaQQQ struct{}
+
+func (s alpacaQQQ) Name() string {
+	return "Alpaca: QQQ / SQQQ"
+}
+
+func (s alpacaQQQ) Model() string {
+	return "qqq002"
+}
+
+func (s alpacaQQQ) MarketId() string {
+	return "alpaca"
+}
+
+func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
+	return map[sentio.Side]string{
+		sentio.LONG:  "QQQ",
+		sentio.SHORT: "SQQQ",
+	}
+}
+
+func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
+	if !market.IsMarketOpened() {
+		return nil, sentio.ErrMarketClosed
+	}
+
+	var (
+		portfolio sentio.Portfolio
+		orders    []sentio.StrategyOrder
+		ok        bool
+		err       error
+	)
+
+	if portfolio, err = market.Portfolio(); err != nil {
+		return nil, err
+	}
+
+	for side, symbol := range s.PositionSymbols() {
+		var (
+			position sentio.Position
+			t        = market.Time().Now()
+		)
+
+		if portfolio != nil {
+			position, ok = portfolio.Get(symbol)
+			ok = ok && position.GetSize() != 0
+		} else {
+			ok = false
+		}
+
+		// Close positions before market closed
+		if ok && t.Hour() >= 19 && t.Minute() >= 45 {
+			orders = append(orders, sentio.StrategyOrder{
+				Symbol: symbol,
+				Action: sentio.OrderSell,
+				Ratio:  1,
+			})
+			continue
+		}
+
+		if proba < 0 {
+			continue
+		}
+
+		// Close LONG position
+		if ok && sentio.LONG == side && proba < 1.0006 {
+			orders = append(orders, sentio.StrategyOrder{
+				Symbol: symbol,
+				Action: sentio.OrderSell,
+				Ratio:  1,
+			})
+			continue
+		}
+
+		if ok && sentio.SHORT == side && proba > .999 {
+			orders = append(orders, sentio.StrategyOrder{
+				Symbol: symbol,
+				Action: sentio.OrderSell,
+				Ratio:  1,
+			})
+			continue
+		}
+
+		if t.Hour() == 19 && t.Minute() >= 45 {
+			continue
+		}
+
+		if sentio.LONG == side && proba > 1.001 {
+			if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
+				continue
+			}
+
+			orders = append(orders, sentio.StrategyOrder{
+				Symbol: symbol,
+				Action: sentio.OrderBuy,
+				Ratio: func() float64 {
+					if ok {
+						return .3
+					} else {
+						return .6
+					}
+				}(),
+			})
+			continue
+		}
+
+		if sentio.SHORT == side && proba < .9998 {
+			if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
+				continue
+			}
+
+			orders = append(orders, sentio.StrategyOrder{
+				Symbol: symbol,
+				Action: sentio.OrderBuy,
+				Ratio: func() float64 {
+					if ok {
+						return .3
+					} else {
+						return .8
+					}
+				}(),
+			})
+			continue
+		}
+	}
+
+	return orders, nil
+}

+ 1 - 1
strategy/alpaca/qqq/strategy.go → strategy/alpaca/qqq30/strategy.go

@@ -13,7 +13,7 @@ func (s alpacaQQQ) Name() string {
 }
 
 func (s alpacaQQQ) Model() string {
-	return "qqq002"
+	return "qqq001"
 }
 
 func (s alpacaQQQ) MarketId() string {

+ 0 - 140
strategy/ibkr/qqq/qqq.go

@@ -1,140 +0,0 @@
-package main
-
-import (
-	"git.beejay.kim/Gshopper/sentio"
-)
-
-var Strategy = _ib_qqq{
-	clock: sentio.ClockUTC{},
-}
-
-type _ib_qqq struct {
-	clock sentio.Clock
-}
-
-func (strategy _ib_qqq) Name() string {
-	return "IBKR: QQQ"
-}
-
-func (strategy _ib_qqq) Model() string {
-	return "qqq400"
-}
-
-func (strategy _ib_qqq) MarketId() string {
-	return "ibkr"
-}
-
-func (strategy _ib_qqq) PositionSymbols() map[sentio.Side]string {
-	return map[sentio.Side]string{
-		sentio.LONG: "320227571", // QQQ@NASDAQ
-	}
-}
-
-func (strategy _ib_qqq) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
-	if !market.IsMarketOpened() {
-		return nil, sentio.ErrMarketClosed
-	}
-
-	var (
-		utc       = strategy.clock.Now()
-		portfolio sentio.Portfolio
-		err       error
-	)
-
-	if portfolio, err = market.Portfolio(); err != nil {
-		return nil, err
-	}
-
-	for _, symbol := range strategy.PositionSymbols() {
-		var (
-			position sentio.Position
-			ok       bool
-		)
-
-		if portfolio != nil {
-			position, ok = portfolio.Get(symbol)
-			ok = ok && position.GetSize() != 0
-		}
-
-		// Close positions before market close
-		if ok && utc.Hour() >= 19 && utc.Minute() >= 30 {
-			return []sentio.StrategyOrder{
-				{
-					Symbol: position.GetSymbol(),
-					Action: func() sentio.OrderAction {
-						if position.GetSize() > 0 {
-							return sentio.OrderSell
-						} else {
-							return sentio.OrderBuy
-						}
-					}(),
-					Ratio: 1,
-				},
-			}, nil
-		}
-
-		if proba < 0 {
-			continue
-		}
-
-		// Close LONG position
-		if ok && position.GetSize() > 0 && proba < 1 {
-			return []sentio.StrategyOrder{
-				{
-					Symbol: position.GetSymbol(),
-					Action: sentio.OrderSell,
-					Ratio:  1,
-				},
-			}, nil
-		}
-
-		// Close SHORT position
-		if ok && position.GetSize() < 0 && proba > 1 {
-			return []sentio.StrategyOrder{
-				{
-					Symbol: position.GetSymbol(),
-					Action: sentio.OrderBuy,
-					Ratio:  1,
-				},
-			}, nil
-		}
-
-		if utc.Hour() == 19 && utc.Minute() >= 30 {
-			continue
-		}
-
-		if proba > 1.002 {
-			return []sentio.StrategyOrder{
-				{
-					Symbol: symbol,
-					Action: sentio.OrderBuy,
-					Ratio: func() float64 {
-						if ok {
-							return .3
-						} else {
-							return .6
-						}
-					}(),
-				},
-			}, nil
-		}
-
-		if proba < .998 {
-			return []sentio.StrategyOrder{
-				{
-					Symbol: symbol,
-					Action: sentio.OrderSell,
-					Ratio: func() float64 {
-						if ok {
-							return .3
-						} else {
-							return .6
-						}
-					}(),
-				},
-			}, nil
-		}
-	}
-
-	return nil, nil
-}