Advertisement
godisrob

Untitled

Mar 8th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. //@version=5
  2.  
  3. strategy("EMA Crossover with Dynamic Stop Loss 1:2", overlay=true, default_qty_type=strategy.cash, default_qty_value=3600)
  4.  
  5. // EMA Parameters
  6.  
  7. fastEMA1 = ta.ema(close, 5)
  8.  
  9. fastEMA2 = ta.ema(close, 13)
  10.  
  11. fastEMA3 = ta.ema(close, 21)
  12.  
  13. slowEMA = ta.ema(close, 200)
  14.  
  15. // Plot EMAs on the chart
  16.  
  17. plot(fastEMA1, color=color.green, title="EMA 5")
  18.  
  19. plot(fastEMA2, color=color.orange, title="EMA 13")
  20.  
  21. plot(fastEMA3, color=color.blue, title="EMA 21")
  22.  
  23. plot(slowEMA, color=color.red, title="EMA 200")
  24.  
  25. // Detect crossover of all fast EMAs with the slow EMA within the last 10 candles
  26.  
  27. bullishCrossover = ta.barssince(ta.crossover(fastEMA1, slowEMA)) <= 10 and ta.barssince(ta.crossover(fastEMA2, slowEMA)) <= 10 and ta.barssince(ta.crossover(fastEMA3, slowEMA)) <= 10
  28.  
  29. bearishCrossover = ta.barssince(ta.crossunder(fastEMA1, slowEMA)) <= 10 and ta.barssince(ta.crossunder(fastEMA2, slowEMA)) <= 10 and ta.barssince(ta.crossunder(fastEMA3, slowEMA)) <= 10
  30.  
  31. // Position sizing and risk management
  32.  
  33. capitalPerTrade = 60
  34.  
  35. leverage = 30
  36.  
  37. positionSize = capitalPerTrade * leverage
  38.  
  39. var float maxLoss = 30 // Maximum loss in dollars
  40.  
  41. var float riskRewardRatio = 3 // Risk-reward ratio (3:1)
  42.  
  43. // Calculate stop loss and take profit percentages
  44.  
  45. var float stopLossPercent = maxLoss / positionSize
  46.  
  47. var float takeProfitPercent = riskRewardRatio * stopLossPercent
  48.  
  49. // Track trade status
  50.  
  51. var float activeStopLoss = na
  52.  
  53. var float activeTakeProfit = na
  54.  
  55. var float entryPrice = na
  56.  
  57. // Time settings (New York timezone)
  58.  
  59. newYorkTime = timestamp("America/New_York", year, month, dayofmonth, hour, minute)
  60.  
  61. // Backtesting date range (last 6 months)
  62.  
  63. fromDate = timestamp("America/New_York", 2024, 2, 28, 0, 0)
  64.  
  65. toDate = timestamp("America/New_York", 2025, 3, 5, 0, 0)
  66.  
  67. isInDateRange = (time >= fromDate) and (time <= toDate)
  68.  
  69. // Restrict trading during weekends and outside market hours
  70.  
  71. isWeekday = dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday
  72.  
  73. // Detect New York market hours (winter/summer time)
  74.  
  75. utcHour = hour(time)
  76.  
  77. isMarketOpen = (utcHour >= 14 and utcHour < 22) or (utcHour >= 13 and utcHour < 22)
  78.  
  79. var int tradeHour = na
  80.  
  81. // Prevent consecutive rapid trades
  82.  
  83. lastLongEntry = ta.barssince(strategy.position_size > 0)
  84.  
  85. lastShortEntry = ta.barssince(strategy.position_size < 0)
  86.  
  87. canTrade = lastLongEntry > 10 and lastShortEntry > 10
  88.  
  89. // Execute trades only during valid date range, market hours, and weekdays
  90.  
  91. buy_entry = false
  92. sell_entry = false
  93.  
  94. if bullishCrossover and isInDateRange and isWeekday and isMarketOpen
  95. strategy.entry("Buy", strategy.long)
  96. entryPrice := close
  97. activeStopLoss := entryPrice * (1 - stopLossPercent)
  98. activeTakeProfit := entryPrice * (1 + takeProfitPercent)
  99. buy_entry := true
  100.  
  101. if bearishCrossover and isInDateRange and isWeekday and isMarketOpen
  102. strategy.entry("Sell", strategy.short)
  103. entryPrice := close
  104. activeTakeProfit := entryPrice * (1 - takeProfitPercent)
  105. activeStopLoss := entryPrice * (1 + stopLossPercent)
  106. sell_entry := true
  107.  
  108. // Adjust stop loss when reaching 1:1 risk-reward ratio
  109.  
  110. bool buy_exit = na
  111. bool sell_exit = na
  112. if strategy.position_size > 0
  113. if close >= entryPrice * (1 + stopLossPercent * 2)
  114. activeStopLoss := entryPrice * (1 + stopLossPercent)
  115. if close >= entryPrice * (1 + stopLossPercent)
  116. activeStopLoss := entryPrice
  117. strategy.exit("TP/SL", "Buy", stop=activeStopLoss, limit=activeTakeProfit)
  118. buy_exit := true
  119.  
  120. if strategy.position_size < 0
  121. if close <= entryPrice * (1 - stopLossPercent * 3)
  122. activeStopLoss := entryPrice * (1 - stopLossPercent * 2)
  123. if close <= entryPrice * (1 - stopLossPercent * 3.5)
  124. activeStopLoss := entryPrice * (1 - stopLossPercent * 3)
  125. strategy.exit("TP/SL", "Sell", stop=activeStopLoss, limit=activeTakeProfit)
  126. sell_exit := true
  127.  
  128.  
  129. plotshape(buy_entry, "Buy Entry", color=color.green, style=shape.circle, size=size.tiny, location=location.belowbar)
  130. plotshape(sell_entry, "Sell Entry", color=color.purple, style=shape.circle, size=size.tiny, location=location.abovebar)
  131. plotshape(buy_exit, "Buy Exit", color=color.red, style=shape.circle, size=size.tiny, location=location.abovebar)
  132. 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