Forráskód Böngészése

util.order

- refactoring
Alexey Kim 2 napja
szülő
commit
fcb083015b
2 módosított fájl, 31 hozzáadás és 21 törlés
  1. 19 10
      strategy/alpaca/ppo_20250709/strategy.go
  2. 12 11
      util/order.go

+ 19 - 10
strategy/alpaca/ppo_20250709/strategy.go

@@ -56,7 +56,7 @@ func (strategy qqq) Handle(turn *sentio.Turn, market sentio.Market, rs sentio.Ri
 	)
 
 	// skip too early orders or late orders
-	if (now.Hour() == 9 && now.Minute() < 45) || now.Hour() > 15 {
+	if (now.Hour() == 9 && now.Minute() < 31) || now.Hour() > 15 {
 		return nil
 	}
 
@@ -81,18 +81,27 @@ func (strategy qqq) Handle(turn *sentio.Turn, market sentio.Market, rs sentio.Ri
 	var hasClosedOrders = false
 
 	for i := range orders {
-		if sentio.LONG == orders[i].Intent() &&
-			sentio.Action_SELL == turn.Action {
-			if _, err = market.CloseOrder(orders[i], nil); err != nil {
-				return err
-			}
+		shouldClose := false
 
-			hasClosedOrders = true
-			continue
+		if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.LONG] && sentio.Action_SELL == turn.Action {
+			shouldClose = true
+		}
+
+		if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
+			sentio.LONG == orders[i].Intent() && sentio.Action_SELL == turn.Action {
+			shouldClose = true
+		}
+
+		if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.SHORT] && sentio.Action_BUY == turn.Action {
+			shouldClose = true
+		}
+
+		if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
+			sentio.SHORT == orders[i].Intent() && sentio.Action_BUY == turn.Action {
+			shouldClose = true
 		}
 
-		if sentio.SHORT == orders[i].Intent() &&
-			sentio.Action_BUY == turn.Action {
+		if shouldClose {
 			if _, err = market.CloseOrder(orders[i], nil); err != nil {
 				return err
 			}

+ 12 - 11
util/order.go

@@ -51,28 +51,29 @@ func NewOrder(
 	opts.TakeProfit = ToPtr(sentio.ToFixed(rm.TakeProfit(opts.Symbol, bid), 2))
 	opts.StopLoss = ToPtr(sentio.ToFixed(rm.StopLoss(opts.Symbol, bid), 2))
 
+	// Invert OrderOptions if we will do SHORT for BASE symbol
+	if sentio.SHORT == side && !canLeverage {
+		tmp := opts.StopLoss
+		opts.StopLoss = opts.TakeProfit
+		opts.TakeProfit = tmp
+
+		opts.Action = sentio.Action_SELL
+	}
+
 	// Prevent orders those have too small expected profit
-	if *opts.TakeProfit < bid+TradingRange {
+	if (opts.Action == sentio.Action_BUY && *opts.TakeProfit < bid+TradingRange) ||
+		(opts.Action == sentio.Action_SELL && *opts.TakeProfit > bid+TradingRange) {
 		err = sentio.ErrLowTakeProfit
 		return
 	}
 
 	// Prevent cases when order will be closed ASAP they were opened.
 	// Also, Alpaca requires at least 0.01 gap against base_price
-	if *opts.StopLoss > bid-TradingRange {
+	if opts.Action == sentio.Action_BUY && *opts.StopLoss > bid-TradingRange {
 		err = sentio.ErrHighStoploss
 		return
 	}
 
-	// Invert OrderOptions if we will do SHORT for BASE symbol
-	if sentio.SHORT == side && !canLeverage {
-		tmp := opts.StopLoss
-		opts.StopLoss = opts.TakeProfit
-		opts.TakeProfit = tmp
-
-		opts.Action = sentio.Action_SELL
-	}
-
 	var (
 		account sentio.MarketAccount
 		cash    float64