TradingView Pine Script多重交叉策略

时间:2019-09-28 13:51:13

标签: algorithmic-trading trading pine-script crossover

我急切地希望在交易视图中编写策略时寻求帮助,在该视图中,我试图创建3种不同移动平均线(尤其是9 ema,21 ema和33简单移动平均线)的交叉买卖信号。我想做的是,当9个ema超过21个ema时,存在一个较长的条件,而当21个ema超过33个简单移动平均线时,也具有相同的长条件。我希望这对多头和空头都适用-将9和21用作“小信号”,而将21和33用作“大信号”,我想不通...这是我最近的有以下内容,它将无法正常工作:/请帮助,我将为您的下6件装venmo / cash应用程序!

//@version=3
//study(title="MA Crossover Strategy", overlay = true)
strategy("EMA Crossover Strategy", overlay=true)
src = input(close, title="Source")

price = security(tickerid, period, src)
ema1 = input(9, title="1st EMA Length")
type1 = input("EMA", "1st EMA Type", options=["SMA", "EMA"])

ema2 = input(21, title="2nd EMA Length")
type2 = input("EMA", "2nd EMA Type", options=["SMA", "EMA"])

sma3 = input(33, title="1st MA Length")
type3 = input("SMA", "2nd SMA type", options=["SMA", "EMA"])

price1 = if (type1 == "EMA")
ema(price, ema1)
else
sma(price, ema1)

price2 = if (type2 == "EMA")
sma(price, ema2)
else
ema(price, ema2)

price3 = if (type3 == "SMA")
sma(price, sma3)
else
ema(price, sma3)

//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line,  title="1st EMA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd EMA", color=yellow, linewidth=2, transp=0)
plot(series=price3, style=line, title="1st MA", color=orange, linewidth=2, transp=0)

longCondition = crossover(price1, price2) and crossover(price2, price3)
if (longCondition)
strategy.entry("Long", strategy.long)

shortCondition = crossunder(price1, price2) and crossover(price2, price3)
if (shortCondition)
strategy.entry("Short", strategy.short)

信号不会显示,但是如果您删除两个条件的最后一个“和交叉”部分,它将仅适用于9 ema和21 ema,但是我想合并21 ema和33简单的十字

2 个答案:

答案 0 :(得分:0)

请下次复制带有空格/制表符的代码,因此不需要重新格式化。另外,请勿将[tradingiew-api]标签用于与Pine相关的问题,如标签说明中所述。

您在自己的条件中使用and,这意味着两个交叉必须在同一条上发生才能使条件成立。还要反转第二次MA的ema / sma计算。

始终最好在您的条件上打印标记,以确保它们在您期望的时候也会出现。

//@version=3
//study(title="MA Crossover Strategy", overlay = true)
strategy("EMA Crossover Strategy", overlay=true)
src = input(close, title="Source")

price = security(tickerid, period, src)
ema1 = input(9, title="1st EMA Length")
type1 = input("EMA", "1st EMA Type", options=["SMA", "EMA"])

ema2 = input(21, title="2nd EMA Length")
type2 = input("EMA", "2nd EMA Type", options=["SMA", "EMA"])

sma3 = input(33, title="3rd MA Length")
type3 = input("SMA", "3rd SMA type", options=["SMA", "EMA"])

price1 = if (type1 == "EMA")
    ema(price, ema1)
else
    sma(price, ema1)

price2 = if (type2 == "EMA")
    ema(price, ema2)
else
    sma(price, ema2)

price3 = if (type3 == "SMA")
    sma(price, sma3)
else
    ema(price, sma3)

//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line,  title="1st EMA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd EMA", color=yellow, linewidth=2, transp=0)
plot(series=price3, style=line, title="1st MA", color=orange, linewidth=2, transp=0)

longCondition = crossover(price1, price2) or crossover(price2, price3)
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = crossunder(price1, price2) or crossover(price2, price3)
if (shortCondition)
    strategy.entry("Short", strategy.short)

plotchar(shortCondition, "shortCondition", "▼", location.abovebar, maroon, size = size.tiny)
plotchar(longCondition, "longCondition", "▲", location.belowbar, lime, size = size.tiny)

enter image description here

答案 1 :(得分:0)