Pivot Point Oscillator by Algotrdr
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © algotrdr_

//@version=4

study("Pivot Point Oscillator by Algotrdr", shorttitle="Pivot Point Oscillator by Algotrdr")

Length = input(20, minval=1)
pivchoice = input(title="Pivot Timeframe", defval="Daily", options=["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"])

f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src[1], lookahead = barmerge.lookahead_on)
lastclose = pivchoice == "Daily" ? f_secureSecurity(syminfo.tickerid, "D", close) : pivchoice == "Weekly" ? f_secureSecurity(syminfo.tickerid, "W", close) : pivchoice == "Monthly" ? f_secureSecurity(syminfo.tickerid, "M", close) : pivchoice == "Yearly" ? f_secureSecurity(syminfo.tickerid, "12M", close) :  pivchoice == "Quarterly" ? f_secureSecurity(syminfo.tickerid, "3M", close) : na
lastopen = pivchoice == "Daily" ? f_secureSecurity(syminfo.tickerid, "D", open) : pivchoice == "Weekly" ? f_secureSecurity(syminfo.tickerid, "W", open) : pivchoice == "Monthly" ? f_secureSecurity(syminfo.tickerid, "M", open) : pivchoice == "Yearly" ? f_secureSecurity(syminfo.tickerid, "12M", open) :  pivchoice == "Quarterly" ? f_secureSecurity(syminfo.tickerid, "3M", open) : na
lasthigh = pivchoice == "Daily" ? f_secureSecurity(syminfo.tickerid, "D", high) : pivchoice == "Weekly" ? f_secureSecurity(syminfo.tickerid, "W", high) : pivchoice == "Monthly" ? f_secureSecurity(syminfo.tickerid, "M", high) : pivchoice == "Yearly" ? f_secureSecurity(syminfo.tickerid, "12M", high) :  pivchoice == "Quarterly" ? f_secureSecurity(syminfo.tickerid, "3M", high) : na
lastlow = pivchoice == "Daily" ? f_secureSecurity(syminfo.tickerid, "D", low) : pivchoice == "Weekly" ? f_secureSecurity(syminfo.tickerid, "W", low) : pivchoice == "Monthly" ? f_secureSecurity(syminfo.tickerid, "M", low) : pivchoice == "Yearly" ? f_secureSecurity(syminfo.tickerid, "12M", low) : pivchoice == "Quarterly" ? f_secureSecurity(syminfo.tickerid, "3M", low) : na

pivbase = (lasthigh + lastlow + lastclose) / 3
HL = lasthigh - lastlow

TR5 = pivbase * 4 + (lasthigh - 4 * lastlow)
TR4 = pivbase * 3 + (lasthigh - 3 * lastlow)
TR3 = pivbase * 2 + (lasthigh - 2 * lastlow)
TR2 = pivbase + 1 * (lasthigh - lastlow)
TR1 = pivbase * 2 - lastlow
TS1 = pivbase * 2 - lasthigh
TS2 = pivbase - 1 * (lasthigh - lastlow)
TS3 = pivbase * 2 - (2 * lasthigh - lastlow)
TS4 = pivbase * 3 - (3 * lasthigh - lastlow)
TS5 = pivbase * 4 - (4 * lasthigh - lastlow)

R5 = pivbase + (HL * 2.618)
R4 = pivbase + (HL * 1.618)
R3 = pivbase + (HL * 1.000)
R2 = pivbase + (HL * 0.618)
R1 = pivbase + (HL * 0.382)
S1 = pivbase - (HL * 0.382)
S2 = pivbase - (HL * 0.618)
S3 = pivbase - (HL * 1.000)
S4 = pivbase - (HL * 1.618)
S5 = pivbase - (HL * 2.618)

woodiebase = (lasthigh + lastlow + lastopen * 2)/4

WR3 = lasthigh + 2 * (woodiebase - lastlow) 
WR2 = woodiebase + 1 * HL
WR1 = woodiebase * 2 - lastlow
WS1 = woodiebase * 2 - lasthigh
WS2 = woodiebase - 1 * HL
WS3 = lastlow - 2 * (lasthigh - woodiebase)
WS4 = WS3 - HL
WR4 = WR3 + HL

longmoments = (open < R1 and close > R1) or (open < R2 and close > R2) or (open < R3 and close > R3) or (open < R4 and close > R4) or (open < R5 and close > R5) or (open < pivbase and close > pivbase) or (open < S1 and close > S1) or (open < S2 and close > S2) or (open < S3 and close > S3) or (open < S4 and close > S4) or (open < S5 and close > S5)
shortmoments = (open > R1 and close < R1) or (open > R2 and close < R2) or (open > R3 and close < R3) or (open > R4 and close < R4) or (open > R5 and close < R5) or (open > pivbase and close < pivbase) or (open > S1 and close < S1) or (open > S2 and close < S2) or (open > S3 and close < S3) or (open > S4 and close < S4) or (open > S5 and close < S5)

Tlongmoments = (open < TR1 and close > TR1) or (open < TR2 and close > TR2) or (open < TR3 and close > TR3) or (open < TR4 and close > TR4) or (open < TR5 and close > TR5) or (open < pivbase and close > pivbase) or (open < TS1 and close > TS1) or (open < TS2 and close > TS2) or (open < TS3 and close > TS3) or (open < TS4 and close > TS4) or (open < TS5 and close > TS5)
Tshortmoments = (open > TR1 and close < TR1) or (open > TR2 and close < TR2) or (open > TR3 and close < TR3) or (open > TR4 and close < TR4) or (open > TR5 and close < TR5) or (open > pivbase and close < pivbase) or (open > TS1 and close < TS1) or (open > TS2 and close < TS2) or (open > TS3 and close < TS3) or (open > TS4 and close < TS4) or (open > TS5 and close < TS5)

Wlongmoments = (open < WR1 and close > WR1) or (open < WR2 and close > WR2) or (open < WR3 and close > WR3) or (open < pivbase and close > pivbase) or (open < WS1 and close > WS1) or (open < WS2 and close > WS2) or (open < WS3 and close > WS3)
Wshortmoments = (open > WR1 and close < WR1) or (open > WR2 and close < WR2) or (open > WR3 and close < WR3) or (open > pivbase and close < pivbase) or (open > WS1 and close < WS1) or (open > WS2 and close < WS2) or (open > WS3 and close < WS3)

float FL = 0.0
FL := longmoments ? 1 : 0
float FS = 0.0
FS := shortmoments ? 1 : 0

float TL = 0.0
TL := Tlongmoments ? 1 : 0
float TS = 0.0
TS := Tshortmoments ? 1 : 0

float WL = 0.0
WL := Wlongmoments ? 1 : 0
float WS = 0.0
WS := Wshortmoments ? 1 : 0

x= FL + TL + WL
x2= FS + TS + WS
ema = ema((sum(x,Length)), Length) - ema((sum(x2,Length)), Length)

hline(0)

colorset = (ema >= 0 and ema > ema[1]) ? #388e3c : (ema >= 0 and ema < ema[1]) ? #a5d6a7 : (ema < 0 and ema < ema[1]) ? #d32f2f : (ema < 0 and ema > ema[1]) ? #ef9a9a : na

plot(ema, style=plot.style_columns, color=colorset)

