# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.from AlgorithmImports import *### <summary>### Demonstration algorithm of time in force order settings.### </summary>### <meta name="tag" content="using data" />### <meta name="tag" content="using quantconnect" />### <meta name="tag" content="trading and orders" />class TimeInForceAlgorithm(QCAlgorithm):# Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.def initialize(self):self.set_start_date(2013,10,7)self.set_end_date(2013,10,11)self.set_cash(100000)# The default time in force setting for all orders is GoodTilCancelled (GTC),# uncomment this line to set a different time in force.# We currently only support GTC and DAY.# self.default_order_properties.time_in_force = TimeInForce.dayself._symbol = self.add_equity("SPY", Resolution.MINUTE).symbolself._gtc_order_ticket1 = Noneself._gtc_order_ticket2 = Noneself._day_order_ticket1 = Noneself._day_order_ticket2 = Noneself._gtd_order_ticket1 = Noneself._gtd_order_ticket2 = Noneself._expected_order_statuses = {}# OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.# Arguments:# data: Slice object keyed by symbol containing the stock datadef on_data(self, data):if not self._gtc_order_ticket1:# These GTC orders will never expire and will not be canceled automatically.self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_CANCELED# this order will not be filled before the end of the backtestself._gtc_order_ticket1 = self.limit_order(self._symbol, 10, 100)self._expected_order_statuses[self._gtc_order_ticket1.order_id] = OrderStatus.SUBMITTED# this order will be filled before the end of the backtestself._gtc_order_ticket2 = self.limit_order(self._symbol, 10, 160)self._expected_order_statuses[self._gtc_order_ticket2.order_id] = OrderStatus.FILLEDif not self._day_order_ticket1:# These DAY orders will expire at market close,# if not filled by then they will be canceled automatically.self.default_order_properties.time_in_force = TimeInForce.DAY# this order will not be filled before market close and will be canceledself._day_order_ticket1 = self.limit_order(self._symbol, 10, 140)self._expected_order_statuses[self._day_order_ticket1.order_id] = OrderStatus.CANCELED# this order will be filled before market closeself._day_order_ticket2 = self.limit_order(self._symbol, 10, 180)self._expected_order_statuses[self._day_order_ticket2.order_id] = OrderStatus.FILLEDif not self._gtd_order_ticket1:# These GTD orders will expire on October 10th at market close,# if not filled by then they will be canceled automatically.self.default_order_properties.time_in_force = TimeInForce.GOOD_TIL_DATE(datetime(2013, 10, 10))# this order will not be filled before expiry and will be canceledself._gtd_order_ticket1 = self.limit_order(self._symbol, 10, 100)self._expected_order_statuses[self._gtd_order_ticket1.order_id] = OrderStatus.CANCELED# this order will be filled before expiryself._gtd_order_ticket2 = self.limit_order(self._symbol, 10, 160)self._expected_order_statuses[self._gtd_order_ticket2.order_id] = OrderStatus.FILLED# Order event handler. This handler will be called for all order events, including submissions, fills, cancellations.# This method can be called asynchronously, ensure you use proper locks on thread-unsafe objectsdef on_order_event(self, orderEvent):self.debug(f"{self.time}{orderEvent}")# End of algorithm run event handler. This method is called at the end of a backtest or live trading operation.def on_end_of_algorithm(self):for orderId, expectedStatus in self._expected_order_statuses.items():order = self.transactions.get_order_by_id(orderId)if order.status != expectedStatus:raise AssertionError(f"Invalid status for order {orderId} - Expected: {expectedStatus}, actual: {order.status}")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。