Advertisement
andypartridge47

Error

Oct 22nd, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. //@version=5
  2. strategy("Learning Scripts 2.4", overlay=true)
  3.  
  4. stopLossPercentage = input.float(3, "Stop Loss Percentage") * 0.001
  5.  
  6. takeProfitPercentage = input.float(3, "Take Profit Percentage") * 0.015
  7.  
  8.  
  9. ema12 = ta.ema(close, 12)
  10. ema21 = ta.ema(close, 21)
  11.  
  12. // 1. Create two persistent variables for the take profit and stop loss lines
  13.  
  14. // We use var so that they values stay persistent
  15. // If we didn't use var these values would be changed to na every new bar
  16. // When we use var we have to tell pinesript that this will be a line
  17. var line stopLossLine = na
  18. var line takeProfitLine = na
  19.  
  20. // 2. Add logic to manually reset the lines or extend the lines
  21.  
  22. if strategy.position_size == 0 // If we don't have a position we reset the lines to na
  23. stopLossLine := na
  24. takeProfitLine := na
  25. else
  26. stopLossLine.set_x2(bar_index) // If we do have a position we extend the lines to the next bar
  27. takeProfitLine.set_x2(bar_index)
  28.  
  29.  
  30. if ta.crossover(ema12, ema21) and strategy.position_size <= 0
  31. stopLoss = close * (1 - stopLossPercentage)
  32.  
  33. takeProfit = close * (1 + takeProfitPercentage)
  34.  
  35.  
  36. // 3. Draw the start of the lines when we enter a position
  37.  
  38. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  39. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  40.  
  41. // if ta.crossover(ema12, ema21) and strategy.position_size <= 0 // Change this line to
  42. if ta.crossover(ema12, ema21) and strategy.position_size == 0
  43.  
  44. strategy.entry("Long", strategy.long)
  45. strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
  46.  
  47. if ta.crossunder(ema12, ema21) and strategy.position_size >= 0
  48. stopLoss = close * (1 + stopLossPercentage)
  49.  
  50. takeProfit = close * (1 - takeProfitPercentage)
  51.  
  52.  
  53. // 3. Draw the start of the lines when we enter a position
  54.  
  55. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  56. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  57.  
  58. strategy.entry("Short", strategy.short)
  59. strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
  60.  
  61.  
  62. bandColor = ema12 > ema21 ? color.green : color.red
  63. plot(ema12, color=bandColor, linewidth=1)
  64. plot(ema21, color=bandColor, linewidth=3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement