Advertisement
Guest User

Untitled

a guest
Jan 21st, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 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. // Entry logic
  16. if (longCondition)
  17. strategy.entry("Long", strategy.long)
  18.  
  19. // Retrieve the bar index of the first open trade
  20. entryBarIndex = strategy.opentrades.entry_bar_index(strategy.opentrades - 1)
  21.  
  22. // Calculate bars since entry
  23. barsSinceEntry = na(entryBarIndex) ? na : bar_index - entryBarIndex
  24.  
  25. // Stop loss and take profit levels
  26. stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100)
  27. takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100)
  28.  
  29. // Exit conditions
  30. exitCondition1 = close < stopLossLevel
  31. exitCondition2 = close > takeProfitLevel
  32. exitCondition3 = not na(barsSinceEntry) and barsSinceEntry > 4 // Exit after 5 bars from entry
  33.  
  34. // Close position if any exit condition is met
  35. if (exitCondition1 or exitCondition2 or exitCondition3)
  36. strategy.close("Long")
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement