You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vanilla JS环境使用TradingView API的createPositionLine遇TypeError错误

问题诊断与修复

报错Uncaught (in prompt) TypeError: Cannot read properties of undefined (reading 'createPointset')本质是createPositionLine()内部依赖的对象未初始化,核心原因是API调用方式错误,以下是具体修复方案:

1. 修正事件监听方法的参数格式

你的代码中onReverse和onClose的调用不符合TradingView官方API规范——这些事件方法仅接受回调函数作为参数,不需要提前传入字符串提示语。错误的参数会导致内部PositionLine实例初始化失败,进而触发createPointset相关错误。

修正后的代码

widget.onChartReady(function() {
    const positionLine = widget.chart().createPositionLine();
    
    positionLine.onModify(function() {
        this.setText("onModify called");
    })
    .onReverse(function() {
        this.setText("onReverse called");
    })
    .onClose(function() {
        this.setText("onClose called");
    })
    .setText("PROFIT: 71.1 (3.31%)")
    .setQuantity("8.235")
    .setPrice(15.5)
    .setExtendLeft(false)
    .setLineStyle(0)
    .setLineLength(25);
});

2. 验证图表实例有效性

尽管onChartReady回调触发时图表已加载完成,仍建议先确认widget.chart()返回有效实例,避免因widget初始化异常导致的问题:

widget.onChartReady(function() {
    const chartInstance = widget.chart();
    console.log(chartInstance); // 检查控制台输出是否为非undefined的图表对象
    if (!chartInstance) return;
    
    // 后续持仓线创建代码...
});

3. 排查版本与权限问题

  • 确认使用的TradingView Widget版本支持createPositionLine方法,旧版本可能未提供该功能,建议升级至最新稳定版。
  • 检查widget初始化配置,确保已启用相关高级功能权限(部分功能需要特定许可或配置项开启)。

内容的提问来源于stack exchange,提问作者함형민

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:26:10