首页 > 解决方案 > 如何在 pinescript 中存储特定时间的 ATR 值

问题描述

我试图在特定时间存储 atr 值。关键是根据市场开盘的前 30 分钟创建 atr 水平水平。我尝试了几个值,一开始它看起来有效,但在向后滚动时它读出了错误的值。感谢您花时间查看代码

session = input.session(defval='0930-1600', title='Trading Session')
plot_session = input.session(defval='1000-1600', title='Plotting: add one minute to the 
Pick your own session start')
resol = input.timeframe('30', title='length/time Of Opening Range = Trading Session to 
Plotsesion')
show_today = input(false, title='Show only today')
atr_timefr = input('1', title='ATR Timeframe')
atrLkb = input(14, title='ATR Stop Period')
//atrRes = input.timeframe("D", title='ATR Resolution')
atrMult = input.float(0.25, step=0.25, title='ATR Stop Multiplier') 


highTimeFrame = time('D')


//calc for ranges
plt = plot_session
sesstoday = session
sessSpec = session 

//range calculation
hiloreg = ticker.new(syminfo.prefix, syminfo.ticker, session.regular)
sessions(sesstodayw) => time('D', sesstodayw)
bartimeSess = sessions(sesstoday)
newbarSess = bartimeSess != bartimeSess[1]

high_range = ta.valuewhen(newbarSess, high, 0)
low_range = ta.valuewhen(newbarSess, low, 0)

highRes = request.security(hiloreg, resol, high_range)
lowRes = request.security(hiloreg, resol, low_range)
range_1 = highRes - lowRes


//show today 
istoday = year(timenow) == year(time) and month(timenow) == month(time) and 
dayofmonth(timenow) == dayofmonth(time) 
is_today = show_today ? istoday and time('1', sessSpec) : time('1', sessSpec)

    
//atr calculation


atrcollect = openRangeMid 
atr = request.security(syminfo.ticker, atr_timefr, ta.atr(atrLkb))
float entry_atr = na
entry_atr := atrcollect[0] > atrcollect[1] ? atr : entry_atr[1]

//PT Highlightning
plot_pt = time('1', plt) ? color.purple : na


//PT1
highRes2 = highRes + range_1 * (3*.1)
lowRes2 = lowRes - (entry_atr * atrMult)
plot(is_today ? highRes2 : na, color=plot_pt, linewidth=2)
plot(is_today ? lowRes2 : na, color=plot_pt, linewidth=2)

//PT2
highRes3 = highRes2 + range_1 * (3*.1)
lowRes3 = lowRes2 - (entry_atr * atrMult)
plot(is_today ? highRes3 : na, color=plot_pt, linewidth=2)
plot(is_today ? lowRes3 : na, color=plot_pt, linewidth=2)

//PT3
highRes4 = highRes3 + (entry_atr * atrMult)
lowRes4 = lowRes3 - (entry_atr * atrMult)
plot(is_today  ? highRes4 : na, color=plot_pt, linewidth=2)
plot(is_today  ? lowRes4 : na, color=plot_pt, linewidth=2)

标签: pine-script

解决方案


我将举一个例子,说明如何从特定时间段获取数据。

这个想法是,创建一个要从中检索数据的时间窗口。然后,使用该窗口进行所有计算。资源

//@version=5
indicator("My Script")
time_window = input.session(defval="1000-1100", title="Time Window")

var max_atr = 0.0
my_atr = ta.atr(14)
is_in_window = time(timeframe.period, time_window + ":1234567")

if is_in_window
    // We are entering allowed hours; reset atr
    if not is_in_window[1]
        max_atr := my_atr
    else
        // We are in allowed hours; track atr
        max_atr := math.max(max_atr, my_atr)
        
plot(not is_in_window? max_atr : na, "Max ATR", color.blue, 3, plot.style_circles)

在这里你可以看到有一个时间窗口,我们有点盲目地进行计算。当我们超出时间窗口时,我们不再进行任何计算并使用最后一个值。因此,我们有一个固定值。

在此处输入图像描述


推荐阅读