# 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 datetime import timedeltafrom AlgorithmImports import *### <summary>### Demonstration on how to access order tickets right after placing an order.### </summary>class OrderTicketAssignmentDemoAlgorithm(QCAlgorithm):'''Demonstration on how to access order tickets right after placing an order.'''def initialize(self):self.set_start_date(2013, 10, 7)self.set_end_date(2013, 10, 11)self.set_cash(100000)self._symbol = self.add_equity("SPY").symbolself.trade_count = 0self.consolidate(self._symbol, timedelta(hours=1), self.hour_consolidator)def hour_consolidator(self, bar: TradeBar):# Reset self.ticket to None on each new barself.ticket = Noneself.ticket = self.market_order(self._symbol, 1, asynchronous=True)self.debug(f"{self.time}: Buy: Price {bar.price}, order_id: {self.ticket.order_id}")self.trade_count += 1def on_order_event(self, order_event: OrderEvent):# We cannot access self.ticket directly because it is assigned asynchronously:# this order event could be triggered before self.ticket is assigned.ticket = order_event.ticketif ticket is None:raise AssertionError("Expected order ticket in order event to not be null")if order_event.status == OrderStatus.SUBMITTED and self.ticket is not None:raise AssertionError("Field self.ticket not expected no be assigned on the first order event")self.debug(ticket.to_string())def on_end_of_algorithm(self):# Just checking that orders were placedif not self.portfolio.invested or self.trade_count != self.transactions.orders_count:raise AssertionError(f"Expected the portfolio to have holdings and to have {self.trade_count} trades, but had {self.transactions.orders_count}")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。