Fibonacci Time Ratios For ThinkOrSwim

SleepyZ

Moderator - Expert
VIP
Lifetime
Here is my attempt at Fibonacci Time Ratios using a base of a Swing High/Low study I had previously done.

It uses the input swing1's high and low pivots from one previous swing so that it is not changing while the candles are currently forming. Everything will start at left most high or low in the previous swing at 0% and will extend to a maximum of 4.23%. I have tested it intraday with or without extended hours shown and have felt that without might be better.

There is very little information on this topic that I could find and only found drawing tools rather than code as a basis. So I have no idea whether this is accurate or useful.

Screenshot-2021-08-19-163443.jpg
Ruby:
#SwingHL_2Levels_Fibs

input swing      = 9;
input maxbars    = 30;
input showlevels = Yes;
def sb           = swing;
def sf           = swing;
def na           = Double.NaN;
def lfor         = Lowest(low, sf)[-sf];
def lback        = Lowest(low, sb)[1];
def swinglow     = if low < lfor and low < lback then 1 else 0;
def slow         = if swinglow == 1 then low else slow[1];
plot s1          = if swinglow then low else na;
def hfor         = Highest(high, sf)[-sf];
def hback        = Highest(high, sb)[1];
def swinghigh    = if high > hfor and high > hback then 1 else 0;
def fast         = if swinghigh == 1 then high else fast[1];
plot r1          = if swinghigh then high else na;


input swing1         = 15;
input maxbars1       = 30;
input showlevels1    = Yes;
def sb1              = swing1;
def sf1              = swing1;
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 slow1            = if swinglow1 == 1 then low else slow1[1];
plot s2              = if swinglow1 then low - TickSize() * 0 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 fast1            = if swinghigh1 == 1 then high else fast1[1];
plot r2              = if swinghigh1 then high + TickSize() * 0 else na;
r2.SetStyle(Curve.POINTS);
r2.SetLineWeight(5);
r2.AssignValueColor(if fast1[1] < fast1 then Color.GREEN else if fast1[1] == fast1 then Color.YELLOW else Color.RED);
r2.HideBubble();
r1.SetStyle(Curve.POINTS);
r1.SetLineWeight(2);
r1.AssignValueColor(if fast[1] < fast then Color.GREEN else if fast[1] == fast then Color.YELLOW else Color.RED);
r1.HideBubble();
s2.SetStyle(Curve.POINTS);
s2.SetLineWeight(5);
s2.AssignValueColor(if slow1[1] < slow1 then Color.GREEN else if slow1[1] == slow1 then Color.YELLOW else Color.RED);
s2.HideBubble();
s1.SetStyle(Curve.POINTS);
s1.SetLineWeight(2);
s1.AssignValueColor(if slow[1] < slow then Color.GREEN else if slow[1] == slow then Color.YELLOW else Color.RED);
s1.HideBubble();

input showbubbles_r1s1 = no;
input bubblefactor     = 0;
AddChartBubble(showbubbles_r1s1 and r1, high + TickSize() * bubblefactor, "R1", Color.WHITE, yes);
AddChartBubble(showbubbles_r1s1 and s1, low - TickSize() * bubblefactor, "S1", Color.WHITE, no);
input showbubbles_r2s2 = no;
AddChartBubble(showbubbles_r2s2 and r2, high + TickSize() * bubblefactor, "R2", Color.GREEN, yes);
AddChartBubble(showbubbles_r2s2 and s2, low - TickSize() * bubblefactor, "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];
def r3save = if (r2save) != r2save[1] then r2save[1] else r3save[1];
def s3save = if (s2save) != s2save[1] then s2save[1] else s3save[1];

AddLabel(1, r3save + " " + s3save, Color.WHITE);
def r2bn = if !IsNaN(r2) then BarNumber() else r2bn[1];
def s2bn = if !IsNaN(s2) then BarNumber() else s2bn[1];
def r3bn = if (r2bn) != r2bn[1] then r2bn[1] else r3bn[1];
def s3bn = if (s2bn) != s2bn[1] then s2bn[1] else s3bn[1];

#AddLabel(1, r3bn + " " + s3bn, Color.WHITE);
#Fibonacci
rec data = CompoundValue(1, if (r2save == high) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]) + 1 ;
input numberfibstoshow = 3;

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;
input fib4alevel          = 1.272;
input fib7alevel          = 3.618;
input fib8alevel          = 4.23;


def fibh = max(r2save, s2save);
def fibl = min(r2save, s2save);
def range = absValue(fibh - fibl);
input showfiblines        = no;
input showfiblinesextreme = yes;
plot fibHp = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibh else Double.NaN;
plot fibLp = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibl else Double.NaN;
plot fibM  = if showfiblines == no then Double.NaN  else if datacount <= numberfibstoshow then fibl + range * fibMlevel else Double.NaN;
plot fib1  = if showfiblines == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib1level else Double.NaN;
plot fib2  = if showfiblines == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib2level else Double.NaN;
plot fib3  = if showfiblines == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib3level else Double.NaN;
plot fib4  = if showfiblines == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib4level else Double.NaN;
plot fib5  = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib5level else Double.NaN;
plot fib6  = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib6level else Double.NaN;
plot fib7  = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib7level else Double.NaN;
plot fib8  = if showfiblinesextreme == no then Double.NaN else if datacount <= numberfibstoshow then fibl + range * fib8level else Double.NaN;

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);
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);
fibHp.HideBubble();
fibLp.HideBubble();
fibM.HideBubble();
fib1.HideBubble();
fib2.HideBubble();
fib3.HideBubble();
fib4.HideBubble();
fib5.HideBubble();
fib6.HideBubble();
fib7.HideBubble();
fib8.HideBubble();

input showfib_bubbles = yes;
input n = 5;
def  n1 = n + 1;
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibh[n1], fibh[n1], Color.GREEN, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibl[n1], fibl[n1], Color.RED, no);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib4[n1], "23.6%", Color.CYAN, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib1[n1], "76.4%", Color.CYAN, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib3[n1], "38.2%", Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib2[n1], "61.8%", Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fibM[n1], "50.0%", Color.WHITE, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib5[n1], "161.8%", Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib6[n1], "261.8%", Color.YELLOW, yes);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib7[n1], "161.8%", Color.YELLOW, no);
AddChartBubble(showfib_bubbles and !IsNaN(close[n1]) and IsNaN(close[n]), fib8[n1], "261.8%", Color.YELLOW, no);

#---------------------------------------------------------
#Fibonacci Time Ratios

AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)), "                                           0%", color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == max(HighestAll(r3bn),highestall(s3bn)),  "                                         100%", color = Color.white, stroke = Curve.FIRM);

AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib1level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),  "                                           "+aspercent(fib1level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib2level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib2level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib3level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib3level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib4level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib4level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib4alevel * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib4alevel), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib5level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib5level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib6level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib6level), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib7alevel * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib7alevel), color = Color.white, stroke = Curve.FIRM);
AddVerticalLine(BarNumber() == min(HighestAll(r3bn),highestall(s3bn)) + Round(fib8alevel * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0),   "                                           "+aspercent(fib8alevel), color = Color.white, stroke = Curve.FIRM);
#AddLabel(1, HighestAll(r3bn) + Round(fib1level * AbsValue(HighestAll(r3bn) - (HighestAll(s3bn))), 0), Color.WHITE);
 
Last edited by a moderator:
Here is my attempt at Fibonacci Time Ratios using a base of a Swing High/Low study I had previously done.

It uses the input swing1's high and low pivots from one previous swing so that it is not changing while the candles are currently forming. Everything will start at left most high or low in the previous swing at 0% and will extend to a maximum of 4.23%. I have tested it intraday with or without extended hours shown and have felt that without might be better.

There is very little information on this topic that I could find and only found drawing tools rather than code as a basis. So I have no idea whether this is accurate or useful.
This is very useful. thank you and I am new member thinking of joining the VIP membership. Is it possible to add start and end date and time to this logic instead of using daily swing high/low, so there will be two inputs with start date and time and end date and time. fib extension and time series will be plotted based on those two dates/time. thanks in advance for your help or anyone else. time will be based on 1 min close.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
215 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