Untitled
Guest 346 9th Sep, 2023
//@version=4
strategy(default_qty_value = 155, pyramiding = 4, title="MACD crossover while RSI Oversold/Overbought", overlay=true, shorttitle="MACD Cross + RSI Oversold Overbought", initial_capital = 1000)
//MACD Settings
fastMA = input(title="Fast moving average", defval = 12, minval = 7)
slowMA = input(title="Slow moving average", defval = 26, minval = 7)
signalLength = input(9, minval=1)
//RSI settings
RSIOverSold = input(20, minval=1)
RSIOverBought = input(73, minval=1)
RSIOverLong = input(10, minval=1)
src = close
len = input(14, minval=1, title="Length")
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
[currMacd,_,_] = macd(close[0], fastMA, slowMA, signalLength)
[prevMacd,_,_] = macd(close[1], fastMA, slowMA, signalLength)
signal = ema(currMacd, signalLength)
crossoverBear = cross(currMacd, signal) and currMacd < signal ? avg(currMacd, signal) : na
crossoverBull = cross(currMacd, signal) and currMacd > signal ? avg(currMacd, signal) : na
// Configure backtest start date with inputs
startDate = input(title="Start Date", defval=8, minval=1, maxval=31)
startMonth = input(title="Start Month", defval=3, minval=1, maxval=12)
startYear = input(title="Start Year", defval=2021, minval=1800, maxval=2100)
afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0))
sellRate = input(title="Strategy Sell Rate", defval=2, minval=1, maxval=4)
// Make an input to configure the maximum position size
maxPos = input(title="Maximum Position Size", type=input.integer, defval=15000)
// Look how much quantity we can still trade
posToTrade = maxPos - abs(strategy.position_size)
rsiCrossUnder = crossunder(rsi, RSIOverBought)
if (afterStartDate == true)
if (crossoverBull and crossoverBull[1] == false and rsi > RSIOverLong and (posToTrade > 0))
strategy.entry("Long", strategy.long)
label.new(x=bar_index, y=low - 0.2*tr, text="Buy", color=color.green, textcolor=color.black, style=label.style_labelup)
if (rsiCrossUnder)
strategy.entry("Short", strategy.short)
label.new(x=bar_index, y=high + 0.2*tr, text="Sell", color=color.red, textcolor=color.black, style=label.style_labeldown)
//plot(strategy.netprofit)
To share this paste please copy this url and send to your friends
RAW Paste Data