{"spec_id":"indicator-bollinger","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nindicator-bollinger: Bollinger Bands Indicator Chart\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Prevent this script from shadowing the bokeh package\nsys.path = [p for p in sys.path if \"implementations\" not in p]\n\nimport numpy as np\nimport pandas as pd\nfrom bokeh.io import output_file, save\nfrom bokeh.models import Band, 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-3)\nBRAND = \"#009E73\"  # Position 1 - first series (price)\nACCENT_BLUE = \"#4467A3\"  # Position 3 - SMA and bands\nACCENT_ORANGE = \"#C475FD\"  # Position 2 - optional\n\n# Data - Generate synthetic stock price data\nnp.random.seed(42)\nn_days = 120\n\n# Generate realistic price movement using random walk with drift\ndates = pd.date_range(\"2024-01-01\", periods=n_days, freq=\"B\")  # Business days\nreturns = np.random.normal(0.0005, 0.015, n_days)  # Daily returns with slight upward drift\nprice = 100 * np.cumprod(1 + returns)\n\n# Calculate Bollinger Bands (20-period SMA, 2 standard deviations)\nwindow = 20\nsma = pd.Series(price).rolling(window=window).mean().values\nstd = pd.Series(price).rolling(window=window).std().values\nupper_band = sma + 2 * std\nlower_band = sma - 2 * std\n\n# Create DataFrame for cleaner handling\ndf = pd.DataFrame({\"date\": dates, \"close\": price, \"sma\": sma, \"upper_band\": upper_band, \"lower_band\": lower_band})\n\n# Drop NaN values from the start (due to rolling window)\ndf = df.dropna().reset_index(drop=True)\n\n# Create ColumnDataSource\nsource = ColumnDataSource(df)\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"indicator-bollinger · 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\n# Add the band fill between upper and lower bands\nband = Band(\n    base=\"date\",\n    lower=\"lower_band\",\n    upper=\"upper_band\",\n    source=source,\n    fill_alpha=0.15,\n    fill_color=ACCENT_BLUE,\n    line_color=ACCENT_BLUE,\n    line_alpha=0.4,\n)\np.add_layout(band)\n\n# Plot the bands and price lines with legend\n# Upper band\nupper_line = p.line(\n    \"date\", \"upper_band\", source=source, line_color=ACCENT_BLUE, line_width=2, line_dash=\"solid\", alpha=0.6\n)\n\n# Lower band\nlower_line = p.line(\n    \"date\", \"lower_band\", source=source, line_color=ACCENT_BLUE, line_width=2, line_dash=\"solid\", alpha=0.6\n)\n\n# Middle band (SMA) - dashed line\nsma_line = p.line(\"date\", \"sma\", source=source, line_color=ACCENT_BLUE, line_width=3, line_dash=\"dashed\", alpha=0.9)\n\n# Price line - most prominent (first series in Okabe-Ito)\nprice_line = p.line(\"date\", \"close\", source=source, line_color=BRAND, line_width=5, alpha=1.0)\n\n# Add hover tool for interactivity\nhover = HoverTool(\n    tooltips=[\n        (\"Date\", \"@date{%F}\"),\n        (\"Close\", \"$@close{0.2f}\"),\n        (\"SMA (20)\", \"$@sma{0.2f}\"),\n        (\"Upper Band\", \"$@upper_band{0.2f}\"),\n        (\"Lower Band\", \"$@lower_band{0.2f}\"),\n    ],\n    formatters={\"@date\": \"datetime\"},\n    mode=\"vline\",\n    renderers=[price_line],\n)\np.add_tools(hover)\n\n# Create legend\nlegend = Legend(\n    items=[(\"Close Price\", [price_line]), (\"SMA (20)\", [sma_line]), (\"Upper/Lower Band (±2σ)\", [upper_line])],\n    location=\"top_left\",\n)\n\np.add_layout(legend, \"right\")\n\n# Style the plot - text sizing for 4800×2700 px\np.title.text_font_size = \"28pt\"\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\"\n\n# Legend styling\np.legend.label_text_font_size = \"16pt\"\np.legend.glyph_width = 40\np.legend.glyph_height = 25\np.legend.spacing = 12\np.legend.padding = 15\n\n# Grid styling - subtle\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\np.title.text_color = INK\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\nif p.legend:\n    p.legend.background_fill_color = ELEVATED_BG\n    p.legend.border_line_color = INK_SOFT\n    p.legend.label_text_color = INK_SOFT\n\n# Save interactive HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome (Selenium 4 / Selenium Manager)\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)  # let bokeh's JS render the canvas\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}