{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-candlestick",
  "title": "Candlestick Chart",
  "description": "Live OHLC candles in your chart colors, with an OHLC info bar, a crosshair and a price badge.",
  "dependencies": [
    "@wick-charts/react@^0.9.2"
  ],
  "registryDependencies": [
    "card",
    "https://wick-charts.eeff.io/r/wick-chart.json"
  ],
  "files": [
    {
      "path": "blocks/chart-candlestick.tsx",
      "content": "\"use client\"\n\nimport { useEffect, useState } from \"react\"\n\nimport {\n  CandlestickSeries,\n  Crosshair,\n  InfoBar,\n  NumberFlow,\n  type OHLCData,\n  TimeAxis,\n  YAxis,\n  YLabel,\n  formatSignificant,\n} from \"@wick-charts/react\"\n\nimport {\n  Card,\n  CardAction,\n  CardContent,\n  CardDescription,\n  CardHeader,\n  CardTitle,\n} from \"@/components/ui/card\"\nimport { WickChart } from \"@/components/ui/wick-chart\"\n\nconst CANDLE_MS = 5000\nconst TICK_MS = 500\nconst HISTORY = 80\n\n// The block's price formats; change them for your market. Below 1, `formatSignificant` keeps\n// the digits a small price needs (0.0₆95), as the library default does.\nconst formatPrice = (value: number) =>\n  Math.abs(value) < 1\n    ? formatSignificant(value)\n    : value.toLocaleString(\"en-US\", {\n        minimumFractionDigits: 2,\n        maximumFractionDigits: 2,\n      })\n\n// Inside the chart, prices from 1,000 up drop the cents, so the axis column and the OHLC row fit a phone.\nconst formatChartPrice = (value: number) =>\n  Math.abs(value) < 1000\n    ? formatSignificant(value)\n    : value.toLocaleString(\"en-US\", { maximumFractionDigits: 0 })\n\nconst candleOptions = { bodyWidthRatio: 0.7 }\n\n// Seeded, so the server render and the browser start from the same candles.\nfunction seededRandom(seed: number): () => number {\n  let state = seed\n\n  return () => {\n    state = (state * 1_664_525 + 1_013_904_223) % 4_294_967_296\n\n    return state / 4_294_967_296\n  }\n}\n\n// Demo feed: each tick moves the last candle, a new candle opens every 5 s.\nfunction openCandle(time: number, price: number): OHLCData {\n  return { time, open: price, high: price, low: price, close: price }\n}\n\nfunction tradeInto(\n  candle: OHLCData,\n  random: () => number = Math.random\n): OHLCData {\n  const close = candle.close * (1 + (random() - 0.5) * 0.002)\n\n  return {\n    ...candle,\n    close,\n    high: Math.max(candle.high, close),\n    low: Math.min(candle.low, close),\n  }\n}\n\nfunction seedCandles(): OHLCData[] {\n  const start =\n    Math.floor(Date.now() / CANDLE_MS) * CANDLE_MS - HISTORY * CANDLE_MS\n  const candles: OHLCData[] = []\n  const random = seededRandom(7)\n  let price = 68_000\n\n  for (let i = 0; i < HISTORY; i++) {\n    let candle = openCandle(start + i * CANDLE_MS, price)\n    for (let trade = 0; trade < CANDLE_MS / TICK_MS; trade++) {\n      candle = tradeInto(candle, random)\n    }\n\n    candles.push(candle)\n    price = candle.close\n  }\n\n  return candles\n}\n\nexport function ChartCandlestick() {\n  const [candles, setCandles] = useState(seedCandles)\n\n  useEffect(() => {\n    const timer = setInterval(() => {\n      setCandles((previous) => {\n        const last = previous[previous.length - 1]\n        if (!last) return previous\n\n        const isNewCandle = Date.now() >= last.time + CANDLE_MS\n        if (isNewCandle) {\n          return [\n            ...previous.slice(1),\n            tradeInto(openCandle(last.time + CANDLE_MS, last.close)),\n          ]\n        }\n\n        return [...previous.slice(0, -1), tradeInto(last)]\n      })\n    }, TICK_MS)\n\n    return () => clearInterval(timer)\n  }, [])\n\n  const first = candles[0]?.open ?? 0\n  const latest = candles[candles.length - 1]?.close ?? 0\n  const change = first === 0 ? 0 : ((latest - first) / first) * 100\n\n  return (\n    <Card>\n      <CardHeader>\n        <CardTitle>BTC / USD</CardTitle>\n        <CardDescription>5-second candles, simulated feed</CardDescription>\n        <CardAction className=\"text-right\">\n          <NumberFlow\n            value={latest}\n            format={formatPrice}\n            className=\"text-2xl font-semibold tabular-nums\"\n          />\n          <div\n            className={\n              change >= 0\n                ? \"text-xs text-emerald-600 dark:text-emerald-400\"\n                : \"text-xs text-red-600 dark:text-red-400\"\n            }\n          >\n            {change >= 0 ? \"+\" : \"\"}\n            {change.toFixed(2)}%\n          </div>\n        </CardAction>\n      </CardHeader>\n      <CardContent>\n        <WickChart\n          className=\"aspect-auto h-[280px]\"\n          viewport={{ initialRange: 48 }}\n        >\n          <InfoBar format={formatChartPrice} />\n          <CandlestickSeries\n            data={{ label: \"BTC\", data: candles }}\n            options={candleOptions}\n          />\n          <YAxis format={formatChartPrice} />\n          <TimeAxis />\n          <Crosshair />\n          <YLabel />\n        </WickChart>\n      </CardContent>\n    </Card>\n  )\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "charts",
    "finance"
  ],
  "type": "registry:block"
}