英文:
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'
// © KabeerMehdi
//@version=5
//MACD auto-trading strategy
strategy("MACD", 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 > signal1 and macd2 > signal2 and macd3 > signal3
shortCondition = macd1 < signal1 and macd2 < signal2 and macd3 < 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("Long", strategy.long)
// Enter short position
if shortCondition
strategy.entry("Short", 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论