//version=5
strategy("50 EMA and 200 EMA Crossover Strategy", overlay=true)

// Inputs for EMAs
emaShortLength = input(50, title="Short EMA Length")
emaLongLength = input(200, title="Long EMA Length")

// Calculate EMAs
emaShort = ta.ema(close, emaShortLength)
emaLong = ta.ema(close, emaLongLength)

// Plot EMAs
plot(emaShort, color=color.blue, linewidth=2, title="50 EMA")
plot(emaLong, color=color.red, linewidth=2, title="200 EMA")

// Buy/Sell Signals
longCondition = ta.crossover(emaShort, emaLong) // 50 EMA crosses above 200 EMA
shortCondition = ta.crossunder(emaShort, emaLong) // 50 EMA crosses below 200 EMA

if (longCondition)
strategy.entry("Buy", strategy.long)

if (shortCondition)
strategy.close("Buy")
strategy.entry("Sell", strategy.short)

// Close short position on a new long signal
if (longCondition and strategy.position_size < 0)
strategy.close("Sell")
strategy.entry("Buy", strategy.long)

// Close long position on a new short signal
if (shortCondition and strategy.position_size > 0)
strategy.close("Buy")
strategy.entry("Sell", strategy.short)

// Optional: Risk management
riskPercentage = input(1, title="Risk % per Trade", minval=0.1, maxval=10)
capital = strategy.equity
risk = capital * (riskPercentage / 100)

strategy.risk.max_drawdown(value=risk, type=strategy.percent)

// Add visual signals
plotshape(series=longCondition, color=color.green, style=shape.labelup, location=location.belowbar, size=size.small, title="Buy Signal")
plotshape(series=shortCondition, color=color.red, style=shape.labeldown, location=location.abovebar, size=size.small, title="Sell Signal")
Fundamental AnalysisTechnical IndicatorsTrend Analysis

إخلاء المسؤولية