• Get $40 off VIP by signing up for a free account! Sign Up

Magic Fibonacci 1.272 Indicator for ThinkorSwim v2


Well-known 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 https://usethinkscript.com/threads/rainbow-indicators-%E2%80%93-bb-and-moving-average.294/
# 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.


  • jKjrvTs.png
    165.3 KB · Views: 923

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@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
@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.
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);
@BenTen I see...so the latest version with 0.618 addition, the logic is the same (trace from the prior day RTH close to the close of the first RTH bar next day), yes?
@heramone Are you looking for the script to plot Fib 1.272 on your chart? If so, the code and its shareable link are in post #1.

I just want to double-check something about what it plots. It takes yesterday's RTH close, waits for the 1st candle of the current day's RTH open, and plots 1.272 Fib from yesterday's RTH close to today's RTH, 1 candle Close. Is that correct? Thank you.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
259 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.