Measure indicator time elapsed

TMac

New member
I have a strategy that utilizes trailing ATR. I’m trying to figure out a way to measure the average time elapsed (number of candles) before the indicator switches directions.
Any suggestions would be greatly appreciated.
 
Solution
Hello.

I`ve been trying to figure out a way to measure the time elapsed for multiple indicators.
My goal is to find the average duration before an indicator switches.
For example, if I wanted to know how much time does the price stay above a moving average.
Or if I have some super trend indicator, how long does a trend usually last etc.

Any help or suggestions on how to measure this and find an average would be greatly appreciated.


your post1 words are vague and confusing.
what defines 'switches directions' ? that doesn't tell us what you are really after.

svanoy answered your question the best he could with the limited information. it counts and calculates an average count. you could take his code and modify it.


learn...
@TMac
Here is a quick example I threw together get you started:
It counts and averages the # of bars between bars that engulf the prior 5 bars.
You should be able to redefine the 'SwitchDirectionCounter' to fit your needs.
Ruby:
def bn = BarNumber();
def engulfing = high == highest(high,5) and low == lowest(low,5);

def SwitchDirectionCounter = if bn == 1
        then 1
    else if engulfing
        then SwitchDirectionCounter[1]+1
    else SwitchDirectionCounter[1];

def DirectionCounter = if bn ==1 or SwitchDirectionCounter != SwitchDirectionCounter[1]
        then 1
    else DirectionCounter[1]+1;

def RunningCountBetweenSwitch = if bn == 1 then 0
    else if SwitchDirectionCounter != SwitchDirectionCounter[1]
        then (RunningCountBetweenSwitch[1]+DirectionCounter[1])
    else RunningCountBetweenSwitch[1];
 
def AverageCountBetweenSwitch = RunningCountBetweenSwitch/SwitchDirectionCounter;
  
addchartbubble(SwitchDirectionCounter != SwitchDirectionCounter[1], high, "Average # of bars = "+Round(AverageCountBetweenSwitch,0),color.white);
 
Hello.

I`ve been trying to figure out a way to measure the time elapsed for multiple indicators.
My goal is to find the average duration before an indicator switches.
For example, if I wanted to know how much time does the price stay above a moving average.
Or if I have some super trend indicator, how long does a trend usually last etc.

Any help or suggestions on how to measure this and find an average would be greatly appreciated.
 
Hello.

I`ve been trying to figure out a way to measure the time elapsed for multiple indicators.
My goal is to find the average duration before an indicator switches.
For example, if I wanted to know how much time does the price stay above a moving average.
Or if I have some super trend indicator, how long does a trend usually last etc.

Any help or suggestions on how to measure this and find an average would be greatly appreciated.


your post1 words are vague and confusing.
what defines 'switches directions' ? that doesn't tell us what you are really after.

svanoy answered your question the best he could with the limited information. it counts and calculates an average count. you could take his code and modify it.


learn how to use some functions to help you debug and understand what the code is doing.
add this to the end of post2 code, to display some values.
i think it will help you understand what that code is doing.

Code:
addverticalline(engulfing, "TRIGGER", color.cyan);

addchartbubble(1, low,
 bn + "  bn\n" +
 SwitchDirectionCounter + "  swdir\n" +
 DirectionCounter + "  dir\n" +
 RunningCountBetweenSwitch + "  cnt\n" + 
 AverageCountBetweenSwitch + "  avg"
, color.yellow, no);
#


your post3 starts out just as confusing, mentioning , 'duration before an indicator switches'. this is too vague to act on. 'indicator switches' ? what variable is switching, from what to what?

your example gives us a clue as to what you are really after, 'price above a moving average'. this is something definite, that has a starting point and an ending point, a duration of bars that can be counted.

here is a test code to count bars when close is above an average. it is similar to post2 code


Code:
# period_bar_counter_template_00
#---------------------------------------
def na = double.nan;
def bn = barnumber();
#---------------------------------------
# test signals , 2 averages

# template_buy_sell_avgs_cross
# buy sell signals based on averages crossing
def price = close;
input ma1_len = 9;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 21;
input ma2_type =  AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);

input show_ma1_line = yes;
plot z1 = if show_ma1_line then ma1 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();

input show_ma2_line = no;
plot z2 = if show_ma2_line then ma2 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();

#---------------------------------------

# conditions , start , stop
def start_pricexupma1 = (close crosses above ma1);
def stop_pricexdwnma1 = (close crosses below ma1);
#def start_ma1xup = (ma1 crosses above ma2);
#def stop_ma1xdwn = (ma1 crosses below ma2);

# time periods
def priceabovema1 = (close > ma1);
#def ma1above = (ma1 > ma2);

# count bars during each time period , priceabovema1. reset count on each new period.
def barcnt = if bn == 1 then 0
 else if stop_pricexdwnma1 then 0
 else if start_pricexupma1 then 1
 else if priceabovema1 then barcnt[1] + 1
 else barcnt[1];

# count bars during all the time periods
def totalcnt = if bn == 1 then 0
 else if  priceabovema1 then totalcnt[1] + 1
 else totalcnt[1];

# count quantity of time periods
def qty = if bn == 1 then 0
 else if start_pricexupma1 then qty[1] + 1
 else qty[1];

def avg = round(totalcnt / qty, 2);

addchartbubble(1, low*0.997,
 barcnt + "  cnt\n" +
 totalcnt + "  ttl\n" +
 qty + "  qty\n" +
 avg + "  avg"
, (if barcnt > 0 then color.yellow else color.gray), no);
#

wEiCnbd.jpg
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
418 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top