Tradingview松树脚本是一件大事:不能使用可变变量作为安全功能的参数

时间:2019-12-29 12:30:22

标签: syntax pine-script technical-indicator

老实说,在脚本语言中实现选项而没有后端问题有多么困难。我得到以下错误“不能将可变变量用作安全函数的参数”,这是因为我一直在传递在if语句中设置的变量。在各种排列方式中尝试了变量间接寻址,函数间接寻址,甚至我所知的一些方法都已成功完成此操作,但问题仍然存在。

我想要一种干净的方法来编写此代码,而不必使用三元运算或变通方法。在重复的问题中建议三元运算和函数变通,但不能回答我要查找的内容。任何编程语言脚本或面向对象的语言都可以使用if语句来处理选项,而该语言则不能。

// @version=4
study("multi_ma_pos", "multi_ma", overlay=true)

get_period(interval_input) =>
    period = ""
    if (timeframe.isintraday)
        interval = interval_input > timeframe.multiplier ? interval_input : timeframe.multiplier
        period := tostring(interval)
    else
        period := timeframe.period
    period

ma_opt = input(title="ma_opt", defval="13/34", options=["23/75/616", "13/34"])
ma_interval = input(title="ma_interval", type=input.integer, defval=240)

// Declare variables
shrt_term = close
midt_term = close
long_term = close
is_opt_selected = false

if (ma_opt == "23/75/616" and not is_opt_selected)
    shrt_term := sma(close, 23)
    midt_term := sma(close, 65)
    long_term := sma(close, 137)
    is_opt_selected := true

if (ma_opt == "13/34" and not is_opt_selected)
    shrt_term := ema(close, 13)
    midt_term := ema(close, 34)
    long_term := sma(close, 34)      
    is_opt_selected := true

shrt_term_out = security(syminfo.tickerid, get_period(ma_interval), shrt_term)
midt_term_out = security(syminfo.tickerid, get_period(ma_interval), midt_term)
long_term_out = security(syminfo.tickerid, get_period(ma_interval), long_term)

plot(shrt_term_out, title="shrt_term", color=#ffa726, linewidth=2)
plot(midt_term_out, title="midt_term", color=#ffa726, linewidth=4)
plot(long_term_out, title="long_term", color=#e65100, linewidth=4)

1 个答案:

答案 0 :(得分:0)

以下是根据此post的建议给出的答案。这远非理想,但也许对于那些遇到此问题的人来说,他们可以从一个问题,一个建议,然后解决这个无能的问题的解决方案中受益。希望以下解决方案具有一些易于阅读的编码样式。

// @version=4
study("multi_ma_pos", "multi_ma", overlay=true)

get_period(interval_input) =>
    period = ""
    if (timeframe.isintraday)
        interval = interval_input > timeframe.multiplier ? interval_input : timeframe.multiplier
        period := tostring(interval)
    else
        period := timeframe.period
    period

ma_opt = input(title="ma_opt", defval="13/34", options=["23/75/616", "13/34"])
ma_interval = input(title="ma_interval", type=input.integer, defval=240)

get_multi_ma(ma_opt, ma_interval) =>
    // delcare output variables
    shrt_term_out = float(na)
    midt_term_out = float(na)
    long_term_out = float(na)

    period = get_period(ma_interval)

    shrt_term_out := (ma_opt == "23/75/616")  ? security(syminfo.tickerid, period, ema(close, 23)) 
       : security(syminfo.tickerid, period, sma(close, 13))

    midt_term_out := (ma_opt == "23/75/616")  ? security(syminfo.tickerid, period, ema(close, 75)) 
       : security(syminfo.tickerid, period, sma(close, 34))

    long_term_out := (ma_opt == "23/75/616")  ? security(syminfo.tickerid, period, ema(close, 616)) : security(syminfo.tickerid, period, ema(close, 34))

    // out the output variables
    [shrt_term_out, midt_term_out, long_term_out]

[shrt_term_out, midt_term_out, long_term_out] = get_multi_ma(ma_opt, ma_interval)

plot(shrt_term_out, title="shrt_term", color=#ffa726, linewidth=2)
plot(midt_term_out, title="midt_term", color=#ffa726, linewidth=4)
plot(long_term_out, title="long_term", color=#e65100, linewidth=4)