divinedestinyman

Buy and Sell signals from MACD and RSI script

PEPPERSTONE:US30   Dow Jones Industrial Average Index
This code includes various input parameters that users can configure, including RSI and MACD periods, RSI overbought and oversold levels, MACD timeframes and signal period, and stop loss and take profit levels.

By using the input() function, users can easily change the values of these parameters through the script's settings/inputs, without needing to modify the code itself


//@version=5
// Author: divinedestinyman
strategy("RSI-MACD Strategy with Multiple Timeframes", overlay=true)

// Define input parameters
rsi_timeframe = input(title="RSI Timeframe", type=input.resolution, defval="D")
rsi_period = input(title="RSI Period", type=input.integer, defval=14)
rsi_overbought = input(title="RSI Overbought Level", type=input.integer, defval=70)
rsi_oversold = input(title="RSI Oversold Level", type=input.integer, defval=30)
macd_timeframe = input(title="MACD Timeframe", type=input.resolution, defval="D")
macd_fast = input(title="MACD Fast Period", type=input.integer, defval=12)
macd_slow = input(title="MACD Slow Period", type=input.integer, defval=26)
macd_signal = input(title="MACD Signal Period", type=input.integer, defval=9)
stop_loss = input(title="Stop Loss", type=input.float, defval=0.02)
take_profit = input(title="Take Profit", type=input.float, defval=0.02)

// Calculate RSI and MACD values
rsi_value = request.security(syminfo.tickerid, rsi_timeframe, rsi(close, rsi_period))
= request.security(syminfo.tickerid, macd_timeframe, macd(close, macd_fast, macd_slow, macd_signal))

// Generate buy and sell signals
rsi_buy_signal = crossover(rsi_value, rsi_oversold)
rsi_sell_signal = crossunder(rsi_value, rsi_overbought)
macd_buy_signal = crossover(macd_line, macd_signal_line)
macd_sell_signal = crossunder(macd_line, macd_signal_line)

// Plot RSI and MACD lines
plot(rsi_value, color=color.yellow, title="RSI")
plot(macd_line, color=color.blue, title="MACD Line")
plot(macd_signal_line, color=color.red, title="MACD Signal Line")

// Execute long and short trades based on signals
if (rsi_buy_signal and macd_buy_signal)
strategy.entry("Buy", strategy.long)
if (rsi_sell_signal and macd_sell_signal)
strategy.entry("Sell", strategy.short)

// Close long and short trades based on signals
if (macd_sell_signal and strategy.position_size > 0)
strategy.close("Buy")
if (macd_buy_signal and strategy.position_size < 0)
strategy.close("Sell")

// Set stop loss and take profit levels for long and short positions
strategy.exit("Exit Long", "Buy", stop=close * (1 - stop_loss), limit=close * (1 + take_profit))
strategy.exit("Exit Short", "Sell", stop=close * (1 + stop_loss), limit=close * (1 - take_profit))
Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.