OPEN-SOURCE SCRIPT

Momentum, RSI, and MACD Strategy with Stop Loss

//version=5
strategy("Momentum, RSI, and MACD Strategy with Stop Loss", overlay=true, calc_on_every_tick=true)

// Input for Momentum
length = input(12, title="Momentum Length")
price = close

// Input for RSI
rsiLength = input(14, title="RSI Length")
rsiSource = close
rsiValue = ta.rsi(rsiSource, rsiLength)

// Input for MACD
fast_length = input(12, title="Fast Length")
slow_length = input(26, title="Slow Length")
src = close
signal_length = input.int(9, title="Signal Smoothing")
sma_source = input.string("EMA", title="Oscillator MA Type", options=["SMA", "EMA"])
sma_signal = input.string("EMA", title="Signal Line MA Type", options=["SMA", "EMA"])

// ATR Inputs and Calculation
atrLength = input.int(title="ATR Length", defval=14, minval=1)
smoothing = input.string(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
switch smoothing
"RMA" => ta.rma(source, length)
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
=> ta.wma(source, length)
atrValue = ma_function(ta.tr(true), atrLength)

// Calculating Momentum
momentum(seria, length) =>
mom = seria - seria[length]
mom
mom0 = momentum(price, length)
mom1 = momentum(mom0, 1)

// Calculating MACD
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// Long Entry Condition
longCondition = mom0 > 0 and mom1 > 0 and rsiValue > 50 and macd > 0
if (longCondition)
strategy.entry("MomRSIMACD Long", strategy.long, stop=high+syminfo.mintick, comment="MomRSIMACD Long")
strategy.exit("Exit Long", from_entry="MomRSIMACD Long", stop=price-15, limit=price+15)


// Short Entry Condition
shortCondition = mom0 < 0 and mom1 < 0 and rsiValue < 50 and macd < 0
if (shortCondition)
strategy.entry("MomRSIMACD Short", strategy.short, stop=low-syminfo.mintick, comment="MomRSIMACD Short")
strategy.exit("Exit Short", from_entry="MomRSIMACD Short", stop=price+15, limit=price-15)

Wyłączenie odpowiedzialności