Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

If Last Three Recent Pivots are Lower High, Lower Low, Lower High and Price Cross Below…

  1. User

    If Last Three Recent Pivots are Lower High, Lower Low, Lower High and Price Cross Below Most Recent Lower Low and Then Consider that as Down Trend, If Last Three Recent Pivots are Higher Low, Higher High, Higher Low and Price Cross Above Most Recent Higher High then Consider Trend as Up Trend: //@version=5 indicator("Multi Time Frame Trend Detector", overlay=true)

    // === User Inputs ===

    // Time Frame Inputs higher_tf = input.timeframe("D", "Higher Time Frame") intermediate_tf = input.timeframe("60", "Intermediate Time Frame")

    // Pivot Parameters pivot_left = input.int(5, "Pivot Left Bars", minval=1) pivot_right = input.int(5, "Pivot Right Bars", minval=1) num_pivots_to_show = input.int(5, "Number of Recent Pivots to Show", minval=1) // Note: Not used anymore

    // Moving Average Length ma_length = input.int(21, "MA Length")

    // === Time Frame Data Retrieval ===

    // 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)

    // Intermediate Time Frame Data itf_high = request.security(syminfo.tickerid, intermediate_tf, high) itf_low = request.security(syminfo.tickerid, intermediate_tf, low) itf_close = request.security(syminfo.tickerid, intermediate_tf, close)

    // === Pivot Point Detection ===

    // Higher Time Frame Pivots htf_pivot_high = ta.pivothigh(htf_high, pivot_left, pivot_right) htf_pivot_low = ta.pivotlow(htf_low, pivot_left, pivot_right)

    // Intermediate Time Frame Pivots itf_pivot_high = ta.pivothigh(itf_high, pivot_left, pivot_right) itf_pivot_low = ta.pivotlow(itf_low, pivot_left, pivot_right)

    // Shift pivots back by pivot_right bars htf_pivot_high_shifted = htf_pivot_high[pivot_right] htf_pivot_low_shifted = htf_pivot_low[pivot_right] htf_close_shifted = htf_close[pivot_right]

    itf_pivot_high_shifted = itf_pivot_high[pivot_right] itf_pivot_low_shifted = itf_pivot_low[pivot_right] itf_close_shifted = itf_close[pivot_right]

    // === Moving Averages ===

    htf_ma = ta.sma(htf_close, ma_length) htf_ma_shifted = htf_ma[pivot_right]

    itf_ma = ta.sma(itf_close, ma_length) itf_ma_shifted = itf_ma[pivot_right]

    // === Trend Determination ===

    // Higher Time Frame Trend var string htf_trend = na if not na(htf_pivot_high_shifted) htf_trend := "Downtrend" else if not na(htf_pivot_low_shifted) htf_trend := "Uptrend" else htf_trend := na(htf_trend[1]) ? "Sideways" : htf_trend[1]

    if htf_close_shifted > htf_ma_shifted htf_trend := htf_trend == "Downtrend" ? "Sideways" : "Uptrend" else htf_trend := htf_trend == "Uptrend" ? "Sideways" : "Downtrend"

    // Intermediate Time Frame Trend var string itf_trend = na if not na(itf_pivot_high_shifted) itf_trend := "Downtrend" else if not na(itf_pivot_low_shifted) itf_trend := "Uptrend" else itf_trend := na(itf_trend[1]) ? "Sideways" : itf_trend[1]

    if itf_close_shifted > itf_ma_shifted itf_trend := itf_trend == "Downtrend" ? "Sideways" : "Uptrend" else itf_trend := itf_trend == "Uptrend" ? "Sideways" : "Downtrend"

    // === Trend Table Display ===

    // Create Table with 2 columns for HTF and ITF trends var table trend_table = table.new(position.top_right, 1, 2, border_width=1)

    // Default Trend Table Colors htf_trend_up_color_default = color.new(color.green, 70) htf_trend_down_color_default = color.new(color.red, 70) htf_trend_sideways_color_default = color.new(color.orange, 70)

    itf_trend_up_color_default = color.new(color.green, 70) itf_trend_down_color_default = color.new(color.red, 70) itf_trend_sideways_color_default = color.new(color.orange, 70)

    // Apply colors to the trend backgrounds htf_bgcolor = htf_trend == "Uptrend" ? htf_trend_up_color_default : htf_trend == "Downtrend" ? htf_trend_down_color_default : htf_trend_sideways_color_default

    itf_bgcolor = itf_trend == "Uptrend" ? itf_trend_up_color_default : itf_trend == "Downtrend" ? itf_trend_down_color_default : itf_trend_sideways_color_default

    // Update the table cells on every bar table.cell(trend_table, 0, 0, "HTF Trend: " + htf_trend, text_color=color.white, bgcolor=htf_bgcolor) table.cell(trend_table, 0, 1, "ITF Trend: " + itf_trend, text_color=color.white, bgcolor=itf_bgcolor)

    // === Pivot Plotting Management ===

    // Default pivot plot styles htf_pivot_high_color_default = color.red htf_pivot_low_color_default = color.lime itf_pivot_high_color_default = color.purple itf_pivot_low_color_default = color.yellow pivot_shape_size = size.tiny

    // Plot pivot points for HTF plotshape(not na(htf_pivot_high_shifted) ? htf_high[pivot_right * 2] : na, title="HTF Pivot High", style=shape.triangledown, location=location.absolute, color=htf_pivot_high_color_default, size=pivot_shape_size, offset=-pivot_right * 2) plotshape(not na(htf_pivot_low_shifted) ? htf_low[pivot_right * 2] : na, title="HTF Pivot Low", style=shape.triangleup, location=location.absolute, color=htf_pivot_low_color_default, size=pivot_shape_size, offset=-pivot_right * 2)

    // Plot pivot points for ITF plotshape(not na(itf_pivot_high_shifted) ? itf_high[pivot_right * 2] : na, title="ITF Pivot High", style=shape.triangledown, location=location.absolute, color=itf_pivot_high_color_default, size=pivot_shape_size, offset=-pivot_right * 2) plotshape(not na(itf_pivot_low_shifted) ? itf_low[pivot_right * 2] : na, title="ITF Pivot Low", style=shape.triangleup, location=location.absolute, color=itf_pivot_low_color_default, size=pivot_shape_size, offset=-pivot_right * 2)

    // === Moving Averages Plotting ===

    // Plot the moving averages (properties adjustable from the Style tab) plot(htf_ma, title="HTF MA") plot(itf_ma, title="ITF MA")

  2. Assistant

    It looks like you're trying to implement specific trend detection logic based on the sequence of recent pivot points:

    • Downtrend: If the last three recent pivots are Lower High (LH), Lower Low (LL), Lower High (LH), and the price crosses below the most recent Lower Low, then consider the trend as Downtrend.

    • Uptrend: If the last three recent pivots are Higher Low (HL), Higher High (HH), Higher Low (HL), and the price crosses above the most recent Higher High, then consider the trend as Uptrend.

    Your current code calculates pivot points but doesn't sequence them or check for these specific conditions. Let's modify your code to implement this logic.


    Step-by-Step Guide to Implement the Desired Logic

    1. Store Recent Pivot Points

    We need to collect recent pivot highs and lows along with their types, values, and indices. We'll use arrays to store this information.

    //@version=5
    indicator("Multi Time Frame Trend Detector", overlay=true)
    
    // User Inputs
    pivot_left = input.int(5, "Pivot Left Bars", minval=1)
    pivot_right = input.int(5, "Pivot Right Bars", minval=1)
    
    // Initialize arrays to store pivot information
    var pivot_types = array.new_string()
    var pivot_values = array.new_float()
    var pivot_indices = array.new_int()
    
    // Detect pivot highs and lows on the current timeframe
    pivot_high = ta.pivothigh(high, pivot_left, pivot_right)
    pivot_low = ta.pivotlow(low, pivot_left, pivot_right)
    
    // Shift pivots back by pivot_right bars to confirm pivots
    pivot_high_shifted = pivot_high[pivot_right]
    pivot_low_shifted = pivot_low[pivot_right]
    

    When a pivot high or low is detected, store its type, value, and bar index in the arrays:

    // Store Pivot Highs
    if not na(pivot_high_shifted)
        array.push(pivot_types, "PH")
        array.push(pivot_values, high[pivot_right * 2])
        array.push(pivot_indices, bar_index - pivot_right * 2)
    
    // Store Pivot Lows
    if not na(pivot_low_shifted)
        array.push(pivot_types, "PL")
        array.push(pivot_values, low[pivot_right * 2])
        array.push(pivot_indices, bar_index - pivot_right * 2)
    
    // Limit the array size to the last 5 pivots to prevent it from growing indefinitely
    if array.size(pivot_types) > 5
        array.shift(pivot_types)
        array.shift(pivot_values)
        array.shift(pivot_indices)
    

    2. Determine Pivot Sequences

    Now, we'll analyze the sequence of pivots to detect the specific patterns for uptrend or downtrend.

    // Initialize the trend as 'Neutral'
    string trend = "Neutral"
    
    if array.size(pivot_types) >= 3
        int last = array.size(pivot_types) - 1
        // Get the last three pivots
        string pivot_type_1 = array.get(pivot_types, last - 2)
        float pivot_value_1 = array.get(pivot_values, last - 2)
        
        string pivot_type_2 = array.get(pivot_types, last - 1)
        float pivot_value_2 = array.get(pivot_values, last - 1)
        
        string pivot_type_3 = array.get(pivot_types, last)
        float pivot_value_3 = array.get(pivot_values, last)
        
        // For Downtrend: LH, LL, LH
        bool is_lh1 = pivot_type_1 == "PH"
        bool is_ll = pivot_type_2 == "PL"
        bool is_lh2 = pivot_type_3 == "PH"
        bool lower_highs = pivot_value_3 < pivot_value_1
        bool lower_lows = pivot_value_2 < pivot_value_2[1]  // Previous pivot low
        
        if is_lh1 and is_ll and is_lh2 and lower_highs
            // Check if price crosses below the most recent Lower Low
            if close < pivot_value_2
                trend := "Downtrend"
        
        // For Uptrend: HL, HH, HL
        bool is_hl1 = pivot_type_1 == "PL"
        bool is_hh = pivot_type_2 == "PH"
        bool is_hl2 = pivot_type_3 == "PL"
        bool higher_lows = pivot_value_3 > pivot_value_1
        bool higher_highs = pivot_value_2 > pivot_value_2[1]  // Previous pivot high
        
        if is_hl1 and is_hh and is_hl2 and higher_lows
            // Check if price crosses above the most recent Higher High
            if close > pivot_value_2
                trend := "Uptrend"
    

    Notes:

    • pivot_value_2[1] refers to the pivot value before the current one. Ensure you have enough elements in the array to avoid errors.

    • Adjust the indices in the array accordingly if needed.

    3. Display the Trend

    We'll display the detected trend using a table.

    // Create a table to display the trend
    var table trend_table = table.new(position.top_right, 1, 1, border_width=1)
    
    bgcolor = trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.gray, 70)
    
    // Update the table with the trend information
    if barstate.islast
        table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
    

    4. Complete Modified Code

    Here is the complete code incorporating the changes:

    //@version=5
    indicator("Multi Time Frame Trend Detector", overlay=true)
    
    // User Inputs
    pivot_left = input.int(5, "Pivot Left Bars", minval=1)
    pivot_right = input.int(5, "Pivot Right Bars", minval=1)
    
    // Initialize arrays to store pivot information
    var pivot_types = array.new_string()
    var pivot_values = array.new_float()
    var pivot_indices = array.new_int()
    
    // Detect pivot highs and lows on the current timeframe
    pivot_high = ta.pivothigh(high, pivot_left, pivot_right)
    pivot_low = ta.pivotlow(low, pivot_left, pivot_right)
    
    // Shift pivots back by pivot_right bars to confirm pivots
    pivot_high_shifted = pivot_high[pivot_right]
    pivot_low_shifted = pivot_low[pivot_right]
    
    // Store Pivot Highs
    if not na(pivot_high_shifted)
        array.push(pivot_types, "PH")
        array.push(pivot_values, high[pivot_right * 2])
        array.push(pivot_indices, bar_index - pivot_right * 2)
    
    // Store Pivot Lows
    if not na(pivot_low_shifted)
        array.push(pivot_types, "PL")
        array.push(pivot_values, low[pivot_right * 2])
        array.push(pivot_indices, bar_index - pivot_right * 2)
    
    // Limit the array size to the last 5 pivots
    if array.size(pivot_types) > 5
        array.shift(pivot_types)
        array.shift(pivot_values)
        array.shift(pivot_indices)
    
    // Initialize the trend as 'Neutral'
    string trend = "Neutral"
    
    if array.size(pivot_types) >= 3
        int last = array.size(pivot_types) - 1
        // Get the last three pivots
        string pivot_type_1 = array.get(pivot_types, last - 2)
        float pivot_value_1 = array.get(pivot_values, last - 2)
        
        string pivot_type_2 = array.get(pivot_types, last - 1)
        float pivot_value_2 = array.get(pivot_values, last - 1)
        
        string pivot_type_3 = array.get(pivot_types, last)
        float pivot_value_3 = array.get(pivot_values, last)
        
        // Get previous pivot values for comparison
        float prev_pivot_high = na
        float prev_pivot_low = na
        for i = array.size(pivot_types) - 4 to 0 by -1
            if array.get(pivot_types, i) == "PH" and na(prev_pivot_high)
                prev_pivot_high := array.get(pivot_values, i)
            if array.get(pivot_types, i) == "PL" and na(prev_pivot_low)
                prev_pivot_low := array.get(pivot_values, i)
        
        // For Downtrend: LH, LL, LH
        bool is_lh1 = pivot_type_1 == "PH" and not na(prev_pivot_high) and pivot_value_1 < prev_pivot_high
        bool is_ll = pivot_type_2 == "PL" and not na(prev_pivot_low) and pivot_value_2 < prev_pivot_low
        bool is_lh2 = pivot_type_3 == "PH" and pivot_value_3 < pivot_value_1
        
        if is_lh1 and is_ll and is_lh2
            // Check if price crosses below the most recent Lower Low
            if close < pivot_value_2
                trend := "Downtrend"
        
        // For Uptrend: HL, HH, HL
        bool is_hl1 = pivot_type_1 == "PL" and not na(prev_pivot_low) and pivot_value_1 > prev_pivot_low
        bool is_hh = pivot_type_2 == "PH" and not na(prev_pivot_high) and pivot_value_2 > prev_pivot_high
        bool is_hl2 = pivot_type_3 == "PL" and pivot_value_3 > pivot_value_1
        
        if is_hl1 and is_hh and is_hl2
            // Check if price crosses above the most recent Higher High
            if close > pivot_value_2
                trend := "Uptrend"
    

    Explanation:

    • Pivot Comparisons: By comparing the current pivot values with the previous ones, we determine if they are higher or lower, thus identifying Higher Highs, Higher Lows, Lower Highs, and Lower Lows.

    • Trend Detection: The conditions is_lh1 and is_ll and is_lh2 for downtrend and is_hl1 and is_hh and is_hl2 for uptrend ensure that the specific sequences are met.

    • Price Cross Confirmation: The checks close < pivot_value_2 for downtrend and close > pivot_value_2 for uptrend confirm that the price has crossed the significant pivot point after the sequence, as per your requirement.

    5. Display the Trend

    // Create a table to display the trend
    var table trend_table = table.new(position.top_right, 1, 1, border_width=1)
    
    bgcolor = trend == "Uptrend" ? color.new(color.green, 70) : trend == "Downtrend" ? color.new(color.red, 70) : color.new(color.gray, 70)
    
    // Update the table with the trend information
    if barstate.islast
        table.cell(trend_table, 0, 0, "Trend: " + trend, text_color=color.white, bgcolor=bgcolor)
    

    Additional Notes

    • Pivot Confirmation: By shifting the pivots back by pivot_right bars, we ensure that we only consider confirmed pivot points.

    • Array Management: Limiting the array size keeps the script efficient. Adjust the size based on how many pivots you need for your analysis.

    • Comparison Logic: Ensure that comparisons between pivot values are logically consistent with your definition of higher highs/lows and lower highs/lows.

    • Price Cross Checks: The checks for the price crossing above or below the pivot are crucial as they confirm the trend after the pivot sequence.


    Moving Averages and Multi-Timeframe Considerations

    If you want to incorporate moving averages or perform this analysis on multiple timeframes (higher and intermediate), you can adapt the above logic by applying it to the respective timeframe data.


    Visualization

    You can also enhance the script by plotting the pivot points and the detected trends on the chart for better visualization.

    // Plot pivot points
    plotshape(not na(pivot_high_shifted) ? high[pivot_right * 2] : na, title="Pivot High", style=shape.triangledown, location=location.absolute, color=color.red, size=size.tiny, offset=-pivot_right * 2)
    plotshape(not na(pivot_low_shifted) ? low[pivot_right * 2] : na, title="Pivot Low", style=shape.triangleup, location=location.absolute, color=color.green, size=size.tiny, offset=-pivot_right * 2)
    

    Testing and Validation

    • Backtesting: After implementing the script, backtest it on historical data to validate that it correctly identifies trends as per your logic.

    • Parameter Adjustment: You might need to adjust pivot_left and pivot_right values to suit the volatility and characteristics of the instrument you're analyzing.


    I hope this helps you implement your desired trend detection logic. If you have any questions or need further assistance with adjusting the script for multi-timeframe analysis or incorporating additional indicators, feel free to ask!