Magic Fibonacci 1.272 Indicator for ThinkorSwim v2

korygill

korygill

Member
VIP
Warehouse
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

http://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];

# 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;
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.
 
D

Duction

New member
korygill, is there a way to edit the script to include additional fib levels? 1.618? Thank you.
 
C

Craighaber71

Member
VIP
Warehouse
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
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@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));
 
C

Craighaber71

Member
VIP
Warehouse
I copied and pasted to custom scan and it came back with errors?
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Craighaber71 The code in my comment was a respond to @Duction question. Was not meant to be used with the scanner.
 
AGD

AGD

New member
Marketplace
After adding study, TOS chart shrinks.
Any idea, Why this happening
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@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
C

Craighaber71

Member
VIP
Warehouse
@AGD.....Go to settings the price axis....and click on fit studies
 
D

Duction

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

BenTen

Administrative
Staff
VIP
Warehouse
@Duction That I’m not sure. Maybe @korygill can help.
 
S

skynetgen

Active member
Warehouse
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);
 
S

skynetgen

Active member
Warehouse
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
 

Top