I made an indicator that colors bars with a smaller open to close range than the previous 3 bars. I used this code:
I like now to improve that indicator by adding a selectable look back period and made these changes:
Unfortunately that didn´t work and it colors every bar, with a smaller open to close range than the entire selected look back period
(see attached chart). What I´m doing wrong here?
Many thanks in advance for every tip.
Can you help answer these questions from other members on futures io?
It colors every bar because the code does not keep in mind the prior result. Basically your condition would be false, if there is a single bar within the lookback period "barsback" that has a smaller body then the current bar. A workaround would be to use a boolean variable "narrowBody", set that variable to true, and then loop through the prior bars. In case that you find a single bar that has a smaller body than the current bar you may set the boolean to false and exit the loop:
The baisc logic of the indicator - detecting a narrow range - requires that it runs in mode "CalculateOnBarClose = true". If you wish to run it in "CalculateOnBarClose = false" to get a preliminary result, then the code above would be highly inefficient, as you would need to run through the entire loop with every incoming tick. Therefore to make the indicator efficient for use with "CalculateOnBarClose = false" you would need to modify the code, for example:
Not tested.
The following 3 users say Thank You to Fat Tails for this post:
It makes no sense to run this indicator in mode "CalculateOnBarClose = false. I will try therefore your first code.
Does the complete code goes to the "OnBarUpdate()" section?
It makes no sense to run this indicator in mode "CalculateOnBarClose = false. I will try therefore your first code.
Does the complete code goes to the "OnBarUpdate()" section?
The first example should be included with OnBarUpdate().
The following user says Thank You to Fat Tails for this post: