Create Trading View Indicator that Find Trend in Higher Time Frame using Following Details, Use Pivots to Find Trend: 1. Definitions:
Higher High (HH): A peak that is higher than the previous peak.
Higher Low (HL): A low that is higher than the previous low.
Lower High (LH): A peak that is lower than the previous peak.
Lower Low (LL): A low that is lower than the previous low.
2. Logic to Identify an Uptrend:
An uptrend is present when:
Each subsequent high is higher than the previous high (Higher High).
Each subsequent low is higher than the previous low (Higher Low).
Steps:
Check if current_high > previous_high.
Check if current_low > previous_low.
If both conditions are met, the trend is an uptrend.
3. Logic to Identify a Downtrend:
A downtrend is present when:
Each subsequent high is lower than the previous high (Lower High).
Each subsequent low is lower than the previous low (Lower Low).
Steps:
Check if current_high < previous_high.
Check if current_low < previous_low.
If both conditions are met, the trend is a downtrend.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a Pine Script™ indicator for TradingView that identifies the trend based on higher timeframe pivots. This indicator detects uptrends and downtrends by analyzing Higher Highs (HH), Higher Lows (HL), Lower Highs (LH), and Lower Lows (LL) on a user-defined higher timeframe.
Indicator Features:
Higher Timeframe Selection: Allows you to choose the higher timeframe (e.g., Daily, Weekly).
Pivot Length Configuration: Customize the number of bars to define pivot points.
Trend Identification:
Uptrend: Each subsequent pivot high is higher than the previous pivot high and each subsequent pivot low is higher than the previous pivot low.
Downtrend: Each subsequent pivot high is lower than the previous pivot high and each subsequent pivot low is lower than the previous pivot low.
Visual Indicators: Background color changes to indicate the trend, and arrows mark the trend changes on the chart.
Pine Script™ Code:
pinescript
//@version=5
indicator("Higher Timeframe Trend Using Pivots", overlay=true,
shorttitle="HTF Pivot Trend",
description="Identifies uptrends and downtrends based on higher timeframe pivot highs and lows.")
// === Input Parameters ===
higher_tf = input.timeframe("D", title="Higher Timeframe",
options=["1", "3", "5", "15", "30", "60", "240", "D", "W", "M"]) // Higher timeframe selection
pivot_len = input.int(5, title="Pivot Length", minval=1,
tooltip="Number of bars on each side to define a pivot.") // Pivot length
// === Function to Calculate Higher Timeframe Trend ===
calc_htf_trend() =>
// Initialize persistent variables to store pivot highs and lows
var float last_pivot_high = na
var float prev_pivot_high = na
var float last_pivot_low = na
var float prev_pivot_low = na
var int trend = 0 // 1 for uptrend, -1 for downtrend, 0 for neutral
// Detect pivot high and pivot low
pivot_high = ta.pivothigh(high, pivot_len, pivot_len)
pivot_low = ta.pivotlow(low, pivot_len, pivot_len)
// Update pivot highs
if not na(pivot_high)
prev_pivot_high := last_pivot_high
last_pivot_high := pivot_high
// Update pivot lows
if not na(pivot_low)
prev_pivot_low := last_pivot_low
last_pivot_low := pivot_low
// Determine trend based on pivot comparisons
if (not na(last_pivot_high) and not na(prev_pivot_high) and
not na(last_pivot_low) and not na(prev_pivot_low))
if (last_pivot_high > prev_pivot_high) and (last_pivot_low > prev_pivot_low)
trend := 1 // Uptrend detected
else if (last_pivot_high < prev_pivot_high) and (last_pivot_low < prev_pivot_low)
trend := -1 // Downtrend detected
else
trend := 0 // Neutral or no clear trend
trend
// === Retrieve Higher Timeframe Trend ===
trend_htf = request.security(syminfo.tickerid, higher_tf, calc_htf_trend(),
lookahead=barmerge.lookahead_off)
// === Visualization ===
// Background Color Indication
bgcolor(trend_htf == 1 ? color.new(color.green, 90) :
trend_htf == -1 ? color.new(color.red, 90) : na)
// Plot Uptrend Arrow
plotshape(
trend_htf == 1,
title="Uptrend",
location=location.belowbar,
color=color.green,
style=shape.triangleup,
size=size.small,
offset=-pivot_len)
// Plot Downtrend Arrow
plotshape(
trend_htf == -1,
title="Downtrend",
location=location.abovebar,
color=color.red,
style=shape.triangledown,
size=size.small,
offset=-pivot_len)
// === Optional: Display Current Trend as Label ===
var label trend_label = na
if (trend_htf == 1)
label.delete(trend_label)
trend_label := label.new(bar_index, high, "Uptrend",
color=color.green, style=label.style_label_up,
textcolor=color.white, size=size.small)
else if (trend_htf == -1)
label.delete(trend_label)
trend_label := label.new(bar_index, low, "Downtrend",
color=color.red, style=label.style_label_down,
textcolor=color.white, size=size.small)
else
label.delete(trend_label)
Explanation of the Code:
Inputs:
Higher Timeframe (higher_tf): Allows the user to select the desired higher timeframe (default is Daily).
Pivot Length (pivot_len): Determines the number of bars to consider on each side when identifying pivot points.
calc_htf_trend Function:
Pivot Detection:
Pivot High (ta.pivothigh): Identifies a pivot high if the current high is higher than the pivot_len bars before and after it.
Pivot Low (ta.pivotlow): Identifies a pivot low if the current low is lower than the pivot_len bars before and after it.
Trend Determination:
Uptrend: If the latest pivot high is higher than the previous pivot high and the latest pivot low is higher than the previous pivot low.
Downtrend: If the latest pivot high is lower than the previous pivot high and the latest pivot low is lower than the previous pivot low.
Neutral: If neither condition is met.
Retrieving Higher Timeframe Trend:
The request.security function calls the calc_htf_trend function on the specified higher timeframe. The lookahead=barmerge.lookahead_off parameter ensures that the indicator doesn't look into future bars, maintaining the integrity of the trend detection.
Visualization:
Background Color (bgcolor): Changes to a light green for an uptrend and light red for a downtrend.
Arrows (plotshape): Displays upward-pointing triangles below the bars during an uptrend and downward-pointing triangles above the bars during a downtrend.
Trend Labels (Optional): Displays a label indicating "Uptrend" or "Downtrend" on the chart for clearer visualization.
Usage Instructions:
Add the Indicator to Your Chart:
Open TradingView.
Navigate to the Pine Editor tab at the bottom of the page.
Paste the above Pine Script code into the editor.
Click Add to Chart.
Configure Inputs:
Click on the indicator's settings (gear icon) on the chart.
Under the Inputs tab, select your desired Higher Timeframe and Pivot Length.
Interpret the Signals:
Background Color:
Green: Indicates an uptrend on the higher timeframe.
Red: Indicates a downtrend on the higher timeframe.
Arrows:
Upward Triangle: Marks the presence of an uptrend.
Downward Triangle: Marks the presence of a downtrend.
Trend Labels (if enabled): Provide textual confirmation of the current trend.
Notes:
Pivot Confirmation: Pivot points require a certain number of bars to confirm their validity based on the pivot_len input. Ensure that your chart has enough historical data to accurately detect pivots.
Higher Timeframe Selection: Choosing a significantly higher timeframe (e.g., Monthly) may result in fewer trend signals. Adjust the timeframe based on your trading strategy.
Performance: Using longer pivot lengths or higher timeframes may impact the performance slightly, especially on charts with a large number of bars.
Feel free to customize the indicator further to suit your trading style and preferences. Happy trading!