Guest User

Untitled

a guest
Jan 21st, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. //@version=5
  2. strategy("SMA 10/50 5L 5P 5D Test", overlay=true)
  3.  
  4. // Define the SMAs
  5. sma10 = ta.sma(close, 10)
  6. sma50 = ta.sma(close, 50)
  7.  
  8. // Entry condition: 10-day SMA crosses above 50-day SMA
  9. longCondition = ta.crossover(sma10, sma50)
  10.  
  11. // Define stop loss and take profit percentages
  12. stopLossPercent = 5
  13. takeProfitPercent = 5
  14.  
  15. // Variable to track bars since entry
  16. var int entryBarIndex = na // Bar index at the time of entry
  17.  
  18. // Entry logic
  19. if (longCondition)
  20.     strategy.entry("Long", strategy.long)
  21.     entryBarIndex := bar_index // Record the bar index at entry
  22.  
  23. // Calculate bars since entry
  24. barsSinceEntry = na(entryBarIndex) ? na : bar_index - entryBarIndex
  25.  
  26. // Stop loss and take profit levels
  27. stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100)
  28. takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100)
  29.  
  30. // Exit conditions
  31. exitCondition1 = close < stopLossLevel
  32. exitCondition2 = close > takeProfitLevel
  33. exitCondition3 = not na(barsSinceEntry) and barsSinceEntry > 4 // Exit after 5 bars from entry
  34.  
  35. // Close position if any exit condition is met
  36. if exitCondition1 or exitCondition2 or exitCondition3
  37.     strategy.close("Long")
Add Comment
Please, Sign In to add comment