I am trying to learn Thinkscript and I've come across something I haven't been able to solve.
Imagine you're looking at a 10 minute chart. We could draw a line of the high of the 1 hour candle if we wanted. Each 1 hour candle high would be numbered, 1, 2, 3, 4, etc. For each 10 minute bar inside a 1 hour candle, we'd number them 1, 2, 3, 4, 5, 6, 1, 2, 3 ,4 ,5 ,6, 1, 2, etc
I have gotten really close:
As you can see, the hourly highs are drawn, and numbered. However, I cannot figure out how to properly count the 10 minute bars properly. Here is the relevant snippet from the code:
It seems like the "else" never gets utilized. But if I inverse the condition and put the increment in the "then", it doesn't work either. Can someone help me get this working, or understand why it can't?
Here is the entire code:
And here is a shared chart: http://tos.mx/JDZeL7J
Imagine you're looking at a 10 minute chart. We could draw a line of the high of the 1 hour candle if we wanted. Each 1 hour candle high would be numbered, 1, 2, 3, 4, etc. For each 10 minute bar inside a 1 hour candle, we'd number them 1, 2, 3, 4, 5, 6, 1, 2, 3 ,4 ,5 ,6, 1, 2, etc
I have gotten really close:
As you can see, the hourly highs are drawn, and numbered. However, I cannot figure out how to properly count the 10 minute bars properly. Here is the relevant snippet from the code:
Code:
def lower_period_bar_index =
if higher_period_bar_changed
then 0 # reset to 0 after higher period bar change
else lower_period_bar_index[1] + 1; # otherwise, count up
It seems like the "else" never gets utilized. But if I inverse the condition and put the increment in the "then", it doesn't work either. Can someone help me get this working, or understand why it can't?
Here is the entire code:
Code:
DECLARE UPPER;
#------------------------------------
# INPUTS
#------------------------------------
input higher_period = AggregationPeriod.HOUR;
#------------------------------------
# TIMEFRAMES
#------------------------------------
def lower_period = GetAggregationPeriod();
def lower_period_bar_count = higher_period / lower_period;
def H = high(period = higher_period);
def L = low(period = higher_period);
def O = open(period = higher_period);
def C = close(period = higher_period);
def NAN = double.Nan;
def higher_period_bar_changed =
H != H[1] or
O != O[1] or
L != L[1] or
C != C[1];
def higher_period_bar_index =
if higher_period_bar_changed
then higher_period_bar_index[1] + 1
else higher_period_bar_index[1];
def lower_period_bar_index =
if higher_period_bar_changed
then 0 # reset to 0 after higher period bar change
else lower_period_bar_index[1] + 1; # otherwise, count up
def even_higher_period_bar = higher_period_bar_index % 2 == 0;
#------------------------------------
# OPEN / CLOSE LINES
#------------------------------------
plot highline = H;
highline.AssignValueColor(if O < C then Color.Green else Color.Red);
highline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highline.SetLineWeight(1);
#------------------------------------
# DISPLAY
#------------------------------------
# higher period bar index
AddChartBubble(H != H[1] or O != O[1] or L != L[1] or C != C[1], H, higher_period_bar_index, Color.GREEN);
# lower period bar index
AddChartBubble(yes, H + 1, lower_period_bar_index);
And here is a shared chart: http://tos.mx/JDZeL7J