Magic Fibonacci 1.272 Indicator for ThinkorSwim v2


Active member
I have re-worked the code for Magic Fibonacci 1.272 in this post. Please see that post for more background on this indicator, and its origination.

This Fibonacci indicator was inspired by a day trader on Twitter (@pierhk). He traces his fibs "the unconventional way" by connecting the close of the previous day with the close of the first opening bar from the current trading day. He often does it on the 5m time frame to find resistance and use it as profit target.

This update, version 2.0, has new logic to allow the indicator to show when the Extended session is visible and when the Extended session is not visible.

Sidebar: it took me a few iterations and some debugging to find the right conditions, and work around a few bugs/nuances in thinkscript around time. I will post another thread on that soon.

Here is an example of a flex grid with Extended hours visible and not visible. Notice the Magic Fibonacci 1.272 Indicators displays as expected on both charts.

Magic Fibonacci 1.272


Link to Flex Grid

Source Code for Magic Fibonacci 1.272

# Magic Fibonacci 1.272
# Author: Kory Gill, @korygill
# Gap feature modfied by WalkingBallista
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190823-1300-KG    - fixed logic to find first bar of trading day
#                       so study works with extended sessions showing/not showing.
#                       Removed logic around gapUp/gapDown.
# ...
# ...
declare upper;
declare hide_on_daily;
declare once_per_bar;

def vClose = close;
def nan = Double.NaN;

# debug and extra indicator flags
# could make input variables, but that makes
# the study name longer, and most people
# probably want these off by default
def showPrevClose = 0;
def showOpen = 0;

# logic
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollOver[-1] and firstBarOfDay[-1])
    then 1
    else 0;

def dayOpen = if firstBarOfDay then vClose else dayOpen[1];
def prevDayClose = if lastBarOfDay then vClose else prevDayClose[1];

# fibs
def delta = if firstBarOfDay then dayOpen - prevDayClose else delta[1];
def fib1272 = if firstBarOfDay then prevDayClose + delta * 1.272 else fib1272[1];

# colors, see
# plots
# Plot nan on the bar before a new plot starts on firstBarOfDay.
# This causes the plot to draw without an annoying connecting line from
# one day to the next.
plot fib = if firstBarOfDay[-1] then nan else fib1272;

plot pOpen = if showOpen and !firstBarOfDay[-1] then dayOpen else nan;
plot pPrevClose = if showPrevClose and !lastBarOfDay[-1] then prevDayClose else nan;

Disclaimers and Notices

You are free to use this code for personal use, and make derivative works from it. You are not granted permission to use this code (or derivative works) for commercial purposes which includes and is not limited to selling, reselling, or packaging with other commercial indicators. Headers and attribution in this code should remain as provided, and any derivative works should extend the existing headers.


Active member
2019 Donor
I've been trying to scan for stocks that cross the magic fib but keeps coming back with no results..Any chance that you have a scan for this...Thanks


@Duction Here is the code to plot Fib 1.618. Note: this add-on is from the previous version so it will not work when you have extended market on.

def fib1618 = prevDayClose + delta * 1.618;
def pFib1618 = if firstBarOfDay[-1] then nan else fib1618;
plot fib16 = pFib1618;


@AGD It works fine for me until I scroll to the end of the left side. Looks like it was caused by the oldest Fib from pre-market. Is that your issue as well? One way to fix this is just to scroll a bit to the right side (to skip the oldest pre-market session) or turn of extended hours.
  • Like
Reactions: AGD


New member
@BenTen is there a way to display the price and coefficient for the corresponding fib level? Similar to what displays when using the fib retracement tool in TOS. Thank you.


Well-known member
Nice touch of adding no vertical line between days fix. but there is small bug with it : it doesnt display first and last bar during current day. here is the fix:

plot firstfib=if isrollover or isrollover[1] then fib1272 else nan;
plot fib =  if  isrollover then nan else fib1272;
fib.setpaintingStrategy(paintingStrategy.LINE_VS_POINTS); fib.setLineWeight(2);


Well-known member
I really like this indicator. here is version with 0.618 level added and more bugs fixed. Best used on 30m chart, but useful on 5m as well
here is the signal:
in between lines: avoid
above 1.272 - long . below - short. By itself is not sufficient but a great check indicator


Well-known member
can this be converted into scan on 5 min aggregation ?
Also possible to plot/scan price difference between 1.272 and 0.618 levels be 10% and higher?

Why not a scan when the candles pierce through the 1.272? But also have the scan show up in on "result change" the time it happens. Many times if the candle does not pierce through the 1.272 it serves as support and resistance...So I am only assuming any % from it would more or less feel like consolidation.


Active member
2019 Donor
The guy that has been referenced as an inspiration for this indicator @pierhk doesn't seem to be on Twitter anymore. Does anyone have a link to his youtube by any chance or any other online location? Thank you.

Similar threads