OPEN-SOURCE SCRIPT

Contrarian Period High & Low

230
Contrarian Period High & Low


This indicator pairs nicely with the Contrarian 100 MA and can be located here:
Contrarian 100 MA


Overview
The "Contrarian Period High & Low" indicator is a powerful technical analysis tool designed for traders seeking to identify key support and resistance levels and capitalize on contrarian trading opportunities. By tracking the highest highs and lowest lows over user-defined periods (Daily, Weekly, or Monthly), this indicator plots historical levels and generates buy and sell signals when price breaks these levels in a contrarian manner. A unique blue dot counter and action table enhance decision-making, making it ideal for swing traders, trend followers, and those trading forex, stocks, or cryptocurrencies. Optimized for daily charts, it can be adapted to other timeframes with proper testing.
How It Works
The indicator identifies the highest high and lowest low within a specified period (e.g., daily, weekly, or monthly) and draws horizontal lines for the previous period’s extremes on the chart. These levels act as dynamic support and resistance zones. Contrarian signals are generated when the price crosses below the previous period’s low (buy signal) or above the previous period’s high (sell signal), indicating potential reversals. A blue dot counter tracks consecutive buy signals, and a table displays the count and recommended action, helping traders decide whether to hold or flip positions.
Key Components

Period High/Low Levels: Tracks the highest high and lowest low for each period, plotting red lines for highs and green lines for lows from the bar where they occurred, extending for a user-defined length (default: 200 bars).
Contrarian Signals: Generates buy signals (blue circles) when price crosses below the previous period’s low and sell signals (white circles) when price crosses above the previous period’s high, designed to capture potential reversals.
Blue Dot Tracker: Counts consecutive buy signals (“blue dots”). If three or more occur, it suggests a stronger trend, with the table recommending whether to “Hold Investment” or “Flip Investment.”
Action Table: A 2x2 table in the bottom-right corner displays the blue dot count and action (“Hold Investment” if count ≥ 4, else “Flip Investment”) for quick reference.

Mathematical Concepts

Period Detection: Uses an approximate bar count to define periods (1 bar for Daily, 5 bars for Weekly, 20 bars for Monthly on a daily chart). When a new period starts, the previous period’s high/low is finalized and plotted.
High/Low Tracking:
Highest high (periodHigh) and lowest low (periodLow) are updated within the period.
Lines are drawn at these levels when the period ends, starting from the bar where the extreme occurred (periodHighBar, periodLowBar).


Signal Logic:
Buy signal: ta.crossunder(close[1], prevPeriodLow) and not lowBroken and barstate.isconfirmed
Sell signal: ta.crossover(close[1], prevPeriodHigh) and not highBroken and barstate.isconfirmed
Flags (highBroken, lowBroken) prevent multiple signals for the same level within a period.


Blue Dot Counter: Increments on each buy signal, resets on a sell signal or if price exceeds the entry price after three or more buy signals.

Entry and Exit Rules

Buy Signal (Blue Circle): Triggered when the price crosses below the previous period’s low, suggesting a potential oversold condition and buying opportunity. The signal appears as a blue circle below the price bar.
Sell Signal (White Circle): Triggered when the price crosses above the previous period’s high, indicating a potential overbought condition and selling opportunity. The signal appears as a white circle above the price bar.
Blue Dot Tracker:
Increments blueDotCount on each buy signal and sets an entryPrice on the first buy.
Resets on a sell signal or if price exceeds entryPrice after three or more buy signals.
If blueDotCount >= 3, the table suggests holding; if >= 4, it reinforces “Hold Investment.”


Exit Rules: Exit a buy position on a sell signal or when price exceeds the entry price after three or more buy signals. Combine with other tools (e.g., trendlines, support/resistance) for additional confirmation. Always apply proper risk management.

Recommended Usage
The "Contrarian Period High & Low" indicator is optimized for daily charts but can be adapted to other timeframes (e.g., 1H, 4H) with adjustments to the period bar count. It excels in markets with clear support/resistance levels and potential reversal zones. Traders should:

Backtest the indicator on their chosen asset and timeframe to validate signal reliability.
Combine with other technical tools (e.g., moving averages, Fibonacci levels) for stronger trade confirmation.
Adjust barsPerPeriod (e.g., ~120 bars for Weekly on hourly charts) based on the chart timeframe and market volatility.
Monitor the action table to guide position management based on blue dot counts.

Customization Options

Period Type: Choose between Daily, Weekly, or Monthly periods (default: Monthly).
Line Length: Set the length of high/low lines in bars (default: 200).
Show Highs/Lows: Toggle visibility of period high (red) and low (green) lines.
Max Lines to Keep: Limit the number of historical lines displayed (default: 10).
Hide Signals: Toggle buy/sell signal visibility for a cleaner chart.
Table Display: A fixed table in the bottom-right corner shows the blue dot count and action, with yellow (Hold) or green (Flip) backgrounds based on the count.

Why Use This Indicator?
The "Contrarian Period High & Low" indicator offers a unique blend of support/resistance visualization and contrarian signal generation, making it a versatile tool for identifying potential reversals. Its clear visual cues (lines and signals), blue dot tracker, and actionable table provide traders with an intuitive way to monitor market structure and manage trades. Whether you’re a beginner or an experienced trader, this indicator enhances your ability to spot key levels and time entries/exits effectively.
Tips for Users

Test the indicator thoroughly on your chosen market and timeframe to optimize settings (e.g., adjust barsPerPeriod for non-daily charts).
Use in conjunction with price action or other indicators for stronger trade setups.
Monitor the action table to decide whether to hold or flip positions based on blue dot counts.
Ensure your chart timeframe aligns with the selected period type (e.g., daily chart for Monthly periods).
Apply strict risk management to protect against false breakouts.

Happy trading with the Contrarian Period High & Low indicator! Share your feedback and strategies in the TradingView community!

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。