Hi,
I'm new to algotrading and quantconnect. To familiarize myself with the framework I wanted to try to implement a simple strategy I read about:
- buy SPY on the close of the last trading day of the month
- sell SPY on close on the first trading day of the month
I tried to implement it using the Algorithm Framework from the docs, but I am having some trouble.
(I am aware that this would be fairly simple to implement without the algorithm framework, by just setting 2 market orders using scheduling rules, but I would like to know how this could be implemented using the framework)
I attached my code, but I still have a lot of things that are not working:
- Currently the algorithm buys/sells at open (because I'm using Resolution.Daily I presume), how could I switch it to buying/selling on close?
- I am using `list(algorithm.DateRules.MonthEnd(symbol, 0).GetDates(algorithm.Time, algorithm.Time)):` to check if today is the last trading day, but it seems clumsy (and i'm not sure it would work in liveTrading). Is there a better way?
- Sometimes, buys or sells are spread across the opening of 2 days. Is it because market order on open have a different behaviour than during the day? How can I tell the algorithm to keep trying to buy in real time until everything is bought, instead of trying at the next day's open? (so instead of buying 387 shares on day 1 open, and 1 share on day 2 open, it buys 387 shares on day 1 open, and 1 share on day 1 open + 1 minute)
- for some reason an insight is not emitted on Aug 31 2020, which should be the last tradable day of the month (and the date doesn't seem to even be taken into account
Any other feedback on how I could improve the code would be really appreciated!
Derek Melchin
Hi Charles,
Welcome to QC!
> Currently the algorithm buys/sells at open (because I'm using Resolution.Daily I presume), how could I switch it to buying/selling on close?
To do this with the Algorithm Framework, we'd have to create a custom execution model that uses the MarketOnOpenOrder and MarketOnCloseOrder order types. We'd also have to increase the data resolution since MarketOnCloseOrders can only be placed when the market is open.
> I am using `list(algorithm.DateRules.MonthEnd(symbol, 0).GetDates(algorithm.Time, algorithm.Time)):` to check if today is the last trading day, but it seems clumsy (and i'm not sure it would work in liveTrading). Is there a better way?
After increasing the data resolution, we can determine if it's the last trading day of the month with
next_market_open = algorithm.Securities[symbol].Exchange.Hours.GetNextMarketOpen(slice.Time, False) last_trading_day = next_market_open.month != slice.Time.month
See the attached backtest for reference.
> Sometimes, buys or sells are spread across the opening of 2 days. Is it because market order on open have a different behaviour than during the day?
This is because the EqualWeightingPortfolioConstructionModel rebalances everyday. The current insight period should be shortened in the algorithm above so the trade lasts only 1 day.
insightExpiry = Expiry.EndOfDay(algorithm.Time) - timedelta(seconds=1)
However, when we increase the data resolution, this is not an issue.
> for some reason an insight is not emitted on Aug 31 2020, which should be the last tradable day of the month (and the date doesn't seem to even be taken into account
This is fixed when using the new method of determining if it's the last trading day of the month.
Best,
Derek Melchin
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.
Charles Bournhonesque
Thank you so much!
Charles Bournhonesque
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!