Advertisement
andypartridge47

GG 101

Nov 8th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. //@version=5
  2. strategy("GG 101", 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. // Declare stopLoss and takeProfit variables
  13. var float stopLoss = na
  14. var float takeProfit = na
  15.  
  16. // 1. Put these "na"s.
  17. var line stopLossLine = na
  18. var line takeProfitLine = na
  19.  
  20. // 2. Adding logic to manually reset or extend the lines based on if we are in a position or not.
  21. if strategy.position_size == 0
  22. stopLossLine := na
  23. takeProfitLine := na
  24. else
  25. stopLossLine.set_x2(bar_index)
  26. takeProfitLine.set_x2(bar_index)
  27.  
  28. longCondition = ta.crossover(ema12, ema21)
  29. shortCondition = ta.crossunder(ema12, ema21)
  30.  
  31. if longCondition and strategy.position_size == 0
  32. stopLoss := close * (1 - 0.03)
  33. takeProfit := close * (1 + 0.03)
  34.  
  35. // 3. 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. // Open a long position at market price
  40. strategy.entry("Long", strategy.long)
  41.  
  42. // Set stop loss and take profit levels
  43. strategy.exit("Take Profit/Stop Loss", from_entry="Long", stop=stopLoss, limit=takeProfit)
  44.  
  45. if shortCondition and strategy.position_size == 0
  46. stopLoss := close * (1 + 0.03)
  47. takeProfit := close * (1 - 0.03)
  48.  
  49. // 3. Draw the start of the lines when we enter a position.
  50. stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
  51. takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
  52.  
  53. // Open a short position at market price
  54. strategy.entry("Short", strategy.short)
  55.  
  56. // Set stop loss and take profit levels
  57. strategy.exit("Take Profit/Stop Loss", from_entry="Short", stop=stopLoss, limit=takeProfit)
  58.  
  59. bandColor50 = sma50 < sma50[1] ? #ffa73c : color.rgb(33, 184, 243)
  60. bandColor = ema12 > ema21 ? color.green : color.red
  61. plot(ema12, color=bandColor)
  62. plot(ema21, color=bandColor)
  63. plot(sma50, color=bandColor50)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement