Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pine script - PINESCRIPT - Plotting Sunday/Monday High & Low for duration of whole week

I'm trying to make an indicator that identifies the High/Low range of every Sunday/Monday and plots horizontal lines from those high/low price points for the entire week.

I've come up with something that works, but I know it's not the right way to go about this. However, here's what I want the end result to look like: S/M High/Low Example

I've modified someone else's code that plots the high/low of every Sun/Mon and I've duplicated the "plot" and added an offset to paste it over every day of the week. I know this isn't the right way to go about it, as I would rather plot a single solid line for every week. Here is my code. Any help is highly appreciated.

Code:

//@version=2


//title
study(title="SM_Range", shorttitle="SM_Range", overlay=true)

wid = input(1, minval=1, title="Line Width") 
tra = input(0, minval=0, title="Line Opacity") 
sty = linebr
clr = black


mon = 0
tue = 0
wed = 0
thu = 0
fri = 0


// holds the daily price levels
highPrice = security(tickerid, 'D', high)
lowPrice  = security(tickerid, 'D', low)

//adjust lines "offset" per timeframe

if (period == "5")
    tue := 289
    wed := 578
    thu := 867
    fri := 1156

if (period == "15")
    tue := 96
    wed := 192
    thu := 288
    fri := 384

if (period == "30")
    mon := 0
    tue := 48
    wed := 96
    thu := 144
    fri := 192

if (period == "60")
    tue := 24
    wed := 48
    thu := 72
    fri := 96

if (period == "120")
    tue := 11
    wed := 22
    thu := 33
    fri := 44


//function which is called by plot to establish day of the week is monday return true or false
isMonday() => dayofweek(time('D')) == sunday ? 1 : 0


// Monday Plot
mh = plot(isMonday() and highPrice ? highPrice:  na, title="Monday High", style=sty, offset=mon,     linewidth=wid, color=clr, transp=tra)
ml = plot(isMonday() and lowPrice  ? lowPrice :  na, title="Monday Low",  style=sty, offset=mon,         linewidth=wid, color=clr, transp=tra)
fill(mh, ml, color=blue)

// Tuesday Plot
th = plot(isMonday() and highPrice ? highPrice:  na, title="Monday High", style=sty, offset=tue, linewidth=wid, color=clr, transp=tra)
tl = plot(isMonday() and lowPrice  ? lowPrice :  na, title="Monday Low",  style=sty, offset=tue, linewidth=wid, color=clr, transp=tra)
//fill(th, tl, color=orange)

// Wednesday Plot
wh = plot(isMonday() and highPrice ? highPrice:  na, title="Monday High", style=sty, offset=wed, linewidth=wid, color=clr, transp=tra)
wl = plot(isMonday() and lowPrice  ? lowPrice :  na, title="Monday Low",  style=sty, offset=wed, linewidth=wid, color=clr, transp=tra)
//fill(wh, wl, color=orange)

// Thursday Plot
thh = plot(isMonday() and highPrice ? highPrice:  na, title="Monday High", style=sty, offset=thu, linewidth=wid, color=clr, transp=tra)
thl = plot(isMonday() and lowPrice  ? lowPrice :  na, title="Monday Low",  style=sty, offset=thu, linewidth=wid, color=clr, transp=tra)
//fill(thh, thl, color=orange)

// Friday Plot
fh = plot(isMonday() and highPrice ? highPrice:  na, title="Monday High", style=sty, offset=fri, linewidth=wid, color=clr, transp=tra)
fl = plot(isMonday() and lowPrice  ? lowPrice :  na, title="Monday Low",  style=sty, offset=fri, linewidth=wid, color=clr, transp=tra)
//fill(fh, fl, color=red)
question from:https://stackoverflow.com/questions/65875691/pinescript-plotting-sunday-monday-high-low-for-duration-of-whole-week

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will do what you asked minus extending the lines through the Sunday/Monday session.

enter image description here

//@version=4
study(title="My Indicator", overlay=true)

new_period(p) => p and na(p[1])

sm = time(timeframe.period, "0000-0000:12")
var float h = close
var float l = close
if new_period(sm)
    h := close
    l := close
if sm
    h := max(h, high)
    l := min(l, low)
    
bg_color = sm ? color.blue : na
bgcolor(bg_color)
plot(h, color=color.green)
plot(l, color=color.red)

If it's essential that the lines extend all the way I think you would have to create lines as you go, perhaps with an array.

Make a new session for the entire 5-day period and set the first x coordinate for the line. Update until the session renews again, then create a new line.

I'm not familiar with the Forex session but change the 0000-0000 as necessary, right now it's midnight-to-midnight for Sunday and Monday.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...