Auto Fib (Fibonacci) Levels Indicator for ThinkorSwim

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

Ive been looking for a study like this for a long time !! Thank you so much !! @tomsk How can I add 2 more fib levels to this study the 1.272 and the -27.2 ? please help lol
 
The code that @BLT provided is very customizable. You can either change the fib levels via the user interface for any of the following standard definitions

Code:
input fib1level           = .236;
input fib2level           = .382;
input fibMlevel           = .500;
input fib3level           = .618;
input fib4level           = .764;
input fib5level           = 1.618;
input fib6level           = 2.618;
input fib7level           = -.618;
input fib8level           = -1.618;

Or, you can define more fib levels and add more plot lines. Either approach should be fine
 
Is it possible to plot the swing hi/low further out...if you have a 5-10 day chart plot it 5-10 days out instead of that trading day...More helpful for swing trades
 
Is it possible to plot the swing hi/low further out...if you have a 5-10 day chart plot it 5-10 days out instead of that trading day...More helpful for swing trades
Thank you . Im going to look to see what you added so I can learn . Thank you so much BLT ! @BLT Why when I goto TSLA the fibs are flipped upside down ? the negative fibs are up above the high and the positive fib levels are below ?

Hopefully v1.3 covers both the above comments

Code:
#SwingHL_Fibs
#v1    - 01.19.2020 - BLT - Swing High/Low with options to show all swings or last swing, fib levels, number of swings to display fib levels, and chart bubbles identifying swings and fib levels. The fib bubbles can be moved left/right in the expansion by the number of bars at the input: bubble_mover 
#v1.2  - 01.19.2020 - BLT - Made fib bubbles automatically reflect fib level inputs, which were hard coded in v1
#v1.3  = 01.20.2020 - BLT - Added more fib lines, adjusted fib levels based upon whether r2 is 100%, and extended fib lines into all of expansion

input showlastswing = yes;
input swing_back1 = 5;
input swing_forward1 = 5;
input maxbars1 = 30;

def na = Double.NaN;
def bn = barnumber();
def sb1 = swing_back1;
def sf1 = swing_forward1;
def lfor1 = Lowest(low, sf1)[-sf1];
def lback1 = Lowest(low, sb1)[1];
def swinglow1 = if low < lfor1 and low < lback1 then 1 else 0;
def swinglowbar = if low < lfor1 and low < lback1 then bn else na;
def hfor1  = Highest(high, sf1)[-sf1];
def hback1 = Highest(high, sb1)[1];
def swinghigh1   = if high > hfor1 and high > hback1 then 1 else 0;
def swinghighbar = if high > hfor1 and high > hback1 then bn else na;
def lastswing    = highestall(swinghighbar)>highestall(swinglowbar);

#Swings identifed as points
plot r2 = if showlastswing ==yes and bn==highestall(swinghighbar) then high else if showlastswing == no and swinghigh1 then high else na;
plot s2 = if showlastswing == yes and bn==highestall(swinglowbar) then low else if showlastswing == no and swinglow1 then low else na;

r2.SetStyle(Curve.POINTS);
r2.SetLineWeight(4);
r2.SetDefaultColor(Color.GREEN);
r2.HideBubble();
s2.SetStyle(Curve.POINTS);
s2.SetLineWeight(4);
s2.SetDefaultColor(Color.RED);
s2.HideBubble();

input bubbleoffsetfactor  = 3;
input showbubbles_r2s2    = yes;
AddChartBubble(showbubbles_r2s2 and r2, high + TickSize() * bubbleoffsetfactor, "R2", Color.GREEN, yes);
AddChartBubble(showbubbles_r2s2 and s2, low - TickSize() * bubbleoffsetfactor, "S2", Color.RED, no);

#Store Previous Data
def r2save = if !IsNaN(r2) then r2 else r2save[1];
def s2save = if !IsNaN(s2) then s2 else s2save[1];

#Fibonacci

input numberfibstoshow = 1;

#Retracement Fibonacci Levels
input fib1level           = .236;
input fib2level           = .382;
input fibMlevel           = .500;
input fib3level           = .618;
input fib4level           = .764;

#Extended Fibonacci Levels - These will create complementary extensions below zero as well as those inputs below which are over 1
input showfibextensions   = yes;
input fib5extlevel        = 1.272;
input fib6extlevel        = 1.618;
input fib7extlevel        = 2.618;
input fib8extlevel        = 4.236;

#Fibonacci Lines
def fibh  = r2save;
def fibl  = s2save;
def range = if isnan(close) then range[1] else fibh - fibl;
def data  = CompoundValue(1, if (r2save == high) and range<0 or (s2save == low) and range>0 then data[1] + 1 else data[1], 0);
def datacount = if isnan(close) then datacount[1] else (HighestAll(data) - data[1]) + 1;

input showfiblines = yes;

plot fibHp = r2save;
plot fibLp = s2save;
def fibmext=if !isnan(fibl + range * fibMlevel) then fibmext[1] else fibl + range * fibMlevel;
plot fibM  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fibMlevel else na;
plot fib1  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib1level else na;
plot fib2  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib2level else na;
plot fib3  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib3level else na;
plot fib4  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib4level else na;

plot fib5  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib5extlevel else na;
plot fib6  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib6extlevel else na;
plot fib7  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib7extlevel else na;
plot fib8  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib8extlevel else na;

plot fib5a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib5extlevel else na;
plot fib6a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib6extlevel else na;
plot fib7a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib7extlevel else na;
plot fib8a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib8extlevel else na;

fibHp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibLp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibHp.SetLineWeight(2);
fibLp.SetLineWeight(2);
fibM.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibHp.SetDefaultColor(Color.GREEN);
fibLp.SetDefaultColor(Color.RED);
fibM.SetDefaultColor(Color.WHITE);
fib1.SetDefaultColor(Color.CYAN);
fib2.SetDefaultColor(Color.YELLOW);
fib3.SetDefaultColor(Color.YELLOW);
fib4.SetDefaultColor(Color.CYAN);
fib5.setdefaultColor(color.yellow);
fib6.setdefaultColor(color.yellow);
fib7.setdefaultColor(color.yellow);
fib8.setdefaultColor(color.yellow);
fib5a.setdefaultColor(color.yellow);
fib6a.setdefaultColor(color.yellow);
fib7a.setdefaultColor(color.yellow);
fib8a.setdefaultColor(color.yellow);

#To Hide Price Bubbles on Right Axis, remove the "#" sign on the following HideBubble() code
#fibM.HideBubble();
#fib1.HideBubble();
#fib2.HideBubble();
#fib3.HideBubble();
#fib4.HideBubble();
#fib5.HideBubble();
#fib6.HideBubble();
#fib7.HideBubble();
#fib8.HideBubble();
#fib5a.HideBubble();
#fib6a.HideBubble();
#fib7a.HideBubble();
#fib8a.HideBubble();

input showfib_bubbles = yes;
input bubble_mover    = 5; #Moves bubbles left/right then number of bars input here
def   n               = bubble_mover;
def   n1              = n +1;
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibh[n1],  aspercent(if !lastswing then 1 else 0), Color.green, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibl[n1],  aspercent(if !lastswing then 0 else 1), Color.red, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib1[n1],  if !lastswing then aspercent(fib1level) else aspercent(1-fib1level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib2[n1],  if !lastswing then aspercent(fib2level) else aspercent(1-fib2level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib3[n1],  if !lastswing then aspercent(fib3level) else aspercent(1-fib3level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib4[n1],  if !lastswing then aspercent(fib4level) else aspercent(1-fib4level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibm[n1],  if !lastswing then aspercent(fibmlevel) else aspercent(1-fibmlevel), Color.white, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5[n1], if lastswing then aspercent(1-fib5extlevel) else aspercent(fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6[n1], if lastswing then  aspercent(1-fib6extlevel) else aspercent(fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7[n1], if lastswing then aspercent(1-fib7extlevel) else aspercent(fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8[n1], if lastswing then aspercent(1-fib8extlevel) else aspercent(fib8extlevel), Color.yellow, yes);

AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5a[n1], if lastswing then aspercent(fib5extlevel) else aspercent(1-fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6a[n1], if lastswing then aspercent(fib6extlevel) else aspercent(1-fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7a[n1], if lastswing then aspercent(fib7extlevel) else aspercent(1-fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8a[n1], if lastswing then aspercent(fib8extlevel) else aspercent(1-fib8extlevel), Color.yellow, yes);
 
@BLT Could this be made into a scan for anything on the Daily that closes above the 61.8% of the latest trend ?
Added a scanning capability for retracements as described in the header of the following study. Please save the study as: SwingHL_Fibs_v14 to use the study in the scanner
Code:
#SwingHL_Fibs_v14
#v1    - 01.19.2020 - BLT - Swing High/Low with options to show all swings or last swing, fib levels, number of swings to display fib levels, and chart bubbles identifying swings and fib levels. The fib bubbles can be moved left/right in the expansion by the number of bars at the input: bubble_mover 
#v1.2  - 01.19.2020 - BLT - Made fib bubbles automatically reflect fib level inputs, which were hard coded in v1
#v1.3  - 01.20.2020 - BLT - Added more fib lines, adjusted fib levels based upon whether r2 is 100%, and extended fib lines into all of expansion
#v1.4  - 01.20.2020 - BLT - Added a scan capability for fib retracement levels. The plot: lastswing determines by being 1 or 0 the fib percentage levels as in the following Table

#Table to determine retracement fib levels for scan purposes
#lastswing ==   1 (100%)     0  (0%)  Upper Level of most recent swing
#fib1      ==   23.6%        76.4%
#fib2      ==   38.2%        61.8%
#fibm      ==   50.0%        50.0%
#fib3      ==   61.8%        38.2%
#fib4      ==   76.4%        23.6%

#Example Scan #1 requested where close > 61.8% and lastswing = 1 (100%) with this studies name saved as: SwingHL_Fibs_v14
  #In Scanner enter: close > SwingHL_Fibs_v14().fib3 and SwingHL_Fibs_v14().lastswing is equal to 1

#Example Scan #2 where close crosses above 61.8% within "x" bars and lastswing = 1 (100%) with this studies name saved as: SwingHL_Fibs_v14
  #In Scanner enter: close crosses above SwingHL_Fibs_v14().fib3 within 3 bars and SwingHL_Fibs_v14().lastswing is equal to 1

input showlastswing = yes;
input swing_back1 = 5;
input swing_forward1 = 5;
input maxbars1 = 30;

def na = Double.NaN;
def bn = barnumber();
def sb1 = swing_back1;
def sf1 = swing_forward1;
def lfor1 = Lowest(low, sf1)[-sf1];
def lback1 = Lowest(low, sb1)[1];
def swinglow1 = if low < lfor1 and low < lback1 then 1 else 0;
def swinglowbar = if low < lfor1 and low < lback1 then bn else na;
def hfor1  = Highest(high, sf1)[-sf1];
def hback1 = Highest(high, sb1)[1];
def swinghigh1   = if high > hfor1 and high > hback1 then 1 else 0;
def swinghighbar = if high > hfor1 and high > hback1 then bn else na;
plot lastswing    = highestall(swinghighbar)>highestall(swinglowbar);

#Swings identifed as points
plot r2 = if showlastswing ==yes and bn==highestall(swinghighbar) then high else if showlastswing == no and swinghigh1 then high else na;
plot s2 = if showlastswing == yes and bn==highestall(swinglowbar) then low else if showlastswing == no and swinglow1 then low else na;

r2.SetStyle(Curve.POINTS);
r2.SetLineWeight(4);
r2.SetDefaultColor(Color.GREEN);
r2.HideBubble();
s2.SetStyle(Curve.POINTS);
s2.SetLineWeight(4);
s2.SetDefaultColor(Color.RED);
s2.HideBubble();

input bubbleoffsetfactor  = 3;
input showbubbles_r2s2    = yes;
AddChartBubble(showbubbles_r2s2 and r2, high + TickSize() * bubbleoffsetfactor, "R2", Color.GREEN, yes);
AddChartBubble(showbubbles_r2s2 and s2, low - TickSize() * bubbleoffsetfactor, "S2", Color.RED, no);

#Store Previous Data
def r2save = if !IsNaN(r2) then r2 else r2save[1];
def s2save = if !IsNaN(s2) then s2 else s2save[1];

#Fibonacci

input numberfibstoshow = 1;

#Retracement Fibonacci Levels
input fib1level           = .236;
input fib2level           = .382;
input fibMlevel           = .500;
input fib3level           = .618;
input fib4level           = .764;

#Extended Fibonacci Levels - These will create complementary extensions below zero as well as those inputs below which are over 1

input fib5extlevel        = 1.272;
input fib6extlevel        = 1.618;
input fib7extlevel        = 2.618;
input fib8extlevel        = 4.236;

#Fibonacci Lines
def fibh  = r2save;
def fibl  = s2save;
def range = if isnan(close) then range[1] else fibh - fibl;
def data  = CompoundValue(1, if (r2save == high) and range<0 or (s2save == low) and range>0 then data[1] + 1 else data[1], 0);
def datacount = if isnan(close) then datacount[1] else (HighestAll(data) - data[1]) + 1;

input showfiblines = yes;

plot fibHp = r2save;
plot fibLp = s2save;
def fibmext=if !isnan(fibl + range * fibMlevel) then fibmext[1] else fibl + range * fibMlevel;
plot fibM  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fibMlevel else na;
plot fib1  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib1level else na;
plot fib2  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib2level else na;
plot fib3  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib3level else na;
plot fib4  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib4level else na;

plot fib5  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib5extlevel else na;
plot fib6  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib6extlevel else na;
plot fib7  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib7extlevel else na;
plot fib8  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib8extlevel else na;

plot fib5a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib5extlevel else na;
plot fib6a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib6extlevel else na;
plot fib7a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib7extlevel else na;
plot fib8a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib8extlevel else na;

fibHp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibLp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibHp.SetLineWeight(2);
fibLp.SetLineWeight(2);
fibM.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibHp.SetDefaultColor(Color.GREEN);
fibLp.SetDefaultColor(Color.RED);
fibM.SetDefaultColor(Color.WHITE);
fib1.SetDefaultColor(Color.CYAN);
fib2.SetDefaultColor(Color.YELLOW);
fib3.SetDefaultColor(Color.YELLOW);
fib4.SetDefaultColor(Color.CYAN);
fib5.setdefaultColor(color.yellow);
fib6.setdefaultColor(color.yellow);
fib7.setdefaultColor(color.yellow);
fib8.setdefaultColor(color.yellow);
fib5a.setdefaultColor(color.yellow);
fib6a.setdefaultColor(color.yellow);
fib7a.setdefaultColor(color.yellow);
fib8a.setdefaultColor(color.yellow);

#To Hide Price Bubbles on Right Axis, remove the "#" sign on the following HideBubble() code
#fibM.HideBubble();
#fib1.HideBubble();
#fib2.HideBubble();
#fib3.HideBubble();
#fib4.HideBubble();
#fib5.HideBubble();
#fib6.HideBubble();
#fib7.HideBubble();
#fib8.HideBubble();
#fib5a.HideBubble();
#fib6a.HideBubble();
#fib7a.HideBubble();
#fib8a.HideBubble();

input showfib_bubbles = yes;
input bubble_mover    = 5; #Moves bubbles left/right then number of bars input here
def   n               = bubble_mover;
def   n1              = n +1;
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibh[n1],  aspercent(if !lastswing then 1 else 0), Color.green, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibl[n1],  aspercent(if !lastswing then 0 else 1), Color.red, yes);

#Fib Retracement Level Chart Bubbles
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib1[n1],  if !lastswing then aspercent(fib1level) else aspercent(1-fib1level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib2[n1],  if !lastswing then aspercent(fib2level) else aspercent(1-fib2level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib3[n1],  if !lastswing then aspercent(fib3level) else aspercent(1-fib3level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib4[n1],  if !lastswing then aspercent(fib4level) else aspercent(1-fib4level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibm[n1],  if !lastswing then aspercent(fibmlevel) else aspercent(1-fibmlevel), Color.white, yes);

#Fib Extension Levels Chart Bubbles
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5[n1], if lastswing then aspercent(1-fib5extlevel) else aspercent(fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6[n1], if lastswing then  aspercent(1-fib6extlevel) else aspercent(fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7[n1], if lastswing then aspercent(1-fib7extlevel) else aspercent(fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8[n1], if lastswing then aspercent(1-fib8extlevel) else aspercent(fib8extlevel), Color.yellow, yes);

AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5a[n1], if lastswing then aspercent(fib5extlevel) else aspercent(1-fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6a[n1], if lastswing then aspercent(fib6extlevel) else aspercent(1-fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7a[n1], if lastswing then aspercent(fib7extlevel) else aspercent(1-fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8a[n1], if lastswing then aspercent(fib8extlevel) else aspercent(1-fib8extlevel), Color.yellow, yes);
 
@BLT Im slowley learning this so dont be too hard on me lol . So I saved SwingHL_Fibs_v14 as a study i go into the scan tab and i add it as study the 61.8 is Fib 3 i highlight that but i cant find a way to specify a close above that level . What am i not doing wrong ? Or what am I not understanding
 
@Buckbull Allow me to respond to behalf of @BLT. He has created V1.4 of the code to be scannable. Here's the process how you'd perform the scan. First save the study. Let's assume you call this study "MyStudy". Once this is done, go to the scanner and enter the following

Code:
close crosses above MyStudy()."fib3"

This will scan for all stocks that have crossed above the 61.8% retracement level. I just ran this scan using a daily aggregation of the S&P 500 and obtained 33 results.
 
@Buckbull Allow me to respond to behalf of @BLT. He has created V1.4 of the code to be scannable. Here's the process how you'd perform the scan. First save the study. Let's assume you call this study "MyStudy". Once this is done, go to the scanner and enter the following

Code:
close crosses above MyStudy()."fib3"

This will scan for all stocks that have crossed above the 61.8% retracement level. I just ran this scan using a daily aggregation of the S&P 500 and obtained 33 results.
SO frustrating when I think im getting the hang of this code stuff , and script stuff reality sets in and i realize I **** AT IT !! The whole study I saved i want it for $10.00 stock with above 1,000,000 volume and when i goto study i click on V1.4 what next @tomsk ? Im lost :mad:
 
It's actually not that difficult if you take it step by step. For starters do have a look at the Scan tutorial that @markos put together. It has all the components of what a scan would comprise. In my opinion it is an excellent piece of work. Take the time to do so and it will be time well spent. It has lots of good info and may take some time to digest.

https://usethinkscript.com/threads/how-to-use-the-tos-scanner.284/
I will read it thanks , I have everything there but i dont know where to specify about the close above the 61.8 am I adding that in the script somewhere ?
 
Added a scanning capability for retracements as described in the header of the following study. Please save the study as: SwingHL_Fibs_v14 to use the study in the scanner
Code:
#SwingHL_Fibs_v14
#v1    - 01.19.2020 - BLT - Swing High/Low with options to show all swings or last swing, fib levels, number of swings to display fib levels, and chart bubbles identifying swings and fib levels. The fib bubbles can be moved left/right in the expansion by the number of bars at the input: bubble_mover
#v1.2  - 01.19.2020 - BLT - Made fib bubbles automatically reflect fib level inputs, which were hard coded in v1
#v1.3  - 01.20.2020 - BLT - Added more fib lines, adjusted fib levels based upon whether r2 is 100%, and extended fib lines into all of expansion
#v1.4  - 01.20.2020 - BLT - Added a scan capability for fib retracement levels. The plot: lastswing determines by being 1 or 0 the fib percentage levels as in the following Table

#Table to determine retracement fib levels for scan purposes
#lastswing ==   1 (100%)     0  (0%)  Upper Level of most recent swing
#fib1      ==   23.6%        76.4%
#fib2      ==   38.2%        61.8%
#fibm      ==   50.0%        50.0%
#fib3      ==   61.8%        38.2%
#fib4      ==   76.4%        23.6%

#Example Scan #1 requested where close > 61.8% and lastswing = 1 (100%) with this studies name saved as: SwingHL_Fibs_v14
  #In Scanner enter: close > SwingHL_Fibs_v14().fib3 and SwingHL_Fibs_v14().lastswing is equal to 1

#Example Scan #2 where close crosses above 61.8% within "x" bars and lastswing = 1 (100%) with this studies name saved as: SwingHL_Fibs_v14
  #In Scanner enter: close crosses above SwingHL_Fibs_v14().fib3 within 3 bars and SwingHL_Fibs_v14().lastswing is equal to 1

input showlastswing = yes;
input swing_back1 = 5;
input swing_forward1 = 5;
input maxbars1 = 30;

def na = Double.NaN;
def bn = barnumber();
def sb1 = swing_back1;
def sf1 = swing_forward1;
def lfor1 = Lowest(low, sf1)[-sf1];
def lback1 = Lowest(low, sb1)[1];
def swinglow1 = if low < lfor1 and low < lback1 then 1 else 0;
def swinglowbar = if low < lfor1 and low < lback1 then bn else na;
def hfor1  = Highest(high, sf1)[-sf1];
def hback1 = Highest(high, sb1)[1];
def swinghigh1   = if high > hfor1 and high > hback1 then 1 else 0;
def swinghighbar = if high > hfor1 and high > hback1 then bn else na;
plot lastswing    = highestall(swinghighbar)>highestall(swinglowbar);

#Swings identifed as points
plot r2 = if showlastswing ==yes and bn==highestall(swinghighbar) then high else if showlastswing == no and swinghigh1 then high else na;
plot s2 = if showlastswing == yes and bn==highestall(swinglowbar) then low else if showlastswing == no and swinglow1 then low else na;

r2.SetStyle(Curve.POINTS);
r2.SetLineWeight(4);
r2.SetDefaultColor(Color.GREEN);
r2.HideBubble();
s2.SetStyle(Curve.POINTS);
s2.SetLineWeight(4);
s2.SetDefaultColor(Color.RED);
s2.HideBubble();

input bubbleoffsetfactor  = 3;
input showbubbles_r2s2    = yes;
AddChartBubble(showbubbles_r2s2 and r2, high + TickSize() * bubbleoffsetfactor, "R2", Color.GREEN, yes);
AddChartBubble(showbubbles_r2s2 and s2, low - TickSize() * bubbleoffsetfactor, "S2", Color.RED, no);

#Store Previous Data
def r2save = if !IsNaN(r2) then r2 else r2save[1];
def s2save = if !IsNaN(s2) then s2 else s2save[1];

#Fibonacci

input numberfibstoshow = 1;

#Retracement Fibonacci Levels
input fib1level           = .236;
input fib2level           = .382;
input fibMlevel           = .500;
input fib3level           = .618;
input fib4level           = .764;

#Extended Fibonacci Levels - These will create complementary extensions below zero as well as those inputs below which are over 1

input fib5extlevel        = 1.272;
input fib6extlevel        = 1.618;
input fib7extlevel        = 2.618;
input fib8extlevel        = 4.236;

#Fibonacci Lines
def fibh  = r2save;
def fibl  = s2save;
def range = if isnan(close) then range[1] else fibh - fibl;
def data  = CompoundValue(1, if (r2save == high) and range<0 or (s2save == low) and range>0 then data[1] + 1 else data[1], 0);
def datacount = if isnan(close) then datacount[1] else (HighestAll(data) - data[1]) + 1;

input showfiblines = yes;

plot fibHp = r2save;
plot fibLp = s2save;
def fibmext=if !isnan(fibl + range * fibMlevel) then fibmext[1] else fibl + range * fibMlevel;
plot fibM  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fibMlevel else na;
plot fib1  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib1level else na;
plot fib2  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib2level else na;
plot fib3  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib3level else na;
plot fib4  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib4level else na;

plot fib5  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib5extlevel else na;
plot fib6  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib6extlevel else na;
plot fib7  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib7extlevel else na;
plot fib8  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibl + range * fib8extlevel else na;

plot fib5a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib5extlevel else na;
plot fib6a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib6extlevel else na;
plot fib7a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib7extlevel else na;
plot fib8a  = if showfiblines == no then na else if datacount <= numberfibstoshow then fibh + range * -fib8extlevel else na;

fibHp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibLp.SetPaintingStrategy(PaintingStrategy.DASHES);
fibHp.SetLineWeight(2);
fibLp.SetLineWeight(2);
fibM.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib5a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib6a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib7a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fib8a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibHp.SetDefaultColor(Color.GREEN);
fibLp.SetDefaultColor(Color.RED);
fibM.SetDefaultColor(Color.WHITE);
fib1.SetDefaultColor(Color.CYAN);
fib2.SetDefaultColor(Color.YELLOW);
fib3.SetDefaultColor(Color.YELLOW);
fib4.SetDefaultColor(Color.CYAN);
fib5.setdefaultColor(color.yellow);
fib6.setdefaultColor(color.yellow);
fib7.setdefaultColor(color.yellow);
fib8.setdefaultColor(color.yellow);
fib5a.setdefaultColor(color.yellow);
fib6a.setdefaultColor(color.yellow);
fib7a.setdefaultColor(color.yellow);
fib8a.setdefaultColor(color.yellow);

#To Hide Price Bubbles on Right Axis, remove the "#" sign on the following HideBubble() code
#fibM.HideBubble();
#fib1.HideBubble();
#fib2.HideBubble();
#fib3.HideBubble();
#fib4.HideBubble();
#fib5.HideBubble();
#fib6.HideBubble();
#fib7.HideBubble();
#fib8.HideBubble();
#fib5a.HideBubble();
#fib6a.HideBubble();
#fib7a.HideBubble();
#fib8a.HideBubble();

input showfib_bubbles = yes;
input bubble_mover    = 5; #Moves bubbles left/right then number of bars input here
def   n               = bubble_mover;
def   n1              = n +1;
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibh[n1],  aspercent(if !lastswing then 1 else 0), Color.green, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibl[n1],  aspercent(if !lastswing then 0 else 1), Color.red, yes);

#Fib Retracement Level Chart Bubbles
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib1[n1],  if !lastswing then aspercent(fib1level) else aspercent(1-fib1level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib2[n1],  if !lastswing then aspercent(fib2level) else aspercent(1-fib2level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib3[n1],  if !lastswing then aspercent(fib3level) else aspercent(1-fib3level), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib4[n1],  if !lastswing then aspercent(fib4level) else aspercent(1-fib4level), Color.cyan, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibm[n1],  if !lastswing then aspercent(fibmlevel) else aspercent(1-fibmlevel), Color.white, yes);

#Fib Extension Levels Chart Bubbles
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5[n1], if lastswing then aspercent(1-fib5extlevel) else aspercent(fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6[n1], if lastswing then  aspercent(1-fib6extlevel) else aspercent(fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7[n1], if lastswing then aspercent(1-fib7extlevel) else aspercent(fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8[n1], if lastswing then aspercent(1-fib8extlevel) else aspercent(fib8extlevel), Color.yellow, yes);

AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5a[n1], if lastswing then aspercent(fib5extlevel) else aspercent(1-fib5extlevel) , Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6a[n1], if lastswing then aspercent(fib6extlevel) else aspercent(1-fib6extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7a[n1], if lastswing then aspercent(fib7extlevel) else aspercent(1-fib7extlevel), Color.yellow, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8a[n1], if lastswing then aspercent(fib8extlevel) else aspercent(1-fib8extlevel), Color.yellow, yes);
@tomsk This is the whole code I am saving correct ?
 
I have already explained that in post # 114 but in case it is not clear, look at the V1.4 code (post #112) where you see that 61.8% is held by the variable fib3level. So once price crosses ABOVE that level your scan condition is hit. I really can't do anything else for you my friend, I have explained all I can. Best of luck.
 
Thanks for all of the enhancements and variations to this AutoFib study @tomsk @BLT @theelderwand !!

@Buckbull I spent days pulling the information together for the Scanner information in #116 above. It is detailed. It takes time, but that is how we all started. Please go through everything twice.
Also in the Tutorials is this excellent piece by @Townsend https://usethinkscript.com/threads/stock-screener-q-a-tips-tricks.1262/
Then there is this: https://usethinkscript.com/threads/thinkscript-q-a-with-links.283/
Next, please click on the Education Tab along the top of ThinkorSwim. That will take you into the TDAmeritrade Learning site. Tons of videos there.

This video is from Don Kaufman who was one of the first employees of TOS. He had direct input in the building of the scanner.

Everything you could need except sleep is located above. Good luck, it will take time.
 
Thanks for all of the enhancements and variations to this AutoFib study @tomsk @BLT @theelderwand !!

@Buckbull I spent days pulling the information together for the Scanner information in #116 above. It is detailed. It takes time, but that is how we all started. Please go through everything twice.
Also in the Tutorials is this excellent piece by @Townsend https://usethinkscript.com/threads/stock-screener-q-a-tips-tricks.1262/
Then there is this: https://usethinkscript.com/threads/thinkscript-q-a-with-links.283/
Next, please click on the Education Tab along the top of ThinkorSwim. That will take you into the TDAmeritrade Learning site. Tons of videos there.

This video is from Don Kaufman who was one of the first employees of TOS. He had direct input in the building of the scanner.

Everything you could need except sleep is located above. Good luck, it will take time.
@markos Im watching and reading right now . Thank you
 
@markos Im watching and reading right now . Thank you
You're welcome. You'll get it, just keep trying different things. btw, some of the people we are blessed with for helping us are busy working as professional traders in Asia, India and Europe. We want to be sure that we respect their time as they certainly don't have to give it.
We all must be measured in our questions, I guess is what I'm saying. :)
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
437 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.
Back
Top