Pine脚本错误&quot&quot 27:输入时的语法错误' i。

发布于 2025-01-20 08:40:55 字数 1689 浏览 2 评论 0原文

我尝试了所有内容,但仍然给我这个错误:“ 第27行:输入'i'的语法错误。“似乎也没有凹痕问题。

请帮助。先感谢您。

'''

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VaheHV

//@version=5
strategy("Momentum Signal", overlay=true)

ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
ema9 = ta.ema(close, 9)
macdLine = ema12 - ema26
macdSignal = ta.ema(macdLine, 9)

crossover = ta.crossover(macdLine, macdSignal)
crossunder = ta.crossunder(macdLine, macdSignal)

if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
    strategy.entry("Long", strategy.long)
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
    strategy.entry("Short", strategy.short)
    
float[] longStopLoss = na
float[] longTakeProfit = na
float[] shortStopLoss = na
float[] shortTakeProfit = na

if strategy.opentrades != 0
    for i = 0 to strategy.opentrades - 1
        if strategy.position_size > 0
            array.push(longStopLoss, ta.lowest(low, 15))
            array.push(longTakeProfit, (1.5*(strategy.opentrades.entry_price(i) - longStopLoss[i]) + strategy.opentrades.entry_price(i)))
            strategy.exit("LongClose", profit = longTakeProfit[i] * 100, loss = longStopLoss[i] * 100)
        else if strategy.position_size < 0
            array.push(shortStopLoss, ta.highest(high, 15))
            array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(shortStopLoss[i] - strategy.opentrades.entry_price(i)))
            strategy.exit("ShortClose", profit = shortTakeProfit[i] * 100, loss = shortStopLoss[i] * 100)

'''

I tried everything but it still gives me this error: "line 27: Syntax error at input 'i'." It seems there are no indentation issues either.

Help please. Thank you in advance.

'''

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VaheHV

//@version=5
strategy("Momentum Signal", overlay=true)

ema12 = ta.ema(close, 12)
ema26 = ta.ema(close, 26)
ema9 = ta.ema(close, 9)
macdLine = ema12 - ema26
macdSignal = ta.ema(macdLine, 9)

crossover = ta.crossover(macdLine, macdSignal)
crossunder = ta.crossunder(macdLine, macdSignal)

if crossover and macdLine < 0 and macdSignal < 0 and close > ta.ema(close, 200)
    strategy.entry("Long", strategy.long)
if crossunder and macdLine > 0 and macdSignal > 0 and close < ta.ema(close, 200)
    strategy.entry("Short", strategy.short)
    
float[] longStopLoss = na
float[] longTakeProfit = na
float[] shortStopLoss = na
float[] shortTakeProfit = na

if strategy.opentrades != 0
    for i = 0 to strategy.opentrades - 1
        if strategy.position_size > 0
            array.push(longStopLoss, ta.lowest(low, 15))
            array.push(longTakeProfit, (1.5*(strategy.opentrades.entry_price(i) - longStopLoss[i]) + strategy.opentrades.entry_price(i)))
            strategy.exit("LongClose", profit = longTakeProfit[i] * 100, loss = longStopLoss[i] * 100)
        else if strategy.position_size < 0
            array.push(shortStopLoss, ta.highest(high, 15))
            array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(shortStopLoss[i] - strategy.opentrades.entry_price(i)))
            strategy.exit("ShortClose", profit = shortTakeProfit[i] * 100, loss = shortStopLoss[i] * 100)

'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-27 08:40:55

pinescript 中的 [] 称为 历史引用运算符。它用于访问变量的历史数据。

您不使用 [] 访问数组元素。相反,您应该使用 array.get() 函数。

另外,下面一行缺少一个右括号。因此,请检查我的示例中的这一行。

array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(shortStopLoss[i] - strategy.opentrades.entry_price(i)))

这应该有效:

if strategy.opentrades != 0
    for i = 0 to strategy.opentrades - 1
        if strategy.position_size > 0
            array.push(longStopLoss, ta.lowest(low, 15))
            array.push(longTakeProfit, (1.5*(strategy.opentrades.entry_price(i) - array.get(longStopLoss, i)) + strategy.opentrades.entry_price(i)))
            strategy.exit("LongClose", profit = array.get(longTakeProfit, i) * 100, loss = array.get(longStopLoss, i) * 100)
        else if strategy.position_size < 0
            array.push(shortStopLoss, ta.highest(high, 15))
            array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(array.get(shortStopLoss, i) - strategy.opentrades.entry_price(i))))
            strategy.exit("ShortClose", profit = array.get(shortTakeProfit, i) * 100, loss = array.get(shortStopLoss, i) * 100)

[] in pinescript is called history reference operator. It is used to access historical data of a variable.

You don't use [] to access array elements. Instead, you should use array.get() function for that.

Also, below line is missing one closing parentheses. So, check this line in my example.

array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(shortStopLoss[i] - strategy.opentrades.entry_price(i)))

This should work:

if strategy.opentrades != 0
    for i = 0 to strategy.opentrades - 1
        if strategy.position_size > 0
            array.push(longStopLoss, ta.lowest(low, 15))
            array.push(longTakeProfit, (1.5*(strategy.opentrades.entry_price(i) - array.get(longStopLoss, i)) + strategy.opentrades.entry_price(i)))
            strategy.exit("LongClose", profit = array.get(longTakeProfit, i) * 100, loss = array.get(longStopLoss, i) * 100)
        else if strategy.position_size < 0
            array.push(shortStopLoss, ta.highest(high, 15))
            array.push(shortTakeProfit, (strategy.opentrades.entry_price(i) - 1.5*(array.get(shortStopLoss, i) - strategy.opentrades.entry_price(i))))
            strategy.exit("ShortClose", profit = array.get(shortTakeProfit, i) * 100, loss = array.get(shortStopLoss, i) * 100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文