Hi,
I'm trying to develop intraday strategy. I need to close all portfolio quantity by the end of each trading day. How can I design this kind of mechanism?
Thanks a lot.
QUANTCONNECT COMMUNITY
Hi,
I'm trying to develop intraday strategy. I need to close all portfolio quantity by the end of each trading day. How can I design this kind of mechanism?
Thanks a lot.
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.
Guadiana
Hi, there are events for EndOfDay... check the documentation and try it.
public override void OnEndOfDay(string symbol) { Liquidate(symbol); }
Guadiana
Here is an example Backtest:
Chiang Chih-I
It works! Thanks you so much!!
Alexandre Catarino
Hi Chiang,
When trading intraday, you want to liquidate all your current positions before the market closes. When you use the OnEndOfDay event handler, the market is already closed and your orders will be executed on the following day.
If you want to make sure that this is not going to happen, you should use schedule events:
// Schedule an event to fire every trading day for a security // The time rule here tells it to fire 10 minutes before SPY's market close Schedule.On(DateRules.EveryDay("SPY"), TimeRules.BeforeMarketClose("SPY", 10), () => { Log("EveryDay.SPY 10 min before close: Fired at: " + Time); });
I have attached a working example.
Please note that if you use daily data, scheduling a BeforeMarketClose will be fired after market close and you will have the same result of using OnEndOfDay.
Guadiana
Thanks for the clarification on differences of OnEndOfDat and BeforeMarketClose @Alex
Nicholas Stein
There is another gocha you should also be aware of.
Liquidate generates market orders. That may not be what you want. To quote a recent message from IB...
We note that you have recently submitted Market Orders in your account(s) ********. Please see important information below:
* Please note that a Market Order is an instruction to trade your order at any price available in the market, subject to any additional instructions for handling/simulating the particular order type you specified and other order conditions you specify when submitting your order. A Market Order is not guaranteed a specific trade price and may trade at an undesirable price. If you would like greater control over the trade prices you receive, please submit your order using a Limit Order, which is an instruction to place your order at or better than the specified limit price, or submit an algorithmic Market Order (IBALGO).
* In accordance with our obligations as a broker, large Market Orders may be split into smaller orders, which will be traded over time. This is designed to reduce the impact of these large orders on the market, including the impact your order has on the market price.
I had a situation where a deep OTM short call option was about to expire and the ask price was .05 with no bid. I put in a market order to buy it presuming it would be at .05. It filled at .28 and I got screwed out of .23 points.
Nick
Zedrick Gilo
Is there a way to add this for universal selection? I could only find this for a single one.
Alexandre Catarino
Hi Hector,
You can loop through all securities in your selected universe and substitute "SPY" in the given example for each security symbol and call Liquidate(string ticker) instead of Liquidate().
However, keep in mind that if all securities in your selected universe have the same exchange hours, e.g. US Equities, they all have the same exchange hours, therefore setting up a single schedure, using any security of that universe, is enough and simpler.
Chiang Chih-I
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!