Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

function - writing a simple condition for pine script

I want to write a simple condition for pine script that if the price hits the plus sign the background color will be green like the following picture. but I wrote it and it wasn't what I expected it makes background only for two candles and I want it for all of the next candles. How can I do that? Thank you This is the picture

//@version=4
study(title="My Indicator", overlay=true)
// Entry Point
entryPointLong = highest(high, 100)
entryPointShort = lowest(low, 100)
// Entry Point Condition
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong, title="Entry Long", style=plot.style_cross, linewidth=5, show_last=1, color=color.green)
plot(entryPointShort, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
// Background Color
bgcolor(entryPointLongCond, transp=70)
bgcolor(entryPointShortCond, transp=70)

Updated Code:

//@version=4
study(title="My Indicator", overlay=true)
// Entry Point
entryPointLong = highest(high, 100)
entryPointShort = lowest(low, 100)
// Stop Loss
stopLossLongInput = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=3) * 0.01
stopLossLong = entryPointLong * (1 - stopLossLongInput)
// Entry Point Condition
entryPointLongTrue = close >= entryPointLong
if entryPointLongTrue
    entryPointLong := stopLossLong
    color.green
entryPointLongCond = close >= entryPointLong ? color.green : na
entryPointShortCond = close <= entryPointShort ? color.red : na
// Plot
plot(entryPointLong, title="Entry Long", style=plot.style_cross, linewidth=5, show_last=1, color=color.green)
plot(entryPointShort, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
plot(stopLossLong, title="Entry Short", style=plot.style_cross, linewidth=5, show_last=1, color=color.red)
// Background Color
bgcolor(entryPointLongCond, transp=70)
bgcolor(entryPointShortCond, transp=70)
question from:https://stackoverflow.com/questions/65868860/writing-a-simple-condition-for-pine-script

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

1 Reply

0 votes
by (71.8m points)
//@version=4
study("My Script", overlay=true)

CO1 = "Crossover", CO2 = "Above"

priceLevel = input(1650)
condition  = input(CO1, options=[CO1, CO2])

var bool triggered = na

if condition == CO1
    triggered := crossover(close, priceLevel)
else if condition == CO2
    triggered := close > priceLevel
else
    triggered := false

bgcolor(triggered ? color.green : na)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...