0

我正在拼命寻求帮助以在交易视图中编写策略,我试图在 3 个不同的 MA 上创建交叉买入和卖出信号,特别是 9 EMA、21 EMA 和 33 简单移动平均线。我想要做的是当 9 EMA 超过 21 EMA 时出现长期条件,并且当 21 EMA 超过 33 简单移动平均线时也会出现相同的长期条件。我希望这适用于多头和空头 - 使用 9 和 21 作为“小信号”,而使用 21 和 33 作为“大信号”,我无法弄清楚......这是我最接近的有以下,它不会正常工作

//@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 简单交叉。

这些是我试图为策略复制的信号,代码用于创建指标,但我很难将其转换为策略。您会看到当 9ema 穿过 21ema 时给出初始信号,而当 21ema 穿过 33 简单 ma 时给出更大的信号......

图片

4

1 回答 1

0

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

and在您的条件下使用,这意味着两个交叉都需要出现在同一个柱上才能使条件为真。还将您的 ema/sma 计算倒转为第二个 MA。

始终最好在您的条件上打印一个标记,以确保它们也在您期望的时候发生。

//@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)

在此处输入图像描述

于 2019-09-29T04:00:42.540 回答