Fold Function Counter

StoneMan

Member
Plus
I am trying to plot the Z score of the true range as a percentile. In order to do that, at each bar I need to fold through my sample period and count how many Z-Score values are lower than my current bars Z score value. If all Z-Score values in the sample period are lower the output will be 1.0. My issue is with getting the counting logic utilizing the fold function right. I can't quite seem to get it.

Ruby:
input Length = 10;

#Calculating volitility Z-Score, true range used as volitility metric
def TrueRange = TrueRange(high, close, low); #Observed value
def TR_Average = Average(TrueRange, Length); #Mean of sample
def TR_StDev = StDev(TrueRange, Length);     #Standard Deviation of sample

def TR_Z_Score = (TrueRange - TR_Average) / TR_StDev;

def TR_Z_Percent_Count =
           fold i = 0
           to Length
           with S = 0
           do if (TR_Z_Score > getValue(TR_Z_Score, i, Length))
           then TR_Z_Percent_Count[1] + 1
           else TR_Z_Percent_Count[1];

def TR_Percentile = TR_Z_Percent_Count / Length;

plot Data = TR_Percentile;
 
Solution
I am trying to plot the Z score of the true range as a percentile. In order to do that, at each bar I need to fold through my sample period and count how many Z-Score values are lower than my current bars Z score value. If all Z-Score values in the sample period are lower the output will be 1.0. My issue is with getting the counting logic utilizing the fold function right. I can't quite seem to get it.

Ruby:
input Length = 10;

#Calculating volitility Z-Score, true range used as volitility metric
def TrueRange = TrueRange(high, close, low); #Observed value
def TR_Average = Average(TrueRange, Length); #Mean of sample
def TR_StDev = StDev(TrueRange, Length);     #Standard Deviation of sample

def TR_Z_Score = (TrueRange - TR_Average) /...
I am trying to plot the Z score of the true range as a percentile. In order to do that, at each bar I need to fold through my sample period and count how many Z-Score values are lower than my current bars Z score value. If all Z-Score values in the sample period are lower the output will be 1.0. My issue is with getting the counting logic utilizing the fold function right. I can't quite seem to get it.

Ruby:
input Length = 10;

#Calculating volitility Z-Score, true range used as volitility metric
def TrueRange = TrueRange(high, close, low); #Observed value
def TR_Average = Average(TrueRange, Length); #Mean of sample
def TR_StDev = StDev(TrueRange, Length);     #Standard Deviation of sample

def TR_Z_Score = (TrueRange - TR_Average) / TR_StDev;

def TR_Z_Percent_Count =
           fold i = 0
           to Length
           with S = 0
           do if (TR_Z_Score > getValue(TR_Z_Score, i, Length))
           then TR_Z_Percent_Count[1] + 1
           else TR_Z_Percent_Count[1];

def TR_Percentile = TR_Z_Percent_Count / Length;

plot Data = TR_Percentile;


see if this is what you are looking for.
within a fold loop, you reference the temp variable, s, not the primary output variable.
s holds values during all the iterations. the primary variable gets a value when it is done looping.

i made this a lower study


Code:
# zscore_fold_counter

#https://usethinkscript.com/threads/fold-function-counter.15604/
#Fold Function Counter
#StoneMan  5/26

declare lower;

input Length = 10;

#Calculating volitility Z-Score, true range used as volitility metric
def TrueRange = TrueRange(high, close, low); #Observed value
def TR_Average = Average(TrueRange, Length); #Mean of sample
def TR_StDev = StDev(TrueRange, Length);     #Standard Deviation of sample

def TR_Z_Score = (TrueRange - TR_Average) / TR_StDev;

#def TR_Z_Percent_Count =
#           fold i = 0
#           to Length
#           with S = 0
#           do if (TR_Z_Score > getValue(TR_Z_Score, i, Length))
#           then TR_Z_Percent_Count[1] + 1
#           else TR_Z_Percent_Count[1];

def TR_Z_Percent_Count =
    fold i = 0
    to Length
    with S = 0
    do s + if (TR_Z_Score > getValue(TR_Z_Score, i, Length)) then 1 else 0;

def TR_Percentile = TR_Z_Percent_Count / Length;

plot Data = TR_Percentile;
#

3i8zTbn.jpg
 
Solution

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

I found and fixed an important bug: in the previous code a sample period (Length) of 3 you get discreet "percentile" levels of 0, .33, and .66. We want this to oscillate from 0 to 1. To get that behavior we have to divide the count by Lenght - 1. The following code includes that change.
Code:
#zscore_fold_counter
#Gets z score percentile of true range
#https://usethinkscript.com/threads/fold-function-counter.15604/
#Fold Function Counter
#StoneMan  5/26/2023
#Bug fix 5/28/2023: Now divide count by Lenght-1 to get proper oscillating behavior

declare lower;

input Length = 10;

#Calculating volitility Z-Score, true range used as volitility metric
def TrueRange = TrueRange(high, close, low); #Observed value
def TR_Average = Average(TrueRange, Length); #Mean of sample
def TR_StDev = StDev(TrueRange, Length);     #Standard Deviation of sample

def TR_Z_Score = (TrueRange - TR_Average) / TR_StDev;

def TR_Z_Percent_Count =
    fold i = 0
    to Length
    with S = 0
    do s + if (TR_Z_Score > getValue(TR_Z_Score, i, Length)) then 1 else 0;

def TR_Percentile = TR_Z_Percent_Count / (Length - 1);

plot TRpercent = TR_Percentile;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
274 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