OPEN-SOURCE SCRIPT

Smart Trading Signals with TP/SL

//version=5
indicator("Smart Trading Signals with TP/SL", shorttitle="STS-TP/SL", overlay=true, max_labels_count=500)

// —————— Input Parameters ——————
// Signal Configuration
signalType = input.string("EMA Crossover", "Signal Type", options=["EMA Crossover", "RSI Divergence"])
emaFastLen = input.int(9, "Fast EMA Length", minval=1)
emaSlowLen = input.int(21, "Slow EMA Length", minval=1)
rsiLength = input.int(14, "RSI Length", minval=1)
rsiOverbought = input.int(70, "RSI Overbought", maxval=100)
rsiOversold = input.int(30, "RSI Oversold", minval=0)

// Risk Management Inputs
useFixedTP = input.bool(true, "Use Fixed TP (%)", tooltip="TP as percentage from entry")
tpPercentage = input.float(2.0, "Take Profit %", step=0.1)
tpPoints = input.float(100, "Take Profit Points", step=1.0)

useFixedSL = input.bool(true, "Use Fixed SL (%)", tooltip="SL as percentage from entry")
slPercentage = input.float(1.0, "Stop Loss %", step=0.1)
slPoints = input.float(50, "Stop Loss Points", step=1.0)

// —————— Calculate Indicators ——————
// EMAs for Trend
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
emaBullish = ta.crossover(emaFast, emaSlow)
emaBearish = ta.crossunder(emaFast, emaSlow)

// RSI for Momentum
rsi = ta.rsi(close, rsiLength)
rsiBullish = ta.crossover(rsi, rsiOversold)
rsiBearish = ta.crossunder(rsi, rsiOverbought)

// —————— Generate Signals ——————
buyCondition = (signalType == "EMA Crossover" and emaBullish) or (signalType == "RSI Divergence" and rsiBullish)
sellCondition = (signalType == "EMA Crossover" and emaBearish) or (signalType == "RSI Divergence" and rsiBearish)

// —————— Calculate TP/SL Levels ——————
var float entryPrice = na
var bool inTrade = false

// Calculate TP/SL based on user input
takeProfitPrice = useFixedTP ? entryPrice * (1 + tpPercentage / 100) : entryPrice + tpPoints
stopLossPrice = useFixedSL ? entryPrice * (1 - slPercentage / 100) : entryPrice - slPoints

// —————— Plot Signals and Levels ——————
if buyCondition and not inTrade
entryPrice := close
inTrade := true
label.new(bar_index, low, "BUY", style=label.style_label_up, color=color.green, textcolor=color.white)
line.new(bar_index, entryPrice, bar_index + 1, entryPrice, color=color.green, width=2)
line.new(bar_index, takeProfitPrice, bar_index + 1, takeProfitPrice, color=color.teal, width=2, style=line.style_dashed)
line.new(bar_index, stopLossPrice, bar_index + 1, stopLossPrice, color=color.red, width=2, style=line.style_dashed)

if sellCondition and inTrade
entryPrice := close
inTrade := false
label.new(bar_index, high, "SELL", style=label.style_label_down, color=color.red, textcolor=color.white)
line.new(bar_index, entryPrice, bar_index + 1, entryPrice, color=color.red, width=2)
line.new(bar_index, takeProfitPrice, bar_index + 1, takeProfitPrice, color=color.teal, width=2, style=line.style_dashed)
line.new(bar_index, stopLossPrice, bar_index + 1, stopLossPrice, color=color.red, width=2, style=line.style_dashed)

// —————— Alerts ——————
alertcondition(buyCondition, title="Buy Signal Alert", message="BUY Signal Triggered")
alertcondition(sellCondition, title="Sell Signal Alert", message="SELL Signal Triggered")

// —————— Plot EMAs and RSI ——————
plot(emaFast, "Fast EMA", color.new(color.blue, 0))
plot(emaSlow, "Slow EMA", color.new(color.orange, 0))

Feragatname