{"spec_id":"indicator-sma","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nindicator-sma: Simple Moving Average (SMA) Indicator Chart\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-19\n\"\"\"\n\nimport os\n\nimport matplotlib.dates as mdates\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 — canonical order\nPRICE_COLOR = \"#009E73\"  # position 1 — price line (first series)\nSMA20_COLOR = \"#C475FD\"  # position 2\nSMA50_COLOR = \"#4467A3\"  # position 3\nSMA200_COLOR = \"#BD8233\"  # position 4\n\n# Data - realistic stock price data with trend and volatility\nnp.random.seed(42)\nn_days = 300\ndates = pd.date_range(\"2024-01-01\", periods=n_days, freq=\"B\")  # Business days\n\nbase_price = 150\nreturns = np.random.normal(0.0003, 0.015, n_days)\ntrend = np.sin(np.linspace(0, 3 * np.pi, n_days)) * 0.001\nreturns = returns + trend\nclose = base_price * np.cumprod(1 + returns)\n\ndf = pd.DataFrame({\"date\": dates, \"close\": close})\ndf[\"sma_20\"] = df[\"close\"].rolling(window=20).mean()\ndf[\"sma_50\"] = df[\"close\"].rolling(window=50).mean()\ndf[\"sma_200\"] = df[\"close\"].rolling(window=200).mean()\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nax.plot(df[\"date\"], df[\"close\"], color=PRICE_COLOR, linewidth=2.5, label=\"Price\", alpha=0.85, zorder=4)\nax.plot(df[\"date\"], df[\"sma_20\"], color=SMA20_COLOR, linewidth=2.0, label=\"SMA 20\", zorder=3)\nax.plot(df[\"date\"], df[\"sma_50\"], color=SMA50_COLOR, linewidth=2.0, label=\"SMA 50\", zorder=2)\nax.plot(df[\"date\"], df[\"sma_200\"], color=SMA200_COLOR, linewidth=2.0, label=\"SMA 200\", zorder=1)\n\n# Style\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"Price ($)\", fontsize=20, color=INK)\nax.set_title(\"indicator-sma · python · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Date formatting with month locator\nax.xaxis.set_major_locator(mdates.MonthLocator())\nax.xaxis.set_major_formatter(mdates.DateFormatter(\"%b %Y\"))\nfig.autofmt_xdate(rotation=30)\n\n# Grid — y-axis only for line chart\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\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# Legend — upper right avoids overlap with early price data\nleg = ax.legend(fontsize=16, loc=\"upper right\")\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.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"}