chart_get_ohlcv
Retrieve the most recent OHLCV bars from the active chart. Specify the number of bars (up to 5000) to obtain the latest price data for analysis.
Instructions
Fetch recent OHLCV bars from the active chart.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | No | Number of most-recent bars to return. Capped at 5000. |
Implementation Reference
- src/tools/chart.ts:132-146 (handler)The main handler function for chart_get_ohlcv. Accepts a count parameter (default 100, max 5000), delegates to page.getOhlcv(), and returns an array of OHLCV bars. Wraps errors in ToolExecutionError.
export async function chartGetOhlcv( input: z.infer<typeof chartGetOhlcvInput>, page: TradingViewPage, ): Promise<z.infer<typeof chartGetOhlcvOutput>> { try { const bars = await page.getOhlcv(input.count); return { bars }; } catch (cause) { throw new ToolExecutionError( 'chart_get_ohlcv', `Failed to fetch ${input.count} bars`, cause, ); } } - src/tools/chart.ts:107-117 (schema)Input schema for chart_get_ohlcv: a single optional integer `count` (1-5000, default 100) describing how many recent bars to return.
export const chartGetOhlcvInput = z .object({ count: z .number() .int() .min(1) .max(5000) .default(100) .describe('Number of most-recent bars to return. Capped at 5000.'), }) .strict(); - src/tools/chart.ts:119-130 (schema)Output schema for chart_get_ohlcv: returns an object with a `bars` array, each bar having time (ISO string), open, high, low, close, and volume (all numbers).
export const chartGetOhlcvOutput = z.object({ bars: z.array( z.object({ time: z.string(), open: z.number(), high: z.number(), low: z.number(), close: z.number(), volume: z.number(), }), ), }); - src/tools/index.ts:93-99 (registration)Registration entry in the TOOLS array. Declares the tool with name 'chart_get_ohlcv', description 'Fetch recent OHLCV bars from the active chart.', and links the input/output schemas and handler function.
{ name: 'chart_get_ohlcv', description: 'Fetch recent OHLCV bars from the active chart.', input: chartGetOhlcvInput, output: chartGetOhlcvOutput, handler: chartGetOhlcv, }, - The TradingViewPage.getOhlcv() helper executed via CDP. Evaluates a JS expression in the page context to slice the last N bars from tvWidget's series data, converting to ISO timestamps.
async getOhlcv(count = 100): Promise<OhlcvBar[]> { const n = Math.max(1, Math.min(count, 5000)); const expr = ` (() => { const w = window.tvWidget; if (!w?.activeChart) return { error: 'tvWidget not available' }; const series = w.activeChart().getSeries?.(); if (!series?.data) return { error: 'series data unavailable' }; const bars = series.data().slice(-${n}).map(b => ({ time: new Date(b.time).toISOString(), open: b.open, high: b.high, low: b.low, close: b.close, volume: b.volume ?? 0, })); return { bars }; })() `; const r = await this.cdp.evaluate<{ error?: string; bars?: OhlcvBar[]; }>(expr); if (r.error) throw new ChartStateError(r.error); return r.bars ?? []; }