Don't Make More than One High Or Low Pivot Continuously, Repaint that with Recent One:…
- User
Don't Make More than One High Or Low Pivot Continuously, Repaint that with Recent One: //@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(2, "Pivot Right Bars", minval=1) // Set default to 2 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)
// 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()
// Initialize series variables for pivot highs and lows var float pivot_high_plot = na var float pivot_low_plot = na
// Add pivot highs to arrays if not na(pivot_high) array.unshift(pivot_types, "H") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_high) // Assign pivot_high to pivot_high_plot for plotting pivot_high_plot := pivot_high else pivot_high_plot := na
// Add pivot lows to arrays if not na(pivot_low) array.unshift(pivot_types, "L") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_low) // Assign pivot_low to pivot_low_plot for plotting pivot_low_plot := pivot_low else pivot_low_plot := na
// Limit the size of the arrays to the last 6 pivots to conserve memory if array.size(pivot_types) > 6 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 three pivots to analyze if array.size(pivot_types) >= 3 // Extract the last three pivots string pivot1_type = array.get(pivot_types, 0) // Most recent pivot 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)
// Define sequences for downtrend and uptrend according to your specifications // For Downtrend: Lower High, Lower Low, Lower High bool is_downtrend_sequence = pivot1_type == "H" and pivot2_type == "L" and pivot3_type == "H" // For Uptrend: Higher Low, Higher High, Higher Low bool is_uptrend_sequence = pivot1_type == "L" and pivot2_type == "H" and pivot3_type == "L" // For Downtrend: recent low (pivot2_value) is lower than previous high (pivot3_value) bool lower_low = pivot2_value < pivot3_value // For Uptrend: recent high (pivot2_value) is higher than previous low (pivot3_value) bool higher_high = pivot2_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_low and price_crossed_below_pivot_low trend := "Downtrend" else if is_uptrend_sequence and higher_high 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 if barstate.islast table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
// Plot the pivot highs and lows outside of any local scope plot(pivot_high_plot, "Pivot High", color=color.red, style=plot.style_circles, linewidth=2) plot(pivot_low_plot, "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)
- Assistant
To achieve the functionality of avoiding consecutive high or low pivots and replacing the previous one with the most recent, you need to adjust your code to check the last pivot type before adding a new one. If the last pivot is of the same type as the new one, you should remove it from the arrays before inserting the new pivot.
Here's how you can modify your code:
1. Modify the pivot handling logic to prevent consecutive same-type pivots:
Replace your pivot addition logic with the following code:
// Add pivot highs to arrays, avoiding consecutive highs if not na(pivot_high) // Check if the last pivot is also a high if array.size(pivot_types) > 0 and array.get(pivot_types, 0) == "H" // Remove the last pivot high array.pop(pivot_types) array.pop(pivot_bars) array.pop(pivot_values) // Now add the new pivot high array.unshift(pivot_types, "H") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_high) // Assign pivot_high to pivot_high_plot for plotting pivot_high_plot := pivot_high else pivot_high_plot := na // Add pivot lows to arrays, avoiding consecutive lows if not na(pivot_low) // Check if the last pivot is also a low if array.size(pivot_types) > 0 and array.get(pivot_types, 0) == "L" // Remove the last pivot low array.pop(pivot_types) array.pop(pivot_bars) array.pop(pivot_values) // Now add the new pivot low array.unshift(pivot_types, "L") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_low) // Assign pivot_low to pivot_low_plot for plotting pivot_low_plot := pivot_low else pivot_low_plot := na2. Explanation of the changes:
- Checking for Consecutive Pivots:
- Before adding a new pivot, the code checks if the last pivot in
pivot_typesis of the same type (high or low). array.get(pivot_types, 0)retrieves the most recent pivot type.
- Before adding a new pivot, the code checks if the last pivot in
- Removing the Previous Pivot if It's the Same Type:
- If the last pivot is of the same type, the code removes it using
array.pop()from all arrays (pivot_types,pivot_bars, andpivot_values).
- If the last pivot is of the same type, the code removes it using
- Adding the New Pivot:
- The new pivot is then added to the arrays using
array.unshift(). - This ensures that the arrays always contain alternating pivot types (e.g., high, low, high, low).
- The new pivot is then added to the arrays using
3. Result of the Modification:
- No Consecutive Same-Type Pivots:
- The code now prevents consecutive pivot highs or pivot lows.
- It replaces any previous pivot of the same type with the more recent one.
- Accurate Trend Detection:
- By ensuring the pivots alternate between highs and lows, your trend detection logic remains accurate.
- It avoids false signals that may occur due to multiple consecutive pivots of the same type.
4. Rest of Your Code Remains the Same:
- Trend Analysis:
- Your existing trend analysis logic that checks sequences of pivots is unaffected by this change.
- Plotting:
- Pivot highs and lows are plotted based on the updated
pivot_high_plotandpivot_low_plotvariables.
- Pivot highs and lows are plotted based on the updated
- Moving Average and Table Display:
- These features continue to function as before.
Final Modified Code Snippet:
Here's the complete modified section for clarity:
// Add pivot highs to arrays, avoiding consecutive highs if not na(pivot_high) if array.size(pivot_types) > 0 and array.get(pivot_types, 0) == "H" // Remove previous pivot high array.pop(pivot_types) array.pop(pivot_bars) array.pop(pivot_values) // Add new pivot high array.unshift(pivot_types, "H") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_high) // Assign pivot_high to pivot_high_plot for plotting pivot_high_plot := pivot_high else pivot_high_plot := na // Add pivot lows to arrays, avoiding consecutive lows if not na(pivot_low) if array.size(pivot_types) > 0 and array.get(pivot_types, 0) == "L" // Remove previous pivot low array.pop(pivot_types) array.pop(part_bars) array.pop(pivot_values) // Add new pivot low array.unshift(pivot_types, "L") array.unshift(pivot_bars, htf_bar_index) array.unshift(pivot_values, pivot_low) // Assign pivot_low to pivot_low_plot for plotting pivot_low_plot := pivot_low else pivot_low_plot := naImplementation Tips:
- Always test your modified script on a small timeframe to ensure it behaves as expected.
- Check the arrays' contents during debugging to verify that consecutive pivots are handled correctly.
- Make sure that the plotting of pivot points reflects the changes and that old pivot points are removed or updated as intended.
By making these adjustments, your script will now avoid plotting multiple consecutive pivot highs or lows and will repaint with the most recent pivot, providing a clearer and more accurate representation of trend changes.
- Checking for Consecutive Pivots: