Search code examples
pine-scriptpine-script-v5

Access 5m Pre-Market data in daily timeframe


previously i had an indicator which plots the GAP vs ADR of the day. For the GAP calculation the market gap = math.abs(open - close[1]) was used. But disadvantage is, that the indicator is refreshing after the new tradingday is opened. Now i want to modify it that the last bar of the premarket session is used for the gap calculation. The bar should be from e.g. 5m Chart inside the premarket AND last-day-after-market time

how can i use the premarket data to plot this ratio inside the Daily chart, where no extended data is available

//@version=5
indicator("GAP im Verhältnis zur ADR", shorttitle="GAP:ADR Ratio")

// Eingabeparameter
length = input.int(10,title = "ADR-Länge")
schwelle_1 = input.float(1, title="Unterer Schwellwert")
schwelle_2 = input.float(2, title="Oberer Schwellwert")
pm_time = input.session('1601-0929:1234567', "Session", group="PreMarket Open")
pm_calc = input.string(title="Calculation Source", defval="Close", options=["Open","High", "Low", "Close"], group="PreMarket Open")

tPM = time("1", pm_time)
var price_last  = 0.0
var is_pmo = 0

ticker_pm = ticker.new(syminfo.prefix, syminfo.ticker, session.extended)
price_lt = request.security_lower_tf(ticker_pm,"1",close)

data_size = array.size(price_lt)
float data_pm = (data_size > 0) ? array.get(price_lt, 0) : na

// Berechnung der Average Daily Range (ADR)
daily_range = high - low
adr = ta.sma(daily_range, length)

// prev calc GAP
//gap = math.abs(open - close[1])
//GAP with Data from PM session
gap = math.abs(open - data_pm[1])

// Berechnung des Verhältnisses von GAP zu ADR
gap_to_adr_ratio =  if (gap / adr) < schwelle_1
    0
else
    (gap / adr)

// Plot des Indikators

plot(gap_to_adr_ratio, title="GAP:ADR Ratio", color=color.blue, linewidth=10, style = plot.style_columns)
//hline(1, "Neutral", color=color.gray)
hline(schwelle_1, "Unterer Schwellenwert", color=color.red)
hline(schwelle_2, "Oberer Schwellenwert", color=color.green)

Solution

  • I solved it with

    ticker_pm = ticker.new(syminfo.prefix, syminfo.ticker, session.extended)
    arr_close_pm = request.security_lower_tf(ticker_pm,'5',close)
    float close_pm = (array.size(arr_close_pm) > 0) ? array.get(arr_close_pm, array.size(arr_close_pm)-1) : na