How to get high and low for a specific time period

99封情书 提交于 2021-01-29 17:23:29

问题


I am trying to write a strategy in Pinescript for trading view and I have to get the high and low values during a certain time period. Like 10:00 to 10:30 AM. In the documentation I can see the time range but not sure how to get high and low for that particular time period.


回答1:


Good afternoon Vignesh,

I am interested in your question and if you get an answer I would be grateful if you could share.

I am not sure if the below might assist you. It captures the high and low of the first 60 mins after UTC 00:00.

    //Session Rules
    bartimeSess = time('D')
    newbarSess = bartimeSess != bartimeSess[1]


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

//Calcul For Opening Range
locHigh = security(syminfo.tickerid, "60", high_range)
locLow = security(syminfo.tickerid, "60", low_range)
range = locHigh - locLow

//Plot Statements For Initial Balance
plot(time(timeframe.period) > 0 ? locHigh : na,title="IB High", color=color.blue, transp=20, linewidth=2)
plot(time(timeframe.period) > 0 ? locLow : na,title="IB Low", color=color.blue, transp=20, linewidth=2)


//Plot Statements For Medium of IB range
plot(locLow + range/2,title="IB Medium", color=color.green, transp=20, linewidth=2)



回答2:


This code is modified from this PineCoders FAQ entry. Note that the hi/lo develops as the monitored period of time elapses:

//@version=4
study("Session hi/lo", "", true)
noPlotOutside = input(true, "Don't plot outside of hours")
showHi = input(true, "Show highs")
showLo = input(true, "Show lows")
srcHi = input(high, "Source for Highs")
srcLo = input(low, "Source for Lows")
timeAllowed = input("1000-1030", "Allowed hours", input.session)

// Check to see if we are in allowed hours using session info on all 7 days of the week.
timeIsAllowed = time(timeframe.period, timeAllowed + ":1234567")
var hi = 10e-10
var lo = 10e10
if timeIsAllowed
    // We are entering allowed hours; reset hi/lo.
    if not timeIsAllowed[1]
        hi := srcHi
        lo := srcLo
    else
        // We are in allowed hours; track hi/lo.
        hi := max(srcHi, hi)
        lo := min(srcLo, lo)

plot(showHi and not(noPlotOutside and not timeIsAllowed)? hi : na, "Highs", color.blue, 3, plot.style_circles)
plot(showLo and not(noPlotOutside and not timeIsAllowed)? lo : na, "Lows", color.fuchsia, 3, plot.style_circles)

Disclosure: the link in this answer points to a PineCoders FAQ entry. I am a member of the PineCoders community and I most probably wrote that FAQ entry. PineCoders is a TradingView-supported group of volunteer Pine coders and PineCoders' website is strictly educational. Neither TradingView nor PineCoders benefits financially from sending traffic to pinecoders.com, and the site contains no affiliate/referral links.



来源:https://stackoverflow.com/questions/65590923/how-to-get-high-and-low-for-a-specific-time-period

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!