建议在Pine Script中从此范围中提取该调用。

huangapple go评论80阅读模式
英文:

It is recommended to extract the call from this scope in Pine Script

问题

我正在尝试为自己创建一个多资产指标作为示例证明。为此,我选择了一个非常简单的指标,它应该在表格中显示3个符号的移动平均线交叉状态。当然,在尝试从下面的for循环中提取ta.ema时,我遇到了一个问题。

//@version=5
indicator("Multi Symbol EMA Cross", overlay=true)

// Define symbols and resolutions
symbols = input.string("BINANCE:BTCUSDT,BINANCE:ETHUSDT,BINANCE:LTCUSDT", "Symbols", inline="symbols")
res = input.string("D", "Resolution", inline="symbols")

// Split symbols into an array
symbolList = str.split(symbols, ",")

// Create a table with two columns and three rows
t = table.new(position.top_right, 2, 3)

// Loop through symbols and plot arrows
for i = 0 to array.size(symbolList) - 1
    // Get symbol name
    sym = array.get(symbolList, i)
    // Get close price of symbol
    price = request.security(sym, res, close)
    // Calculate EMAs
    ema1 = ta.ema(price, 9)
    ema2 = ta.ema(price, 21)
    // Update table cells
    table.cell(t, 0, i, sym)
    table.cell(t, 1, i, ta.crossup(ema1, ema2) ? "+" : ta.crossdown(ema1, ema2) ? "-" : "", ta.crossup(ema1, ema2) ? color.green : ta.crossdown(ema1, ema2) ? color.red : na)

通常情况下,我会尝试使用 := 来将其从for循环中提取出来,但这意味着还必须提取price变量。编写函数也不起作用,因为然后警告会说应该在每次计算上调用它以保持一致性。由于在这个示例中,由于for循环的原因,我无法在主范围上调用request.security,所以我不知道该如何解决这个问题 - 除了硬编码一切以外。

关于Pine Script工作方式,我在这里有什么误解,我可以做什么来解决这些问题?

英文:

I'm trying to write a multi-asset indicator as a proof of example for myself. For this purpose I've chosen a very simplistic indicator that is supposed to display in a table the moving average cross status of 3 symbols in a table. Naturally, I'm running into an issue trying to extract ta.ema, for example, from the for loop below.

//@version=5
indicator("Multi Symbol EMA Cross", overlay=true)

// Define symbols and resolutions
symbols = input.string("BINANCE:BTCUSDT,BINANCE:ETHUSDT,BINANCE:LTCUSDT", "Symbols", inline="symbols")
res = input.string("D", "Resolution", inline="symbols")

// Split symbols into an array
symbolList = str.split(symbols, ",")

// Create a table with two columns and three rows
t = table.new(position.top_right, 2, 3)

// Loop through symbols and plot arrows
for i = 0 to array.size(symbolList) - 1
    // Get symbol name
    sym = array.get(symbolList, i)
    // Get close price of symbol
    price = request.security(sym, res, close)
    // Calculate EMAs
    ema1 = ta.ema(price, 9)
    ema2 = ta.ema(price, 21)
    // Update table cells
    table.cell(t, 0, i, sym)
    table.cell(t, 1, i, ta.crossup(ema1, ema2) ? "+" : ta.crossdown(ema1, ema2) ? "-" : "", ta.crossup(ema1, ema2) ? color.green : ta.crossdown(ema1, ema2) ? color.red : na)

Normally I'd try to do something with := to get it out of the for loop, but this means the price variable also has to be extracted. Writing a function also doesn't work, because then the warning says it should be called on each calculation for consistency. Since, in this example, I can't call requrest.security on the main scope because of the for loop, I have no idea how I'd solve this – other than just hardcoding everything.

What am I misunderstanding here about the way Pine Script works, and what can I do to resolve the issues?

答案1

得分: 1

调用类似 ta.ema 这样的函数在本地作用域中并不是一个好主意。您可能会丢失一些历史数据,并得到不正确的结果。这就是错误消息告诉您的内容。

我会这样做:
为每个交易对创建用户输入。

p_sym2 = input.symbol("BINANCE:ETHUSDT")```

然后创建一个获取 EMA 值的函数。

```p_get_ema(p_sym) =>
    ema1 = ta.ema(close, 9)
    ema2 = ta.ema(close, 21)
    [htf_ema1, htf_ema2] = request.security(p_sym, res, [ema1, ema2])```

然后,您可以为每个交易对调用这个函数多次。

```[sym1_ema1, sym1_ema2] = get_ema(p_sym1)
[sym2_ema1, sym2_ema2] = get_ema(p_sym2)```

现在,`ta.ema` 将在全局作用域中调用。然后可以在另一个函数中处理表格。

<details>
<summary>英文:</summary>

Calling functions like `ta.ema` in local scopes is not a good idea. You might lose some history andm get not correct results. That is what the error message is telling you.

The way I would do is:
Create user inputs for each symbol.

    sym1 = input.symbol(&quot;BINANCE:BTCUSDT&quot;)
    sym2 = input.symbol(&quot;BINANCE:ETHUSDT&quot;)

Then create a function to get the ema values

    get_ema(p_sym) =&gt;
        ema1 = ta.ema(close, 9)
        ema2 = ta.ema(close, 21)
        [htf_ema1, htf_ema2] = request.security(p_sym, res, [ema1, ema2]

And then you would call this function n times for each symbol.

    [sym1_ema1, sym1_ema2] = get_ema(sym1)
    [sym2_ema1, sym2_ema2] = get_ema(sym2)

Now, `ta.ema` will be called in the global scope. Then handle the table maybe in another function.

</details>



huangapple
  • 本文由 发表于 2023年8月10日 14:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定