TradingView - K线 使用详解
2021-11-30 10:44:32
more 
1889

TradingView - K线

文章目录

  • TradingView - K线
    • 1 前言
    • 2 如何使用
      • 2.1 项目引入 Tradingview 静态资源包
          • [强制] 静态资源包 `charting_library-master` 放在目录 `static` 里。
      • 2.2 项目添加 Tradingview 核心库 charting_library.min.js
          • [建议] 在项目的运行入口 `index.html` 里添加
      • 2.3 挂载 Tradingview 到 Vue 组件
          • [建议] 预挂载提升K线渲染效率。
      • 2.4 获取 TradingView 数据并渲染
          • [建议] 批量获取历史数据依次叠加。
      • 2.5 TradingView 时段和交易对变化
          • [建议] 时段变化切换对应时段接口交易对变化重新初始化K线

1 前言

Tradingview是一个价格图表和分析软件提供免费和付费选项由一群交易员和软件开发商在2011年9月推出。投资者可以通过Tradingview查看各种不同金融市场和资产类别的价格图表包括股票、货币对、债券、期货以及加密货币。

2 如何使用

2.1 项目引入 Tradingview 静态资源包

[强制] 静态资源包 charting_library-master 放在目录 static 里。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uqlU8rBz-1616988134216)(./images/tradingview-1.png)]

解释

static 目录下的文件不会被 webpack 处理它们会直接被复制到最终的打包目录默认是dist/static下。必须使用绝对路径引用这些文件这是通过在 config.js 文件中的 build.assetsPublicPath 和 build.assetsSubDirectory 连接来确定的。

2.2 项目添加 Tradingview 核心库 charting_library.min.js

[建议] 在项目的运行入口 index.html 里添加
<script src&#61;&#34;<%&#61; process.env.ASSETS_BASE_URL %><%&#61; process.env.assetsSubDirectory %>/charting_library-master/charting_library/charting_library.min.js&#34;></script>

解释

charting_library.min.jsTradingview 核心库提前缓存到浏览器提升K线渲染效率。

2.3 挂载 Tradingview 到 Vue 组件

[建议] 预挂载提升K线渲染效率。
// tradingview_chart_container 挂载Id<template>
  <div class&#61;&#34;kline&#34;>
    <div id&#61;&#34;tradingview_chart_container&#34; class&#61;&#34;chart_container&#34;></div>
  </div></template>

<script>export default {
    name: &#34;kline&#34;,
    data() {
        return {
            tvWidget: null,          // K线实例
            historyData: [],         // K线历史数据
            readyed: false,          // K线是否成功初始化
            limit: 100,              // K线单次获取历史数据数量
            timeStamp: 0,            // 最后一个数据的时间戳
            local: &#34;zh&#34;,             // K线国际化
            interval: 'D',           // K线时段
            subscribeKlineData: {},  // 实时数据
            webSocketUrl: 'spot/candle-1m:btc-usdt',       // K线实时数据接口
            onRealtimeCallback2: function () {},          // 实时数据
            tvTimeList: {}           // K线周期按钮数据
        }
    },
    beforeMount() {
        // 初始化 TradingView
        TradingView.onready(this.initData())
    },
    methods: {
         // 初始化K线
         initData() {
             let _this &#61; this;
             let ossPath &#61; '/' // 本地 或者 线上路径
             if (process.env.NODE_ENV &#61;&#61; &#34;production&#34;) {
                 ossPath &#61; 'https://btc018.oss-cn-shenzhen.aliyuncs.com/front/'
             }
             this.tvWidget &#61; window.tvWidget &#61; new TradingView.widget({
                 locale: _this.local, // K线国际化
                 symbol: 'BTC/USDT', // 交易对
                 interval: _this.interval, // K线数据时段
                 datafeed: _this.createFeed(), // 数据源
                 library_path : ossPath &#43; &#34;webStatic/charting_library-master/charting_library/&#34;, // K线静态资源路径
                 custom_css_url: ossPath &#43; &#34;webStatic/charting_library-master/charting_library/static/pro-night.css&#34;, // K线主题文件
                 container_id: &#34;tradingview_chart_container&#34;, // 挂载ID
                 // 使用项
                 enabled_features: [
                     ...
                 ],
                 // 禁用项
                 disabled_features: [
                     ...
                 ],
                 ... // 其它配置项
             });
             // 监听K线加载执行自定义事件
             this.tvWidget.onChartReady(function () {
                 // 自定义事件
                 ...
             });
         },   
    }</script>

2.4 获取 TradingView 数据并渲染

[建议] 批量获取历史数据依次叠加。
// ... TradingView Api// 获取历史数据this.historyData &#61; response.data.concat(this.historyData)// 渲染历史数据onDataCallback(_this.historyData, {
    noData: false,});// 没有更多历史数据时停止查询onDataCallback([], {
    noData: true,});// 订阅实时数据this.webSocketUrl &#61; &#96;spot/candle-5m:BTC-USDT&#96;this.wsOn(this.webSocketUrl, {}, this.subKlineCallback);// 渲染实时数据subKlineCallback (data) {
    this.onRealtimeCallback2({
        time: Number(data[0]) || 0,
        open: Number(data[1]) || 0,
        close: Number(data[2]) || 0,
        high: Number(data[3]) || 0,
        low: Number(data[4]) || 0,
        volume: Number(data[5]) || 0,
    });}

解释

批量获取历史数据接口压力小响应速度快提升K线渲染效率。

2.5 TradingView 时段和交易对变化

[建议] 时段变化切换对应时段接口交易对变化重新初始化K线
// 监听交易对this.$bus.on(&#34;symbolChange&#34;, symbolInfo &#61;> {
    if (!this.tvWidget) {
        // 初始化Tradingview
        TradingView.onready(this.initData())
    } else {
        if (symbolInfo.tmId !&#61;&#61; this.oldSymbolInfo.tmId) {
            // 关闭K线订阅
            this.closeKline();
            this.historyData &#61; [];
            this.oldSymbolInfo &#61; symbolInfo            this.initData();
        }
    }});

解释

交易对变化后清除缓存数据关闭数据推送避免K线报错。# TradingView - K线

Statement:
The content of this article does not represent the views of fxgecko website. The content is for reference only and does not constitute investment suggestions. Investment is risky, so you should be careful in your choice! If it involves content, copyright and other issues, please contact us and we will make adjustments at the first time!

Related News

您正在访问的是FxGecko网站。 FxGecko互联网及其移动端产品是中国香港特别行政区成立的Hitorank Co.,LIMITED旗下运营和管理的一款面向全球发行的企业资讯査询工具。

您的IP为 中国大陆地区,抱歉的通知您,不能为您提供查询服务,还请谅解。请遵守当地地法律。