# Assistance with daily bar data and displaying on a lower time frame please.

#### DrTrev4

##### New member
Code:
``````input length = 20;

def period = aggregationPeriod.DAY;

def wick = fold i = 0 to length
with w
do if open(GetSymbol(), period)[i] >= close(GetSymbol(), period)[i]
then w + (high(GetSymbol(), period)[i] - open(GetSymbol(), period)[i])
else w + (open(GetSymbol(), period)[i] - low(GetSymbol(), period)[i]);

plot avgBearReversal = open(GetSymbol(), period) + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period) - (wick / length);
avgBullReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBullReversal.SetDefaultColor(Color.GREEN);``````

What I am doing here is finding the difference between open and the high or the difference between open and low dependant upon whether the close was lower or higher than the open (respectively) across 20 daily bars to establish an average potential price reversal.

NOTE I do not like working directly with the values and will be adding math to work more directly with percentages in the near future.

The problem I am having is, I am only using 20 daily bars which may contain any number of bullish or bearish days (so long as they combine to = 20). I do not want to assume price reacts the same on a bullish day vs a bearish day so I need to query the daily data for the 20 most recent bearish days and the 20 most recent bullish days for my calculations and plotting on a lower time frame.

I am new to ThinkScript and have learned so far that I cannot think about accomplishing tasks in the same way I would other programming languages. Without use of a traditional "for" or "while" loop I am struggling to wrap my mind around how to approach this.

Any help or guidance that might lead me down the correct path would be greatly appreciated.

• halcyonguy
Solution
Code:
``````input length = 20;

def period = aggregationPeriod.DAY;

def wick = fold i = 0 to length
with w
do if open(GetSymbol(), period)[i] >= close(GetSymbol(), period)[i]
then w + (high(GetSymbol(), period)[i] - open(GetSymbol(), period)[i])
else w + (open(GetSymbol(), period)[i] - low(GetSymbol(), period)[i]);

plot avgBearReversal = open(GetSymbol(), period) + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period) - (wick / length);
avgBullReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBullReversal.SetDefaultColor(Color.GREEN);``````

What I am doing here is finding the difference...
Code:
``````input length = 20;

def period = aggregationPeriod.DAY;

def wick = fold i = 0 to length
with w
do if open(GetSymbol(), period)[i] >= close(GetSymbol(), period)[i]
then w + (high(GetSymbol(), period)[i] - open(GetSymbol(), period)[i])
else w + (open(GetSymbol(), period)[i] - low(GetSymbol(), period)[i]);

plot avgBearReversal = open(GetSymbol(), period) + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period) - (wick / length);
avgBullReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBullReversal.SetDefaultColor(Color.GREEN);``````

What I am doing here is finding the difference between open and the high or the difference between open and low dependant upon whether the close was lower or higher than the open (respectively) across 20 daily bars to establish an average potential price reversal.

NOTE I do not like working directly with the values and will be adding math to work more directly with percentages in the near future.

The problem I am having is, I am only using 20 daily bars which may contain any number of bullish or bearish days (so long as they combine to = 20). I do not want to assume price reacts the same on a bullish day vs a bearish day so I need to query the daily data for the 20 most recent bearish days and the 20 most recent bullish days for my calculations and plotting on a lower time frame.

I am new to ThinkScript and have learned so far that I cannot think about accomplishing tasks in the same way I would other programming languages. Without use of a traditional "for" or "while" loop I am struggling to wrap my mind around how to approach this.

Any help or guidance that might lead me down the correct path would be greatly appreciated.

sometimes using an offset on a variable in a fold may not work as expected. var. it is suggested to use getvalue() within folds, to retrieve data. getvalue can be used anytime in place of an offset on a variable. sometimes getvalue will work with a formula for an offset, when the formula won't work as an offset on a variable.

maybe this will help. this study looks at previous bars and counts how many times a condition is true.
it could be changed to add up values when the condition is true. then after the fold, divide by 20, to get an average.

Thank you for the information. I will head in that direction and see what turns out.

sometimes using an offset on a variable in a fold may not work as expected. var. it is suggested to use getvalue() within folds, to retrieve data. getvalue can be used anytime in place of an offset on a variable. sometimes getvalue will work with a formula for an offset, when the formula won't work as an offset on a variable.

maybe this will help. this study looks at previous bars and counts how many times a condition is true.
it could be changed to add up values when the condition is true. then after the fold, divide by 20, to get an average.

I am assuming it is a syntax error that I have yet to figure out. Would you please explain why the if statement by itself is fine but the if statement that is inside the fold errors

Code:
``````def a;
def b;
if (a == 0)
then { b = a + 1; }
else { b = a + 2; }

def c = fold i = 0 to 20 with d = 0
do if (a == 0)
then { b = a + 1; }
else { b = a + 2; }``````

Invalid Statement def c...
Invalid Statement }... (referencing "then" line of code
Invalid Statement }... (referencing "else" line of code

I know this is gonna be a face palm type of moment. It seems there is very little help on google for ThinkScript beyond...well...you lol

• MerryDay
I am assuming it is a syntax error that I have yet to figure out. Would you please explain why the if statement by itself is fine but the if statement that is inside the fold errors

Code:
``````def a;
def b;
if (a == 0)
then { b = a + 1; }
else { b = a + 2; }

def c = fold i = 0 to 20 with d = 0
do if (a == 0)
then { b = a + 1; }
else { b = a + 2; }``````

Invalid Statement def c...
Invalid Statement }... (referencing "then" line of code
Invalid Statement }... (referencing "else" line of code

I know this is gonna be a face palm type of moment. It seems there is very little help on google for ThinkScript beyond...well...you lol

a is not set to a value in your example, before it is referenced.

Code:
``````def a =  some formula.....

def b;
if (a == 0) then {
b = a + 1;
} else {
b = a + 2;
}``````

maybe if-then with brackets don't work in a fold,...? i don't think i've tried that.

not sure what you want the fold to do.
fold won't use the end range number,it will stop at (20 - 1).
this will look at previous bars, the current and 19 previous bars. fold won't process 20, it will stop at 19.
depending the value of a, it will add up different numbers for c , for each bar.

Code:
``````def c = fold i = 0 to 20
with d = 0
do d + ( if (getvalue(a, i) == 0) then
( getvalue(a , i) + 1)
else (getvalue(a, i) + 2) );

# show values under the bars
addchartbubble(1, low, "C " + c + "\nA " + a, color.yellow, no);``````

### Not the exact question you're looking for?

87k+ Posts
188 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?