Advertisement
PineCoders

How to avoid repainting when NOT using 'security()'

Sep 15th, 2019
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. //@version=4
  2. //@author= LucF, for PineCoders
  3.  
  4. // How to avoid repainting when NOT using 'security()' - PineCodersFAQ
  5. // v1.0, 2019.09.15 12:25 — LucF
  6.  
  7. study("How to avoid repainting when NOT using 'security()'", "", true)
  8.  
  9. fastLength = input(10, "Fast Length")
  10. slowLength = input(3, "Slow Length")
  11.  
  12. // —————————— Method 1 (GOOD with "Once Per Bar Close" only)
  13. // LINES: Repainting
  14. // ALERT: Repainting, unless "Once Per Bar Close" is used.
  15. M1 = input(true, "Show Method 1: On close (Repainting)")
  16. ma1_f = sma(close, fastLength)
  17. ma1_s = sma(close, slowLength)
  18. c1 = cross(ma1_f, ma1_s)
  19. plot(M1 ? ma1_f : na, "MA1 Fast", color.gray, 1, transp = 0)
  20. plot(M1 ? ma1_s : na, "MA1 Slow", color.gray, 3, transp = 0)
  21. plotshape(M1 and c1, "Method 1 Marker", shape.circle, location.top, color.gray, text = "1\nR", size = size.tiny)
  22. plotshape(M1 and c1 ? ma1_s : na, "Method 1 Dot", shape.circle, location.absolute, color.gray, size = size.tiny, offset = -0)
  23. alertcondition(c1, "Method 1: cross", "Method 1: cross")
  24.  
  25. // —————————— Method 2 (GOOD with "Once Per Bar")
  26. // LINES: Repainting
  27. // ALERT: Non-Repainting. "Once Per Bar" can and should be used, since alert appears at the beginning
  28. M2 = input(true, "Show Method 2: On close but crossover[1] (Non Repainting)")
  29. // This setting allows hiding the repainting lines in the realtime bar. Note that because "barstate.realtime"
  30. // only becomes true after first price update in the realtime bar, the lines will show until then.
  31. M2NR = input(false, "...Allow repainting lines to show in realtime")
  32. ma2_f = sma(close, fastLength)
  33. ma2_s = sma(close, slowLength)
  34. c2 = cross(ma2_f, ma2_s)[1]
  35. // ————— Method 2b
  36. // Delayed non-repainting MAs and non-repainting alert. The delayed MAs are not optimal.
  37. // ma2_f = sma(close[1], fastLength)
  38. // ma2_s = sma(close[1], slowLength)
  39. // c2 = cross(ma2_f, ma2_s)
  40. plot(M2 and (M2NR or not barstate.isrealtime) ? ma2_f : na, "MA1 Fast", color.lime, 6, transp = 70)
  41. plot(M2 and (M2NR or not barstate.isrealtime) ? ma2_s : na, "MA1 Slow", color.lime, 8, transp = 70)
  42. plotshape(M2 and c2, "Method 2 Marker", shape.cross, location.top, color.lime, text = "2\nNoR", size = size.normal)
  43. plotshape(M2 and c2 ? ma2_s : na, "Method 2 Dot", shape.circle, location.absolute, color.lime, size = size.tiny, offset = -0)
  44. alertcondition(c2, "Method 2: cross", "Method 2: cross")
  45.  
  46. // —————————— Method 3 (NOT RECOMMENDED)
  47. // LINES: Non-Repainting BUT the MAs using "open" rather than "close" are very problematic.
  48. // ALERT: Non-Repainting. "Once Per Bar" can be used.
  49. M3 = input(false, "Show Method 3: On open (Non-Repainting but problematic)")
  50. ma3_f = sma(open, fastLength)
  51. ma3_s = sma(open, slowLength)
  52. c3 = cross(ma3_f, ma3_s)
  53. plot(M3 ? ma3_f : na, "MA1 Fast", color.orange)
  54. plot(M3 ? ma3_s : na, "MA1 Slow", color.orange, 2)
  55. plotshape(M3 and c3, "Method 3 Marker", shape.diamond, location.top, color.orange, text = "3\nNoR", size = size.small)
  56. plotshape(M3 and c3 ? ma3_s : na, "Method 3 Dot", shape.circle, location.absolute, color.orange, size = size.tiny, offset = -0)
  57. alertcondition(c3, "Method 3: cross", "Method 3: cross")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement