Find the Fibonacci Sequence nth term In ThinkOrSwim
i made this with the intention that the sub script could be used in other projects. by itself, it is of no use for trading.
(someone asked for an average, weighted with fib series numbers, and this could be used for that project)
this study will return that nth term, in the Fibonacci sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,...
ex. the 5th term is 3
it will,
. display the nth term number in a label
. draw bubbles with, the barnumber and the series number, up to the desired nth number
i made this with the intention that the sub script could be used in other projects. by itself, it is of no use for trading.
(someone asked for an average, weighted with fib series numbers, and this could be used for that project)
this study will return that nth term, in the Fibonacci sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,...
ex. the 5th term is 3
it will,
. display the nth term number in a label
. draw bubbles with, the barnumber and the series number, up to the desired nth number
Ruby:
# fibonacci_series_nth_term_01
# calc the nth term in a fibonacci series
# https://math.hmc.edu/funfacts/fibonacci-number-formula/
# the sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
# the formula for the n-th term is:
# p1 = (1 + Sqrt[5]) / 2
# p2 = (1 – Sqrt[5]) / 2
# nterm = ( p1^n – p2^n ) / Sqrt(5)
#--------------------------
script fibnum {
input term = 1;
# adjust the input number, so the first output is 0
def n = term - 1;
def p1 = (1 + Sqrt(5))/2;
def p2 = (1 – Sqrt(5))/2;
def fterm = ( power(p1 , n) – power(p2, n ) ) / Sqrt(5);
plot fibvalue = fterm;
}
# ---------------------------
# main
# ---------------------------
def na = double.nan;
def bn = BarNumber();
input show_fib_terms = yes;
#--------------------
# test1 , show nth fib number in a label
input fib_term = 12;
def f1 = fibnum(fib_term);
AddLabel( show_fib_terms, fib_term + " fib term is: " + f1, Color.CYAN);
#--------------------
# test2 , show fib #'s in bubbles on the first few candles
def calcfib = (bn <= fib_term);
def fib_index = if calcfib then bn else na;
def f2 = if !calcfib then na else fibnum(fib_index);
AddChartBubble( show_fib_terms and calcfib, low, bn + "\n" + f2, Color.YELLOW, no);
#-------------------
#
Last edited: