Please forgive my somewhat basic question, but when backtesting using daily bars, is there a way to place a trade at the close on the same day of the calculation using that same close?
For example, if today's close is at least 1% below yesterday's close, then buy at the close. Default is to buy at tomorrow's open which isn't what I'm trying to test.
In the real world, many brokers offer a Limit on Close (LOC) order for this exact circumstance.
Thanks.
Mia Alissi
To implement a strategy that places a trade at the close on the same day using the closing price, you can use the
market_on_close_order
method in QuantConnect. Here's how you can set up your algorithm:Initialize the Algorithm: Set the start and end dates, initial cash, and add the desired equity to your algorithm.
Set Data Resolution: Use daily bars for your data resolution to align with your strategy's requirements.
Define Market On Close Order: Utilize the
market_on_close_order
method to place orders at the market close.Calculate Conditions: In the
on_data
method, calculate if today's close is at least 1% below yesterday's close.Place Order: If the condition is met, use
self.market_on_close_order("SYMBOL", quantity)
to place the order at the market close.Adjust Submission Time Buffer: If necessary, adjust the
MarketOnCloseOrder.submission_time_buffer
to ensure the order is submitted within the required time frame before market close.Here's a sample implementation:
This code will place a market on close order if today's close is at least 1% below yesterday's close. Make sure to adjust the
MarketOnCloseOrder.submission_time_buffer
if your exchange allows for a different submission time.Sources: Market On Open On Close Algorithm, Market On Close Orders - QuantConnect.com, Market On Close Order Buffer Regression Algorithm, Market Orders - QuantConnect.com, Intraday ETF Momentum
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.
Allen Adham
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!