Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Can anyone suggest code that would allow a strategy to check whether conditions were true for the previous X number of bars, where X is a user-input parameter?
I'm not even sure how to clearly ask a search engine, so I thought I'd post here.
Can you help answer these questions from other members on NexusFi?
I'm aware of how to set a user-defined parameter. What I don't know how to do is use that parameter as a variable number of bar numbers.
For example, I can code:
if (Close[2] < KeltnerChannel(1.5, 10).Midline[2]
&& Close[1] < KeltnerChannel(1.5, 10).Midline[1]
&& Close[0] < KeltnerChannel(1.5, 10).Midline[0])
{ DO SOMETHING}
But I want one line that will do the same as the above when I input 3 as the user parameter, and do the equivalent of 4 or 5 or 25 lines like the above as I change that parameter.
(I *knew* I wasn't explaining clearly what I wanted...)
I'm afraid this won't work, either. If the input number was 25, it'd check if the 25th bar back was lower than Keltner Channel's midline 2 bars ago. Though changing the midline's [2] to [MyInput0] would compare Close[25] to midline[25], which is incrementally closer to what I want.
I need it to check whether all the last 25 bars were lower than the Keltner midline's values for those same bars.
Ahhh... I get it. You use variables to track your conditions and then count how many times it's true.
Perfect. I can certainly use this concept for what I'm trying to do. Thank you!
But, I've never seen do/while before.
Does that mean keep doing the 'do' as long as the 'while' condition is true?
Ahhh... I get it. You use variables to track your conditions and then count how many times it's true.
Perfect. I can certainly use this concept for what I'm trying to do. Thank you!
But, I've never seen do/while before.
Does that mean keep doing the 'do' as long as the 'while' condition is true?
A do while loop always executes the code in the block first, and then checks the variable. So in this instance I'm using the do while loop to ensure that it always executes at least once for bar 0. So a do while might not always be the right solution, but the same idea would apply to a foreach or while loop. Keep iterating back in bars until the condition is no longer true.
A do while loop always executes the code in the block first, and then checks the variable. So in this instance I'm using the do while loop to ensure that it always executes at least once for bar 0. So a do while might not always be the right solution, but the same idea would apply to a foreach or while loop. Keep iterating back in bars until the condition is no longer true.