Fibonacci Ratios with Volatility(Weekly Time Frame.)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pbghosh

//@version=5
indicator(title='Fibonacci Ratios with Volatility', shorttitle='WFibo', overlay=true, format=format.price, precision=2)

var totLn = 0.0
var totLnSqr = 0.0
var lnAvg = 0.0
var lnSqrAvg = 0.0
var variance = 0.0
var dailyVolatility = 0.0
var price = 0.0
var priceRange = 0.0
var r1Level = 0.0
var r2Level = 0.0
var r3Level = 0.0
var r4Level = 0.0
var r5Level = 0.0
var r6Level = 0.0
var r7Level = 0.0
var r8Level = 0.0
var r9Level = 0.0
var s1Level = 0.0
var s2Level = 0.0
var s3Level = 0.0
var s4Level = 0.0
var s5Level = 0.0
var s6Level = 0.0
var s7Level = 0.0
var s8Level = 0.0
var s9Level = 0.0

is_new_day() =>
    d = dayofweek
    na(d[1]) or d != d[1]

bars = ta.barssince(is_new_day())

o = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', open, lookahead=barmerge.lookahead_on), 0)
ph = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', high[1], lookahead=barmerge.lookahead_on), 0)
pl = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', low[1], lookahead=barmerge.lookahead_on), 0)
pc = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[1], lookahead=barmerge.lookahead_on), 0)

lowerTimeframe = timeframe.period == '1' or timeframe.period == '2' or timeframe.period == '3' or timeframe.period == '5' or timeframe.period == '10' or timeframe.period == '15' or timeframe.period == '30' or timeframe.period == '45' or timeframe.period == '60' or timeframe.period == '120' or timeframe.period == '240'

c1 = ta.valuewhen(bars == 0, o > ph or o < pl ? o : request.security(syminfo.tickerid, 'W', close[1], lookahead=barmerge.lookahead_on), 0)
c2 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[2], lookahead=barmerge.lookahead_on), 0)
c3 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[3], lookahead=barmerge.lookahead_on), 0)
c4 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[4], lookahead=barmerge.lookahead_on), 0)
c5 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[5], lookahead=barmerge.lookahead_on), 0)
c6 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[6], lookahead=barmerge.lookahead_on), 0)
c7 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[7], lookahead=barmerge.lookahead_on), 0)
c8 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[8], lookahead=barmerge.lookahead_on), 0)
c9 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[9], lookahead=barmerge.lookahead_on), 0)
c10 = ta.valuewhen(bars == 0, request.security(syminfo.tickerid, 'W', close[10], lookahead=barmerge.lookahead_on), 0)

calculateLN() =>
    tot = 0.0
    tot := math.log(c1 / c2) + math.log(c2 / c3) + math.log(c3 / c4) + math.log(c4 / c5) + math.log(c5 / c6) + math.log(c6 / c7) + math.log(c7 / c8) + math.log(c8 / c9) + math.log(c9 / c10)
    tot

calculateLNSquare() =>
    tot = 0.0
    tot := math.pow(math.log(c1 / c2), 2) + math.pow(math.log(c2 / c3), 2) + math.pow(math.log(c3 / c4), 2) + math.pow(math.log(c4 / c5), 2) + math.pow(math.log(c5 / c6), 2) + math.pow(math.log(c6 / c7), 2) + math.pow(math.log(c7 / c8), 2) + math.pow(math.log(c8 / c9), 2) + math.pow(math.log(c9 / c10), 2)
    tot

calculatePivot() =>
    (ph + pl + pc) / 3

pivot = ta.valuewhen(bars == 0, calculatePivot(), 0)

totLn := ta.valuewhen(bars == 0, calculateLN(), 0)
totLnSqr := ta.valuewhen(bars == 0, calculateLNSquare(), 0)
lnAvg := ta.valuewhen(bars == 0, totLn / 9, 0)
lnSqrAvg := ta.valuewhen(bars == 0, totLnSqr / 9, 0)
variance := ta.valuewhen(bars == 0, lnSqrAvg - math.pow(lnAvg, 2), 0)
dailyVolatility := ta.valuewhen(bars == 0, math.sqrt(variance), 0)
price := ta.valuewhen(bars == 0, o > ph or o < pl ? o : c1, 0)
priceRange := ta.valuewhen(bars == 0, dailyVolatility * price, 0)

r1Level := priceRange * 0.236 + price
r2Level := priceRange * 0.382 + price
r3Level := priceRange * 0.500 + price
r4Level := priceRange * 0.618 + price
r5Level := priceRange * 0.786 + price
r6Level := priceRange * 1.000 + price
r7Level := priceRange * 1.236 + price
r8Level := priceRange * 1.618 + price
r9Level := priceRange * 2.000 + price
s1Level := price - priceRange * 0.236
s2Level := price - priceRange * 0.382
s3Level := price - priceRange * 0.500
s4Level := price - priceRange * 0.618
s5Level := price - priceRange * 0.786
s6Level := price - priceRange * 1.000
s7Level := price - priceRange * 1.236
s8Level := price - priceRange * 1.618
s9Level := price - priceRange * 2.000

plot(series=lowerTimeframe ? price : na, title='0%', color=pivot[1] != pivot and lowerTimeframe ? na : color.white, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r1Level : na, title='23.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.green, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r2Level : na, title='38.2%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r3Level : na, title='50%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r4Level : na, title='61.8%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r5Level : na, title='78.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r6Level : na, title='100%', color=pivot[1] != pivot and lowerTimeframe ? na : color.green, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r7Level : na, title='123.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.green, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r8Level : na, title='161.8%', color=pivot[1] != pivot and lowerTimeframe ? na : color.green, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? r9Level : na, title='200%', color=pivot[1] != pivot and lowerTimeframe ? na : color.green, style=plot.style_linebr, linewidth=1)

plot(series=lowerTimeframe ? s1Level : na, title='-23.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.red, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s2Level : na, title='-38.2%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s3Level : na, title='-50%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s4Level : na, title='-61.8%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s5Level : na, title='-78.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.gray, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s6Level : na, title='-100%', color=pivot[1] != pivot and lowerTimeframe ? na : color.red, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s7Level : na, title='-123.6%', color=pivot[1] != pivot and lowerTimeframe ? na : color.red, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s8Level : na, title='-161.8%', color=pivot[1] != pivot and lowerTimeframe ? na : color.red, style=plot.style_linebr, linewidth=1)
plot(series=lowerTimeframe ? s9Level : na, title='-200%', color=pivot[1] != pivot and lowerTimeframe ? na : color.red, style=plot.style_linebr, linewidth=1)

