Dynamic Fibonacci Pivot Points & EMA Crossovers
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PrawinPandey
// @version=5
indicator("Pivot Point, Fibonacci Levels, OHLC & EMA", "Pivot/OHLC/EMA", overlay=true)

ohlc_tip = "This will show the OHLC for selected range. If you select D then todays high-lows and previous days high-lows both will show up, same applies for Weekly and other timeframe's as well."
centrepptip = "This will show Centre Pivot Point lines for Daily, Weekly and Monthly timeframes atonce."

//Pivot Point
bs = input.bool(title="Enable Buy and Sell Indicator", defval=true, inline="BS", group="Buy and Sell Indicator", tooltip='This is initial release and not advised to follow it blindly. Use it on your own risk.')
cpp = input.bool(title="Enable/Disable Centre PP", defval=false, tooltip = centrepptip)
pivotpointsohlc = input.string(title="Timeframe", defval="D", options=["D", "W", "M", "3M", "6M", "12M"], inline="SPP", group="Fibonacci Pivot Points & OHLC")

fbpp = input.bool(title="Fibonacci", defval=true, inline="SPP", group="Fibonacci Pivot Points & OHLC")
ohlc = input.bool(title="OHLC", defval=false, inline="SPP", group="Fibonacci Pivot Points & OHLC")

fibonaccilevels = input.string(title="Timeframe", defval="W", options=["D", "W", "M", "3M", "6M", "12M"], inline="DPP", group="Fibonacci Levels")
dfl = input.bool(title="Dynamic", defval=false, inline="DPP", group="Fibonacci Levels")
ffl = input.bool(title="Fixed", defval=false, inline="DPP", group="Fibonacci Levels")

//OHLC for different timeframes
[dOpen, dHigh, dLow, pHigh, pLow, pClose] = request.security(syminfo.tickerid, pivotpointsohlc, [open, high, low, high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
[DHigh, DLow, fHigh, fLow] = request.security(syminfo.tickerid, fibonaccilevels, [high, low, high[1], low[1]], barmerge.gaps_off, barmerge.lookahead_on)

[High, Low, Close] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
[HIgh, LOw, CLose] = request.security(syminfo.tickerid, "W", [high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
[HIGH, LOW, CLOsE] = request.security(syminfo.tickerid, "M", [high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)

//Calculation
dRange = DHigh-DLow
pRange = pHigh-pLow
fRange = fHigh-fLow

//CPP Calculation
dcpp = (High + Low + Close) / 3.0
wcpp = (HIgh + LOw + CLose) / 3.0
mcpp = (HIGH + LOW + CLOsE) / 3.0

//Dynamic Range
P1 = DHigh - dRange * 0.000
P2 = DHigh - dRange * 0.236
P3 = DHigh - dRange * 0.382
DP = DHigh - dRange * 0.500
P4 = DHigh - dRange * 0.618
P5 = DHigh - dRange * 0.764
P6 = DHigh - dRange * 1.000

//Fixed Range
FP1 = fHigh - fRange * 0.000
FP2 = fHigh - fRange * 0.236
FP3 = fHigh - fRange * 0.382
FDP = fHigh - fRange * 0.500
FP4 = fHigh - fRange * 0.618
FP5 = fHigh - fRange * 0.764
FP6 = fHigh - fRange * 1.000

//Fibonacci
SPP = (pHigh + pLow + pClose) / 3.0
PR1 = SPP + pRange * 0.382
PR2 = SPP + pRange * 0.618
PR3 = SPP + pRange * 1.000
PS1 = SPP - pRange * 0.382
PS2 = SPP - pRange * 0.618
PS3 = SPP - pRange * 1.000

//Dynamic Plots
plot(dfl and P1 ? P1 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic R3", linewidth=2)
plot(dfl and P2 ? P2 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic R2", linewidth=2)
plot(dfl and P3 ? P3 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic R1", linewidth=2)
plot(dfl and DP ? DP : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic PP", linewidth=2)
plot(dfl and P4 ? P4 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic S1", linewidth=2)
plot(dfl and P5 ? P5 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic S2", linewidth=2)
plot(dfl and P6 ? P6 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Dynamic S3", linewidth=2)

//Fixed Plots
plot(ffl and FP1 ? FP1 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed R3", linewidth=2)
plot(ffl and FP2 ? FP2 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed R2", linewidth=2)
plot(ffl and FP3 ? FP3 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed R1", linewidth=2)
plot(ffl and FDP ? FDP : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed PP", linewidth=2)
plot(ffl and FP4 ? FP4 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed S1", linewidth=2)
plot(ffl and FP5 ? FP5 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed S2", linewidth=2)
plot(ffl and FP6 ? FP6 : na, style=plot.style_stepline, color=color.new(color.gray,65), title="Fixed S3", linewidth=2)

//Fibonacci Plots
plot(fbpp and PR3 ? PR3 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci R3")
plot(fbpp and PR2 ? PR2 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci R2")
plot(fbpp and PR1 ? PR1 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci R1")
plot(fbpp and SPP ? SPP : na, style=plot.style_stepline, color=color.new(color.purple,0), title="Fibonacci PP")
plot(fbpp and PS1 ? PS1 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci S1")
plot(fbpp and PS2 ? PS2 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci S2")
plot(fbpp and PS3 ? PS3 : na, style=plot.style_stepline, color=color.new(color.orange,0), title="Fibonacci S3")

//Centre Pivot Points
plot(cpp and dcpp ? dcpp : na, style=plot.style_stepline, color=color.new(color.purple,0), title="Daily CPP", linewidth=1)
plot(cpp and wcpp ? wcpp : na, style=plot.style_stepline, color=color.new(color.purple,0), title="Weekly CPP", linewidth=2)
plot(cpp and mcpp ? mcpp : na, style=plot.style_stepline, color=color.new(color.purple,0), title="Monthly CPP", linewidth=2)

//OHLC Plots
p1 = plot(ohlc and dOpen ? dOpen : na, style=plot.style_stepline, color=color.new(color.gray,90), title="Current Open")
plot(ohlc and dHigh ? dHigh : na, style=plot.style_circles, color=color.new(#13EC13,50), title="Current High")
plot(ohlc and dLow ? dLow : na, style=plot.style_circles, color=color.new(#FF0000,50), title="Current Low")
plot(ohlc and pHigh ? pHigh : na, style=plot.style_stepline, color=color.new(#13EC13,0), title="Previous High")
plot(ohlc and pLow ? pLow : na, style=plot.style_stepline, color=color.new(#FF0000,0), title="Previous Low")
p2 = plot(ohlc and pClose ? pClose : na, style=plot.style_stepline, color=color.new(color.blue,0), title="Previous Close")
fill(p1, p2, color=color.new(color.gray,90))

//EMA
ema = input.bool(title="EMA 14/21", defval=true, inline="EMA", group="EMA Crossover")
ema_a = input.bool(title="EMA 50/100", defval=false, inline="EMA", group="EMA Crossover")
ema_b = input.bool(title="EMA 150/200", defval=false, inline="EMA", group="EMA Crossover")

ema_14 = ta.ema(close, 14)
ema_21 = ta.ema(close, 21)
ema_50 = ta.ema(close, 50)
ema_100 = ta.ema(close, 100)
ema_150 = ta.ema(close, 150)
ema_200 = ta.ema(close, 200)

plot(ema and ema_14 ? ema_14 : na, title="EMA 14", color=color.new(color.red,0), linewidth=1)
plot(ema and ema_21 ? ema_21 : na, title="EMA 21", color=color.new(color.teal,0), linewidth=1)
plot(ema_a and ema_50 ? ema_50 : na, title="EMA 50", color=color.new(color.red,0), linewidth=2)
plot(ema_a and ema_100 ? ema_100 : na, title="EMA 100", color=color.new(color.teal,0), linewidth=2)
plot(ema_b and ema_150 ? ema_150 : na, title="EMA 150", color=color.new(color.red,0), linewidth=2)
plot(ema_b and ema_200 ? ema_200 : na, title="EMA 200", color=color.new(color.teal,0), linewidth=2)

//Variables
GC = open < close                   //Green Candle
RC = open > close                   //Red Candle
MC = (open + close)/2               //Middle Candle
CB = math.abs(close - open)         //Candle Body
CR = high - low                     //Candle Range
US = high - math.max(open, close)   //Upper Shadow
LS = math.min(open, close) - low    //Lower Shadow

//Label Position
LL = low - (ta.atr(30) * 0.6)//Label Low
LH = high + (ta.atr(30) * 0.6)//Label High

//SPINNING CANDLE
BLSC = (US <= CB and LS > 5 * CB)
BRSC = (US > 5 * CB and LS <= CB)
SCBuy = BLSC[1] and GC
SCSell = BRSC[1] and RC
if SCBuy and bs
    label.new(bar_index, LL, text="Buy", style=label.style_label_up, color = color.green, size=size.small, textcolor=color.white, tooltip='Buying pressure.')
if SCSell and bs
    label.new(bar_index, LH, text="Sell", style=label.style_label_down, color = color.red, size=size.small, textcolor=color.white, tooltip='Selling pressure.')
//---END---\\