我正在尝试找出是否有一种方法可以运行几行代码,同时防止其他几行在pine-script中运行。希望下面的示例代码场景能澄清我的意图:
//@version=4
study("Example Code", overlay = true, shorttitle = "ExampleCode")
//I have created an drop down selector input:
DropDownInput = input(title="Select Input”, defval="Green", options=["Green", "Yellow", "Red"])
// Is there a way for me to select multiple rows of the below code to run based on whichever color is selected at the above DropDownInput
// "Green" Code would be something like and if "Green" is selected at DropDownInput only these rows of code would execute
ShortMA = input(defval=15)
LongMA = input(defval=30)
ShortMACode = sma(close,ShortMA)
LongMACode = sma(close,LongMA)
BUYSignal1 = crossover(sma(close,ShortMA), sma(close,LongMA))
line1 = plot(ShortMACode, color=color.silver, title="Short MA", style=plot.style_line, linewidth = 0)
line2 = plot(LongMACode, color=color.black, title="Long MA", style=plot.style_line, linewidth = 0)
fill(line1, line2, color = ShortMACode>LongMACode ? color.green : color.red)
plotshape(BUYSignal1, style=shape.triangleup,location=location.abovebar, color=color.green, size=size.small)
//End of "Green" executed code
// "Yellow" Code would be something like and if "Yellow" is selected at DropDownInput only these rows of code would execute
ShortMA = input(defval=20)
LongMA = input(defval=40)
ShortMACode = sma(close,ShortMA)
LongMACode = sma(close,LongMA)
BUYSignal1 = crossover(sma(close,ShortMA), sma(close,LongMA))
line1 = plot(ShortMACode, color=color.silver, title="Short MA", style=plot.style_line, linewidth = 0)
line2 = plot(LongMACode, color=color.black, title="Long MA", style=plot.style_line, linewidth = 0)
fill(line1, line2, color = ShortMACode>LongMACode ? color.green : color.red)
plotshape(BUYSignal1, style=shape.triangleup,location=location.abovebar, color=color.green, size=size.small)
//End of "Yellow" executed code
// "Red" Code would be something like and if "Red" is selected at DropDownInput only these rows of code would execute
ShortMA = input(defval=30)
LongMA = input(defval=60)
ShortMACode = sma(close,ShortMA)
LongMACode = sma(close,LongMA)
BUYSignal1 = crossover(sma(close,ShortMA), sma(close,LongMA))
line1 = plot(ShortMACode, color=color.silver, title="Short MA", style=plot.style_line, linewidth = 0)
line2 = plot(LongMACode, color=color.black, title="Long MA", style=plot.style_line, linewidth = 0)
fill(line1, line2, color = ShortMACode>LongMACode ? color.green : color.red)
plotshape(BUYSignal1, style=shape.triangleup,location=location.abovebar, color=color.green, size=size.small)
//End of "Red" executed code发布于 2020-12-06 19:09:43
这个应该可以了。
//@version=4
study("Example Code", overlay = true, shorttitle = "ExampleCode")
// ————— Options for inputs
DD0 = "Green", DD1 = "Yellow", DD2 = "Red"
var string DropDownInput = input(title="Select Input", defval=DD0, options=[DD0, DD1, DD2])
var int ShortMAGreen = input(defval=15)
var int LongMAGreen = input(defval=30)
var int ShortMAYellow = input(defval=20)
var int LongMAYellow = input(defval=40)
var int ShortMARed = input(defval=30)
var int LongMARed = input(defval=60)
var int ShortMA = na
var int LongMA = na
var float ShortMACode = na
var float LongMACode = na
var bool BUYSignal1 = na
var bool selectedGreen = DropDownInput == DD0
var bool selectedYellow = DropDownInput == DD1
var bool selectedRed = DropDownInput == DD2
if selectedGreen
ShortMA := ShortMAGreen
LongMA := LongMAGreen
else if selectedYellow
ShortMA := ShortMAYellow
LongMA := LongMAYellow
else if selectedRed
ShortMA := ShortMARed
LongMA := LongMARed
ShortMACode := sma(close, ShortMA)
LongMACode := sma(close, LongMA)
BUYSignal1 := crossover(ShortMACode, LongMACode)
line1 = plot(ShortMACode, color=color.silver, title="Short MA", style=plot.style_line, linewidth = 0)
line2 = plot(LongMACode, color=color.black, title="Long MA", style=plot.style_line, linewidth = 0)
fill(line1, line2, ShortMACode>LongMACode ? color.green : color.red)
plotshape(BUYSignal1, style=shape.triangleup, location=location.abovebar, color=color.green, size=size.small)https://stackoverflow.com/questions/65160921
复制相似问题