{"spec_id":"indicator-sma","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nindicator-sma: Simple Moving Average (SMA) Indicator Chart\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 86/100 | Updated: 2026-05-19\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Legend\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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 (positions 1-4)\nCOLOR_CLOSE = \"#009E73\"  # brand green — close price (first series)\nCOLOR_SMA20 = \"#C475FD\"  # vermillion\nCOLOR_SMA50 = \"#4467A3\"  # blue\nCOLOR_SMA200 = \"#BD8233\"  # reddish purple\n\n# Data — engineered to produce visible golden cross / death cross signals\nnp.random.seed(42)\nn_days = 300\ndates = pd.date_range(start=\"2024-01-01\", periods=n_days, freq=\"B\")\nreturns = np.concatenate(\n    [np.random.normal(0.001, 0.012, 100), np.random.normal(-0.002, 0.015, 80), np.random.normal(0.003, 0.013, 120)]\n)\nprice = 100 * np.exp(np.cumsum(returns))\n\ndf = pd.DataFrame({\"date\": dates, \"close\": price})\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\nsource = ColumnDataSource(df)\n\n# Plot\np = figure(\n    width=4800,\n    height=2700,\n    title=\"indicator-sma · python · bokeh · anyplot.ai\",\n    x_axis_label=\"Date\",\n    y_axis_label=\"Price ($)\",\n    x_axis_type=\"datetime\",\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n)\n\nclose_line = p.line(x=\"date\", y=\"close\", source=source, line_width=5, color=COLOR_CLOSE, alpha=1.0)\nsma20_line = p.line(x=\"date\", y=\"sma_20\", source=source, line_width=2, color=COLOR_SMA20, alpha=0.8, line_dash=\"dashed\")\nsma50_line = p.line(x=\"date\", y=\"sma_50\", source=source, line_width=2, color=COLOR_SMA50, alpha=0.8, line_dash=\"dashed\")\nsma200_line = p.line(\n    x=\"date\", y=\"sma_200\", source=source, line_width=2, color=COLOR_SMA200, alpha=0.8, line_dash=\"dashed\"\n)\n\n# HoverTool for exact values on mouseover\nhover = HoverTool(\n    tooltips=[\n        (\"Date\", \"@date{%F}\"),\n        (\"Close\", \"@close{$0.2f}\"),\n        (\"SMA 20\", \"@sma_20{$0.2f}\"),\n        (\"SMA 50\", \"@sma_50{$0.2f}\"),\n        (\"SMA 200\", \"@sma_200{$0.2f}\"),\n    ],\n    formatters={\"@date\": \"datetime\"},\n    mode=\"vline\",\n)\np.add_tools(hover)\n\n# Legend\nlegend = Legend(\n    items=[\n        (\"Close Price\", [close_line]),\n        (\"SMA 20\", [sma20_line]),\n        (\"SMA 50\", [sma50_line]),\n        (\"SMA 200\", [sma200_line]),\n    ],\n    location=\"top_left\",\n)\np.add_layout(legend)\np.legend.label_text_font_size = \"22pt\"\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.label_text_color = INK_SOFT\n\n# Chrome\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\np.xgrid.grid_line_color = None\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\n\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\n\n# Save HTML then screenshot with headless Chrome\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}