GogoJiang Exclusive Public Release: Script to Calculate Rebound Amount on TradingView
In Pine Script (TradingView's scripting language), to create a script that calculates the rebound amount when a trend forms, you need to detect highs and lows based on specific conditions and compute their difference. Below is an example. This script calculates the price range when the price forms a high or a low and then rebounds from it.
pinescript//@version=5 indicator("Trend Reversal Amount", overlay=true) // Parameter settings length = input.int(14, title="Length") threshold = input.float(1.0, title="Reversal Threshold (%)") // Get highs and lows highestHigh = ta.highest(high, length) lowestLow = ta.lowest(low, length) // Define trend reversal conditions isUpTrend = close > ta.lowest(close, length) and close > ta.lowest(close[1], length) isDownTrend = close < ta.highest(close, length) and close < ta.highest(close[1], length) // Calculate rebound amount reboundAmount = 0.0 if (isUpTrend) reboundAmount := close - lowestLow if (isDownTrend) reboundAmount := highestHigh - close // Show alert when rebound amount exceeds threshold if (isUpTrend and reboundAmount > (lowestLow * threshold / 100)) label.new(x=bar_index, y=low, text="Up Rebound\n" + str.tostring(reboundAmount, format.mintick), color=color.green, style=label.style_label_down, textcolor=color.white, size=size.small) if (isDownTrend and reboundAmount > (highestHigh * threshold / 100)) label.new(x=bar_index, y=high, text="Down Rebound\n" + str.tostring(reboundAmount, format.mintick), color=color.red, style=label.style_label_up, textcolor=color.white, size=size.small) // Plot rebound amount on chart plot(isUpTrend ? reboundAmount : na, color=color.green, title="Up Rebound Amount") plot(isDownTrend ? reboundAmount : na, color=color.red, title="Down Rebound Amount")
Explanation
- Version declaration:
//@version=5indicates that Pine Script version 5 is being used. - Indicator declaration:
indicator("Trend Reversal Amount", overlay=true)sets the indicator to be displayed on the chart. - Parameter settings:
lengthis the period length used to detect the trend.thresholdis the minimum percentage change considered a rebound.
- Getting highs and lows: calculates the highest high and lowest low over the specified period.
- Trend reversal conditions:
- Uptrend (
isUpTrend) occurs when the price rises above the period's lowest low. - Downtrend (
isDownTrend) occurs when the price falls below the period's highest high.
- Uptrend (
- Rebound amount calculation:
- In an uptrend, compute the difference between the current price and the period's lowest low.
- In a downtrend, compute the difference between the period's highest high and the current price.
- Alerts:
- If the rebound amount exceeds the threshold, display a label on the chart.
- Plotting rebound amounts:
- Plot the uptrend rebound amount in green and the downtrend rebound amount in red.
Applying this script to a TradingView chart calculates the rebound amount when a trend forms and triggers an alert if the threshold is exceeded.
× ![]()