Advertisement
andypartridge47

Improved GG strat

Oct 25th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. //@version=5
  2. strategy("GG", overlay=true)
  3.  
  4. smalengt50 = 50
  5. emalengt12 = 12
  6. emalengt21 = 21
  7.  
  8. sma50 = ta.sma(close, 50)
  9. ema12 = ta.ema(close, 12)
  10. ema21 = ta.ema(close, 21)
  11.  
  12. // 1. Adding logic to manually reset or extend the lines based on if we are in a position or not.
  13.  
  14. longcondition = ta.crossover(ema12, ema21)
  15. shortcondition = ta.crossunder(ema12, ema21)
  16.  
  17. // Declare variables
  18. var line stopLossLine = na
  19. var line takeProfitLine = na
  20. var float stopLoss = na
  21. var float takeProfit = na
  22.  
  23. if strategy.position_size == 0
  24. stopLossLine := na
  25. takeProfitLine := na
  26. else
  27. stopLossLine.set_x2(bar_index)
  28. takeProfitLine.set_x2(bar_index)
  29.  
  30. // Check long condition and set stop loss and take profit
  31. if longcondition and strategy.position_size == 0
  32. stopLoss = close * (1 - 0.03)
  33. takeProfit = close * (1 + 0.03)
  34.  
  35. // Draw the start of the lines when we enter a position
  36. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  37. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  38.  
  39. // 2. Draw the start of the lines when we enter a position.
  40.  
  41. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  42. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green) //
  43.  
  44. strategy.entry("Long", strategy.long, stop=stopLoss, limit=takeProfit)
  45. strategy.exit("SL/TP", "Long", stop=stopLoss, limit=takeProfit)
  46.  
  47. if shortcondition and strategy.position_size == 0
  48. stopLoss = close * (1 + 0.03)
  49. takeProfit = close * (1 - 0.03)
  50.  
  51. // 3. Draw the start of the lines when we enter a position.
  52.  
  53. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  54. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  55.  
  56. strategy.entry("Short", strategy.short, stop=stopLoss, limit=takeProfit)
  57. strategy.exit("SL/TP", "Short", stop=stopLoss, limit=takeProfit)
  58.  
  59.  
  60. bandColor50 = sma50 < sma50[1] ? #ffa73c : color.rgb(33, 184, 243)
  61. bandColor = ema12 > ema21 ? color.green : color.red
  62. plot(ema12, color=bandColor)
  63. plot(ema21, color=bandColor)
  64. plot(sma50, color=bandColor50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement