OPEN-SOURCE SCRIPT

RSI + MACD Day Trading Toolkit

44
//version=6
indicator("RSI + MACD Day Trading Toolkit", overlay = true)

//──────────────────────────────────────────────────────────────────────────────
// 1. INPUTS
//──────────────────────────────────────────────────────────────────────────────

// RSI settings
rsiLength = input.int(14, "RSI Length")
rsiOverbought = input.float(70, "RSI Overbought Level", minval = 50, maxval = 100)
rsiOversold = input.float(30, "RSI Oversold Level", minval = 0, maxval = 50)

// MACD settings (classic 12 / 26 / 9)
macdFastLength = input.int(12, "MACD Fast Length")
macdSlowLength = input.int(26, "MACD Slow Length")
macdSignalLength = input.int(9, "MACD Signal Length")

// Risk model selection
riskModel = input.string("ATR", "Risk Model", options = ["ATR", "Percent"])

// ATR-based SL/TP
atrLength = input.int(14, "ATR Length")
atrSLMult = input.float(1.5, "SL ATR Multiplier", minval = 0.1, step = 0.1)
atrTPMult = input.float(2.5, "TP ATR Multiplier", minval = 0.1, step = 0.1)

// Percent-based SL/TP (for scalping on very tight spreads)
slPercent = input.float(0.5, "SL % (when Risk Model = Percent)", minval = 0.05, step = 0.05)
tpPercent = input.float(1.0, "TP % (when Risk Model = Percent)", minval = 0.05, step = 0.05)

// Visual / styling
showSLTPLines = input.bool(true, "Plot Stop Loss / Take Profit Lines")

//──────────────────────────────────────────────────────────────────────────────
// 2. CORE INDICATORS: RSI & MACD
//──────────────────────────────────────────────────────────────────────────────

rsiValue = ta.rsi(close, rsiLength)

// Manual MACD calculation (avoids tuple unpacking issues)
macdFastEMA = ta.ema(close, macdFastLength)
macdSlowEMA = ta.ema(close, macdSlowLength)
macdValue = macdFastEMA - macdSlowEMA
macdSignal = ta.ema(macdValue, macdSignalLength)
macdHist = macdValue - macdSignal

atrValue = ta.atr(atrLength)

// Hide internal plots from price scale (still accessible if you change display)
plot(rsiValue, "RSI", display = display.none)
plot(macdValue, "MACD", display = display.none)
plot(macdSignal, "MACD Sig", display = display.none)
plot(macdHist, "MACD Hist", display = display.none)

//──────────────────────────────────────────────────────────────────────────────
// 3. SIGNAL LOGIC (ENTRY CONDITIONS)
//──────────────────────────────────────────────────────────────────────────────
//
// Idea:
// - LONG bias: RSI emerges from oversold AND MACD crosses above signal below zero
// - SHORT bias: RSI falls from overbought AND MACD crosses below signal above zero
//
// Combines momentum (RSI) with trend confirmation (MACD).
//──────────────────────────────────────────────────────────────────────────────

// RSI events
rsiBullCross = ta.crossover(rsiValue, rsiOversold) // RSI crosses UP out of oversold
rsiBearCross = ta.crossunder(rsiValue, rsiOverbought) // RSI crosses DOWN from overbought

// MACD crossover with trend filter
macdBullCross = ta.crossover(macdValue, macdSignal) and macdValue < 0 // Bullish cross below zero-line
macdBearCross = ta.crossunder(macdValue, macdSignal) and macdValue > 0 // Bearish cross above zero-line

// Raw (ungated) entry signals
rawLongSignal = rsiBullCross and macdBullCross
rawShortSignal = rsiBearCross and macdBearCross

//──────────────────────────────────────────────────────────────────────────────
// 4. STATE MANAGEMENT (SIMULATED POSITION TRACKING)
//──────────────────────────────────────────────────────────────────────────────
//
// position: 1 = long
// -1 = short
// 0 = flat
//
// We track entry price and SL/TP levels as if this were a strategy.
// This is still an indicator – it just computes and plots the logic.
//──────────────────────────────────────────────────────────────────────────────

var int position = 0
var float longEntryPrice = na
var float shortEntryPrice = na
var float longSL = na
var float longTP = na
var float shortSL = na
var float shortTP = na

// Per-bar flags (for plotting / alerts)
var bool longEntrySignal = false
var bool shortEntrySignal = false
var bool longExitSignal = false
var bool shortExitSignal = false

// Reset per-bar flags each bar
longEntrySignal := false
shortEntrySignal := false
longExitSignal := false
shortExitSignal := false

//──────────────────────────────────────────────────────────────────────────────
// 5. EXIT LOGIC (STOP LOSS / TAKE PROFIT / OPPOSITE SIGNAL)
//──────────────────────────────────────────────────────────────────────────────
//
// Exits are evaluated BEFORE new entries on each bar.
//──────────────────────────────────────────────────────────────────────────────

// Stop-loss / take-profit hits for existing positions
longStopHit = position == 1 and not na(longSL) and low <= longSL
longTakeHit = position == 1 and not na(longTP) and high >= longTP

shortStopHit = position == -1 and not na(shortSL) and high >= shortSL
shortTakeHit = position == -1 and not na(shortTP) and low <= shortTP

// Opposite signals can also close positions
reverseToShort = position == 1 and rawShortSignal
reverseToLong = position == -1 and rawLongSignal

// Combine exit conditions
longExitNow = longStopHit or longTakeHit or reverseToShort
shortExitNow = shortStopHit or shortTakeHit or reverseToLong

// Register exits and flatten position
if longExitNow and position == 1
longExitSignal := true
position := 0
longEntryPrice := na
longSL := na
longTP := na

if shortExitNow and position == -1
shortExitSignal := true
position := 0
shortEntryPrice := na
shortSL := na
shortTP := na

//──────────────────────────────────────────────────────────────────────────────
// 6. ENTRY LOGIC WITH RISK MODEL (SL/TP CALCULATION)
//──────────────────────────────────────────────────────────────────────────────
//
// Only take a new trade when flat.
// SL/TP are calculated relative to entry price using either ATR or Percent.
//──────────────────────────────────────────────────────────────────────────────

if position == 0
// Long entry
if rawLongSignal
position := 1
longEntryPrice := close

if riskModel == "ATR"
longSL := longEntryPrice - atrValue * atrSLMult
longTP := longEntryPrice + atrValue * atrTPMult
else // Percent model
longSL := longEntryPrice * (1.0 - slPercent / 100.0)
longTP := longEntryPrice * (1.0 + tpPercent / 100.0)

longEntrySignal := true

// Short entry
else if rawShortSignal
position := -1
shortEntryPrice := close

if riskModel == "ATR"
shortSL := shortEntryPrice + atrValue * atrSLMult
shortTP := shortEntryPrice - atrValue * atrTPMult
else // Percent model
shortSL := shortEntryPrice * (1.0 + slPercent / 100.0)
shortTP := shortEntryPrice * (1.0 - tpPercent / 100.0)

shortEntrySignal := true

//──────────────────────────────────────────────────────────────────────────────
// 7. PLOTTING: ENTRIES, EXITS, STOPS & TARGETS
//──────────────────────────────────────────────────────────────────────────────

// Entry markers
plotshape(longEntrySignal, title = "Long Entry", style = shape.triangleup, location = location.belowbar, color = color.new(color.lime, 0), size = size.small, text = "LONG")
plotshape(shortEntrySignal, title = "Short Entry", style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.small, text = "SHORT")

// Exit markers (generic exits: SL, TP or reversal)
plotshape(longExitSignal, title = "Long Exit", style = shape.xcross, location = location.abovebar, color = color.new(color.orange, 0), size = size.tiny, text = "LX")
plotshape(shortExitSignal, title = "Short Exit", style = shape.xcross, location = location.belowbar, color = color.new(color.orange, 0), size = size.tiny, text = "SX")

// Optional: show SL/TP levels on chart while in position
plot(showSLTPLines and position == 1 ? longSL : na, title = "Long Stop Loss", style = plot.style_linebr, color = color.new(color.red, 0), linewidth = 1)
plot(showSLTPLines and position == 1 ? longTP : na, title = "Long Take Profit", style = plot.style_linebr, color = color.new(color.lime, 0), linewidth = 1)

plot(showSLTPLines and position == -1 ? shortSL : na, title = "Short Stop Loss", style = plot.style_linebr, color = color.new(color.red, 0), linewidth = 1)
plot(showSLTPLines and position == -1 ? shortTP : na, title = "Short Take Profit", style = plot.style_linebr, color = color.new(color.lime, 0), linewidth = 1)

//──────────────────────────────────────────────────────────────────────────────
// 8. ALERT CONDITIONS
//──────────────────────────────────────────────────────────────────────────────
//
// Configure TradingView alerts using these conditions.
//──────────────────────────────────────────────────────────────────────────────

// Entry alerts
alertcondition(longEntrySignal, title = "Long Entry (RSI+MACD)", message = "RSI+MACD: Long entry signal")
alertcondition(shortEntrySignal, title = "Short Entry (RSI+MACD)", message = "RSI+MACD: Short entry signal")

// Exit alerts (by type: SL vs TP vs reversal)
alertcondition(longStopHit, title = "Long Stop Loss Hit", message = "RSI+MACD: Long STOP LOSS hit")
alertcondition(longTakeHit, title = "Long Take Profit Hit", message = "RSI+MACD: Long TAKE PROFIT hit")
alertcondition(shortStopHit, title = "Short Stop Loss Hit", message = "RSI+MACD: Short STOP LOSS hit")
alertcondition(shortTakeHit, title = "Short Take Profit Hit", message = "RSI+MACD: Short TAKE PROFIT hit")
alertcondition(reverseToShort, title = "Long Exit by Reverse Signal", message = "RSI+MACD: Long exit by SHORT reverse signal")
alertcondition(reverseToLong, title = "Short Exit by Reverse Signal", message = "RSI+MACD: Short exit by LONG reverse signal")

//──────────────────────────────────────────────────────────────────────────────
// 9. QUICK USAGE NOTES
//──────────────────────────────────────────────────────────────────────────────
//
// - Indicador, não estratégia: ele simula posição, SL/TP e sinais de saída.
// - Para backtest/auto, basta portar a mesma lógica para um script `strategy()`
// usando `strategy.entry` e `strategy.exit`.
// - Em day trade, teste ATR vs Percent e ajuste os multiplicadores ao ativo.
//──────────────────────────────────────────────────────────────────────────────

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, alım satım veya diğer türden tavsiye veya öneriler anlamına gelmez ve teşkil etmez. Kullanım Koşulları bölümünde daha fazlasını okuyun.