How to avoid repainting when NOT using security() - PineCoders FAQ
"How to avoid repainting when NOT using security()" by PineCoders, MPL-2.0. Ported to FloScript by FloSignals; modifications under MPL-2.0. Original: How to avoid repainting when NOT using security() (v3.0), read 2026-11-15T18:50:03.919Z; source language v4.
Licence: Mozilla Public License 2.0 (MPL-2.0) — full text. The source is published with it, modifications included, under the same licence.
How to avoid repainting when NOT using security() by PineCoders, ported from Pine v4. Overlay indicator with 6 plots, 6 marker series, alerts and 6 inputs.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// (Notice added by FloSignals: the author's source carries none. It was published open-source on 2019-09-24; since 2019-07-26 the platform it was published on has released such a script under MPL-2.0 automatically.)
//@author=LucF, for PineCoders
// How to avoid repainting when NOT using security() - PineCoders FAQ
// v1.3, 2019.09.24 18:18 — LucF
// PineCoders, Tools and ideas for all Pine coders: pinecoders.com
// This script shows three methods to avoid repainting when not using the "security()" function.
// PineCoders FAQ & Code question linking here: http://www.pinecoders.com/faq_and_code/#how-to-avoid-repainting-when-not-using-the-security-function
// This indicator's TradingView page: https://www.tradingview.com/script/s8kWs84i-How-to-avoid-repainting-when-NOT-using-security/
// ------------------------------------------------------------------------------------------------
// Original author: PineCoders
// Original title: How to avoid repainting when NOT using security()
// Licence: MPL-2.0 (the notice above was added by FloSignals; the original carries none)
// Pine version: 4
// Fetched: 2026-11-15
// Ported to FloScript by FloSignals; modifications under MPL-2.0.
// Converter: @flosignals/floscript 0.1.0, strict mode, score 100/100
// ------------------------------------------------------------------------------------------------
//@floscript=1
//@version=5
// Converted by the FloScript converter from a Pine-compatible v4 source.
indicator("How to avoid repainting when NOT using security() - PineCoders FAQ", "", true)
fastLength = input(10, "Fast Length")
slowLength = input(3, "Slow Length")
// —————————— Method 1 (GOOD with "Once Per Bar Close" alerts only)
// GRAY LINES: Repainting
// GRAY MARKERS: Repainting
// ALERT: Non-repainting if "Once Per Bar Close" is used.
M1 = input(true, "Show Method 1 (gray): On close (Repainting)")
ma1_f = ta.sma(close, fastLength)
ma1_s = ta.sma(close, slowLength)
c1 = ta.cross(ma1_f, ma1_s)
plot(M1 ? ma1_f : na, "MA1 Fast", color.new(color.gray, 0), 1)
plot(M1 ? ma1_s : na, "MA1 Slow", color.new(color.gray, 0), 3)
plotshape(M1 and c1, "Method 1 Marker", shape.circle, location.top, color.gray, text="1\nRepainting", size=size.tiny)
plotshape(M1 and c1 ? ma1_s : na, "Method 1 Dot", shape.circle, location.absolute, color.gray, size=size.tiny)
alertcondition(c1, "Method 1: cross", "Method 1: cross")
// —————————— Method 2 (GOOD with "Once Per Bar" alerts)
// GREEN LINES: Repainting (but can be hidden)
// GREEN MARKERS: Non-Repainting
// ALERT: Non-Repainting, even when used with "Once Per Bar". "Once Per Bar" should be used, since alert appears at the beginning of the realtime bar.
M2 = input(true, "Show Method 2 (green): 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(true, "...Allow Method 2 repainting lines to show in realtime")
ma2_f = ta.sma(close, fastLength)
ma2_s = ta.sma(close, slowLength)
c2 = ta.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.new(color.green, 75), 7)
plot(M2 and (M2NR or not barstate.isrealtime) ? ma2_s : na, "MA1 Slow", color.new(color.lime, 75), 10)
plotshape(M2 and c2, "Method 2 Marker", shape.cross, location.top, color.lime, text="2\nNot\nRepainting", size=size.normal)
plotshape(M2 and c2 ? ma2_s : na, "Method 2 Dot", shape.circle, location.absolute, color.lime, size=size.tiny)
alertcondition(c2, "Method 2: cross", "Method 2: cross")
// —————————— Method 3 (NOT RECOMMENDED IN THIS CASE, but could be useful in special cases)
// ORANGE LINES: Non-Repainting BUT the MAs using "open" rather than "close" are not ideal.
// ORANGE MARKERS: Non-Repainting
// ALERT: Non-Repainting, even when used with "Once Per Bar".
M3 = input(true, "Show Method 3 (orange): On open (Non-Repainting but problematic)")
ma3_f = ta.sma(open, fastLength)
ma3_s = ta.sma(open, slowLength)
c3 = ta.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\nNot\nRepainting", size=size.small)
plotshape(M3 and c3 ? ma3_s : na, "Method 3 Dot", shape.circle, location.absolute, color.orange, size=size.tiny)
alertcondition(c3, "Method 3: cross", "Method 3: cross")
This file is under the Mozilla Public License 2.0; it is a port of the original by PineCoders, with the original licence header kept and the changes made for FloScript noted in the file.
- v1
Library refresh 2026-11-15
Disclaimer. Scripts are written and published by members, or ported by FloSignals from open-source originals, and are their authors' own work and opinion. FloSignals does not review them for correctness and nothing in a script is financial advice or a recommendation to buy or sell anything — read the Risk Disclaimer. Posting here is under the Community Rules; a script that is not the poster's to post can be reported as the Copyright & Takedowns page says.

Comments (0)
No comments yet. Members can leave one.