# Pivots and 50% with Auto for ThinkorSwim
# Converted from PineScript by ERD using Grok, xAI
# Version: 1.7.4 - Feb 19, 2025
# Inputs
input pivotType = {default Traditional, Fibonacci, Woodie, Classic, DM, Camarilla};
input timeFrame = {default Auto, Day, Week, Month, Quarter, Year};
input lookBack = 2; # Number of pivots back to show
input useDailyBased = yes; # Use daily-based values or intraday
input autoPivots = yes; # Enable Auto Pivots feature
input showLabels = yes; # Show labels on chart
# Define aggregation period based on timeframe input
def aggPeriod;
switch (timeFrame) {
case Auto:
aggPeriod = if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN then AggregationPeriod.DAY else AggregationPeriod.WEEK;
case Day:
aggPeriod = AggregationPeriod.DAY;
case Week:
aggPeriod = AggregationPeriod.WEEK;
case Month:
aggPeriod = AggregationPeriod.MONTH;
case Quarter:
aggPeriod = AggregationPeriod.QUARTER;
case Year:
aggPeriod = AggregationPeriod.YEAR;
}
# Fetch OHLC data from selected timeframe
def h = high(period = aggPeriod)[1];
def l = low(period = aggPeriod)[1];
def c = close(period = aggPeriod)[1];
def o = open(period = aggPeriod);
# Variables to track highest high and lowest low for Auto Pivots
def periodHigh = high(period = aggPeriod);
def periodLow = low(period = aggPeriod);
# Helper variables for pivot calculations
def range = h - l;
def rangeW = h - l;
def rangeC = h - l;
def rangeCam = h - l;
def x = if c > o then h * 2 + l + c else if c < o then h + l * 2 + c else h + l + c * 2;
# Flags to determine pivot type
def isTraditional = pivotType == pivotType.Traditional;
def isFibonacci = pivotType == pivotType.Fibonacci;
def isWoodie = pivotType == pivotType.Woodie;
def isClassic = pivotType == pivotType.Classic;
def isDM = pivotType == pivotType.DM;
def isCamarilla = pivotType == pivotType.Camarilla;
# Detect new timeframe period
def isNewPeriod = if aggPeriod == AggregationPeriod.DAY then GetDay() != GetDay()[1]
else if aggPeriod == AggregationPeriod.WEEK then GetWeek() != GetWeek()[1]
else if aggPeriod == AggregationPeriod.MONTH then GetMonth() != GetMonth()[1]
else if aggPeriod == AggregationPeriod.QUARTER then Floor((GetMonth() - 1) / 3) != Floor((GetMonth()[1] - 1) / 3)
else if aggPeriod == AggregationPeriod.YEAR then GetYear() != GetYear()[1]
else GetDay() != GetDay()[1]; # Fallback for Auto
# Count periods back
def periodCount = if isNewPeriod then periodCount[1] + 1 else periodCount[1];
# Define current year and limit to last lookBack periods
def currentYear = GetYear();
def minYear = currentYear - lookBack; # e.g., 2025 - 2 = 2023
def dataYear = if aggPeriod == AggregationPeriod.YEAR then GetYear() - 1 else GetYear(); # Adjust for [1] offset
def withinLookBack = if aggPeriod == AggregationPeriod.YEAR then dataYear > minYear and dataYear <= currentYear else periodCount <= lookBack;
# Final pivot calculations
def PP = if withinLookBack then
if isTraditional or isFibonacci or isClassic then (h + l + c) / 3
else if isWoodie then (h + l + o * 2) / 4
else if isDM then x / 4
else if isCamarilla then (h + l + c) / 3
else Double.NaN
else Double.NaN;
def R1 = if withinLookBack then
if isTraditional or isClassic then PP * 2 - l
else if isFibonacci then PP + 0.382 * range
else if isWoodie then PP * 2 - l
else if isDM then x / 2 - l
else if isCamarilla then c + rangeCam * 1.1 / 12.0
else Double.NaN
else Double.NaN;
def S1 = if withinLookBack then
if isTraditional or isClassic then PP * 2 - h
else if isFibonacci then PP - 0.382 * range
else if isWoodie then PP * 2 - h
else if isDM then x / 2 - h
else if isCamarilla then c - rangeCam * 1.1 / 12.0
else Double.NaN
else Double.NaN;
def R2 = if withinLookBack then
if isTraditional then PP + range
else if isFibonacci then PP + 0.618 * range
else if isWoodie then PP + rangeW
else if isClassic then PP + rangeC
else if isCamarilla then c + rangeCam * 1.1 / 6.0
else Double.NaN
else Double.NaN;
def S2 = if withinLookBack then
if isTraditional then PP - range
else if isFibonacci then PP - 0.618 * range
else if isWoodie then PP - rangeW
else if isClassic then PP - rangeC
else if isCamarilla then c + rangeCam * 1.1 / 6.0
else Double.NaN
else Double.NaN;
def R3 = if withinLookBack then
if isTraditional then PP * 2 + (h - 2 * l)
else if isFibonacci then PP + 1 * range
else if isWoodie then h + 2 * (PP - l)
else if isClassic then PP + 2 * rangeC
else if isCamarilla then c + rangeCam * 1.1 / 4.0
else Double.NaN
else Double.NaN;
def S3 = if withinLookBack then
if isTraditional then PP * 2 - (2 * h - l)
else if isFibonacci then PP - 1 * range
else if isWoodie then l - 2 * (h - PP)
else if isClassic then PP - 2 * rangeC
else if isCamarilla then c + rangeCam * 1.1 / 4.0
else Double.NaN
else Double.NaN;
def R4 = if withinLookBack then
if isTraditional then PP * 3 + (h - 3 * l)
else if isWoodie then R3 + rangeW
else if isClassic then PP + 3 * rangeC
else if isCamarilla then c + rangeCam * 1.1 / 2.0
else Double.NaN
else Double.NaN;
def S4 = if withinLookBack then
if isTraditional then PP * 3 - (3 * h - l)
else if isWoodie then S3 - rangeW
else if isClassic then PP - 3 * rangeC
else if isCamarilla then c + rangeCam * 1.1 / 2.0
else Double.NaN
else Double.NaN;
def R5 = if withinLookBack and isTraditional then PP * 4 + (h - 4 * l) else Double.NaN;
def S5 = if withinLookBack and isTraditional then PP * 4 - (4 * h - l) else Double.NaN;
def P_R1 = if withinLookBack and isTraditional then (PP + R1) / 2 else Double.NaN;
def R1_R2 = if withinLookBack and isTraditional then (R1 + R2) / 2 else Double.NaN;
def R2_R3 = if withinLookBack and isTraditional then (R2 + R3) / 2 else Double.NaN;
def R3_R4 = if withinLookBack and isTraditional then (R3 + R4) / 2 else Double.NaN;
def R4_R5 = if withinLookBack and isTraditional then (R4 + R5) / 2 else Double.NaN;
def P_S1 = if withinLookBack and isTraditional then (PP + S1) / 2 else Double.NaN;
def S1_S2 = if withinLookBack and isTraditional then (S1 + S2) / 2 else Double.NaN;
def S2_S3 = if withinLookBack and isTraditional then (S2 + S3) / 2 else Double.NaN;
def S3_S4 = if withinLookBack and isTraditional then (S3 + S4) / 2 else Double.NaN;
def S4_S5 = if withinLookBack and isTraditional then (S4 + S5) / 2 else Double.NaN;
# Auto Pivots logic with period break
def plotPP = if isNewPeriod then Double.NaN else if withinLookBack then PP else Double.NaN;
def plotR1 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > PP) then R1 else Double.NaN;
def plotS1 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < PP) then S1 else Double.NaN;
def plotR2 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R1) then R2 else Double.NaN;
def plotS2 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S1) then S2 else Double.NaN;
def plotR3 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R2) then R3 else Double.NaN;
def plotS3 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S2) then S3 else Double.NaN;
def plotR4 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R3) then R4 else Double.NaN;
def plotS4 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S3) then S4 else Double.NaN;
def plotR5 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R4) then R5 else Double.NaN;
def plotS5 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S4) then S5 else Double.NaN;
def plotP_R1 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > PP) then P_R1 else Double.NaN;
def plotR1_R2 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R1) then R1_R2 else Double.NaN;
def plotR2_R3 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R2) then R2_R3 else Double.NaN;
def plotR3_R4 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R3) then R3_R4 else Double.NaN;
def plotR4_R5 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodHigh > R4) then R4_R5 else Double.NaN;
def plotP_S1 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < PP) then P_S1 else Double.NaN;
def plotS1_S2 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S1) then S1_S2 else Double.NaN;
def plotS2_S3 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S2) then S2_S3 else Double.NaN;
def plotS3_S4 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S3) then S3_S4 else Double.NaN;
def plotS4_S5 = if isNewPeriod then Double.NaN else if withinLookBack and (!autoPivots or periodLow < S4) then S4_S5 else Double.NaN;
# Plot statements for chart and price scale
plot PPlot = plotPP;
PPlot.SetDefaultColor(Color.Yellow);
PPlot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotPP), "P: " + AsPrice(plotPP), Color.Yellow);
plot R1Plot = plotR1;
R1Plot.SetDefaultColor(Color.Yellow);
R1Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR1), "R1: " + AsPrice(plotR1), Color.Yellow);
plot S1Plot = plotS1;
S1Plot.SetDefaultColor(Color.Yellow);
S1Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS1), "S1: " + AsPrice(plotS1),Color.Yellow);
plot R2Plot = plotR2;
R2Plot.SetDefaultColor(Color.Yellow);
R2Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR2), "R2: " + AsPrice(plotR2), Color.Yellow);
plot S2Plot = plotS2;
S2Plot.SetDefaultColor(Color.Yellow);
S2Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS2), "S2: " + AsPrice(plotS2), Color.Yellow);
plot R3Plot = plotR3;
R3Plot.SetDefaultColor(Color.Yellow);
R3Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR3), "R3: " + AsPrice(plotR3), Color.Yellow);
plot S3Plot = plotS3;
S3Plot.SetDefaultColor(Color.Yellow);
S3Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS3), "S3: " + AsPrice(plotS3), Color.Yellow);
plot R4Plot = plotR4;
R4Plot.SetDefaultColor(Color.Yellow);
R4Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR4), "R4: " + AsPrice(plotR4), Color.Yellow);
plot S4Plot = plotS4;
S4Plot.SetDefaultColor(Color.Yellow);
S4Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS4), "S4: " + AsPrice(plotS4), Color.Yellow);
plot R5Plot = plotR5;
R5Plot.SetDefaultColor(Color.Yellow);
R5Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR5), "R5: " + AsPrice(plotR5), Color.Yellow);
plot S5Plot = plotS5;
S5Plot.SetDefaultColor(Color.Yellow);
S5Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS5), "S5: " + AsPrice(plotS5), Color.Yellow);
plot P_R1Plot = plotP_R1;
P_R1Plot.SetDefaultColor(Color.Yellow);
P_R1Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotP_R1), "P_R1: " + AsPrice(plotP_R1), Color.Yellow);
plot R1_R2Plot = plotR1_R2;
R1_R2Plot.SetDefaultColor(Color.Yellow);
R1_R2Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR1_R2), "R1_R2: " + AsPrice(plotR1_R2), Color.Yellow);
plot R2_R3Plot = plotR2_R3;
R2_R3Plot.SetDefaultColor(Color.ORANGE);
R2_R3Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR2_R3), "R2_R3: " + AsPrice(plotR2_R3), Color.ORANGE);
plot R3_R4Plot = plotR3_R4;
R3_R4Plot.SetDefaultColor( Color.Yellow);
R3_R4Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR3_R4), "R3_R4: " + AsPrice(plotR3_R4), Color.Yellow);
plot R4_R5Plot = plotR4_R5;
R4_R5Plot.SetDefaultColor( Color.Yellow);
R4_R5Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotR4_R5), "R4_R5: " + AsPrice(plotR4_R5), Color.Yellow);
plot P_S1Plot = plotP_S1;
P_S1Plot.SetDefaultColor( Color.Yellow);
P_S1Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotP_S1), "P_S1: " + AsPrice(plotP_S1), Color.Yellow);
plot S1_S2Plot = plotS1_S2;
S1_S2Plot.SetDefaultColor( Color.Yellow);
S1_S2Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS1_S2), "S1_S2: " + AsPrice(plotS1_S2), Color.Yellow);
plot S2_S3Plot = plotS2_S3;
S2_S3Plot.SetDefaultColor( Color.Yellow);
S2_S3Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS2_S3), "S2_S3: " + AsPrice(plotS2_S3), Color.Yellow);
plot S3_S4Plot = plotS3_S4;
S3_S4Plot.SetDefaultColor( Color.Yellow);
S3_S4Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS3_S4), "S3_S4: " + AsPrice(plotS3_S4), Color.Yellow);
plot S4_S5Plot = plotS4_S5;
S4_S5Plot.SetDefaultColor( Color.Yellow);
S4_S5Plot.SetStyle(Curve.Firm);
AddLabel(showLabels and !IsNaN(plotS4_S5), "S4_S5: " + AsPrice(plotS4_S5), Color.Yellow);
Auto Pivots and 50% (ToS).txt
Displaying Auto Pivots and 50% (ToS).txt.