Create Trading View Indicator that Find Trend in Higher Time Frame using Following Details, Use Pivots to Find Trend, even Minor Pivots: 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
Certainly! Below is a TradingView indicator script written in Pine Script (version 5) that identifies the trend on a higher timeframe using pivots, including minor pivots, based on your specified logic.
This script will:
Identify pivot highs and lows.
Determine if there is an uptrend or downtrend according to your definitions.
Display the trend on the chart.
pine
//@version=5
indicator("Higher Timeframe Trend Finder", overlay=true)
// Input for higher timeframe
higher_tf = input.timeframe("D", "Higher Timeframe")
// Get higher timeframe data
[htf_open, htf_high, htf_low, htf_close] = request.security(syminfo.tickerid, higher_tf, [open, high, low, close])
// Parameters for pivot detection
pivot_left = input.int(5, "Pivot Left Bars", minval=1)
pivot_right = input.int(5, "Pivot Right Bars", minval=1)
// Identify pivot highs and lows on higher timeframe
htf_pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right)
htf_pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)
// Store pivot highs and lows with their bar index
var pivot_highs = array.new<float>()
var pivot_high_bars = array.new<int>()
var pivot_lows = array.new<float>()
var pivot_low_bars = array.new<int>()
if not na(htf_pivot_high)
array.push(pivot_highs, htf_pivot_high)
array.push(pivot_high_bars, bar_index)
if not na(htf_pivot_low)
array.push(pivot_lows, htf_pivot_low)
array.push(pivot_low_bars, bar_index)
// Ensure arrays have at least two elements to compare
uptrend = false
downtrend = false
if array.size(pivot_highs) >= 2 and array.size(pivot_lows) >= 2
// Get the last two pivot highs and lows
last_high = array.get(pivot_highs, array.size(pivot_highs) - 1)
prev_high = array.get(pivot_highs, array.size(pivot_highs) - 2)
last_low = array.get(pivot_lows, array.size(pivot_lows) - 1)
prev_low = array.get(pivot_lows, array.size(pivot_lows) - 2)
// Determine uptrend
if last_high > prev_high and last_low > prev_low
uptrend := true
downtrend := false
// Determine downtrend
else if last_high < prev_high and last_low < prev_low
downtrend := true
uptrend := false
else
uptrend := false
downtrend := false
else
uptrend := false
downtrend := false
// Plotting the trend on the chart
plotshape(uptrend, title="Uptrend", style=shape.labelup, location=location.belowbar, color=color.green, text="Uptrend")
plotshape(downtrend, title="Downtrend", style=shape.labeldown, location=location.abovebar, color=color.red, text="Downtrend")
Explanation:
Indicator Setup:
We start by specifying the Pine Script version and giving a name to the indicator.
if not na(htf_pivot_high)
array.push(pivot_highs, htf_pivot_high)
array.push(pivot_high_bars, bar_index)
if not na(htf_pivot_low)
array.push(pivot_lows, htf_pivot_low)
array.push(pivot_low_bars, bar_index)
Determining the Trend:
We check if we have at least two pivot highs and lows to compare.
We compare the last two pivot highs and the last two pivot lows according to your logic.
pine
if array.size(pivot_highs) >= 2 and array.size(pivot_lows) >= 2
// Get the last two pivot highs and lows
last_high = array.get(pivot_highs, array.size(pivot_highs) - 1)
prev_high = array.get(pivot_highs, array.size(pivot_highs) - 2)
last_low = array.get(pivot_lows, array.size(pivot_lows) - 1)
prev_low = array.get(pivot_lows, array.size(pivot_lows) - 2)
// Determine uptrend
if last_high > prev_high and last_low > prev_low
uptrend := true
downtrend := false
// Determine downtrend
else if last_high < prev_high and last_low < prev_low
downtrend := true
uptrend := false
else
uptrend := false
downtrend := false
else
uptrend := false
downtrend := false
Plotting the Trend:
We use plotshape to display labels on the chart where an uptrend or downtrend is identified.