Display number of bars since condition was true


Category:
0
0

right now, the count returns the number of valid conditions out of the past 10 periods, with the color  changing based on that condition and one other.  i’d prefer how many periods it’s been since the last instance of a valid condition, again with the color changes.  Can you help?  Thanks!

input period = 14;
input capMultiplier = 4.0;

def timer = SecondsTillTime(1615);
def deltaT = AbsValue(timer[-1] – timer);

def momentum = volume/deltaT;

def aveSM = Average(momentum, period);
def sdAve = aveSM + 2 * StDev(momentum, period);
def spikeCap = TotalSum(momentum) / BarNumber() * capMultiplier;

input count_length = 10;

def count_above1 = sum (momentum > sdave, count_length) ;

def count_above2 = sum (momentum > spikecap, count_length);

plot value = count_above1;

value.AssignValueColor(if value < 1 then color.red else if count_above2 >= count_above1 then color.green else color.white);

Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on April 7, 2021 8:49 am
421 views
0
Private answer

Each post in this Q&A Forum must be targeted to provide solutions that benefit the majority of our viewers. This is not the place to request one-off solutions that only serve a single individual. That's what our prefessional services are for.

So I have updated the title of your question to reach the broadest possible audience and the solution I am providing is designed as a template to serve all of our viewers.

def condition = volume > Average(volume, 14) * 1.2;
rec countBars = if condition then 1 else countBars[1] + 1;
plot data = countBars;

Using this template, those who are comfortable writing their own custom tools for Thinkorswim will be able to create counters for just about anything they can imagine.

There is a more advanced solution in which the condition to be counted is two dimensional. Meaning that it resets each time a crossover event occurs and continues counting bars while the price and moving average continue in the same condition as the crossover. That post can be found here:

https://www.hahn-tech.com/ans/display-bars-since-cross-above-hma/

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 7, 2021 10:55 am
0
okay, thanks. in my instance, i think this did most of it: def condition = momentum > sdave; rec countBars = if condition then 1 else countBars[1] + 1; plot data = countBars;
( at April 7, 2021 12:33 pm)