Pinescript - EMA 交叉和随机交叉

Pinescript - EMA cross & Stochastic cross

我正在尝试在 Pinescript 中编写一些代码,说明 9 EMA 何时穿过 20 EMA 以显示多头策略。相反,当 20 EMA 穿过 9 EMA 时,我想展示做空策略。这对我来说似乎很好用。

当随机 RSI 显示多头时,我遇到麻烦的地方是 long.strategy,但我只希望当 9 EMA 超过 20 EMA 时该陈述为真。

这是我的代码:

    //@version=2
strategy("Isaac Signals2", overlay=true)

//ema
ema3 = ema(close,9)
ema4 = ema(close,20)
long_ema = crossover(ema3,ema4)
short_ema = crossover(ema4,ema3)

//stochrsi
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
src = input(close, title="RSI Source")

rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)

data = (60-k[1])/2
data2 = (k[1]-40)/2

long_stoch = k[1] >= d[1] and k[2] <= d[2] and k <= 60 and k >= 10
short_stoch = k[1] <= d[1] and k[2] >= d[2] and k >= 40 and k <= 95

//entries
if (long_ema) 
    strategy.entry("buy", strategy.long)
**if (long_ema and long_stoch)
    strategy.entry("buy+1", strategy.long)**
if (short_ema) 
    strategy.entry("sell", strategy.short)

我在上面加粗的地方是我挣扎的地方。请帮忙!

这可能对您有所帮助

//@version=2
strategy("Isaac Signals2", overlay=true)

//ema
ema3 = ema(close,9)
ema4 = ema(close,20)
long_ema = crossover(ema3,ema4)
short_ema = crossover(ema4,ema3)

//stochrsi
smoothK = input(3, minval=1)
smoothD = input(3, minval=1)
lengthRSI = input(14, minval=1)
lengthStoch = input(14, minval=1)
src = input(close, title="RSI Source")

rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = sma(k, smoothD)

data = (60-k[1])/2
data2 = (k[1]-40)/2

long_stoch = k[1] >= d[1] and k[2] <= d[2] and k <= 60 and k >= 10
short_stoch = k[1] <= d[1] and k[2] >= d[2] and k >= 40 and k <= 95

//entries
if (long_ema) 
    strategy.entry("buy", strategy.long)
if (ema3 > ema4 and long_stoch) // ema3 > ema4 means that crossover was already and uptrend is continuing 
    strategy.entry("buy+1", strategy.long)
if (short_ema) 
    strategy.entry("sell", strategy.short)