Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("EMA Crossover with Dynamic Stop Loss 1:2", overlay=true, default_qty_type=strategy.cash, default_qty_value=3600)
- // EMA Parameters
- fastEMA1 = ta.ema(close, 5)
- fastEMA2 = ta.ema(close, 13)
- fastEMA3 = ta.ema(close, 21)
- slowEMA = ta.ema(close, 200)
- // Plot EMAs on the chart
- plot(fastEMA1, color=color.green, title="EMA 5")
- plot(fastEMA2, color=color.orange, title="EMA 13")
- plot(fastEMA3, color=color.blue, title="EMA 21")
- plot(slowEMA, color=color.red, title="EMA 200")
- // Detect crossover of all fast EMAs with the slow EMA within the last 10 candles
- bullishCrossover = ta.barssince(ta.crossover(fastEMA1, slowEMA)) <= 10 and ta.barssince(ta.crossover(fastEMA2, slowEMA)) <= 10 and ta.barssince(ta.crossover(fastEMA3, slowEMA)) <= 10
- bearishCrossover = ta.barssince(ta.crossunder(fastEMA1, slowEMA)) <= 10 and ta.barssince(ta.crossunder(fastEMA2, slowEMA)) <= 10 and ta.barssince(ta.crossunder(fastEMA3, slowEMA)) <= 10
- // Position sizing and risk management
- capitalPerTrade = 60
- leverage = 30
- positionSize = capitalPerTrade * leverage
- var float maxLoss = 30 // Maximum loss in dollars
- var float riskRewardRatio = 3 // Risk-reward ratio (3:1)
- // Calculate stop loss and take profit percentages
- var float stopLossPercent = maxLoss / positionSize
- var float takeProfitPercent = riskRewardRatio * stopLossPercent
- // Track trade status
- var float activeStopLoss = na
- var float activeTakeProfit = na
- var float entryPrice = na
- // Time settings (New York timezone)
- newYorkTime = timestamp("America/New_York", year, month, dayofmonth, hour, minute)
- // Backtesting date range (last 6 months)
- fromDate = timestamp("America/New_York", 2024, 2, 28, 0, 0)
- toDate = timestamp("America/New_York", 2025, 3, 5, 0, 0)
- isInDateRange = (time >= fromDate) and (time <= toDate)
- // Restrict trading during weekends and outside market hours
- isWeekday = dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday
- // Detect New York market hours (winter/summer time)
- utcHour = hour(time)
- isMarketOpen = (utcHour >= 14 and utcHour < 22) or (utcHour >= 13 and utcHour < 22)
- var int tradeHour = na
- // Prevent consecutive rapid trades
- lastLongEntry = ta.barssince(strategy.position_size > 0)
- lastShortEntry = ta.barssince(strategy.position_size < 0)
- canTrade = lastLongEntry > 10 and lastShortEntry > 10
- // Execute trades only during valid date range, market hours, and weekdays
- buy_entry = false
- sell_entry = false
- if bullishCrossover and isInDateRange and isWeekday and isMarketOpen
- strategy.entry("Buy", strategy.long)
- entryPrice := close
- activeStopLoss := entryPrice * (1 - stopLossPercent)
- activeTakeProfit := entryPrice * (1 + takeProfitPercent)
- buy_entry := true
- if bearishCrossover and isInDateRange and isWeekday and isMarketOpen
- strategy.entry("Sell", strategy.short)
- entryPrice := close
- activeTakeProfit := entryPrice * (1 - takeProfitPercent)
- activeStopLoss := entryPrice * (1 + stopLossPercent)
- sell_entry := true
- // Adjust stop loss when reaching 1:1 risk-reward ratio
- bool buy_exit = na
- bool sell_exit = na
- if strategy.position_size > 0
- if close >= entryPrice * (1 + stopLossPercent * 2)
- activeStopLoss := entryPrice * (1 + stopLossPercent)
- if close >= entryPrice * (1 + stopLossPercent)
- activeStopLoss := entryPrice
- strategy.exit("TP/SL", "Buy", stop=activeStopLoss, limit=activeTakeProfit)
- buy_exit := true
- if strategy.position_size < 0
- if close <= entryPrice * (1 - stopLossPercent * 3)
- activeStopLoss := entryPrice * (1 - stopLossPercent * 2)
- if close <= entryPrice * (1 - stopLossPercent * 3.5)
- activeStopLoss := entryPrice * (1 - stopLossPercent * 3)
- strategy.exit("TP/SL", "Sell", stop=activeStopLoss, limit=activeTakeProfit)
- sell_exit := true
- plotshape(buy_entry, "Buy Entry", color=color.green, style=shape.circle, size=size.tiny, location=location.belowbar)
- plotshape(sell_entry, "Sell Entry", color=color.purple, style=shape.circle, size=size.tiny, location=location.abovebar)
- plotshape(buy_exit, "Buy Exit", color=color.red, style=shape.circle, size=size.tiny, location=location.abovebar)
- plotshape(sell_entry, "Sell Exit", color=color.lime, style=shape.circle, size=size.tiny, location=location.belowbar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement