// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Piotr Kowalski "piecioshka"
// Docs (EN): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/overlays/envelopes/envelopes.md
// Docs (PL): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/overlays/envelopes/envelopes.pl.md
// @version=6
indicator('Envelopes', overlay = true)
// Good envelope settings:
// - 3% around a 21-day simple moving average
// - 5% around a 10-week simple moving average
// - 10% around a 40-week simple moving average
aColor = color.rgb(56, 104, 174)
bColor = color.rgb(215, 107, 52)
len = input.int(title = 'Length', defval = 21, minval = 1)
percent = input.float(title = 'Percent', defval = 3.0, minval = 0.1, step = 0.1)
aSeries = ta.sma(close, len)
center = plot(aSeries, 'SMA', color=color.new(color.white, 30), force_overlay=true)
plotUpper = plot(aSeries * (1 + percent / 100), color=aColor, force_overlay=true)
plotLower = plot(aSeries * (1 - percent / 100), color=bColor, force_overlay=true)
fill(plotUpper, center, color = color.new(aColor, 90))
fill(center, plotLower, color = color.new(bColor, 90))