Selaa lähdekoodia

Strategy

- add Cooldown
Alexey Kim 3 viikkoa sitten
vanhempi
commit
ef0610ea66

+ 5 - 1
strategy.go

@@ -1,6 +1,9 @@
 package sentio
 
-import "cmp"
+import (
+	"cmp"
+	"time"
+)
 
 type Strategy interface {
 	Name() string
@@ -9,6 +12,7 @@ type Strategy interface {
 	MarketId() string
 	PositionSymbols() map[Side]string
 	Interval() uint8
+	Cooldown() time.Duration
 
 	Handle(market Market, proba float64) ([]StrategyOrder, error)
 }

+ 0 - 157
strategy/alpaca/qqq12f/strategy.go

@@ -1,157 +0,0 @@
-package main
-
-import (
-	"git.beejay.kim/Gshopper/sentio"
-)
-
-var Strategy = alpacaQQQ{}
-
-type alpacaQQQ struct{}
-
-func (s alpacaQQQ) Name() string {
-	return "Alpaca: QQQ [TQQQ : SQQQ]"
-}
-
-func (s alpacaQQQ) Model() string {
-	return "qqq12f"
-}
-
-func (s alpacaQQQ) MarketId() string {
-	return "alpaca"
-}
-
-func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
-	return map[sentio.Side]string{
-		sentio.BASE:  "QQQ",
-		sentio.LONG:  "TQQQ",
-		sentio.SHORT: "SQQQ",
-	}
-}
-
-func (s alpacaQQQ) Interval() uint8 {
-	return 15
-}
-
-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
-		err       error
-	)
-
-	if portfolio, err = market.Portfolio(); err != nil {
-		return nil, err
-	}
-
-	for side, symbol := range s.PositionSymbols() {
-		var (
-			position sentio.Position
-			ok       bool
-			t        = market.Time().Now()
-		)
-
-		// no need to trade BASE quote
-		if sentio.BASE == side {
-			continue
-		}
-
-		// skip too early trades
-		if t.Hour() < 14 {
-			continue
-		}
-
-		if portfolio != nil {
-			position, ok = portfolio.Get(symbol)
-			ok = ok && position != nil && 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 {
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderSell,
-				Ratio:  1,
-			})
-		}
-
-		// Close SHORT position
-		if ok && sentio.SHORT == side && proba > .9999 {
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderSell,
-				Ratio:  1,
-			})
-		}
-
-		if t.Hour() == 19 && t.Minute() > 35 {
-			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.0005 {
-				// new trade position
-				size = .6
-			}
-
-			if size > 0 {
-				orders = append(orders, sentio.StrategyOrder{
-					Symbol: symbol,
-					Action: sentio.OrderBuy,
-					Ratio:  size,
-				})
-			}
-
-			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,
-				})
-			}
-
-			continue
-		}
-	}
-
-	return orders, nil
-}

+ 0 - 157
strategy/alpaca/qqq13/strategy.go

@@ -1,157 +0,0 @@
-package main
-
-import (
-	"git.beejay.kim/Gshopper/sentio"
-)
-
-var Strategy = alpacaQQQ{}
-
-type alpacaQQQ struct{}
-
-func (s alpacaQQQ) Name() string {
-	return "Alpaca: QQQ [TQQQ : SQQQ]"
-}
-
-func (s alpacaQQQ) Model() string {
-	return "qqq13"
-}
-
-func (s alpacaQQQ) MarketId() string {
-	return "alpaca"
-}
-
-func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
-	return map[sentio.Side]string{
-		sentio.BASE:  "QQQ",
-		sentio.LONG:  "TQQQ",
-		sentio.SHORT: "SQQQ",
-	}
-}
-
-func (s alpacaQQQ) Interval() uint8 {
-	return 5
-}
-
-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
-		err       error
-	)
-
-	if portfolio, err = market.Portfolio(); err != nil {
-		return nil, err
-	}
-
-	for side, symbol := range s.PositionSymbols() {
-		var (
-			position sentio.Position
-			ok       bool
-			t        = market.Time().Now()
-		)
-
-		// no need to trade BASE quote
-		if sentio.BASE == side {
-			continue
-		}
-
-		// skip too early trades
-		if t.Hour() < 15 {
-			continue
-		}
-
-		if portfolio != nil {
-			position, ok = portfolio.Get(symbol)
-			ok = ok && position != nil && position.GetSize() != 0
-		} else {
-			ok = false
-		}
-
-		// Close positions before market closed
-		if ok && t.Hour() == 20 && 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 {
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderSell,
-				Ratio:  1,
-			})
-		}
-
-		// Close SHORT position
-		if ok && sentio.SHORT == side && proba > .9999 {
-			orders = append(orders, sentio.StrategyOrder{
-				Symbol: symbol,
-				Action: sentio.OrderSell,
-				Ratio:  1,
-			})
-		}
-
-		if t.Hour() == 20 && t.Minute() > 35 {
-			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.0007 {
-				// new trade position
-				size = .6
-			}
-
-			if size > 0 {
-				orders = append(orders, sentio.StrategyOrder{
-					Symbol: symbol,
-					Action: sentio.OrderBuy,
-					Ratio:  size,
-				})
-			}
-
-			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,
-				})
-			}
-
-			continue
-		}
-	}
-
-	return orders, nil
-}

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

@@ -2,6 +2,7 @@ package main
 
 import (
 	"git.beejay.kim/Gshopper/sentio"
+	"time"
 )
 
 var Strategy = alpacaQQQ{}
@@ -32,6 +33,10 @@ func (s alpacaQQQ) Interval() uint8 {
 	return 5
 }
 
+func (s alpacaQQQ) Cooldown() time.Duration {
+	return time.Minute * time.Duration(s.Interval()*6)
+}
+
 func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
 	var (
 		portfolio sentio.Portfolio