{"spec_id":"indicator-macd","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nindicator-macd: MACD Technical Indicator Chart\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 81/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\n# Okabe-Ito palette\nBRAND = \"#009E73\"  # MACD\nSIGNAL_COLOR = \"#BD8233\"  # imprint ochre — signal line (categorical contrast)\n\n# Data - Generate realistic stock price data and calculate MACD\nnp.random.seed(42)\n\nn_days = 150\ndates = pd.date_range(\"2025-06-01\", periods=n_days, freq=\"B\")\n\nreturns = np.random.normal(0.0005, 0.015, n_days)\nprice = pd.Series(100 * np.exp(np.cumsum(returns)))\n\n# Calculate EMAs for MACD\nema_12 = price.ewm(span=12, adjust=False).mean()\nema_26 = price.ewm(span=26, adjust=False).mean()\n\n# MACD components\nmacd_line = ema_12 - ema_26\nsignal_line = macd_line.ewm(span=9, adjust=False).mean()\nhistogram = macd_line - signal_line\n\n# Use data from day 35 onwards (stable EMA values)\nstart_idx = 35\ndates = dates[start_idx:]\nmacd_line = macd_line.values[start_idx:]\nsignal_line = signal_line.values[start_idx:]\nhistogram = histogram.values[start_idx:]\n\n# Create histogram colors using diverging approach\nhist_positive = histogram >= 0\nhist_colors = [\n    \"#4467A3\" if val else \"#BD8233\"  # Blue for positive, purple for negative\n    for val in hist_positive\n]\n\n# Create figure\nfig = make_subplots(rows=1, cols=1, vertical_spacing=0.1)\n\n# Add MACD histogram bars\nfig.add_trace(\n    go.Bar(x=dates, y=histogram, name=\"Histogram\", marker={\"color\": hist_colors}, opacity=0.7, showlegend=True)\n)\n\n# Add MACD line\nfig.add_trace(go.Scatter(x=dates, y=macd_line, name=\"MACD (12, 26)\", line={\"color\": BRAND, \"width\": 3}, mode=\"lines\"))\n\n# Add Signal line\nfig.add_trace(\n    go.Scatter(x=dates, y=signal_line, name=\"Signal (9)\", line={\"color\": SIGNAL_COLOR, \"width\": 3}, mode=\"lines\")\n)\n\n# Add zero reference line\nfig.add_hline(\n    y=0,\n    line={\"color\": INK_SOFT, \"width\": 2, \"dash\": \"dash\"},\n    annotation_text=\"Zero Line\",\n    annotation_position=\"left\",\n    annotation_font_size=16,\n)\n\n# Update layout for theme-adaptive styling\nfig.update_layout(\n    title={\n        \"text\": \"indicator-macd · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Date\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"showgrid\": True,\n    },\n    yaxis={\n        \"title\": {\"text\": \"MACD Value\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"gridcolor\": GRID,\n        \"linecolor\": INK_SOFT,\n        \"showgrid\": True,\n        \"zeroline\": False,\n    },\n    legend={\n        \"font\": {\"size\": 18, \"color\": INK_SOFT},\n        \"x\": 0.01,\n        \"y\": 0.99,\n        \"xanchor\": \"left\",\n        \"yanchor\": \"top\",\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    margin={\"l\": 100, \"r\": 60, \"t\": 100, \"b\": 80},\n    annotations=[\n        {\n            \"text\": \"Parameters: EMA(12), EMA(26), Signal(9)\",\n            \"xref\": \"paper\",\n            \"yref\": \"paper\",\n            \"x\": 0.99,\n            \"y\": 0.01,\n            \"xanchor\": \"right\",\n            \"yanchor\": \"bottom\",\n            \"font\": {\"size\": 16, \"color\": INK_SOFT},\n            \"showarrow\": False,\n        }\n    ],\n)\n\n# Save as PNG\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\n\n# Save as HTML for interactivity\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}