AlpacaBrokerageModel refuses extended-hours limit orders with the GTC time in force, which Alpaca accepts
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
調査の方向性
Common/Brokerages/AlpacaBrokerageModel.csの170~176行付近と、Tests/Common/Brokerages/AlpacaBrokerageModelTests.csの対応するケースを確認してください。ルールとテストを更新し、延長取引時間中の指値注文ではDayまたはGoodTilCanceledを受け付け、その他の注文タイプとGoodTilDateは引き続き拒否するようにしてください。Common/Messages/Messages.Brokerages.csのメッセージと、Documentation/Resources/brokerages/alpaca/orders.phpのOutsideRegularTradingHours行も更新してください。テストが受け付ける両方の有効期間値をカバーし、文書化された制限が一致したら完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C#
- スター
- 21.9k
- フォーク
- 5.3k
- 平均マージ
- 19時間 31分
- マージ済み PR(30日)
- 44
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
QuantConnect/Lean のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
QuantConnect/Lean#9889 ·
メンテナーはふだん 1 日以内に返信
-
library-request
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
QuantConnect/Lean#9879 ·
メンテナーはふだん 1 日以内に返信
-
library-request
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
QuantConnect/Lean#9743 ·
メンテナーはふだん 1 日以内に返信
-
library-request
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
QuantConnect/Lean#9500 ·
メンテナーはふだん 1 日以内に返信
-
Library Upgrade: dglオープンlibrary-request
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
QuantConnect/Lean#9499 ·
メンテナーはふだん 1 日以内に返信
QuantConnect/Lean の issue をすべて見る
似ている issue
-
documentation
難易度 1/5 1〜3時間 初心者へのやさしさ 62/100
-
bug misleading tooling trivial
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
Docs example projects pinned to a literal library version instead of 3.*対応中かも @DaveSkender が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
facioquo/stock-indicators-dotnet#2372 ·
メンテナーはふだん 1 日以内に返信
-
[Docs] officecli-docx SKILL.md still describes `\n` as a soft line break after #262対応中かも @giprime が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
Facepunch/sbox-public#12097 ·
メンテナーはふだん 2 日以内に返信