Create a Indicator That Can Detect Higher Time Frame Trend using High and Low Pivot and…
- User
Create a Indicator That Can Detect Higher Time Frame Trend using High and Low Pivot and Show Trend Condition in the box on Top Right Corner of the Chart
- Assistant
Certainly! I can help you create a TradingView Pine Script indicator that detects the higher time frame trend using high and low pivots, and displays the trend condition in a box on the top right corner of the chart.
This indicator will:
- Use pivot highs and lows from a higher time frame to determine the trend.
- Display the current trend condition ("Uptrend", "Downtrend", or "Sideways") in a box on the top right corner of your chart.
Pine Script Code:
//@version=5 indicator("Higher Time Frame Trend Detector", overlay=true) // User Inputs higher_tf = input.timeframe("D", "Higher Time Frame") pivot_left = input.int(5, "Pivot Left Bars", minval=1) pivot_right = input.int(5, "Pivot Right Bars", minval=1) // Get Higher Time Frame Data htf_high = request.security(syminfo.tickerid, higher_tf, high) htf_low = request.security(syminfo.tickerid, higher_tf, low) htf_close = request.security(syminfo.tickerid, higher_tf, close) // Detect Pivots in Higher Time Frame pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right) pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right) // Initialize Trend Variable var string trend = "" // Determine Trend Based on Pivots if pivot_high trend := "Downtrend" else if pivot_low trend := "Uptrend" else trend := nz(trend[1], "Sideways") // Optional: Use Moving Average to Supplement Trend Detection ma_length = input.int(21, "MA Length") htf_ma = ta.sma(htf_close, ma_length) if htf_close > htf_ma trend := trend == "Downtrend" ? "Sideways" : "Uptrend" else trend := trend == "Uptrend" ? "Sideways" : "Downtrend" // Display Trend Condition var label trend_label = label.new(x=bar_index, y=na) label.set_xy(trend_label, bar_index, htf_high) label.set_text(trend_label, "Trend: " + trend) label.set_color(trend_label, color.none) label.set_style(trend_label, label.style_label_upper_right) label.set_textcolor(trend_label, color.white) label.set_bgcolor(trend_label, trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)) // Plotting for Visualization (Optional) plot(pivot_high ? htf_high[pivot_right] : na, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2) plot(pivot_low ? htf_low[pivot_right] : na, "Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2) plot(htf_ma, "Higher TF MA", color=color.blue) // Cleanup Old Labels to Prevent Clutter if barstate.islast label.delete(trend_label[1])
How the Indicator Works:
-
Higher Time Frame Selection:
- The indicator requests data from a higher time frame (default is daily
"D"). - You can change the
higher_tfinput to any desired time frame (e.g.,"W"for weekly).
- The indicator requests data from a higher time frame (default is daily
-
Pivot Detection:
- Uses
ta.pivothighandta.pivotlowfunctions to detect pivot highs and lows in the higher time frame data. pivot_leftandpivot_rightdetermine the sensitivity of the pivot detection.
- Uses
-
Trend Determination:
- Uptrend: Detected when a pivot low is found.
- Downtrend: Detected when a pivot high is found.
- If neither a new pivot high nor low is detected, the trend retains its previous state, defaulting to "Sideways" if no prior trend is set.
- Optionally, the indicator uses a moving average (default length
21) to refine the trend detection:- If the close is above the moving average, and the trend is not "Downtrend", it sets the trend to "Uptrend".
- If the close is below the moving average, and the trend is not "Uptrend", it sets the trend to "Downtrend".
-
Display Trend Condition:
- A label is created and placed in the top right corner of the chart.
- The label displays the current trend condition with a background color:
- Green for Uptrend
- Red for Downtrend
- Orange for Sideways
- The label updates every bar to reflect the current trend condition.
-
Visualization:
- Pivot highs and lows are plotted on the chart for visual reference.
- The higher time frame moving average is also plotted.
-
Label Management:
- To prevent clutter, old labels are deleted as new ones are created, ensuring only one label is visible on the chart.
Customization:
-
Higher Time Frame (
higher_tf):- Adjust to your preferred higher time frame (e.g.,
"W"for weekly,"M"for monthly).
- Adjust to your preferred higher time frame (e.g.,
-
Pivot Sensitivity (
pivot_left,pivot_right):- Lower values make pivot detection more sensitive (more pivots detected).
- Higher values make pivot detection less sensitive (fewer pivots detected).
-
Moving Average Length (
ma_length):- Adjust the length of the moving average used to supplement trend detection.
How to Use the Indicator:
-
Add the Indicator to Your Chart:
- Copy the code above.
- In TradingView, go to Pine Editor (usually found at the bottom of the screen).
- Paste the code into the editor.
- Click on Add to Chart.
-
Adjust Settings as Needed:
- Click on the gear icon next to the indicator's name in the chart to access settings.
- Modify the higher time frame, pivot sensitivity, and moving average length as desired.
-
Interpreting the Trend Condition:
- The box on the top right corner displays the current trend based on higher time frame pivots.
- Use this information to align your trading decisions with the higher time frame trend.
Notes:
-
Real-Time Updates:
- The indicator updates in real-time with each new bar.
- Trend changes are detected as new pivot highs or lows are confirmed.
-
Limitations:
- Pivot points are only confirmed after
pivot_leftandpivot_rightbars have elapsed. - This means there is a lag in trend detection equal to the sum of
pivot_leftandpivot_rightbars.
- Pivot points are only confirmed after
-
Enhancements:
- You can modify the trend detection logic to incorporate other indicators or conditions.
- For advanced trend analysis, consider combining with other tools like RSI, MACD, or price action patterns.
Feel free to ask if you have any questions or need further customization!