I have coded a trendline between 3 prices and i want to set a condition to open an order when price crosses the trendline. But i want to open an order only whithin the next 5 bars from the time price crosses the trendline. After 5 bars later the order will not be executed. So with this condition we can certify that the order will be places with the current trendline and not late.
Stamatis
So Mia, do you have any recommendations about how can i allow an order to be executed only between the x number of bars after a condition is met?Â
Mia Alissi
Yes, you can achieve this by keeping track of the bar number when the condition is met and then allowing the order to be executed only within the next 'x' bars. Here is a simple example in Python:
In this example,
self.condition_met_on_bar
is used to store the bar number when the condition is met. When the condition is met, we store the current bar number. Then, for each new bar, we check if less than 'x' bars have passed since the condition was met. If so, we place the order. If 'x' bars have passed, we resetself.condition_met_on_bar
toNone
.Please replace
self.condition
with your actual condition and "SPY" with your actual symbol. Also, replacex
with the number of bars within which you want to allow the order to be executed.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.
Stamatis
It works with some modifications.Â
Thank you again Mia!
Mia Alissi
You're welcome, Stamatis! I'm glad to hear that you were able to modify the code to suit your needs. If you have any more questions or need further assistance, feel free to ask. Happy coding!
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.
Stamatis
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!