Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //@author= LucF, for PineCoders
- // How to avoid repainting when NOT using 'security()' - PineCodersFAQ
- // v1.0, 2019.09.15 12:25 — LucF
- study("How to avoid repainting when NOT using 'security()'", "", true)
- fastLength = input(10, "Fast Length")
- slowLength = input(3, "Slow Length")
- // —————————— Method 1 (GOOD with "Once Per Bar Close" only)
- // LINES: Repainting
- // ALERT: Repainting, unless "Once Per Bar Close" is used.
- M1 = input(true, "Show Method 1: On close (Repainting)")
- ma1_f = sma(close, fastLength)
- ma1_s = sma(close, slowLength)
- c1 = cross(ma1_f, ma1_s)
- plot(M1 ? ma1_f : na, "MA1 Fast", color.gray, 1, transp = 0)
- plot(M1 ? ma1_s : na, "MA1 Slow", color.gray, 3, transp = 0)
- plotshape(M1 and c1, "Method 1 Marker", shape.circle, location.top, color.gray, text = "1\nR", size = size.tiny)
- plotshape(M1 and c1 ? ma1_s : na, "Method 1 Dot", shape.circle, location.absolute, color.gray, size = size.tiny, offset = -0)
- alertcondition(c1, "Method 1: cross", "Method 1: cross")
- // —————————— Method 2 (GOOD with "Once Per Bar")
- // LINES: Repainting
- // ALERT: Non-Repainting. "Once Per Bar" can and should be used, since alert appears at the beginning
- M2 = input(true, "Show Method 2: On close but crossover[1] (Non Repainting)")
- // This setting allows hiding the repainting lines in the realtime bar. Note that because "barstate.realtime"
- // only becomes true after first price update in the realtime bar, the lines will show until then.
- M2NR = input(false, "...Allow repainting lines to show in realtime")
- ma2_f = sma(close, fastLength)
- ma2_s = sma(close, slowLength)
- c2 = cross(ma2_f, ma2_s)[1]
- // ————— Method 2b
- // Delayed non-repainting MAs and non-repainting alert. The delayed MAs are not optimal.
- // ma2_f = sma(close[1], fastLength)
- // ma2_s = sma(close[1], slowLength)
- // c2 = cross(ma2_f, ma2_s)
- plot(M2 and (M2NR or not barstate.isrealtime) ? ma2_f : na, "MA1 Fast", color.lime, 6, transp = 70)
- plot(M2 and (M2NR or not barstate.isrealtime) ? ma2_s : na, "MA1 Slow", color.lime, 8, transp = 70)
- plotshape(M2 and c2, "Method 2 Marker", shape.cross, location.top, color.lime, text = "2\nNoR", size = size.normal)
- plotshape(M2 and c2 ? ma2_s : na, "Method 2 Dot", shape.circle, location.absolute, color.lime, size = size.tiny, offset = -0)
- alertcondition(c2, "Method 2: cross", "Method 2: cross")
- // —————————— Method 3 (NOT RECOMMENDED)
- // LINES: Non-Repainting BUT the MAs using "open" rather than "close" are very problematic.
- // ALERT: Non-Repainting. "Once Per Bar" can be used.
- M3 = input(false, "Show Method 3: On open (Non-Repainting but problematic)")
- ma3_f = sma(open, fastLength)
- ma3_s = sma(open, slowLength)
- c3 = cross(ma3_f, ma3_s)
- plot(M3 ? ma3_f : na, "MA1 Fast", color.orange)
- plot(M3 ? ma3_s : na, "MA1 Slow", color.orange, 2)
- plotshape(M3 and c3, "Method 3 Marker", shape.diamond, location.top, color.orange, text = "3\nNoR", size = size.small)
- plotshape(M3 and c3 ? ma3_s : na, "Method 3 Dot", shape.circle, location.absolute, color.orange, size = size.tiny, offset = -0)
- alertcondition(c3, "Method 3: cross", "Method 3: cross")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement