OPEN-SOURCE SCRIPT

ADR% Extension Levels from SMA 50

156
I created this indicator inspired by RealSimpleAriel (a swing trader I recommend following on X) who does not buy stocks extended beyond 4 ADR% from the 50 SMA and uses extensions from the 50 SMA at 7-8-9-10-11-12-13 ADR% to take profits with a 20% position trimming.

RealSimpleAriel's strategy (as I understood it):
-> Focuses on leading stocks from leading groups and industries, i.e., those that have grown the most in the last 1-3-6 months (see on Finviz groups and then select sector-industry).
-> Targets stocks with the best technical setup for a breakout, above the 200 SMA in a bear market and above both the 50 SMA and 200 SMA in a bull market, selecting those with growing Earnings and Sales.
-> Buys stocks on breakout with a stop loss set at the day's low of the breakout and ensures they are not extended beyond 4 ADR% from the 50 SMA.
-> 3-5 day momentum burst: After a breakout, takes profits by selling 1/2 or 1/3 of the position after a 3-5 day upward move.
-> 20% trimming on extension from the 50 SMA: At 7 ADR% (ADR% calculated over 20 days) extension from the 50 SMA, takes profits by selling 20% of the remaining position. Continues to trim 20% of the remaining position based on the stock price extension from the 50 SMA, calculated using the 20-period ADR%, thus trimming 20% at 8-9-10-11 ADR% extension from the 50 SMA. Upon reaching 12-13 ADR% extension from the 50 SMA, considers the stock overextended, closes the remaining position, and evaluates a short.
-> Trailing stop with ascending SMA: Uses a chosen SMA (10, 20, or 50) as the definitive stop loss for the position, depending on the stock's movement speed (preferring larger SMAs for slower-moving stocks or for long-term theses). If the stock's closing price falls below the chosen SMA, the entire position is closed.
In summary:
-->Buy a breakout using the day's low of the breakout as the stop loss (this stop loss is the most critical).
--> Do not buy stocks extended beyond 4 ADR% from the 50 SMA.
--> Sell 1/2 or 1/3 of the position after 3-5 days of upward movement.
--> Trim 20% of the position at each 7-8-9-10-11-12-13 ADR% extension from the 50 SMA.
--> Close the entire position if the breakout fails and the day's low of the breakout is reached.
--> Close the entire position if the price, during the rise, falls below a chosen SMA (10, 20, or 50, depending on your preference).
--> Definitively close the position if it reaches 12-13 ADR% extension from the 50 SMA.

I used Grok from X to create this indicator. I am not a programmer, but based on the ADR% I use, it works.
Below is Grok from X's description of the indicator:


Script Description
The script is a custom indicator for TradingView that displays extension levels based on ADR% relative to the 50-period Simple Moving Average (SMA). Below is a detailed description of its features, structure, and behavior:
1. Purpose of the Indicator
Name: "ADR% Extension Levels from SMA 50".

Objective: Draw horizontal blue lines above and below the 50-period SMA, corresponding to specific ADR% multiples (4, 7, 8, 9, 10, 11, 12, 13). These levels represent potential price extension zones based on the average daily percentage volatility.

Overlay: The indicator is overlaid on the price chart (overlay=true), so the lines and SMA appear directly on the price graph.

2. Configurable Inputs
The indicator allows users to customize parameters through TradingView settings:
SMA Length (smaLength):
Default: 50 periods.

Description: Specifies the number of periods for calculating the Simple Moving Average (SMA). The 50-period SMA serves as the reference point for extension levels.

Constraint: Minimum 1 period.

ADR% Length (adrLength):
Default: 20 periods.

Description: Specifies the number of days to calculate the moving average of the daily high/low ratio, used to determine ADR%.

Constraint: Minimum 1 period.

Scale Factor (scaleFactor):
Default: 1.0.

Description: An optional multiplier to adjust the distance of extension levels from the SMA. Useful if levels are too close or too far due to an overly small or large ADR%.

Constraint: Minimum 0.1, increments of 0.1.

Tooltip: "Adjust if levels are too close or far from SMA".

3. Main Calculations
50-period SMA:
Calculated with ta.sma(close, smaLength) using the closing price (close).

Serves as the central line around which extension levels are drawn.

ADR% (Average Daily Range Percentage):
Formula: 100 * (ta.sma(dhigh / dlow, adrLength) - 1).

Details:
dhigh and dlow are the daily high and low prices, obtained via request.security(syminfo.tickerid, "D", high/low) to ensure data is daily-based, regardless of the chart's timeframe.

The dhigh / dlow ratio represents the daily percentage change.

The simple moving average (ta.sma) of this ratio over 20 days (adrLength) is subtracted by 1 and multiplied by 100 to obtain ADR% as a percentage.

The result is multiplied by scaleFactor for manual adjustments.

Extension Levels:
Defined as ADR% multiples: 4, 7, 8, 9, 10, 11, 12, 13.

Stored in an array (levels) for easy iteration.

For each level, prices above and below the SMA are calculated as:
Above: sma50 * (1 + (level * adrPercent / 100))

Below: sma50 * (1 - (level * adrPercent / 100))

These represent price levels corresponding to a percentage change from the SMA equal to level * ADR%.

4. Visualization
Horizontal Blue Lines:
For each level (4, 7, 8, 9, 10, 11, 12, 13 ADR%), two lines are drawn:
One above the SMA (e.g., +4 ADR%).

One below the SMA (e.g., -4 ADR%).

Color: Blue (color.blue).

Style: Solid (style=line.style_solid).

Management:
Each level has dedicated variables for upper and lower lines (e.g., upperLine1, lowerLine1 for 4 ADR%).

Previous lines are deleted with line.delete before drawing new ones to avoid overlaps.

Lines are updated at each bar with line.new(bar_index[1], level, bar_index, level), covering the range from the previous bar to the current one.

Labels:
Displayed only on the last bar (barstate.islast) to avoid clutter.

For each level, two labels:
Above: E.g., "4 ADR%", positioned above the upper line (style=label.style_label_down).

Below: E.g., "-4 ADR%", positioned below the lower line (style=label.style_label_up).

Color: Blue background, white text.

50-period SMA:
Drawn as a gray line (color.gray) for visual reference.

Diagnostics:
ADR% Plot: ADR% is plotted in the status line (orange, histogram style) to verify the value.

ADR% Label: A label on the last bar near the SMA shows the exact ADR% value (e.g., "ADR%: 2.34%"), with a gray background and white text.

5. Behavior
Dynamic Updating:
Lines update with each new bar to reflect new SMA 50 and ADR% values.

Since ADR% uses daily data ("D"), it remains constant within the same day but changes day-to-day.

Visibility Across All Bars:
Lines are drawn on every bar, not just the last one, ensuring visibility on historical data as well.

Adaptability:
The scaleFactor allows level adjustments if ADR% is too small (e.g., for low-volatility symbols) or too large (e.g., for cryptocurrencies).

Compatibility:
Works on any timeframe since ADR% is calculated from daily data.

Suitable for symbols with varying volatility (e.g., stocks, forex, cryptocurrencies).

6. Intended Use
Technical Analysis: Extension levels represent significant price zones based on average daily volatility. They can be used to:
Identify potential price targets (e.g., take profit at +7 ADR%).

Assess support/resistance zones (e.g., -4 ADR% as support).

Measure price extension relative to the 50 SMA.

Trading: Useful for strategies based on breakouts or mean reversion, where ADR% levels indicate reversal or continuation points.

Debugging: Labels and ADR% plot help verify that values align with the symbol’s volatility.

7. Limitations
Dependence on Daily Data: ADR% is based on daily dhigh/dlow, so it may not reflect intraday volatility on short timeframes (e.g., 1 minute).

Extreme ADR% Values: For low-volatility symbols (e.g., bonds) or high-volatility symbols (e.g., meme stocks), ADR% may require adjustments via scaleFactor.

Graphical Load: Drawing 16 lines (8 upper, 8 lower) on every bar may slow the chart for very long historical periods, though line management is optimized.

ADR% Formula: The formula 100 * (sma(dhigh/dlow, Length) - 1) may produce different values compared to other ADR% definitions (e.g., (high - low) / close * 100), so users should be aware of the context.

8. Visual Example
On a chart of a stock like TSLA (daily timeframe):
The 50 SMA is a gray line tracking the average trend.

Assuming an ADR% of 3%:
At +4 ADR% (12%), a blue line appears at sma50 * 1.12.

At -4 ADR% (-12%), a blue line appears at sma50 * 0.88.

Other lines appear at ±7, ±8, ±9, ±10, ±11, ±12, ±13 ADR%.

On the last bar, labels show "4 ADR%", "-4 ADR%", etc., and a gray label shows "ADR%: 3.00%".

ADR% is visible in the status line as an orange histogram.

9. Code: Technical Structure
Language: Pine Script version=5.

Inputs: Three configurable parameters (smaLength, adrLength, scaleFactor).

Calculations:
SMA: ta.sma(close, smaLength).

ADR%: 100 * (ta.sma(dhigh / dlow, adrLength) - 1) * scaleFactor.

Levels: sma50 * (1 ± (level * adrPercent / 100)).

Graphics:
Lines: Created with line.new, deleted with line.delete to avoid overlaps.

Labels: Created with label.new only on the last bar.

Plots: plot(sma50) for the SMA, plot(adrPercent) for debugging.

Optimization: Uses dedicated variables for each line (e.g., upperLine1, lowerLine1) for clear management and to respect TradingView’s graphical object limits.

10. Possible Improvements
Option to show lines only on the last bar: Would reduce visual clutter.

Customizable line styles: Allow users to choose color or style (e.g., dashed).

Alert for anomalous ADR%: A message if ADR% is too small or large.

Dynamic levels: Allow users to specify ADR% multiples via input.

Optimization for short timeframes: Adapt ADR% for intraday timeframes.

Conclusion
The script creates a visual indicator that helps traders identify price extension levels based on daily volatility (ADR%) relative to the 50 SMA. It is robust, configurable, and includes debugging tools (ADR% plot and labels) to verify values. The ADR% formula based on dhigh/dlow

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.