Hello,
In the algorithm attached I sell an index option at close and attach a stopMarket order to act as a stop loss. I noticed high/low bar data issues so I wanted to make a custom fill model to key off the close price instead of high/low prices. I believe I set the fill model correctly but I see no debugging logs indicating the custom model is never used. Please let me know if I am missing something. The attached algorithm shows a stopMarket order being triggered at a high price ($29) because the ask high data is not correct but the lower close price of $0.10 should have resulted in no fill being triggered.
Thank you!
Jesse
Mia Alissi
Reviewing Jesse's Python code for bugs, here are the identified issues:
Custom Fill Model Implementation: The custom fill model
MyFillModelis correctly defined but not correctly set for the option contract. The methodSetFillModelis used onself.activeOption, which is returned byAddOptionContract. However,AddOptionContractreturns aSecurityobject, not anOptionobject. TheSetFillModelmethod should be called on theOptionobject itself, not on the returned value fromAddOptionContract. This might be why the custom fill model seems not to be used.Time Check in OnData: The time check
if self.Time.hour == 16 and self.Time.minute == 0:is used to execute code at a specific time. However, this approach might not be reliable due to the possibility of data arriving slightly before or after the exact minute. It's better to use scheduled events for precise timing.Stop Market Order Quantity: In the line
self.stopMarketOrderTicket = self.StopMarketOrder(self.activeOption.Symbol, 1, 29, tag="StopMarket Order"), the quantity is set to1for the stop market order. If the intention is to close the position opened by the market order, the quantity should match but with the opposite sign. If the market order is for selling (-1), the stop market order should be for buying (1), which is correct in this context. However, if the position size changes, this needs to be adjusted accordingly.Lack of Order Event Handling Logic: In the
OnOrderEventmethod, there's no implementation provided. This method should include logic to handle different order events, such as logging or adjusting strategy based on order fills, partial fills, cancellations, etc.Use of
datetime.min: While not directly a bug in the provided code, usingdatetime.minas a placeholder forself.entryTimeandself.exitTimemight lead to confusion or errors in date comparisons later in the algorithm. It's usually better to useNonefor uninitialized dates/times and check forNonebefore performing date/time operations.Error in Research Notebook: In the research notebook (
research.ipynb), there's an inconsistency in the dates used forstart_timeandend_timevariables. Theend_timeis set to a date before thestart_time, which would result in an error or empty DataFrame when querying historical data. This is not directly related to the algorithm but could affect the analysis or testing of the strategy.Missing Logic for Active Option Management: The code snippet does not show how the algorithm manages or tracks the active option (
self.activeOption) beyond placing the initial market and stop market orders. For a complete strategy, there should be logic to handle the expiration of the option, any assignment or exercise events, and possibly selecting a new option to trade as the strategy progresses.These points highlight areas in the code that may need attention or correction to ensure the algorithm functions as intended, especially regarding the custom fill model's application and the precise timing of actions within the
OnDatamethod.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.
Jesse Limtiaco
Calling SetFillModel on the option object itself produced the same results. The custom fill model is still not being used.
Mia Alissi
After reviewing Jesse's updated Python code and considering the issue with the custom fill model still not being used, here are some potential issues and considerations:
Custom Fill Model Method Signature: The custom fill model
MyFillModelcorrectly overrides theStopMarketFillmethod. However, it's important to ensure that the method signature exactly matches what the LEAN engine expects. The method signature seems correct, but it's crucial to verify against the latest documentation or source code, as discrepancies can lead to the method not being called.Setting the Fill Model on the Correct Security Object: Jesse mentioned that calling
SetFillModelon the option object itself produced the same results. It's important to ensure that the fill model is set on the correct security object. In the provided code, the fill model is set on the option chain object (self.option) before any contracts are added. This is correct, but it's also necessary to ensure that the fill model is applied to the specific option contract that is being traded. In theOnDatamethod, after selecting the specific option contract (self.activeOption = self.AddOptionContract(option.Symbol)), it might be necessary to explicitly set the fill model again for this specific contract if the LEAN engine does not automatically inherit the fill model from the option chain.Verification of Order Types: Ensure that the custom fill model's
StopMarketFillmethod is indeed applicable to the types of orders being placed. The method name suggests it is specifically for stop market orders. If the orders being placed are of a different type, or if there's any mismatch in how orders are being identified and processed, the custom fill model might not be invoked as expected.Debugging and Logging: To diagnose why the custom fill model is not being used, add logging statements within the
StopMarketFillmethod to confirm whether it's being called. This can help identify if the issue is with the method not being invoked or if the logic within the method is not executing as expected.Review LEAN Engine Updates: If the issue persists despite the above considerations, review any recent updates or changes to the LEAN engine that might affect how custom fill models are applied or invoked. The QuantConnect forum and GitHub repository might have relevant discussions or documentation on this topic.
Consult QuantConnect Support: If all else fails and the custom fill model still does not seem to be used, consider reaching out to QuantConnect support or the community forum with detailed information about the issue. The QuantConnect community and support team can provide specific advice based on the latest platform capabilities and best practices.
Given the information provided, these are general suggestions to troubleshoot the issue. Without access to the full context of how the LEAN engine is currently handling custom fill models, these are starting points for further investigation.
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.
Jesse Limtiaco
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!