AlpacaBrokerageModel refuses extended-hours limit orders with the GTC time in force, which Alpaca accepts
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 76/100
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.cslines 170-176:if (supportsOutsideTradingHours && (order.Type != OrderType.Limit || order.TimeInForce is not DayTimeInForce))returnsfalsewithMessages.AlpacaBrokerageModel.TradingOutsideRegularHoursNotSupported.Common/Messages/Messages.Brokerages.cslines 218-222: the message says "Only Limit orders with Day TIF are supported outside regular trading hours."Tests/Common/Brokerages/AlpacaBrokerageModelTests.csline 47-49:(OrderType.Limit, TimeInForce.GoodTilCanceled, false)with the comment "The only supported case" on theDayrow.- The brokerage plugin forwards whatever it is given:
Lean.Brokerages.Alpaca/QuantConnect.AlpacaBrokerage/AlpacaBrokerageExtensions.cslines 66-70 (WithDuration(...)thenWithExtendedHours(...)), andGoodTilCanceledTimeInForcemaps toTimeInForce.Gtc(line 338). Nothing in the plugin needs to change. - The docs snippet
Documentation/Resources/brokerages/alpaca/orders.php(theOutsideRegularTradingHoursrow: "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
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di QuantConnect/Lean
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
QuantConnect/Lean#9889 ·
I maintainer di solito rispondono entro 1 giorno
-
library-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
QuantConnect/Lean#9879 ·
I maintainer di solito rispondono entro 1 giorno
-
[Library Upgrade] autogluonApertalibrary-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
QuantConnect/Lean#9743 ·
I maintainer di solito rispondono entro 1 giorno
-
library-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
QuantConnect/Lean#9500 ·
I maintainer di solito rispondono entro 1 giorno
-
Library Upgrade: dglApertalibrary-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
QuantConnect/Lean#9499 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di QuantConnect/Lean
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
area:re1 bounty help wanted needs-game
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
StackExchange/StackExchange.Redis#3274 ·
I maintainer di solito rispondono entro 1 giorno
-
affected/v17 affected/v18 area/frontend type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
umbraco/Umbraco-CMS#24186 ·
I maintainer di solito rispondono entro 2 giorni