Gap Fill With Auto Fibonacci For ThinkOrSwim
most up-to-date versionRuby:#Premarket criteria input debug = no; def ymd = GetYYYYMMDD(); def capture = ymd != ymd[1]; def Count = CompoundValue(1, if capture then Count[1] + 1 else Count[1], 0); def daycount = (HighestAll(Count) - Count) + 1 ; def x1 = CompoundValue(1, if daycount[1] == 2 and daycount == 1 then high else if daycount == 1 then Max(high, x1[1]) else x1[1], high); def x2 = CompoundValue(1, if daycount[1] == 3 and daycount == 2 then high else if daycount == 2 then Max(high, x2[1]) else x2[1], high); AddLabel(debug, "Premarket Highs: " + x1 + " " + x2 , Color.WHITE); def c = close(period = AggregationPeriod.DAY); def c1 = close(period = AggregationPeriod.DAY)[1]; def c2 = close(period = AggregationPeriod.DAY)[2]; def c3 = close(period = AggregationPeriod.DAY)[3]; AddLabel(debug, "Daily Close : " + c1 + " " + c2 + " " + c3, Color.YELLOW); def h1 = CompoundValue(1, if daycount[1] == 2 and daycount == 1 then high else if daycount == 1 and secondsfromtime(1600)<=0 then Max(high, x1[1]) else x1[1], high); def h2 = CompoundValue(1, if daycount[1] == 3 and daycount == 2 then high else if daycount == 2 and secondsfromtime(1600)<=0 then Max(high, x2[1]) else x2[1], high);def h = high(period = AggregationPeriod.DAY); AddLabel(debug, "Daily High : " + h1 + " " + h2, Color.WHITE); def c2bn = highestall( if daycount==3 and secondsFromTime(1600)==0 then barnumber() else 0 ); addlabel(debug,c2bn); ### end premarket definitions #FibChoices v1 #Request usethinkscript.com @theelderwand to duplicate a swimdicators video's chart's indicator #BLT 20191224 #v1 20191228 per request @zeek, changed time range method so that timerange_begin/timerange_end inputs would allow for post close of prior day, current day premarket, and custom user time ranges, with the Fibonacci lines drawn across the entire chart. input method = {default premarket, aggregation, developing_reg_thrs, developing_ext_thrs, time_range}; input aggregation = AggregationPeriod.DAY; input aggregation_periodsback = 0; input timerange_begin = 0000; input timerange_end = 1600; # Examples: pre-market setting: 0000,0929; previous close through pre-market: 1600,0929; # intraday opening range: 0930,1030 input display_upper_extended_fibs = no; input display_lower_extended_fibs = no; input fib1 = .0; input fib2 = .0; input fib3 = .0; input fib4 = .78; input fib5 = .88; input showchart_bubbles = no; input bubble_option = {default fiblevel, pricelevel}; input bubblemover = 1; #used to move the bubble left and right def bn = BarNumber(); #Time Range - Determines High/Low Where TimeRange_Begin Times are Between 1600 and 2359 Following the Prior Day's Close def globexOpen = if (if GetDayOfWeek(GetYYYYMMDD()) == 1 then SecondsFromTime(timerange_begin) >= 0 else SecondsFromTime(timerange_begin) >= 0) or SecondsFromTime(timerange_end) < 0 then 1 else 0; def globexReset = if globexOpen and !globexOpen[1] then 1 else 0; def globexHigh = CompoundValue(1, If((high > globexHigh[1] and globexOpen) or globexReset, high, globexHigh[1]), high); def globexLow = CompoundValue(1, If((low < globexLow[1] and globexOpen) or globexReset, low, globexLow[1]), low); def globexhighbn = if high == (globexHigh) then bn else globexhighbn[1]; def globexlowbn = if low == (globexLow) then bn else globexlowbn[1]; #Time Range - Determines High/Low with TimeRange_Begin Times From 0000 for PreMarket, Current Day or Intraday Pereiods def day = GetDay() == GetLastDay(); def lastday = if day and SecondsFromTime(timerange_begin) >= 0 then bn else Double.NaN; def hhamt = if day and bn == LowestAll(lastday) then high else if SecondsFromTime(timerange_end) <= 0 then Max(high, hhamt[1]) else hhamt[1]; def hhbn = if high == (hhamt) then bn else hhbn[1]; def llamt = if day and bn == LowestAll(lastday) then low else if SecondsFromTime(timerange_end) <= 0 then Min(low, llamt[1]) else llamt[1]; def llbn = if low == (llamt) then bn else llbn[1]; #Fibonacci High/Lows Determined based upon input method selected def hh; def ll; switch (method){ case premarket: hh = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then if x1 > x2 then h1 else h2 else 0); ll = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then if x1 > x2 then c1 else c2 else 0); case aggregation: hh = high(period = aggregation)[aggregation_periodsback]; ll = low(period = aggregation)[aggregation_periodsback]; case developing_reg_thrs: hh = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then high else if GetTime() > RegularTradingStart(GetYYYYMMDD()) and high > hh[1] then high else hh[1]; ll = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD()) then low else if GetTime() > RegularTradingStart(GetYYYYMMDD()) and low < ll[1] then low else ll[1]; case developing_ext_thrs: hh = if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD()) then high else if high > hh[1] then high else hh[1]; ll = if GetTime() crosses above RegularTradingEnd(GetYYYYMMDD()) then low else if low < ll[1] then low else ll[1]; case time_range: #Lines will display across the entire chart hh = if Between(timerange_begin, 1600, 2359) then HighestAll(if BarNumber() == HighestAll(globexhighbn) then globexHigh else Double.NaN) else HighestAll(if bn == HighestAll(hhbn) then (hhamt) else Double.NaN); ll = if Between(timerange_begin, 1600, 2359) then LowestAll(if BarNumber() == HighestAll(globexlowbn) then globexLow else Double.NaN) else LowestAll(if bn == HighestAll(llbn) then llamt else Double.NaN); } #Fibonacci Line Plots def range = hh - ll; def f1 = ll + range * fib1; def f2 = ll + range * fib2; def f3 = ll + range * fib3; def f4 = ll + range * fib4; def f5 = ll + range * fib5; def hh1 = hh; def ll1 = ll; def ff1 = f1; def ff2 = f2; def ff3 = f3; def ff4 = f4; def ff5 = f5; input fib_line_choice = {default horizontal, points, dashes}; def paintingStrategy = if fib_line_choice == fib_line_choice.horizontal then PaintingStrategy.HORIZONTAL else if fib_line_choice == fib_line_choice.points then PaintingStrategy.POINTS else PaintingStrategy.DASHES; plot hhp = if method==method.premarket and barnumber()<= c2bn then double.nan else hh1; hhp.SetPaintingStrategy(paintingStrategy); hhp.SetDefaultColor(Color.GREEN); hhp.SetLineWeight(1); plot llp = if method==method.premarket and barnumber()<= c2bn then double.nan else ll1; llp.SetPaintingStrategy(paintingStrategy); llp.SetDefaultColor(Color.RED); llp.SetLineWeight(1); plot f1p = if method==method.premarket and barnumber()<= c2bn then double.nan else ff1; f1p.SetPaintingStrategy(paintingStrategy); f1p.SetDefaultColor(Color.YELLOW); f1p.SetLineWeight(1); plot f2p = if method==method.premarket and barnumber()<= c2bn then double.nan else ff2; f2p.SetPaintingStrategy(paintingStrategy); f2p.SetDefaultColor(Color.YELLOW); f2p.SetLineWeight(1); plot f3p = if method==method.premarket and barnumber()<= c2bn then double.nan else ff3; f3p.SetPaintingStrategy(paintingStrategy); f3p.SetDefaultColor(Color.WHITE); f3p.SetLineWeight(1); plot f4p = if method==method.premarket and barnumber()<= c2bn then double.nan else ff4; f4p.SetPaintingStrategy(paintingStrategy); f4p.SetDefaultColor(Color.YELLOW); f4p.SetLineWeight(1); plot f5p = if method==method.premarket and barnumber()<= c2bn then double.nan else ff5; f5p.SetPaintingStrategy(paintingStrategy); f5p.SetDefaultColor(Color.YELLOW); f5p.SetLineWeight(1); #Fibonacci - Upper Extended Fib Lines plot uhhp = if display_upper_extended_fibs == no then Double.NaN else range + hh1; uhhp.SetPaintingStrategy(paintingStrategy); uhhp.SetDefaultColor(Color.GREEN); uhhp.SetLineWeight(1); plot uf1p = if display_upper_extended_fibs == no then Double.NaN else range + ff1; uf1p.SetPaintingStrategy(paintingStrategy); uf1p.SetDefaultColor(Color.YELLOW); uf1p.SetLineWeight(1); plot uf2p = if display_upper_extended_fibs == no then Double.NaN else range + ff2; uf2p.SetPaintingStrategy(paintingStrategy); uf2p.SetDefaultColor(Color.YELLOW); uf2p.SetLineWeight(1); plot uf3p = if display_upper_extended_fibs == no then Double.NaN else range + ff3; uf3p.SetPaintingStrategy(paintingStrategy); uf3p.SetDefaultColor(Color.WHITE); uf3p.SetLineWeight(1); plot uf4p = if display_upper_extended_fibs == no then Double.NaN else range + ff4; uf4p.SetPaintingStrategy(paintingStrategy); uf4p.SetDefaultColor(Color.YELLOW); uf4p.SetLineWeight(1); plot uf5p = if display_upper_extended_fibs == no then Double.NaN else range + ff5; uf5p.SetPaintingStrategy(paintingStrategy); uf5p.SetDefaultColor(Color.YELLOW); uf5p.SetLineWeight(1); #Fibonacci - Lower Extended Fib Lines plot Lllp = if display_lower_extended_fibs == no then Double.NaN else ll - range; Lllp.SetPaintingStrategy(paintingStrategy); Lllp.SetDefaultColor(Color.RED); Lllp.SetLineWeight(1); plot Lf1p = if display_lower_extended_fibs == no then Double.NaN else ll - range * fib1; Lf1p.SetPaintingStrategy(paintingStrategy); Lf1p.SetDefaultColor(Color.YELLOW); Lf1p.SetLineWeight(1); plot Lf2p = if display_lower_extended_fibs == no then Double.NaN else ll - range * fib2; Lf2p.SetPaintingStrategy(paintingStrategy); Lf2p.SetDefaultColor(Color.YELLOW); Lf2p.SetLineWeight(1); plot Lf3p = if display_lower_extended_fibs == no then Double.NaN else ll - range * fib3; Lf3p.SetPaintingStrategy(paintingStrategy); Lf3p.SetDefaultColor(Color.WHITE); Lf3p.SetLineWeight(1); plot Lf4p = if display_lower_extended_fibs == no then Double.NaN else ll - range * fib4; Lf4p.SetPaintingStrategy(paintingStrategy); Lf4p.SetDefaultColor(Color.YELLOW); Lf4p.SetLineWeight(1); plot Lf5p = if display_lower_extended_fibs == no then Double.NaN else ll - range * fib5; Lf5p.SetPaintingStrategy(paintingStrategy); Lf5p.SetDefaultColor(Color.YELLOW); Lf5p.SetLineWeight(1); #Bubbles Displaying Fib Levels in Right Expansion def n = bubblemover; def n1 = n + 1; AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), hhp[n1], 1, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), ff1[n1], fib1, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), ff2[n1], fib2, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), ff3[n1], fib3, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), ff4[n1], fib4, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), ff5[n1], fib5, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), llp[n1], 0, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uf1p[n1], 1 + fib1, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uf2p[n1], 1 + fib2, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uf3p[n1], 1 + fib3, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uf4p[n1], 1 + fib4, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uf5p[n1], 1 + fib5, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lf1p[n1], -fib1, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lf2p[n1], -fib2, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lf3p[n1], -fib3 , Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lf4p[n1], -fib4, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lf5p[n1], -fib5, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), uhhp[n1], 2, Color.GRAY, yes); AddChartBubble(showchart_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), Lllp[n1], -1, Color.GRAY, yes); #Hiding Fib Line Price Levels Usually Displayed on Right Price Axis hhp.HideBubble(); llp.HideBubble(); uhhp.HideBubble(); Lllp.HideBubble(); f1p.HideBubble(); f2p.HideBubble(); f3p.HideBubble(); f4p.HideBubble(); f5p.HideBubble(); Lf1p.HideBubble(); Lf2p.HideBubble(); Lf3p.HideBubble(); Lf4p.HideBubble(); Lf5p.HideBubble(); uf1p.HideBubble(); uf2p.HideBubble(); uf3p.HideBubble(); uf4p.HideBubble(); uf5p.HideBubble();
Last edited by a moderator: