{"spec_id":"indicator-bollinger","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nindicator-bollinger: Bollinger Bands Indicator Chart\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette - first color is always brand green\nBRAND = \"#009E73\"\nSECONDARY = \"#C475FD\"\nTERTIARY = \"#4467A3\"\n\n# Data - Generate realistic stock price data with Bollinger Bands\nnp.random.seed(42)\nn_days = 120\n\n# Generate realistic price movement using random walk\ndates = pd.date_range(\"2024-01-01\", periods=n_days, freq=\"B\")\nreturns = np.random.normal(0.0005, 0.015, n_days)\nprice_base = 150\nclose = price_base * np.cumprod(1 + returns)\n\n# Calculate Bollinger Bands (20-period SMA, 2 standard deviations)\nwindow = 20\nsma = pd.Series(close).rolling(window=window).mean()\nstd = pd.Series(close).rolling(window=window).std()\nupper_band = sma + 2 * std\nlower_band = sma - 2 * std\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot the filled area between bands (volatility envelope)\nax.fill_between(dates, lower_band, upper_band, alpha=0.15, color=TERTIARY, label=\"Volatility Band (±2σ)\")\n\n# Plot upper and lower bands\nax.plot(dates, upper_band, color=TERTIARY, linewidth=2, alpha=0.7)\nax.plot(dates, lower_band, color=TERTIARY, linewidth=2, alpha=0.7)\n\n# Plot middle band (SMA) as dashed line\nax.plot(dates, sma, color=SECONDARY, linewidth=2.5, linestyle=\"--\", label=\"SMA (20-day)\")\n\n# Plot closing price prominently in brand color\nax.plot(dates, close, color=BRAND, linewidth=3, label=\"Close Price\")\n\n# Style\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"Price ($)\", fontsize=20, color=INK)\nax.set_title(\"indicator-bollinger · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Format x-axis dates\nfig.autofmt_xdate(rotation=30)\nax.xaxis.set_major_locator(plt.MaxNLocator(8))\n\n# Spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\n# Grid - subtle y-axis only\nax.yaxis.grid(True, alpha=0.1, linewidth=0.8, color=INK_SOFT)\n\n# Legend\nleg = ax.legend(fontsize=16, loc=\"upper left\", framealpha=0.95)\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    leg.get_frame().set_linewidth(0.8)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}