{"spec_id":"kagi-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nkagi-basic: Basic Kagi 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# Remove script directory from sys.path to avoid shadowing bokeh package\nscript_dir = os.path.dirname(os.path.abspath(__file__))\nwhile script_dir in sys.path:\n    sys.path.remove(script_dir)\nif \"\" in sys.path:\n    sys.path.remove(\"\")\nif \".\" in sys.path:\n    sys.path.remove(\".\")\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, Legend, LegendItem\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\nYANG_COLOR = \"#009E73\"  # First series - bluish green\nYIN_COLOR = \"#AE3030\"  # imprint red — bearish\n\n# Data generation\nnp.random.seed(42)\nn_days = 250\n\n# Simulate a stock price with trends\nbase_price = 100.0\nreturns = np.random.normal(0.001, 0.02, n_days)\n# Add some trending periods\nreturns[20:60] += 0.003  # Uptrend\nreturns[80:120] -= 0.004  # Downtrend\nreturns[150:200] += 0.002  # Uptrend\nprices = base_price * np.cumprod(1 + returns)\n\n# Kagi chart algorithm\nreversal_pct = 0.04  # 4% reversal threshold\n\ncurrent_price = prices[0]\ndirection = 1  # 1 for up, -1 for down\nis_yang = True  # Start as yang (thick)\nline_index = 0\nlast_high = prices[0]\nlast_low = prices[0]\n\n# Store kagi line segments: (x1, y1, x2, y2, is_yang)\nsegments = []\n\nfor i in range(1, len(prices)):\n    price = prices[i]\n    reversal_amount = current_price * reversal_pct\n\n    if direction == 1:  # Currently going up\n        if price > current_price:\n            # Continue upward - extend vertical line\n            if price > last_high:\n                is_yang = True  # Becomes yang when exceeds previous high\n            last_high = max(last_high, price)\n            segments.append((line_index, current_price, line_index, price, is_yang))\n            current_price = price\n        elif current_price - price >= reversal_amount:\n            # Reversal down - draw horizontal shoulder\n            segments.append((line_index, current_price, line_index + 1, current_price, is_yang))\n            line_index += 1\n            direction = -1\n            if price < last_low:\n                is_yang = False  # Becomes yin when falls below previous low\n            last_low = min(last_low, price)\n            segments.append((line_index, current_price, line_index, price, is_yang))\n            current_price = price\n    else:  # Currently going down\n        if price < current_price:\n            # Continue downward - extend vertical line\n            if price < last_low:\n                is_yang = False  # Becomes yin when falls below previous low\n            last_low = min(last_low, price)\n            segments.append((line_index, current_price, line_index, price, is_yang))\n            current_price = price\n        elif price - current_price >= reversal_amount:\n            # Reversal up - draw horizontal waist\n            segments.append((line_index, current_price, line_index + 1, current_price, is_yang))\n            line_index += 1\n            direction = 1\n            if price > last_high:\n                is_yang = True  # Becomes yang when exceeds previous high\n            last_high = max(last_high, price)\n            segments.append((line_index, current_price, line_index, price, is_yang))\n            current_price = price\n\n# Prepare data for ColumnDataSource - separate yang and yin\nxs_yang, ys_yang = [], []\nxs_yin, ys_yin = [], []\n\nfor seg in segments:\n    x1, y1, x2, y2, yang = seg\n    if yang:\n        xs_yang.append([x1, x2])\n        ys_yang.append([y1, y2])\n    else:\n        xs_yin.append([x1, x2])\n        ys_yin.append([y1, y2])\n\nsource_yang = ColumnDataSource(data={\"xs\": xs_yang, \"ys\": ys_yang})\nsource_yin = ColumnDataSource(data={\"xs\": xs_yin, \"ys\": ys_yin})\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"kagi-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"Line Index\",\n    y_axis_label=\"Price ($)\",\n)\n\n# Plot kagi lines - yang (thick) and yin (thin) with ColumnDataSource\nyang_renderer = p.multi_line(xs=\"xs\", ys=\"ys\", source=source_yang, line_color=YANG_COLOR, line_width=8)\nyin_renderer = p.multi_line(xs=\"xs\", ys=\"ys\", source=source_yin, line_color=YIN_COLOR, line_width=3)\n\n# Styling - scaled for 4800x2700 canvas\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\n\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\n\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\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\n# Grid styling - subtle, solid lines\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Add legend with larger, more prominent text\nlegend = Legend(\n    items=[\n        LegendItem(label=\"Yang (Uptrend)\", renderers=[yang_renderer]),\n        LegendItem(label=\"Yin (Downtrend)\", renderers=[yin_renderer]),\n    ],\n    location=\"top_left\",\n    label_text_font_size=\"20pt\",\n)\n\np.add_layout(legend)\np.legend.background_fill_color = ELEVATED_BG\np.legend.background_fill_alpha = 0.9\np.legend.border_line_color = INK_SOFT\np.legend.label_text_color = INK_SOFT\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome via Selenium\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)\n\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\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}