I have a question for the pine script.
When the rsi is 30 If the ta.crossover (ema20, ema50) appears between 1 and 3 candle closures, enter the Long strategy,
When rsi is 70 Is it possible to enter the short strategy if the ta.crossunder (ema20, ema50) comes out between 1 and 3 candle close?
//@version=5
strategy("new_strategy", overlay=true, margin_long=100, margin_short=100)
// ema200
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
emalong = ta.crossover(ema20,ema50)
emashort = ta.crossunder(ema20,ema50)
//rsi
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"
rsilong = ta.crossover(rsi,30)
rsishort = ta.crossunder(rsi,70)
longCondition =
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition =
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
Sure you can do that. The ema cross is your trigger condition. Therefore, when that happens, you need to use the ta.barssince()
function to see when RSI > 70
or RSI < 30
happened.
I set the number of bars since the RSI < 30
to 10 just to show you an example.
//@version=5
indicator("My script", overlay=true)
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
rsi_val = ta.rsi(close, 14)
plot(ema20, color=color.green)
plot(ema50, color=color.red)
plotchar(rsi_val, "RSI", "")
ema_long_cross = ta.crossover(ema20, ema50)
rsi_long_condition = rsi_val < 30
rsi_long_barssince = ta.barssince(rsi_long_condition)
buy_condition = ema_long_cross and (rsi_long_barssince > 0) and (rsi_long_barssince <= 10)
plotshape(ema_long_cross)
plotshape(buy_condition, "Buy", shape.labelup, location.belowbar, color.green, 0, "Buy", color.white)