[英]how to store ATR value on specific time in pinescript
我試圖在特定時間存儲 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)
我將舉例說明如何從特定時間段獲取數據。
這個想法是,創建一個您想要從中檢索數據的時間窗口。 然后,使用該窗口進行所有計算。 來源
//@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)
在這里你可以看到有一個時間窗口,我們有點盲目並進行計算。 當我們超出時間窗口時,我們不再進行任何計算並使用最后一個值。 因此,我們有一個固定的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.