pine-scriptpine-script-v5

Pine Script (Version 5) get historic data from daily chart and make it available in 1 minute chart


Here is my problem. I need to get the open price for January 2nd 2024 and push it into an array, now I can do this from the daily chart. But when I switch to a 1 minute chart the historic data/bars for January 2nd 2024 are no longer available so my code does nothing. Any help is greatly appreciated.

Here is my code.

//@version=5
indicator("Open Price Storage", overlay = true, dynamic_requests=true)

// Define the target date for 2024 January 2nd
targetYear = 2024
targetMonth = 1
targetDay = 2

// Get the open price on the target date from the daily timeframe
dailyOpenPrice = request.security(syminfo.tickerid, "D", (year == targetYear and month == targetMonth and dayofmonth == targetDay) ? open : na)

// Initialize an array to store the open price
var float[] openPriceArray = array.new_float(1, na)

// Store the open price in the array once
if na(array.get(openPriceArray, 0)) and not na(dailyOpenPrice)
    array.set(openPriceArray, 0, dailyOpenPrice)

// Plot the stored open price on the chart for visibility
if array.size(openPriceArray) > 0 and not na(array.get(openPriceArray, 0))
    openPriceLabel = array.get(openPriceArray, 0)
    label.new(bar_index, high, "Open Price: " + str.tostring(openPriceLabel), color=color.orange, textcolor=color.black)

// Log the stored open price
if array.size(openPriceArray) > 0 and not na(array.get(openPriceArray, 0))
    for i = 0 to array.size(openPriceArray) - 1
        line.new(bar_index, close, bar_index + 1, array.get(openPriceArray, i), color=color.red)

Solution

  • If there are no bars for Jan, 2, then you cannot get this information, unfortunately.

    If you really need a specific price to be used in your script, then you could pass it via a user input.