# Magic Fibonacci 1.272 Indicator for ThinkorSwim v2

#### korygill

##### Well-known member
VIP
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

https://tos.mx/nVqihA

Source Code for Magic Fibonacci 1.272

Code:
``````# 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];

#
# 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;
fib.SetDefaultColor(CreateColor(0,0,255));

plot pOpen = if showOpen and !firstBarOfDay[-1] then dayOpen else nan;
plot pPrevClose = if showPrevClose and !lastBarOfDay[-1] then prevDayClose else nan;
pOpen.SetDefaultColor(CreateColor(0,255,0));
pPrevClose.SetDefaultColor(CreateColor(255,0,0));``````

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.

#### Attachments

• jKjrvTs.png
165.3 KB · Views: 923
korygill, is there a way to edit the script to include additional fib levels? 1.618? Thank you.

@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.

Code:
``````def fib1618 = prevDayClose + delta * 1.618;
def pFib1618 = if firstBarOfDay[-1] then nan else fib1618;
plot fib16 = pFib1618;
fib16.SetDefaultColor(CreateColor(0,0,255));``````

After adding study, TOS chart shrinks.
Any idea, Why this happening

@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.

AGD
@AGD.....Go to settings the price axis....and click on fit studies

@AGD.....Go to settings the price axis....and click on fit studies
I have that setting, happens when I add ext hour on 5min chart

@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:

Code:
``````plot firstfib=if isrollover or isrollover[1] then fib1272 else nan;
plot fib =  if  isrollover then nan else fib1272;
fib.SetDefaultColor(color.pink);
firstfib.SetDefaultColor(color.pink);
firstfib.setpaintingStrategy(paintingStrategy.LINE_VS_POINTS);firstfib.setLineWeight(2);
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?

@john3 Where do you see the 0.618 addition?

@BenTen On page 1, from Skynetgen

@john3 That's a modified version. If you want the original version, use the one in post #1.

[I dont get it. This amenmend script where should I added in the original script.

@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.

@john3

...connecting the close of the previous day with the close of the first opening bar from the current trading day.

87k+ Posts
259 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?