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.