用于功能和 atr 功能

问题描述 投票:0回答:2

我是 for 函数和 ta.atr() 函数的新手

我的想法是 做多条件1:价格上限>=30% 多头条件 2:最后 7 根柱的 ATR 至少为 < 5% Long condition 3: Previous bar is above EMA 10 Long condition 4: Previous bar is ATR >5%

做空条件1:价格下跌>=30% 做空条件 2:最后 7 根柱的 ATR 至少为 < 5% Short condition 3:Previous bar is below EMA 10 Short condition 4:Previous bar is ATR >5%

我的代码是

    //@version=5
strategy(title="ATR-based Long/Short Trading Strategy", overlay=true)

// Input variables
length = input.int(title="ATR Lookback Length", defval=7, minval=1)
atrLength = input.int(title="ATR Length", defval=10, minval=1)
atrPercent = input.float(title="ATR Percent", defval=5, minval=1, maxval=50) / 100

// Calculate ATR
atr = ta.atr(1)
atr_ma = ta.sma(atr, atrLength)

// Calculate conditions
priceChange = (close - close[length]) / close[length] * 100
atr_break = close > atr_ma * (1 + atrPercent)
price_above_ema = close > ta.ema(close, 10)
num_bars_atr = 0
for i = 1 to length
    if ta.atr(i) < atr_ma * atrPercent
        num_bars_atr := i
        break

// Buy condition
buy_condition = (priceChange >= 30) and (num_bars_atr >= length) and (price_above_ema) and (atr_break)

// Sell condition
sell_condition = (priceChange <= -30) and (num_bars_atr >= length) and (not price_above_ema) and (not atr_break)

// Buy and sell logic
if buy_condition
    strategy.entry("Long", strategy.long)
if sell_condition
    strategy.entry("Short", strategy.short)

并且tradingview运行时有bug。我不知道如何解决它。 行错误是第 19 行 --> if ta.atr(i) < atr_ma * atrPercent error show is:

无法使用参数“length”=“i”调用“ta.atr”。的一个论点 使用了“series int”类型,但期望使用“simple int”。

pine-script pine-script-v5 trading
2个回答
0
投票

好吧,我们先把事情搞清楚。

什么是软件错误? 来源

软件错误是设计、开发、 或导致其产生的计算机软件的操作 不正确或意外的结果,或以意想不到的方式行事。

您所拥有的不是错误。您没有正确使用该功能。正如错误消息所示,

ta.atr()
需要
simple int
。这意味着这些类型的变量:

  • 脚本执行期间不要更改
  • 在编译时未知

您可以阅读this以了解有关变量类型的更多信息。

您正在循环中调用

ta.atr()
函数并更改其
length
值。因此,编译器的抱怨是正确的。

如果您想计算最后 x 条中某个条件的出现次数,您可以使用此处描述的方法。


0
投票

我可以在函数中使用可变长度吗?

您可以在以下 Pine 函数中使用“series int”长度(因此长度因条而异):alma()、bb()、bbw()、cci()、change()、cmo() 、 cog()、correlation()、dev()、falling()、highest()、highestbars()、linreg()、lowest()、lowestbars()、mfi()、mom()、percentile_linear_interpolation()、percentile_nearest_rank ()、percentrank()、rising()、roc()、sma()、stdev()、stoch()、sum()、variance()、vwma()、wma() 和 wpr()。

alexgrover 的允许系列作为长度的函数脚本提供了 ema()、atr()、lsma()、variance()、covariance()、stdev() 和 correlation() 函数的版本。

脚本网址 https://www.tradingview.com/script/kY5hhjA7-Functions-Allowing-Series-As-Length-PineCoders-FAQ/

© www.soinside.com 2019 - 2024. All rights reserved.