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.
Draw Trend on Top Right Corner using Table, Don't Write anything on Candles
Assistant
To create a TradingView indicator that identifies the trend on a higher time frame using pivot points (even minor pivots), we'll use Pine Script (version 5). The indicator will determine the trend based on the provided logic and display it on the top right corner using a table.
Here's the complete code:
pine
//@version=5
indicator("Trend Indicator using Pivots on Higher Time Frame", overlay=true)
// User inputs
htf = input.timeframe('60', 'Higher Time Frame') // Default higher time frame is 60 minutes
left = input.int(2, 'Pivot Left Bars', minval=1) // Number of left bars for pivot detection
right = input.int(2, 'Pivot Right Bars', minval=1) // Number of right bars for pivot detection
// Variables to store pivot highs and lows
var float previousHigh = na
var float previousLow = na
var float currentHigh = na
var float currentLow = na
// Trend variable
var string trend = 'Unknown'
// Request higher time frame pivot highs and lows
htf_pivotHigh = request.security(syminfo.tickerid, htf, ta.pivothigh(high, left, right))
htf_pivotLow = request.security(syminfo.tickerid, htf, ta.pivotlow(low, left, right))
// Update highs and lows
if not na(htf_pivotHigh)
previousHigh := currentHigh
currentHigh := htf_pivotHigh
if not na(htf_pivotLow)
previousLow := currentLow
currentLow := htf_pivotLow
// Determine the trend
if not na(previousHigh) and not na(previousLow) and not na(currentHigh) and not na(currentLow)
if currentHigh > previousHigh and currentLow > previousLow
trend := 'Uptrend'
else if currentHigh < previousHigh and currentLow < previousLow
trend := 'Downtrend'
else
trend := trend // Keep previous trend if conditions are not met
// Create a table to display the trend
var table trendTable = table.new(position.top_right, 1, 1)
// Determine background color based on the trend
bgcolor = trend == 'Uptrend' ? color.new(color.green, 0) :
trend == 'Downtrend' ? color.new(color.red, 0) :
color.new(color.gray, 0)
// Update the table with the current trend
table.cell(trendTable, 0, 0, trend + ' (' + htf + ')', bgcolor=bgcolor, text_color=color.white)
Explanation:
Indicator Declaration:
We declare the indicator with indicator("Trend Indicator using Pivots on Higher Time Frame", overlay=true).
User Inputs:
htf: Allows the user to select the higher time frame.
left and right: Define the number of bars to the left and right for pivot detection.
Variables Initialization:
We initialize variables to store the previous and current pivot highs and lows.
The trend is initially set to 'Unknown'.
Pivot Highs and Lows Detection:
We use request.security() to fetch pivot highs and lows from the higher time frame.
The ta.pivothigh() and ta.pivotlow() functions detect pivot points based on the specified left and right bars.
Updating Highs and Lows:
When a new pivot high or low is detected, we update the previous and current high/low variables.
Trend Determination:
We check if both the current high is higher/lower than the previous high and the current low is higher/lower than the previous low.
If both conditions are met, we set the trend to 'Uptrend' or 'Downtrend' accordingly.
If conditions are not met, we keep the previous trend.
Displaying the Trend:
We create a table positioned at the top right corner to display the trend.
The background color of the table cell changes based on the trend (green for uptrend, red for downtrend, gray otherwise).
We update the table cell with the current trend and the selected higher time frame.
Note: The indicator strictly adheres to the requirement of not drawing anything on the candles and only displaying the trend information in the top right corner using a table.
This indicator will help you identify the trend based on higher time frame pivot points, and it updates in real-time without repainting, ensuring reliable trend detection.