mysti9uemirage
New member
Hello, I'm modifying a script and based on the reversal values, I wanted to calculate fib extension values. I think my calculations are correct (in yellow). However, I want to draw a line from bar number 131 (calculated prev2Bar) to bar number 263 (barNumber + 100) for the value 821.5956, and label it (without bubbles).
As you can see in the condition below, this value will be calculated at the firstBarOfDay and latestBar. However, I want to plot that value about 100 bars before the calculated value, and 100 bars after, but nothing in between.
I can kind of draw a line by using this code (not complete but should give you an idea of what I tried):
Code:
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
input Offset = 0; #hint Offset: use this to squeeze the Nth bar inwards.
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
(beforeStart[1+offset] == 1 and beforeStart[offset] == 0) or
(isRollover[offset] and beforeStart[offset] == 0)
then 1
else 0;
def lastBarOfDay = if
(afterEnd[-1-offset] == 1 and afterEnd[offset] == 0) or
(isRollover[-1-offset] and firstBarOfDay[-1-offset])
then 1
else 0;
def prev1Up_618 = if (firstBarOfDay or isLastBar) then close + (prev1UpDiff * level_618) else double.NaN;
Plot plot_prev1Up_618 = if !IsNaN(prev1Up_618) then prev1Up_618 else
if !IsNaN(prev1Up_618[1]) then prev1Up_618[1] else
if !IsNaN(prev1Up_618[2]) then prev1Up_618[2] else
if !IsNaN(prev1Up_618[3]) then prev1Up_618[3] else
if !IsNaN(prev1Up_618[4]) then prev1Up_618[4] else
if !IsNaN(prev1Up_618[5]) then prev1Up_618[5] else
if !IsNaN(prev1Up_618[6]) then prev1Up_618[6] else double.NaN;
However, that looks really dumb, and I have to do that for a bunch of other fib lines.... there has to be a more efficient way to do this...
I think I need some kind of loop combined with some condition, so I looked into the fold operator but I'm not sure how to use it to achieve what I want. Is there any way you can show me how to plot an arbitrary number (let's say 100) for 100 bars previous to the firstBarOfDay until 100 bars after the firstBarOfDay ?
I was trying something like this, but there are syntactical errors and it has been taking me too long to tweak it to try to get the correct syntax with the logic that I want... Please help?
Code:
plot pl_prev1Up_618 = fold index = 1 to 100 with v while (!IsNaN(prev1Up_618[i]) or !IsNaN(prev1Up_618[-i])) if !IsNaN(prev1Up_618[i]) then prev1Up_618[i] else if !IsNaN(prev1Up_618[-i])) then prev1Up_618[-i] else double.NaN;
Thank you in advance