{"spec_id":"indicator-macd","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nindicator-macd: MACD Technical Indicator Chart\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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# imprint palette\nMACD_COLOR = \"#009E73\"  # green — MACD line\nSIGNAL_COLOR = \"#BD8233\"  # ochre — signal line (categorical contrast with MACD green)\n\n# Histogram colors — semantic positive/negative\nHIST_POSITIVE = \"#4467A3\"  # blue — above zero\nHIST_NEGATIVE = \"#AE3030\"  # red — below zero\n\n# Configure seaborn styling\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Generate synthetic stock price data for MACD calculation\nnp.random.seed(42)\nn_days = 120\ndates = pd.date_range(\"2024-01-01\", periods=n_days, freq=\"B\")\n\n# Simulate stock price movement with trend and volatility\nreturns = np.random.normal(0.001, 0.015, n_days)\nprice = 100 * np.exp(np.cumsum(returns))\n\n# Calculate Exponential Moving Averages\ndf = pd.DataFrame({\"date\": dates, \"close\": price})\ndf[\"ema12\"] = df[\"close\"].ewm(span=12, adjust=False).mean()\ndf[\"ema26\"] = df[\"close\"].ewm(span=26, adjust=False).mean()\n\n# Calculate MACD components\ndf[\"macd\"] = df[\"ema12\"] - df[\"ema26\"]\ndf[\"signal\"] = df[\"macd\"].ewm(span=9, adjust=False).mean()\ndf[\"histogram\"] = df[\"macd\"] - df[\"signal\"]\n\n# Drop initial periods where EMAs are not stable\ndf = df.iloc[33:].reset_index(drop=True)\n\n# Prepare histogram colors\ndf[\"hist_color\"] = np.where(df[\"histogram\"] >= 0, HIST_POSITIVE, HIST_NEGATIVE)\n\n# Create figure with proper sizing for 4800x2700 at 300 DPI\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Plot histogram as bars using seaborn-compatible approach\nfor i, row in df.iterrows():\n    ax.bar(\n        row[\"date\"],\n        row[\"histogram\"],\n        width=0.7,\n        color=row[\"hist_color\"],\n        alpha=0.6,\n        label=\"Histogram\" if i == 0 else \"\",\n    )\n\n# Plot MACD line\nsns.lineplot(data=df, x=\"date\", y=\"macd\", ax=ax, color=MACD_COLOR, linewidth=3, label=\"MACD (12, 26)\")\n\n# Plot Signal line\nsns.lineplot(data=df, x=\"date\", y=\"signal\", ax=ax, color=SIGNAL_COLOR, linewidth=3, label=\"Signal (9)\")\n\n# Add zero reference line\nax.axhline(y=0, color=INK_SOFT, linestyle=\"--\", linewidth=1.5, alpha=0.5)\n\n# Style the plot\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"MACD Value\", fontsize=20, color=INK)\nax.set_title(\"indicator-macd · seaborn · anyplot.ai\", fontsize=24, color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Remove top and right spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Grid styling\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n\n# Legend configuration\nax.legend(fontsize=16, loc=\"upper left\", framealpha=0.95)\n\n# Rotate x-axis labels for better readability\nplt.xticks(rotation=45, ha=\"right\")\n\n# Add annotation for MACD parameters\nax.annotate(\n    \"MACD Parameters: 12, 26, 9\",\n    xy=(0.98, 0.02),\n    xycoords=\"axes fraction\",\n    fontsize=14,\n    ha=\"right\",\n    va=\"bottom\",\n    color=INK,\n    bbox={\"boxstyle\": \"round,pad=0.5\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.95},\n)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}