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

"//@version=5 indicator("20.1) Refined Flower and Fire tbl Multi-Time Frame",…

  1. User

    "//@version=5 indicator("20.1) Refined Flower and Fire tbl Multi-Time Frame", overlay=true, max_bars_back=500)

    // Function to compute 'Flower' condition flower_condition_tf() => rsi9 = ta.rsi(close, 9) ema20_high = ta.ema(high, 20) rma20_low = ta.rma(low, 20) Flower = ta.crossover(rsi9, 30) and ema20_high < rma20_low Flower

    // Function to get last Flower time and price in higher time frame flower_info() => Flower = flower_condition_tf() lastTime = ta.valuewhen(Flower, time, 0) lastPrice = ta.valuewhen(Flower, close, 0) [lastTime, lastPrice]

    // Function to compute 'Fire' condition fire_condition_tf() => rsi9 = ta.rsi(close, 9) Fire = ta.crossover(rsi9, 90) Fire

    // Function to get last Fire time and price in higher time frame fire_info() => Fire = fire_condition_tf() lastTime = ta.valuewhen(Fire, time, 0) lastPrice = ta.valuewhen(Fire, close, 0) [lastTime, lastPrice]

    // Function to format the timestamp formatTime(ts, isIntraday) => yr = year(ts) mnth = month(ts) day = dayofmonth(ts) if isIntraday hr = hour(ts) min = minute(ts) str.format("{0}-{1}-{2} {3}:{4}", str.tostring(day, '00'), str.tostring(mnth, '00'), str.tostring(yr), str.tostring(hr, '00'), str.tostring(min, '00')) else str.format("{0}-{1}-{2}", str.tostring(day, '00'), str.tostring(mnth, '00'), str.tostring(yr))

    // Create table var table myTable = table.new(position.top_right, 7, 5, bgcolor=color.white, frame_color=color.black, frame_width=1)

    // Add table headers if barstate.isfirst table.cell(myTable, 0, 0, "Time Frame", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 1, 0, "Flower Event", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 2, 0, "Flower Date/Time", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 3, 0, "Flower LTP", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 4, 0, "Fire Event", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 5, 0, "Fire Date/Time", text_color=color.black, bgcolor=color.gray) table.cell(myTable, 6, 0, "Fire LTP", text_color=color.black, bgcolor=color.gray)

    // Function to process each time frame process_time_frame(tf, tf_label, row) => // Flower info [lastFlowerTime, lastFlowerPrice] = request.security(syminfo.tickerid, tf, flower_info(), lookahead=barmerge.lookahead_off) // Fire info [lastFireTime, lastFirePrice] = request.security(syminfo.tickerid, tf, fire_info(), lookahead=barmerge.lookahead_off)

    if barstate.islast
        table.cell(myTable, 0, row, tf_label, text_color=color.black)
        
        // Flower data
        if not na(lastFlowerTime)
            table.cell(myTable, 1, row, "🌸", text_color=color.black)
            table.cell(myTable, 2, row, formatTime(lastFlowerTime, tf == '60'), text_color=color.black)
            table.cell(myTable, 3, row, str.format("{0,number,#.00}", lastFlowerPrice), text_color=color.black)
        else
            table.cell(myTable, 1, row, "No Data", text_color=color.black)
            table.cell(myTable, 2, row, "N/A", text_color=color.black)
            table.cell(myTable, 3, row, "N/A", text_color=color.black)
        
        // Fire data
        if not na(lastFireTime)
            table.cell(myTable, 4, row, "🔥", text_color=color.black)
            table.cell(myTable, 5, row, formatTime(lastFireTime, tf == '60'), text_color=color.black)
            table.cell(myTable, 6, row, str.format("{0,number,#.00}", lastFirePrice), text_color=color.black)
        else
            table.cell(myTable, 4, row, "No Data", text_color=color.black)
            table.cell(myTable, 5, row, "N/A", text_color=color.black)
            table.cell(myTable, 6, row, "N/A", text_color=color.black)
    

    // Process each time frame process_time_frame('60', "1H", 1) process_time_frame('D', "1D", 2) process_time_frame('W', "1W", 3) process_time_frame('M', "1M", 4)

    // Plot 'Flower' symbols on the current chart's time frame rsi9 = ta.rsi(close, 9) ema20_high = ta.ema(high, 20) rma20_low = ta.rma(low, 20) Flower = ta.crossover(rsi9, 30) and ema20_high < rma20_low plotshape(Flower, location=location.belowbar, color=color.yellow, style=shape.labelup, text="🌸", title="Flower")

    // Plot 'Fire' symbols on the current chart's time frame Fire = ta.crossover(rsi9, 90) plotshape(Fire, location=location.abovebar, color=color.red, style=shape.labeldown, text="🔥", title="Fire")"

    is code me table me jo data h wo kese static h jb bhi apan time frame change kr rahe h chart ka.

  2. Assistant

    The issue you're experiencing—where the table data remains static when changing the chart's timeframe—is likely due to how the timeframes are defined and how the table updates are triggered in your Pine Script. Let's break down the problem and provide a solution to ensure the table dynamically updates based on the chart's current timeframe.

    Understanding the Problem

    1. Fixed Timeframes:

      • In your script, you’ve defined specific higher timeframes ('60' for 1-hour, 'D' for daily, 'W' for weekly, and 'M' for monthly) regardless of the chart’s current timeframe.
      • When you change the chart's timeframe, these fixed resolutions may not align properly. For instance, if you switch to a higher timeframe like 'D', the '60' (1-hour) might no longer be considered higher relative to the new chart timeframe, leading to inconsistencies or static data.
    2. Table Update Logic:

      • The table is updated only when barstate.islast is true. This means that the table updates only on the last bar of the chart.
      • When changing timeframes, especially to a higher one, the barstate.islast condition might not trigger as expected, preventing the table from updating with the new timeframe's data.

    Proposed Solution

    To make the table dynamic and responsive to changes in the chart's timeframe, follow these steps:

    1. Define Timeframes Relative to the Current Chart's Timeframe:

      • Instead of using absolute timeframes like '60', 'D', 'W', and 'M', define them relative to the chart's current timeframe.
      • This ensures that regardless of the chart's timeframe, the table will always reference higher (or lower) timeframes appropriately.
    2. Automatically Adjust Timeframes:

      • Use a list of desired timeframes and filter them based on the current chart's timeframe to ensure they are higher.
    3. Update the Table on Every Bar:

      • Instead of relying solely on barstate.islast, update the table on every bar to ensure it reflects the latest data, especially after a timeframe change.
    4. Example Implementation:

    Here's how you can modify your script to incorporate these changes:

    //@version=5
    indicator("20.1) Refined Flower and Fire tbl Multi-Time Frame", overlay=true, max_bars_back=500)
    
    // Function to compute 'Flower' condition
    flower_condition_tf() =>
        rsi9 = ta.rsi(close, 9)
        ema20_high = ta.ema(high, 20)
        rma20_low = ta.rma(low, 20)
        ta.crossover(rsi9, 30) and ema20_high < rma20_low
    
    // Function to get last Flower time and price in higher time frame
    flower_info() =>
        Flower = flower_condition_tf()
        lastTime = ta.valuewhen(Flower, time, 0)
        lastPrice = ta.valuewhen(Flower, close, 0)
        [lastTime, lastPrice]
    
    // Function to compute 'Fire' condition
    fire_condition_tf() =>
        rsi9 = ta.rsi(close, 9)
        ta.crossover(rsi9, 90)
    
    // Function to get last Fire time and price in higher time frame
    fire_info() =>
        Fire = fire_condition_tf()
        lastTime = ta.valuewhen(Fire, time, 0)
        lastPrice = ta.valuewhen(Fire, close, 0)
        [lastTime, lastPrice]
    
    // Function to format the timestamp
    formatTime(ts, isIntraday) =>
        yr = year(ts)
        mnth = month(ts)
        day = dayofmonth(ts)
        if isIntraday
            hr = hour(ts)
            min = minute(ts)
            str.format("{0}-{1}-{2} {3}:{4}", str.tostring(day, '00'), str.tostring(mnth, '00'), str.tostring(yr), str.tostring(hr, '00'), str.tostring(min, '00'))
        else
            str.format("{0}-{1}-{2}", str.tostring(day, '00'), str.tostring(mnth, '00'), str.tostring(yr))
    
    // Create table
    var table myTable = table.new(position.top_right, 7, 5, bgcolor=color.white, frame_color=color.black, frame_width=1)
    
    // Add table headers
    if barstate.isfirst
        table.cell(myTable, 0, 0, "Time Frame", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 1, 0, "Flower Event", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 2, 0, "Flower Date/Time", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 3, 0, "Flower LTP", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 4, 0, "Fire Event", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 5, 0, "Fire Date/Time", text_color=color.black, bgcolor=color.gray)
        table.cell(myTable, 6, 0, "Fire LTP", text_color=color.black, bgcolor=color.gray)
    
    // Define desired higher timeframes relative to the current chart's timeframe
    // You can adjust this list based on your needs
    higher_timeframes = ["60", "D", "W", "M"]  // 1H, 1D, 1W, 1M
    
    // Function to process each timeframe
    process_time_frame(tf, tf_label, row) =>
        // Flower info
        [lastFlowerTime, lastFlowerPrice] = request.security(syminfo.tickerid, tf, flower_info(), lookahead=barmerge.lookahead_off)
        // Fire info
        [lastFireTime, lastFirePrice] = request.security(syminfo.tickerid, tf, fire_info(), lookahead=barmerge.lookahead_off)
        
        // Update table on every bar
        table.cell(myTable, 0, row, tf_label, text_color=color.black)
        
        // Flower data
        if not na(lastFlowerTime)
            table.cell(myTable, 1, row, "🌸", text_color=color.black)
            table.cell(myTable, 2, row, formatTime(lastFlowerTime, tf == '60'), text_color=color.black)
            table.cell(myTable, 3, row, str.format("{0,number,#.00}", lastFlowerPrice), text_color=color.black)
        else
            table.cell(myTable, 1, row, "No Data", text_color=color.black)
            table.cell(myTable, 2, row, "N/A", text_color=color.black)
            table.cell(myTable, 3, row, "N/A", text_color=color.black)
        
        // Fire data
        if not na(lastFireTime)
            table.cell(myTable, 4, row, "🔥", text_color=color.black)
            table.cell(myTable, 5, row, formatTime(lastFireTime, tf == '60'), text_color=color.black)
            table.cell(myTable, 6, row, str.format("{0,number,#.00}", lastFirePrice), text_color=color.black)
        else
            table.cell(myTable, 4, row, "No Data", text_color=color.black)
            table.cell(myTable, 5, row, "N/A", text_color=color.black)
            table.cell(myTable, 6, row, "N/A", text_color=color.black)
    
    // Iterate through each higher timeframe and process
    for i = 0 to array.size(higher_timeframes) - 1
        tf = array.get(higher_timeframes, i)
        tf_label = tf == "60" ? "1H" : tf
        process_time_frame(tf, tf_label, i + 1)
    
    // Plot 'Flower' symbols on the current chart's timeframe
    rsi9 = ta.rsi(close, 9)
    ema20_high = ta.ema(high, 20)
    rma20_low = ta.rma(low, 20)
    Flower = ta.crossover(rsi9, 30) and ema20_high < rma20_low
    plotshape(Flower, location=location.belowbar, color=color.yellow, style=shape.labelup, text="🌸", title="Flower")
    
    // Plot 'Fire' symbols on the current chart's timeframe
    Fire = ta.crossover(rsi9, 90)
    plotshape(Fire, location=location.abovebar, color=color.red, style=shape.labeldown, text="🔥", title="Fire")
    

    Key Changes Explained

    1. Removed barstate.islast:

      • By removing the condition if barstate.islast, the table updates on every bar, ensuring it remains current even when the chart's timeframe changes.
    2. Dynamic Timeframe Labels:

      • The script now dynamically labels the timeframes. For example, '60' is labeled as '1H', and others remain as 'D', 'W', 'M'. This makes it easier to manage and understand the table entries.
    3. Loop Through Timeframes:

      • Using a for loop to iterate through the list of higher timeframes makes the code cleaner and easier to manage, especially if you decide to add or remove timeframes in the future.
    4. Relative Timeframe Handling:

      • While the script still uses absolute timeframes ('60', 'D', 'W', 'M'), by updating the table on every bar and processing each timeframe dynamically, it ensures that the table reflects the most recent data relative to the current chart's timeframe.

    Additional Recommendations

    1. Ensure Higher Timeframes Are Indeed Higher:

      • Depending on the chart's current timeframe, some of the defined higher timeframes might not actually be higher. You might want to add logic to adjust or filter the higher_timeframes list based on the current chart's timeframe.
    2. Optimize Performance:

      • Requesting multiple higher timeframes can be resource-intensive. Ensure that you monitor the script's performance and optimize as needed, perhaps by limiting the number of timeframes or optimizing the conditions.
    3. Handle Timeframe Changes Gracefully:

      • Consider adding conditions to detect when the chart's timeframe changes and handle any necessary recalculations or table resets to ensure data integrity.

    Conclusion

    By updating the table on every bar and ensuring that timeframe processing is handled dynamically, the table should now reflect the latest "Flower" and "Fire" events regardless of changes to the chart's timeframe. This makes your indicator more robust and responsive to different viewing preferences.

    If you continue to experience issues, consider adding debug statements or using TradingView’s built-in debugging tools to trace how the data changes with different chart timeframes.