chart_get_state
Retrieve the current chart state including symbol, timeframe, visible studies, and last price from TradingView Desktop.
Instructions
Read the current chart state: symbol, timeframe, visible studies, and last price.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/chart.ts:23-36 (handler)The chartGetState handler function that executes the tool logic. It takes an empty input and a TradingViewPage instance, delegates to page.getChartState(), and wraps errors in ToolExecutionError.
export async function chartGetState( _input: z.infer<typeof chartGetStateInput>, page: TradingViewPage, ): Promise<z.infer<typeof chartGetStateOutput>> { try { return await page.getChartState(); } catch (cause) { throw new ToolExecutionError( 'chart_get_state', 'Failed to read chart state', cause, ); } } - src/tools/chart.ts:15-21 (schema)Input schema (empty object, strict) and output schema (symbol, timeframe, studies, lastPrice) for chart_get_state.
export const chartGetStateInput = z.object({}).strict(); export const chartGetStateOutput = z.object({ symbol: z.string(), timeframe: TimeframeSchema, studies: z.array(z.string()), lastPrice: z.number().nullable(), }); - src/tools/index.ts:69-78 (registration)Registration of chart_get_state in the TOOLS array with name, description, input/output schemas, and handler function.
export const TOOLS: ToolDef<any, any>[] = [ // --- chart --- { name: 'chart_get_state', description: 'Read the current chart state: symbol, timeframe, visible studies, and last price.', input: chartGetStateInput, output: chartGetStateOutput, handler: chartGetState, }, - src/tools/index.ts:13-15 (registration)Import of chartGetState, chartGetStateInput, and chartGetStateOutput from chart.ts, used in the tool registration.
chartGetState, chartGetStateInput, chartGetStateOutput, - The getChartState() method on TradingViewPage class that actually executes the logic via CDP: evaluates JavaScript in the browser to read TV widget state (symbol, resolution, studies, lastPrice) and maps the resolution to a canonical timeframe.
async getChartState(): Promise<ChartState> { const expr = ` (() => { const w = window.tvWidget || window.TradingView?.widget; if (!w || !w.activeChart) { return { error: 'tvWidget not available — is the chart loaded?' }; } const chart = w.activeChart(); const symbol = chart.symbol(); const resolution = chart.resolution(); const studies = chart.getAllStudies?.()?.map(s => s.name) ?? []; let lastPrice = null; try { const series = chart.getSeries?.(); lastPrice = series?.lastPrice?.() ?? null; } catch (_) { /* ignore */ } return { symbol, resolution, studies, lastPrice }; })() `; const raw = await this.cdp.evaluate<{ error?: string; symbol?: string; resolution?: string; studies?: string[]; lastPrice?: number | null; }>(expr); if (raw.error) { throw new ChartStateError(raw.error); } const tf = TV_TO_TIMEFRAME[raw.resolution ?? '']; if (!tf) { throw new ChartStateError( `Unknown TradingView resolution "${raw.resolution}" — add a mapping.`, ); } return { symbol: raw.symbol ?? '', timeframe: tf, studies: raw.studies ?? [], lastPrice: raw.lastPrice ?? null, }; }