This Pine Script is designed to create a TradingView indicator called "Lifetime High Drop Indicator" that helps identify candles that have dropped a specified percentage from the lifetime high within a defined lookback period.
Here is a detailed explanation of the script:
Indicator Declaration: pine
indicator(title="Lifetime High Drop Indicator", shorttitle="LTH Drop", overlay=true) This line defines the indicator title, short title, and specifies that the indicator should be overlaid on the price chart.
User Inputs: pine
lookbackPeriod = input.int(20, title="Lookback Period (Days)") dropThreshold = input.float(20, title="Drop Threshold (%)") These input statements allow the user to input the lookback period in days and the percentage drop threshold for identifying qualifying candles.
Variable Initialization: pine
var float lifetimeHigh = na var int recentQualifiedBar = na These lines initialize variables to store the lifetime high price and the index of the most recent candle that meets the drop threshold.
Lifetime High Calculation: pine
if na(lifetimeHigh) or high > lifetimeHigh lifetimeHigh := high This block of code updates the lifetime high value whenever a new high is encountered.
Percentage Drop Calculation: pine
currentDropPercent = (lifetimeHigh - close) / lifetimeHigh * 100 Calculates the percentage drop from the lifetime high for each candle.
Highlight Condition Logic: pine
highlightCondition = (currentDropPercent >= dropThreshold) and (bar_index >= (last_bar_index - lookbackPeriod)) Defines the logic for identifying candles within the lookback period that meet the threshold for a significant drop from the lifetime high.
Update Recent Qualified Bar: pine
if highlightCondition recentQualifiedBar := bar_index Updates the index of the most recent candle that meets the drop threshold.
Plot Shapes and Labels: pine
plotshape(series=highlightCondition, location=location.belowbar, style=shape.triangledown, size=size.tiny, color=color.green) if highlightCondition label.new(bar_index, low - 3, text=str.tostring(currentDropPercent, '0.0') + "%", style=label.style_label_up, size=size.tiny, color=color.new(color.green, 0), textcolor=color.black, yloc=yloc.belowbar) Plots a down arrow shape and a label for all candles that meet the drop threshold.
Updating the Lifetime High Line: pine
var line l = na if bar_index == 0 l := line.new(x1=bar_index, y1=lifetimeHigh, x2=bar_index, y2=lifetimeHigh, color=color.blue, width=1, extend=extend.right) else line.set_xy1(l, x=0, y=lifetimeHigh) line.set_xy2(l, x=bar_index, y=lifetimeHigh) This code keeps the lifetime high line up-to-date across the chart by either creating a new line or updating the existing line.
Overall, this script calculates the percentage drop from the lifetime high for each candle, highlights candles that meet the drop threshold within the specified lookback period, and provides visual clues on the price chart for these qualifying candles.