error-handling - 松脚本: “Can' t call 'security' inside: 'if' , 'for' ”

标签 error-handling pine-script

因为我找不到任何方法可以在多个符号上测试我的Pine Script策略,所以我创建了一种遍历整个脚本的方法。

在此,我为10个不同的符号制作了10个变量,如下所示:

ersteTicker = "AAPL"
zweiteTicker = "MSFT"
dritterTicker = "..."

比起我从1到10的循环,并进行了10次If-query,这使我在每个循环中都得到了如下正确的符号:
a = 1
    for i = 0 to 10
        if a == 1
            tickerID = ersteTicker
        if a == 2
            tickerID = .....

现在我应该一切都很好,但是现在控制台会返回一条错误消息:

第75行:不能在“if”,“for”内部调用“security”

有人知道如何绕过这个问题吗?

最好的祝福
基督教

P.S .:我已经测试了另一个小的脚本,在这个脚本中,即使我也使用安全功能创建了for循环,控制台也不会向我返回此错误消息。

(看起来像这样)
//@version=3
strategy("Meine Strategie", overlay=true)
tickerID = "ADS"
vergleichstimeframe = "D"
TaesRSLPeriode = 200
a = 1

myEma() => ema(close, TaesRSLPeriode)
for i = 0 to 10 
    if ( a == 1)
        Daily_ema = security(tickerID, vergleichstimeframe, myEma())
        //plot(Daily_ema*TagesRSLGrenzwert)

        longCondition = crossover(sma(close, 14), sma(close, 28))
        if (longCondition)
            strategy.entry("My Long Entry Id", strategy.long)

        shortCondition = crossunder(sma(close, 14), sma(close, 28))
        if (shortCondition)
            strategy.entry("My Short Entry Id", strategy.short)

最佳答案

这是全局安全性的示例。该安全性不能位于for或if语句之内。如果您需要更多符号-使用更多证券。但是请记住,您不能从一组符号中选择一个符号并使用该符号调用安全性(因为它将是可变变量,并且不能将它们用于安全性):

//@version=3
strategy("Meine Strategie", overlay=true)
tickerID = "ADS"
vergleichstimeframe = "D"
TaesRSLPeriode = 200
a = 1

myEma() => ema(close, TaesRSLPeriode)

// this always must stay global 
Daily_ema = security(tickerID, vergleichstimeframe, myEma())
// here you could put more secureties:
//Daily_ema1 = security(tickerID1, vergleichstimeframe, myEma())
//Daily_ema2 = security(tickerID2, vergleichstimeframe, myEma())
//Daily_ema3 = security(tickerID3, vergleichstimeframe, myEma())
// ...

for i = 0 to 10 
    if a == 1
        if Daily_ema > Daily_ema[i] // actual using of the security's result
            strategy.entry("My Long Entry Id", strategy.long)

关于error-handling - 松脚本: “Can' t call 'security' inside: 'if' , 'for' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59837568/

相关文章:

pine-script - 松本中的线角

angular - 使用 catchError 更新组件

ruby - 引发自定义异常、返回常量或返回符号?为什么?

pine-script - TradingView 帮助使用多个 strategy.exit 调用

python - 如何在 TradingView Pine 脚本中运行条件语句?

pine-script - 我在 Pine Script 中遇到 TradingView 的 'end of line without continuation' 错误

Pine-Script + 优化,时间更短,回测

php - 专辑编码错误,简单的PHP

javascript - 将jQuery变量传递给PHP以进行错误处理

java.io.FileNotFoundException 错误