简体   繁体   中英

How to Plot Price label when Pivot crossover or Previous Day High or Low Crossover Occur-Pine Script

hi how to plot a label showing close price above a bar when pivot crossover occur. I require to show a label when a bar closes above R1 or Previous day high and a bar closes below S1 or Previous day low. Iam able to plot shapes but dont know how to plot label.please help in coding.

//////////////////////////////////////////////////////////////////////////

//@version=4
study(title="CENTRAL PIVOT RANGE", shorttitle="CPR", overlay=true)

//Get Candle and Time frameDatabase
pivotType = input(title="CPR TIME FRAME", defval='D')

getSeries(e, timeFrame) =>security(syminfo.tickerid, timeFrame, e, lookahead=barmerge.lookahead_on)

//Colors
cprColor = color.blue
rColor = color.green
sColor = color.red
phColor = color.navy
plColor = color.maroon

//Line Style & Transparency
lStyle = plot.style_stepline
lTransp = 0

//Fill Transparency
fTransp = 99

//Switches
switch1 = input(title="DAILY CPR", type=input.bool, defval=true)
switch6 = input(title="PREVIOUS DAY HIGH & LOW", type=input.bool, defval=true)


//Main Pivot Data
H = getSeries(high[1], 'D')
L = getSeries(low[1], 'D')
C = getSeries(close[1], 'D')

//Central Pivot Range
P = (H + L + C) / 3
BC = (H + L) / 2
TC = P - BC + P

//Resistance Levels
R1 = P * 2 - L
R2 = P + H - L
R3 = H + 2 * (P - L)
R4 = R3 + R2 - R1

//Support Levels
S1 = P * 2 - H
S2 = P - (H - L)
S3 = L - 2 * (H - P)
S4 = S3 - (S1 - S2)

plot(switch1 ? R4 : na, title="R4", color=rColor, transp=lTransp, style=lStyle)
plot(switch1 ? R3 : na, title="R3", color=rColor, transp=lTransp, style=lStyle)
plot(switch1 ? R2 : na, title="R2", color=rColor, transp=lTransp, style=lStyle)
plot(switch1 ? R1 : na, title="R1", color=rColor, transp=lTransp, style=lStyle)

p1 = plot(switch1 ? TC : na, title="TC", color=cprColor, transp=lTransp, style=lStyle)
plot(switch1 ? P : na, title="PIVOT", color=cprColor, transp=lTransp, style=plot.style_circles)
p2 = plot(switch1 ? BC : na, title="BC", color=cprColor, transp=lTransp, style=lStyle)

plot(switch1 ? S1 : na, title="S1", color=sColor, transp=lTransp, style=lStyle)
plot(switch1 ? S2 : na, title="S2", color=sColor, transp=lTransp, style=lStyle)
plot(switch1 ? S3 : na, title="S3", color=sColor, transp=lTransp, style=lStyle)
plot(switch1 ? S4 : na, title="S4", color=sColor, transp=lTransp, style=lStyle)

//Fill Transparency in CPR
fill(p1, p2, color=color.blue, transp=fTransp)


//Previous Day High and Low
prev_day_high = security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
prev_day_low = security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)

plot(switch6 ? prev_day_high : na, title="PDH", color=phColor, transp=lTransp, linewidth=1, style=lStyle)
plot(switch6 ? prev_day_low : na, title="PDL", color=plColor, transp=lTransp, linewidth=1, style=lStyle)

//Cpr Alert
cprlong=crossover(close,R1)
cprshort=crossunder(close,S1)

//Plot Cpr Alert
plotshape(cprlong, title="R1 Crossing Up", style=shape.triangleup, size=size.tiny, location=location.belowbar, color=color.green, transp=0)
plotshape(cprshort, title="S1 Crossing Down", style=shape.triangledown, size=size.tiny, location=location.abovebar, color=color.red, transp=0)

Usetostring to convert close to a string and label.new to create a new label when the condition is true:

if cprlong
    label.new(bar_index, na, text=tostring(close), yloc=yloc.abovebar) 

if cprshort
    label.new(bar_index, na, text=tostring(close), yloc=yloc.abovebar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM