Hi,
I would like to create exit strategy that executes 2 times a year, - at the start of a new year, and at the beginning of July.
How can i do this?
I would appreciate any help i can get.
Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
Creating a Timed Exit strategy twice a year using a date in QuantConnect.
Continue ReadingRefer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
How do you create a Timed Exit strategy using a date?
Samwel Kibet | April 2021
Hi,
I would like to create exit strategy that executes 2 times a year, - at the start of a new year, and at the beginning of July.
How can i do this?
I would appreciate any help i can get.
QuantConnectâ„¢ 2025. All Rights Reserved
VasilK
Hi!
I took it as a challange and came up with this code - hope it helps :)
How it works:
1. Each day we check if it's the specific date to trade:
2. Each day we call the 2nd function to execute the trade, and only when the boolean value is True, we actually execute the trade. Once we executed the trade, we reset the boolean to False and wait for the next cycle.
Here is a log from the code I developed, tesdting for the last few years:
2015-01-02 10:00:00 :Execute trade at Date: Friday 02. January 2015
2015-07-01 10:00:00 :Execute trade at Date: Wednesday 01. July 2015
2016-01-04 10:00:00 :Execute trade at Date: Monday 04. January 2016
2016-07-01 10:00:00 :Execute trade at Date: Friday 01. July 2016
2017-01-03 10:00:00 :Execute trade at Date: Tuesday 03. January 2017
2017-07-03 10:00:00 :Execute trade at Date: Monday 03. July 2017
2018-01-02 10:00:00 :Execute trade at Date: Tuesday 02. January 2018
2018-07-02 10:00:00 :Execute trade at Date: Monday 02. July 2018
2019-01-02 10:00:00 :Execute trade at Date: Wednesday 02. January 2019
2019-07-01 10:00:00 :Execute trade at Date: Monday 01. July 2019
2020-01-02 10:00:00 :Execute trade at Date: Thursday 02. January 2020
2020-07-01 10:00:00 :Execute trade at Date: Wednesday 01. July 2020
2021-01-04 10:00:00 :Execute trade at Date: Monday 04. January 2021
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.
Samwel Kibet
Hey, Thanks for the help. Appreciate it!
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.
Vladimir
Samwel Kibet,
You can executes strategy 2 times a year simply by using
self.Schedule.On(self.DateRules.MonthStart ('SPY')
and specifying MONTHES = [1, 7] which you want to trade.
2015-01-02 12:00:00 : Execute trade at Date: Friday 02. January 2015
2015-07-01 12:00:00 : Execute trade at Date: Wednesday 01. July 2015
2016-01-04 12:00:00 : Execute trade at Date: Monday 04. January 2016
2016-07-01 12:00:00 : Execute trade at Date: Friday 01. July 2016
2017-01-03 12:00:00 : Execute trade at Date: Tuesday 03. January 2017
2017-07-03 12:00:00 : Execute trade at Date: Monday 03. July 2017
2018-01-02 12:00:00 : Execute trade at Date: Tuesday 02. January 2018
2018-07-02 12:00:00 : Execute trade at Date: Monday 02. July 2018
2019-01-02 12:00:00 : Execute trade at Date: Wednesday 02. January 2019
2019-07-01 12:00:00 : Execute trade at Date: Monday 01. July 2019
2020-01-02 12:00:00 : Execute trade at Date: Thursday 02. January 2020
2020-07-01 12:00:00 : Execute trade at Date: Wednesday 01. July 2020
2021-01-04 12:00:00 : Execute trade at Date: Monday 04. January 2021
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.
VasilK
Much more elegant solution Vladimir - thanks for sharing! :)
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!