Back

Day Trading - Close all positions at close of day?

I'm a newbie and I have a strategy that needs to close any open positions at market close. I would figure that the OnEndOfDay() event would help make this happen, but after reading the post at the link below, I am confused about whether this is the proper way to close out all your positions at the end of the day.

https://www.quantconnect.com/forum/discussion/656

According to that post, it is unclear whether OnEndOfDay() fires at the actual market close (4pm) or midnight. Even if it closes at 4pm instead of midnight, the last unanswered reply to the post suggests that it fires after the last bar is generated and any request to sell a security would fire at open the next trading day.

I tried the following code, but despite that I am holding shares, it fails to sell even over a full month of backtesting:
public void OnEndOfDay(Symbol symbol)
{
Liquidate(symbol);
}


Some pointers in the right direction would be greatly appreciated. Thanks in advance!

Update Backtest








Your snippet above would indeed create market on open orders, which would fill the next morning as the end of day event is after close. You can however do what you want in several ways:

1. Schedule the algorithm to liquidate at a specific time. This is broker agnostic (not all brokerages support market on close order types). You can use the schedule API for this.
Schedule.On(DateRules.EveryDay("SPY"), TimeRules.BeforeMarketClose("SPY", 10), () =>
{
Liquidate();
});


2. Use Market On Close orders to liquidate at the exact market close price. This in the base API: MarketOnCloseOrder("SPY", 100);

1

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.


Hey Chris!
Welcome to QuantConnect, glad you're here. The OnEndOfDay method is called 10 minutes before closing to allow you to close out your position(s). Depending on your resolution it will fire every second or every minute and will attempt to liquidate your assets. It does it ten minutes in advance to account for slippage and liquidation problems when you have a lot of shares.

Something else you can check out is the order type called MarketOnClose that will fire at 3:59 or 3:59:59 respectively.

Heres an example MarketOnOpenOrder("AAPL", quantity); MarketOnCloseOrder("AAPL", quantity);

2

Thanks Jared and Travis. I tried using MarketOnCloseOrder to close my position... I figured I'd only need to call it once in my OnData handler and it would schedule it for close that market day. Apparently not the case. See what I tried on line 70 of the attached sample project. How can I simply close my position at the end of the day (if I have one)?



For clarity, the code you see apparently never sells my position at market close.
0

Hey Chris, I think you meant to attach your project. An important note, in order to attach the project you'll need to run a backtest. I've thrown together an example algorithm that shows why the OnEndOfDay is not the best place for equity market on close orders. The main reason is that exchanges require MOC orders to be submitted at least 15 minutes before the exchange closes.

1

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.


Hi Michael,

Just to clarify, in your code above you use a schedule event to create market-on-close orders 16 minutes before closing. This will succeed, where the OnEndOfDay logic would otherwise fail (because it is called too close to market end)?

Thanks

0

Correct Stephen, I prefer to use scheduled events in most cases since if I need to adjust the time then it's easy to do so.

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed