def on_order_event(self, order_event: OrderEvent) :
if order_event.status == OrderStatus.FILLED :
tag = order_event.ticket.tag
if not tag :
quantity = order_event.fill_quantity
symbol = order_event.symbol.value
self.live_logger(f"on_order_event: no tag for order {quantity} x {symbol}")
else :
self.strategy_positions[tag] += order_event.fill_quantity
My on_order_event handler is throwing this error:
Runtime Error: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the gld_friday - Warning: No quote information available at 12/02/2011 15:58:00 America/New_York, order filled using TradeBar data key exist in the collection and/or that collection is not empty. at on_order_event self.strategy_positions[tag] += order_event.fill_quantity
Normally, the ticket.tag contains a key of self.strategy_positions but on_order_event is overwriting this tag if no quote information is available.
Can this behavior be deactivated somehow?
Mia Alissi
To address the issue David is facing with the
on_order_event
handler, we need to ensure that thetag
from theOrderEvent
is correctly managed and does not lead to aKeyError
when accessingself.strategy_positions
. Here are some steps and code adjustments to help resolve this:Check for Tag Existence: Before attempting to update
self.strategy_positions
, check if thetag
exists in the dictionary. If it doesn't, initialize it to avoid aKeyError
.Initialize Tags: Ensure that all potential tags are initialized in
self.strategy_positions
before they are used.Logging and Debugging: Add logging to capture the state of the
tag
andself.strategy_positions
for debugging purposes.Here's how you can modify the
on_order_event
method:Additional Considerations:
OrderEvent
is overwriting the tag due to missing quote information, ensure that the tag is set correctly when the order is created or updated. You can use theupdate_tag
method of theOrderTicket
to set or update the tag as needed.self.strategy_positions
to help identify any issues with tag management.By implementing these changes, you should be able to prevent the
KeyError
and ensure that your strategy positions are updated correctly based on the order events.Sources: Complete Order Tag Update Algorithm, Update Order Regression Algorithm, Order Events - QuantConnect.com, Time In Force Algorithm, Event Handlers - QuantConnect.com
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
David Maucher
My strategy relies on the tag and it is always existing. However, QuantConnect overwrites my tag in case there is no quote information available.
David Maucher
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!