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)[0] + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period)[0] - (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.

Thank you in advance.
 
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)[0] + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period)[0] - (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)[0] + (wick / length);
avgBearReversal.setPaintingStrategy(PaintingStrategy.HORIZONTAL);
avgBearReversal.setDefaultColor(Color.RED);

plot avgBullReversal = open(GetSymbol(), period)[0] - (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.

Thank you in advance.

sometimes using an offset on a variable in a fold may not work as expected. var[1]. 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.

https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474
 
Solution

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

sometimes using an offset on a variable in a fold may not work as expected. var[1]. 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.

https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474
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
 
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);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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