【问题标题】:Question on Pine-script: Executing multiple alternative rows of code关于 Pine-script 的问题:执行多行替代代码
【发布时间】:2020-12-05 19:13:12
【问题描述】:

我正在尝试找出是否有一种方法可以运行多行代码,同时防止其他几行在 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

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    应该这样做。

    //@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)
    

    【讨论】:

    • 谢谢比约恩!我会测试一下:)
    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2015-03-09
    • 1970-01-01
    • 2023-04-04
    • 2012-12-25
    • 2012-07-02
    相关资源
    最近更新 更多