Writing a Pinescript code for Auto trading with the MACD but get this error

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

Writing a Pinescript code for Auto trading with the MACD but get this error

问题

这是我写的代码,但当我运行它时出现了以下错误:
编译错误。第24行:找不到函数或函数引用 'crossover'。

// © KabeerMehdi

//@version=5
//MACD自动交易策略

strategy("MACD", overlay=true)

// 定义变量
longLength = 26
shortLength = 12
signalLength = 9

// 在不同时间框架上计算MACD和信号线
macd1 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal1 = ta.ema(macd1, signalLength)
macd2 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal2 = ta.ema(macd2, signalLength)
macd3 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal3 = ta.ema(macd3, signalLength)

// 定义进出场条件
longCondition = macd1 > signal1 and macd2 > signal2 and macd3 > signal3
shortCondition = macd1 < signal1 and macd2 < signal2 and macd3 < signal3
exitCondition = ta.crossover(macd1, signal1) or ta.crossover(macd1, signal1) or ta.crossover(macd2, signal2) or ta.crossover(macd2, signal2) or ta.crossover(macd3, signal3) or ta.crossover(macd3, signal3)

// 进入多头头寸
if longCondition
    strategy.entry("Long", strategy.long)

// 进入空头头寸
if shortCondition
    strategy.entry("Short", strategy.short)

// 退出头寸
if exitCondition
    strategy.close()

我尝试过在Google上查找并检查函数名称是否错误,但我并没有。而且我使用的是版本5,所以这应该可以工作。

英文:

This is my code that I've written but when I run it I get this error:
Compilation error. Line 24: Could not find function or function reference 'crossover'

// &#169; KabeerMehdi

//@version=5
//MACD auto-trading strategy

strategy(&quot;MACD&quot;, overlay=true)

// Define variables
longLength = 26
shortLength = 12
signalLength = 9

// Calculate MACD and signal line on different time frames
macd1 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal1 = ta.ema(macd1, signalLength)
macd2 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal2 = ta.ema(macd2, signalLength)
macd3 = ta.ema(close, shortLength) - ta.ema(close, longLength)
signal3 = ta.ema(macd3, signalLength)

// Define entry and exit conditions
longCondition = macd1 &gt; signal1 and macd2 &gt; signal2 and macd3 &gt; signal3
shortCondition = macd1 &lt; signal1 and macd2 &lt; signal2 and macd3 &lt; signal3
exitCondition = crossover(macd1, signal1) or crossover(macd1, signal1) or crossover(macd2, signal2) or crossover(macd2, signal2) or crossover(macd3, signal3) or crossover(macd3, signal3)

// Enter long position
if longCondition
    strategy.entry(&quot;Long&quot;, strategy.long)

// Enter short position
if shortCondition
    strategy.entry(&quot;Short&quot;, strategy.short)

// Exit position
if exitCondition
    strategy.close()

I tried to google and see if i got the function name wrong but I didn't ANd plus I'm using v5 so this should work.

答案1

得分: 1

在版本5中,您应该将以下部分替换为:

crossover(x, y)

替换为:

ta.crossover(x, y)
英文:

In version 5 you should replace :<br>

crossover(x, y)

by : <br>

ta.crossover(x, y)

huangapple
  • 本文由 发表于 2023年1月9日 02:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050423.html
匿名

发表评论

匿名网友

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

确定