Hello,
Noob question here. I am doing a backtest on US SPW index options and I noticed my stop orders kept getting filled at seemingly crazy prices. I dug a little deeper and pulled the data and noticed several instances of the minutely data where the ask high/low or bid high/low did not make any sense to me. The data seems to be rolled up from tick data provided by algoseek, so I cannot say for sure whether this happens a lot but as a trader of several years, this doesn't make sense to me. Below is an example of what I am describing. There are many more examples of this, even within the same trading day and with other option securities. In the below example, the ask high is 30.00, the ask low is 0.20 and the ask close is .25. This is the first minute bar after the open, but this also occurs throughout the day. This is impossible to me because even if the ask high is 30, the bid high is 0.15. There is no way that this relatively liquid market was 0.15 x 30.00 at best bid and offer, the arbitrageurs would have nipped that before it ever got that far. This causes my stop buy orders to fill at any price on this bar up to a stop price of $30 in this example.
Since quantconnect just pulls this data from algoseek and cleaning all of it would be beyong my scope, my question is about working around this. Is there a way to write some logic into onData that examines this and stops the backtester from filling at $30? Perhaps a way to tell the backtester to only execute using bid or ask close? Or to scan the high/low numbers and compare them to the opposite side and override the high/low numbers to something reasonable like one tick over/under the opposite high/low?
Thank you!
striketypesymboltimeaskcloseaskhighasklowaskopenbidhighbidlowbidopenbidsizeclosehighlowopenvolume3630PutSPXW 31YNZ3KMVDSLQ|SPX 316/23/2022 8:310.25300.20.250.150.10.151720.20.250.20.259
Mia Alissi
To address Jesse's concern about stop orders getting filled at seemingly unrealistic prices due to extreme bid/ask spreads in the data, you can implement custom logic in your algorithm to filter or adjust these prices before placing orders. Here's a basic approach to handle this situation by examining the bid/ask spread and adjusting the order logic accordingly. This example focuses on limiting the execution of stop orders based on a more reasonable ask price.
This example demonstrates how to check the spread between the ask high and bid low prices before placing a stop order. If the spread is too large (beyond a specified threshold), the algorithm refrains from placing the order to avoid unrealistic fills. You can adjust the
maxSpreadPercentage
to a value that you consider reasonable based on the liquidity and volatility of the market you're trading.Remember, this is a simplified example. Depending on your specific strategy and requirements, you might need to refine this logic further, for instance, by considering the bid/ask close prices or implementing more sophisticated filters to identify and handle outliers in the data.
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
Thanks,
This seems like a reasonable idea but it doesn't address the underlying problem of the data. I still want to place orders as opposed to waiting for a backward looking data filter to allow it. Even if the filter tells me that I can place the order, there is no guarantee that the next bar does not contain the same data issue. If there isn't a way to only use close prices for filling bars or to override the high/low data I don't think that the backtester can be used with any confidence until the data is cleaned.
Mia Alissi
If Jesse is looking for a way to ensure that orders are filled based on more reliable data points, such as the close prices of bid/ask, to mitigate the impact of potentially erroneous high/low data points, one approach could be to customize the order fill model. QuantConnect allows you to create custom fill models by inheriting from the existing fill models and overriding their behavior. This way, Jesse can implement logic to fill orders based on the close prices or any other criteria deemed more reliable.
Here's an example of how to create a custom fill model that uses the ask close price for buy orders and the bid close price for sell orders. This example assumes you're working with equity data, but you can adapt it for options with similar logic.
In this example, the
CustomFillModel
class overrides theMarketFill
method to customize the market order fill logic. It uses the close price of the last data slice for both buy and sell orders. You can adjust the logic insideMarketFill
to use different prices (e.g., ask close or bid close) based on the order direction and your specific needs.This approach allows Jesse to have more control over the prices at which orders are filled, potentially avoiding unrealistic fills due to erroneous high/low data points. However, it's important to note that using only close prices for order fills may not fully reflect real market conditions, especially in fast-moving markets or for assets with wide bid-ask spreads.
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
Is it possible to change the stopmarketorder logic so that the stop triggers based on the close price of a bar instead of the high/low bar data?
Mia Alissi
Yes, it's possible to customize the logic of how a
StopMarketOrder
is triggered in QuantConnect by creating a custom fill model. In the standard behavior, aStopMarketOrder
is triggered when the high price of a bar surpasses the stop price for a buy order, or when the low price of a bar goes below the stop price for a sell order. To change this so that the stop triggers based on the close price of a bar, you would need to implement a custom fill model that overrides the default behavior.Below is an example of how you could implement such a custom fill model. This example assumes you're working with equity data, but the concept can be adapted for other asset classes.
In this custom fill model, the
StopMarketFill
method checks if the close price of the last data point triggers the stop condition for the order. If it does, it creates an order event to simulate the order being filled at the close price.Please note, this is a simplified example to illustrate the concept. Depending on your specific requirements, you might need to add additional logic, such as handling partial fills, slippage, or modeling the fill price more accurately based on available market data.
Also, it's important to understand that modifying the fill logic in this way can lead to backtest results that may not accurately reflect real-world trading conditions, as actual stop market orders can be triggered intrabar in live trading.
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
Can you should me how to implement this logic for index options? I tried to do it and the custom fill model is not being used
Mia Alissi
Implementing custom fill logic for index options in QuantConnect involves a few additional considerations compared to equities due to the nature of options trading. However, the overall approach remains similar: you need to create a custom fill model and assign it to your option security.
One key aspect to remember is that options in QuantConnect are traded as part of the option chain, and you need to ensure that the custom fill model is applied to the specific option contract you're trading. Here's how you can implement and assign a custom fill model for index options:
In this example,
CustomOptionStopMarketFillModel
is a custom fill model designed specifically for options. It overrides theStopMarketFill
method to customize the behavior of stop market orders for options based on the close price of the last data point.In the
MyAlgorithm
class, an option contract is added withself.AddOption("SPX", Resolution.Minute)
, and the custom fill model is assigned to the option security usingself.option.SetFillModel(CustomOptionStopMarketFillModel())
. TheOnData
method demonstrates how to place a stop market order on a specific option contract based on certain criteria (in this case, a put option with a strike price below the underlying price).Please note that this example is simplified and focuses on demonstrating the concept. In a real trading algorithm, you would need to refine the logic for selecting the option contract and managing orders based on your specific strategy requirements. Additionally, keep in mind that modifying
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!