Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン 初心者向け
#9,892 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@AlexCatarino がすでに取り組んでいます。

2026年10月12日 から。

  • #9893 @AlexCatarino による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp, postgresql
領域
api, backend

調査の方向性

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.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.

主要言語
C#
スター
21.9k
フォーク
5.3k
平均マージ
19時間 31分
マージ済み PR(30日)
44

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

QuantConnect/Lean のほかの issue

QuantConnect/Lean の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。