Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

AlpacaBrokerageModel refuses extended-hours limit orders with the GTC time in force, which Alpaca accepts

Aperta Adatta ai principianti
#9,892 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@AlexCatarino ci sta già lavorando.

Dal 12/10/2026.

  • #9893 di @AlexCatarino — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp, postgresql
Ambito
api, backend

Direzione di ricerca

Leggi Common/Brokerages/AlpacaBrokerageModel.cs intorno alle righe 170–176 e i casi corrispondenti in Tests/Common/Brokerages/AlpacaBrokerageModelTests.cs. Aggiorna la regola e i test per accettare ordini limit in orario esteso con Day o GoodTilCanceled, continuando a rifiutare gli altri tipi e GoodTilDate; aggiorna il messaggio in Common/Messages/Messages.Brokerages.cs e la riga OutsideRegularTradingHours in Documentation/Resources/brokerages/alpaca/orders.php. Il lavoro è completato quando i test coprono entrambi i valori di validità accettati e la restrizione documentata corrisponde.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

AlpacaBrokerageModel.CanSubmitOrder refuses every order flagged OutsideRegularTradingHours = true unless it is a Limit order with the Day time in force. Alpaca's Trading API accepts extended_hours: true on limit orders with time_in_force day or gtc (their API reference for POST /v2/orders: "Only works with type limit and time_in_force set to either day or gtc"; the Orders guide: "Only limit orders with time_in_force set to day or gtc orders are accepted as extended hours eligible"). The model is therefore narrower than the brokerage: a GTC extended-hours limit order, which Alpaca would hold across pre-market, regular and after-hours sessions until it fills or is cancelled, is invalidated by Lean before it reaches the brokerage, and the only route an algorithm has is to re-place a Day extended-hours order every session.

Reproduce

Cloud backtest b1cd13d0c2758e53bce69ef4e36c2303 on LEAN 2.5.0.0.18178 (2024-06-03, SPY minute with extended hours, SetBrokerageModel(BrokerageName.Alpaca)), placing two limit orders at the first bar with AlpacaOrderProperties { OutsideRegularTradingHours = true }, one Day and one GoodTilCanceled:

2024-06-03 04:01:00 order event: OrderID: 1 Status: Submitted Quantity: 1 LimitPrice: $461.23
2024-06-03 04:01:00 DAY: ticket 1 status=SUBMITTED error_code=NONE
2024-06-03 04:01:00 order event: OrderID: 2 Status: Invalid Quantity: 1 LimitPrice: $461.23 Message: BrokerageModel declared unable to submit order: [2] Warning - Code: NotSupported - The AlpacaBrokerageModel does not support Limit orders with QuantConnect.Orders.TimeInForces.GoodTilCanceledTimeInForce TIF outside regular hours. Only Limit orders with QuantConnect.Orders.TimeInForces.DayTimeInForce TIF are supported outside regular trading hours.
2024-06-03 04:01:00 GTC: ticket 2 status=INVALID error_code=BROKERAGE_MODEL_REFUSED_TO_SUBMIT_ORDER
from AlgorithmImports import *

class AlpacaExtendedHoursGtcRepro(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2024, 6, 3)
        self.set_end_date(2024, 6, 4)
        self.set_cash(100000)
        self.set_brokerage_model(BrokerageName.ALPACA)
        self._spy = self.add_equity("SPY", Resolution.MINUTE, extended_market_hours=True).symbol
        self._done = False

    def on_data(self, slice):
        if self._done or not slice.contains_key(self._spy):
            return
        self._done = True
        price = self.securities[self._spy].price
        for name, tif in (("DAY", TimeInForce.DAY), ("GTC", TimeInForce.GOOD_TIL_CANCELED)):
            props = AlpacaOrderProperties()
            props.outside_regular_trading_hours = True
            props.time_in_force = tif
            ticket = self.limit_order(self._spy, 1, round(price * 0.9, 2), tag=f"extended-hours {name}", order_properties=props)
            response = ticket.submit_request.response
            self.log(f"{name}: ticket {ticket.order_id} status={ticket.status} error_code={response.error_code if response else None}")

    def on_order_event(self, order_event):
        self.log(f"order event: {order_event}")

Where it happens

  • Common/Brokerages/AlpacaBrokerageModel.cs lines 170-176: if (supportsOutsideTradingHours && (order.Type != OrderType.Limit || order.TimeInForce is not DayTimeInForce)) returns false with Messages.AlpacaBrokerageModel.TradingOutsideRegularHoursNotSupported.
  • Common/Messages/Messages.Brokerages.cs lines 218-222: the message says "Only Limit orders with Day TIF are supported outside regular trading hours."
  • Tests/Common/Brokerages/AlpacaBrokerageModelTests.cs line 47-49: (OrderType.Limit, TimeInForce.GoodTilCanceled, false) with the comment "The only supported case" on the Day row.
  • The brokerage plugin forwards whatever it is given: Lean.Brokerages.Alpaca/QuantConnect.AlpacaBrokerage/AlpacaBrokerageExtensions.cs lines 66-70 (WithDuration(...) then WithExtendedHours(...)), and GoodTilCanceledTimeInForce maps to TimeInForce.Gtc (line 338). Nothing in the plugin needs to change.
  • The docs snippet Documentation/Resources/brokerages/alpaca/orders.php (the OutsideRegularTradingHours row: "Only limit orders with the Day time in force support it") would need the same update.

Proposed change

Accept Limit orders with Day or GoodTilCanceled time in force when OutsideRegularTradingHours is set; keep refusing every other order type and GoodTilDate (Alpaca has no gtd). Update the message and the test case source accordingly.

Open question

Alpaca's gtc extended-hours eligibility covers the overnight session as well. Whether a Lean order with OutsideRegularTradingHours = true should also be considered eligible for the overnight session in the brokerage model's own market-hours checks is a separate question; this issue is only about the time-in-force restriction.

Raised from Intercom conversation 215476336073303.

Lingua principale
C#
Stelle
21.9k
Fork
5.3k
Merge medio
19h 31m
PR unite (30g)
44

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di QuantConnect/Lean

Tutte le issue di QuantConnect/Lean

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.