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)
Referencing Previous Bars that Meet Certain Criteria
In ThinkOrSwim's ThinkScript, I'm trying to create an indicator which displays an average of the past 5 bars which had a range of more than 10 ticks.
For example, lets say last 10 bars had the following ranges (in ticks): 20, 22, 25, 17, 10, 7, 8, 8, 9, 8.
I want my indicator to display a value of 18.8 (i.e, (20+22+25+17+10)/5 = 18.8).
I'm fairly new to ThinkScript and so far I've been able to program an indicator which displays the average of the last 5 bars but I want to make one which will only average those bars which fulfill a certain criteria.
Any help would be appreciated!
Can you help answer these questions from other members on NexusFi?
Not sure this is of any value other than to see how the fold expression is used
input Threshold = 10;
input n = 10;
def Avg = (fold i = 0 to n
with p
do p + getValue(if close > Threshold then close else 0, i, n - 1)) / n;
addLabel(1, "Avg Value " + Avg, Color.White);
AddLabel(1, "Avg " + (Average(close, n)));
Thanks for the reply. But wouldn't the above code average the last 10 bars regardless of whether it had higher than 10 ticks? If the last 10 bars all had less than 10 ticks then the value returned by this code would be 0, would it not?