itruong
itruong

Reputation: 13

Pinescript - EMA cross & Stochastic cross

I am trying to write some code in Pinescript on when the 9 EMA crosses the 20 EMA to show the long strategy. Inversely, I'd like to show the short strategy when the 20 EMA crosses the 9 EMA. This seems to work fine for me.

Where I'm having trouble is show like a second long.strategy when the stochastic RSI is showing long, but I only want the statement to be true if the 9 EMA has crossed the 20 EMA.

Here's my code:

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

Where I have it bolded above is where I'm struggling. Please help!

Upvotes: 1

Views: 3485

Answers (1)

Andrey D
Andrey D

Reputation: 1714

May be this can help

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

Upvotes: 0

Related Questions