It looks like you're trying to implement a trend detection strategy in Pine Script based on specific pivot patterns you described. Specifically, you're looking to:
-
Downtrend: Identify when the most recent four pivots form a sequence of Lower High (LH), Lower Low (LL), Lower High (LH), and Lower Low (LL), followed by the price crossing below the most recent pivot low.
-
Uptrend: Identify when the most recent four pivots form a sequence of Higher Low (HL), Higher High (HH), Higher Low (HL), and Higher High (HH), followed by the price crossing above the most recent pivot high.
-
Sideways: If neither condition for an uptrend nor a downtrend is met.
To achieve this, you'll need to:
- Detect and store the recent pivot highs and lows.
- Analyze the sequence of these pivots to determine if they match your trend criteria.
- Monitor the price action to confirm the trend when the price crosses a significant pivot level.
Below, I'll guide you through modifying your script to implement this logic.
1. Detect and Store Recent Pivots
First, we'll collect the recent pivot highs and lows and store them in arrays. This allows us to access past pivots and analyze their sequence.
//@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)
ma_length = input.int(21, "MA Length")
// 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)
htf_bar_index = request.security(syminfo.tickerid, higher_tf, bar_index)
// 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)
// Shift pivots by pivot_right bars to align them properly
pivot_high_shifted = pivot_high[pivot_right]
pivot_low_shifted = pivot_low[pivot_right]
// Initialize arrays to store pivots
var string[] pivot_types = array.new_string()
var int[] pivot_bars = array.new_int()
var float[] pivot_values = array.new_float()
Whenever a new pivot is detected, we add it to the arrays:
// Add pivot highs to arrays
if not na(pivot_high_shifted)
array.unshift(pivot_types, "H")
array.unshift(pivot_bars, htf_bar_index[pivot_right])
array.unshift(pivot_values, pivot_high_shifted)
// Add pivot lows to arrays
if not na(pivot_low_shifted)
array.unshift(pivot_types, "L")
array.unshift(pivot_bars, htf_bar_index[pivot_right])
array.unshift(pivot_values, pivot_low_shifted)
// Limit the size of the arrays to the last 10 pivots to conserve memory
if array.size(pivot_types) > 10
array.pop(pivot_types)
array.pop(pivot_bars)
array.pop(pivot_values)
2. Analyze the Sequence of Pivots
Now, we analyze the last four pivots to see if they match the sequences you defined for uptrend or downtrend.
// Initialize Trend Variable
var string trend = na
// Check if we have at least four pivots to analyze
if array.size(pivot_types) >= 4
// Extract the last four pivots
string pivot1_type = array.get(pivot_types, 0)
float pivot1_value = array.get(pivot_values, 0)
string pivot2_type = array.get(pivot_types, 1)
float pivot2_value = array.get(pivot_values, 1)
string pivot3_type = array.get(pivot_types, 2)
float pivot3_value = array.get(pivot_values, 2)
string pivot4_type = array.get(pivot_types, 3)
float pivot4_value = array.get(pivot_values, 3)
// Define sequences for uptrend and downtrend
bool is_downtrend_sequence = pivot1_type == "H" and pivot2_type == "L" and pivot3_type == "H" and pivot4_type == "L"
bool is_uptrend_sequence = pivot1_type == "L" and pivot2_type == "H" and pivot3_type == "L" and pivot4_type == "H"
// Check for Lower Highs and Lower Lows
bool lower_highs = pivot1_value < pivot3_value
bool lower_lows = pivot2_value < pivot4_value
// Check for Higher Highs and Higher Lows
bool higher_highs = pivot2_value > pivot4_value
bool higher_lows = pivot1_value > pivot3_value
// Confirm trend with price action crossing pivot levels
bool price_crossed_below_pivot_low = htf_close < pivot2_value // For downtrend, price crosses below the recent low
bool price_crossed_above_pivot_high = htf_close > pivot2_value // For uptrend, price crosses above the recent high
// Determine the trend
if is_downtrend_sequence and lower_highs and lower_lows and price_crossed_below_pivot_low
trend := "Downtrend"
else if is_uptrend_sequence and higher_highs and higher_lows and price_crossed_above_pivot_high
trend := "Uptrend"
else
trend := "Sideways"
else
trend := "Sideways"
3. Monitor Price Action to Confirm the Trend
As per your criteria, after identifying the pivot sequence, we need to confirm the trend when the price crosses the most recent pivot.
This is integrated into the code above with price_crossed_below_pivot_low and price_crossed_above_pivot_high variables.
4. Visualizing the Trend
Finally, we'll display the trend information and visualize the pivots on the chart.
// Display Trend Condition using Table
var table trend_table = table.new(position.top_right, 1, 1)
bgcolor = trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)
// Update the table cell on every bar
table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
// Plotting the Pivots for Visualization
for i = 0 to array.size(pivot_values) - 1
pivot_bar = array.get(pivot_bars, i)
pivot_val = array.get(pivot_values, i)
pivot_type = array.get(pivot_types, i)
if pivot_type == "H"
plot(htf_bar_index == pivot_bar ? pivot_val : na, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2)
else
plot(htf_bar_index == pivot_bar ? pivot_val : na, "Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2)
// Plot the Moving Average (if you wish to include it)
htf_ma = ta.sma(htf_close, ma_length)
plot(htf_ma, "Higher TF MA", color=color.blue)
Full Script
Here's how your modified script would look in its entirety:
//@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)
ma_length = input.int(21, "MA Length")
// 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)
htf_bar_index = request.security(syminfo.tickerid, higher_tf, bar_index)
// 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)
// Shift pivots by pivot_right bars to align them properly
pivot_high_shifted = pivot_high[pivot_right]
pivot_low_shifted = pivot_low[pivot_right]
// Initialize arrays to store pivots
var string[] pivot_types = array.new_string()
var int[] pivot_bars = array.new_int()
var float[] pivot_values = array.new_float()
// Add pivot highs to arrays
if not na(pivot_high_shifted)
array.unshift(pivot_types, "H")
array.unshift(pivot_bars, htf_bar_index[pivot_right])
array.unshift(pivot_values, pivot_high_shifted)
// Add pivot lows to arrays
if not na(pivot_low_shifted)
array.unshift(pivot_types, "L")
array.unshift(pivot_bars, htf_bar_index[pivot_right])
array.unshift(pivot_values, pivot_low_shifted)
// Limit the size of the arrays to the last 10 pivots to conserve memory
if array.size(pivot_types) > 10
array.pop(pivot_types)
array.pop(pivot_bars)
array.pop(pivot_values)
// Initialize Trend Variable
var string trend = na
// Check if we have at least four pivots to analyze
if array.size(pivot_types) >= 4
// Extract the last four pivots
string pivot1_type = array.get(pivot_types, 0)
float pivot1_value = array.get(pivot_values, 0)
string pivot2_type = array.get(pivot_types, 1)
float pivot2_value = array.get(pivot_values, 1)
string pivot3_type = array.get(pivot_types, 2)
float pivot3_value = array.get(pivot_values, 2)
string pivot4_type = array.get(pivot_types, 3)
float pivot4_value = array.get(pivot_values, 3)
// Define sequences for uptrend and downtrend
bool is_downtrend_sequence = pivot1_type == "H" and pivot2_type == "L" and pivot3_type == "H" and pivot4_type == "L"
bool is_uptrend_sequence = pivot1_type == "L" and pivot2_type == "H" and pivot3_type == "L" and pivot4_type == "H"
// Check for Lower Highs and Lower Lows
bool lower_highs = pivot1_value < pivot3_value
bool lower_lows = pivot2_value < pivot4_value
// Check for Higher Highs and Higher Lows
bool higher_highs = pivot2_value > pivot4_value
bool higher_lows = pivot1_value > pivot3_value
// Confirm trend with price action crossing pivot levels
bool price_crossed_below_pivot_low = htf_close < pivot2_value // For downtrend, price crosses below the recent low
bool price_crossed_above_pivot_high = htf_close > pivot2_value // For uptrend, price crosses above the recent high
// Determine the trend
if is_downtrend_sequence and lower_highs and lower_lows and price_crossed_below_pivot_low
trend := "Downtrend"
else if is_uptrend_sequence and higher_highs and higher_lows and price_crossed_above_pivot_high
trend := "Uptrend"
else
trend := "Sideways"
else
trend := "Sideways"
// Display Trend Condition using Table
var table trend_table = table.new(position.top_right, 1, 1)
bgcolor = trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.orange, 70)
// Update the table cell on every bar
table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
// Plotting the Pivots for Visualization
for i = 0 to array.size(pivot_values) - 1
pivot_bar = array.get(pivot_bars, i)
pivot_val = array.get(pivot_values, i)
pivot_type = array.get(pivot_types, i)
if pivot_type == "H"
plot(htf_bar_index == pivot_bar ? pivot_val : na, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2)
else
plot(htf_bar_index == pivot_bar ? pivot_val : na, "Pivot Low", color=color.lime, style=plot.style_circles, linewidth=2)
// Plot the Moving Average (if you wish to include it)
htf_ma = ta.sma(htf_close, ma_length)
plot(htf_ma, "Higher TF MA", color=color.blue)
Notes:
-
Arrays: We use arrays to keep track of the pivots because they allow us to store values across bars.
-
Sequence Check: We ensure that the pivot types match the required sequence for an uptrend or downtrend.
-
Value Comparison: We compare pivot values to check for higher highs/lows or lower highs/lows.
-
Price Confirmation: We confirm the trend only when the price crosses above or below the specific pivot after the sequence.
Testing and Validation
-
Backtesting: Run the script over historical data to see if it correctly identifies trends based on your criteria.
-
Adjust Parameters: You might need to adjust the pivot_left and pivot_right parameters to better fit the market you're analyzing.
-
Visualization: Use the plotted pivots and moving average to visually confirm the trend detection.
This modified script should implement the trend detection logic as per your requirements. Remember to test it thoroughly and adjust parameters as needed to suit different market conditions. If you have any further questions or need assistance with additional modifications, feel free to ask!