首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pine-script上的问题:执行多个可选的代码行

Pine-script上的问题:执行多个可选的代码行
EN

Stack Overflow用户
提问于 2020-12-06 03:13:12
回答 1查看 84关注 0票数 0

我正在尝试找出是否有一种方法可以运行几行代码,同时防止其他几行在pine-script中运行。希望下面的示例代码场景能澄清我的意图:

代码语言:javascript
复制
//@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
EN

回答 1

Stack Overflow用户

发布于 2020-12-06 19:09:43

这个应该可以了。

代码语言:javascript
复制
//@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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65160921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档