TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS 源文件(customIndex.js),放在图表库 static 文件夹下。自定义指标 JS 源代码模板如下:

 __customIndicators = [
{
name: 'ShuBenRSI',
metainfo: {
'_metainfoVersion': 40,
'id': 'ShuBenRSI@tv-basicstudies-1',
'scriptIdPart': '',
'name': 'ShuBenRSI',
'description': 'ShuBenRSI',
'shortDescription': 'ShuBenRSI',
'is_hidden_study': true,
'is_price_study': true,
'isCustomIndicator': true,
'plots': [{'id': 'plot_0', 'type': 'line'}],
'defaults': {
'styles': {
'plot_0': {
'linestyle': 0,
'visible': true,
'linewidth': 1,
'plottype': 2, // 绘制类型为线形图: 2
'trackPrice': true,
'transparency': 40,
'color': '#880000'
}
},
'precision': 1, // 精度 eg:608.4
'inputs': {}
},
'styles': {
'plot_0': {
'title': 'ShuBenRSI',
'histogrambase': 0,
}
},
'inputs': [],
},
constructor: function () {
this.init = function (context, inputCallback) {
this._context = context;
this._input = inputCallback;
//var symbol = 'p1905';
var symbol = PineJS.Std.ticker(this._context); // 获取所选商品代码
this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context));
};
this.main = function (context, inputCallback) {
this._context = context;
this._input = inputCallback;
this._context.select_sym(1);
if(this._context['symbol']['time'] != NaN){
var c = PineJS.Std.close(this._context)-50;
var o = PineJS.Std.open(this._context)-50;
var l = PineJS.Std.low(this._context)-50;
var h = PineJS.Std.high(this._context)-50;
console.log('execute custom index!');
console.log('symbol: ', this._context['symbol']['time']);
return [o, c];
} }
}
}
];

自定义指标 JS

  其中 main 方法会根据数据量(getBar 中的后台获取的数据)的多少自动进行循环遍历,在此可以对数据进行修改,以创建自己的数据曲线(个人理解)

  即 new TradingView.widget() 中使用 indicators_file_name: 'customIndex.js'

  onChartReady() 中将定义指标添加到页面显示中 widget.chart().createStudy('ShuBenRSI', false, false); // 自定义 RSI 曲线

自定义的曲线数据源来自 getBar 方法中回调函数中的 bar 数据,可以在自定义模板中修改此数据。

 __customIndicators = [
{
name: 'ShuBenRSI',
metainfo: {
'_metainfoVersion': 40,
'id': 'ShuBenRSI@tv-basicstudies-1',
'scriptIdPart': '',
'name': 'ShuBenRSI',
//当调用createStudy方法时,它也被用作“name”参数
'description': 'ShuBenRSI',
// 该描述将显示在图表上
'shortDescription': 'ShuBenRSI',
'is_hidden_study': true,
// 指标曲线是否在主数据列窗口中显示
'is_price_study': true,
'isCustomIndicator': true,
'plots': [{'id': 'plot_0', 'type': 'line'}],
'defaults': {
'styles': {
'plot_0': {
'linestyle': 0,
'visible': true,
'linewidth': 1,
'plottype': 2, // 绘制类型为线形图: 2
'trackPrice': true,
'transparency': 40,
'color': '#880000'
}
},
'precision': 3, // 精度 eg:608.4
'inputs': {}
},
'styles': {
'plot_0': {
'title': 'ShuBenRSI',
'histogrambase': 0,
}
},
'inputs': [],
},
constructor: function () {
this.init = function (context, inputCallback) {
var host = window.location;
var host1 = host.href.split('static');
var fakeDataRSI = [];
$.ajaxSetup({ async: false });
$.post(host1[0] + 'cta_posPL_syetem/getChartData',{method:'getDataRSI'}, function (result) {
if(result.result_code == 'success'){
fakeDataRSI = result.data;
} });
this.fakeData = fakeDataRSI;
this.count = 0;
this.time = 0;
this.rsi = 0;
this.infoList = [];
//console.log('init context: ', context);
//console.log(this.count);
this._context = context;
this._input = inputCallback;
//var symbol = 'p1905';
var symbol = PineJS.Std.ticker(this._context); // 获取所选商品代码
this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context));
};
this.main = function (context, inputCallback) {
//this.count += 1;
//console.log('count: ',this.count);
//if(this.count<5)console.log('main fakeData: ', this.fakeData[this.count]);
this._context = context;
this._input = inputCallback;
this._context.select_sym(1);
/*
// RSI 计算
if(this.count > 1 && this.time != this._context['symbol']['time']){
//console.log(PineJS.Std.close(this._context));
this.infoList.push(PineJS.Std.close(this._context));
var upSum = 0;var downSum = 0;
if(this.count > 15){
for(var i = 1; i <= 14; i++){
var change = this.infoList[i] - this.infoList[i - 1];
change > 0 ? upSum += change : downSum -= change;
}
var rs = Math.round(upSum / 14 *1000) / downSum;
this.rsi = Math.round(rs / (1 + rs) * 100 * 1000) / 1000;
//console.log('current: ', this._context['symbol']['time'], 'pretime: ',this.time);
//console.log('infoList: ',this.infoList);
this.infoList.splice(0, 1);
this.time = this._context['symbol']['time'];
//console.log('index close: --- >', PineJS.Std.close(this._context));
return [this.rsi];
}
}
return [this.rsi];
*/
var c = this.fakeData[this.count++]['close'];
//console.log('rsi: ',this.rsi); return [c];
/*
var c = PineJS.Std.close(this._context)-50;
var o = PineJS.Std.open(this._context)-50;
var l = PineJS.Std.low(this._context)-50;
var h = PineJS.Std.high(this._context)-50;
//console.log('execute custom index!');
console.log('symbol: ', this._context['symbol']['time']);
//return [o, c];
*/
}
}
}
];

自定义指标曲线

如果你想要自定义的曲线数据并不是 K 线数据,你可以在自定义模板中,向后台请求 用后台返回的数据。eg:

 __customIndicators = [
{
name: 'ShuBenRSI',
metainfo: {
'_metainfoVersion': 40,
'id': 'ShuBenRSI@tv-basicstudies-1',
'scriptIdPart': '',
'name': 'ShuBenRSI',
//当调用createStudy方法时,它也被用作“name”参数
'description': 'ShuBenRSI',
// 该描述将显示在图表上
'shortDescription': 'ShuBenRSI',
'is_hidden_study': true,
'is_price_study': false,
'isCustomIndicator': true,
'plots': [{'id': 'plot_0', 'type': 'line'}],
'defaults': {
'styles': {
'plot_0': {
'linestyle': 0,
'visible': true,
'linewidth': 1,
'plottype': 2, // 绘制类型为线形图: 2
'trackPrice': true,
'transparency': 40,
'color': '#880000'
}
},
'precision': 3, // 精度 eg:608.4
'inputs': {}
},
'styles': {
'plot_0': {
'title': 'ShuBenRSI',
'histogrambase': 0,
}
},
'inputs': [],
},
constructor: function () {
this.init = function (context, inputCallback) {
var host = window.location.href.split('static')[0];
var fakeDataRSI = [];
$.ajaxSetup({ async: false });
$.post(host + 'cta_posPL_syetem/getChartData',{method:'getDataRSI'}, function (result) {
if(result.result_code == 'success'){
fakeDataRSI = result.data;
}
});
this.fakeData = fakeDataRSI;
this.count = 0;
this.time = 0;
this.rsi = 0;
this.infoList = [];
this._context = context;
this._input = inputCallback;
var symbol = PineJS.Std.ticker(this._context); // 获取所选商品代码
this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context));
};
this.main = function (context, inputCallback) {
this._context = context;
this._input = inputCallback;
this._context.select_sym(1);
var c = this.fakeData[this.count++]['close'];
return [c];
}
}
}
];

数据源不是 K 线数据

TradingView 自定义指标的更多相关文章

  1. AWS EC2 复制实例后,自定义指标无法显示数据

    从一个实例创建了一个AMI,然后通过这个AMI创建新的EC2实例,结果发票自定义指标不会显示: 系统一直在邮件中提示: print() on closed filehandle MDATA at Cl ...

  2. kubernetes学习笔记之十二:资源指标API及自定义指标API

    第一章.前言 以前是用heapster来收集资源指标才能看,现在heapster要废弃了从1.8以后引入了资源api指标监视 资源指标:metrics-server(核心指标) 自定义指标:prome ...

  3. k8s-资源指标API及自定义指标API-二十三

    一. 原先版本是用heapster来收集资源指标才能看,但是现在heapster要废弃了. 从k8s v1.8开始后,引入了新的功能,即把资源指标引入api: 在使用heapster时,获取资源指标是 ...

  4. Kubernetes 学习23 kubernetes资源指标API及自定义指标API

    一.概述 1.上集中我们说到,官方文档提示说从k8s 1.11版本开始,将监控体系指标数据获取机制移向新一代的监控模型.也就意味着对于我们的k8s来讲现在应该有这样两种资源指标被使用.一种是资源指标, ...

  5. k8s之自定义指标API部署prometheus

    1.自定义指标-prometheus node_exporter是agent;PromQL相当于sql语句来查询数据; k8s-prometheus-adapter:prometheus是不能直接解析 ...

  6. k8s系列---资源指标API及自定义指标API

    不得不说千万不要随意更改版本,我用的1.13的版本,然后学到这一步时,还因yaml文件不同,卡住了很久,然后各种google才找到解决办法  https://www.linuxea.com/2112. ...

  7. 简单4步,利用Prometheus Operator实现自定义指标监控

    本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...

  8. Prometheus自定义指标

    1.  自定义指标 为了注册自定义指标,请将MeterRegistry注入到组件中,例如: public class Dictionary { private final List<String ...

  9. Kubernetes 监控:Prometheus Adpater =》自定义指标扩缩容

    使用 Kubernetes 进行容器编排的主要优点之一是,它可以非常轻松地对我们的应用程序进行水平扩展.Pod 水平自动缩放(HPA)可以根据 CPU 和内存使用量来扩展应用,前面讲解的 HPA 章节 ...

随机推荐

  1. codevs 1500 后缀排序

    codevs 1500 后缀排序 http://codevs.cn/problem/1500/  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 天凯是MI ...

  2. 转载http中302与301的区别

    http://blog.csdn.net/qmhball/article/details/7838989 一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于 ...

  3. 把JS和CSS合并到1个文件

    合并JS文件和CSS文件很多人都知道,也用过,目的是为了减少请求数.但有时候我们觉的把JS合并到1个文件,CSS又合并到另外1个文件也是浪费,我们如何能把CSS和JS一起合并进1个文件了? 这里需要使 ...

  4. Sparse AutoEncoder简介

    1. AutoEncoder AutoEncoder是一种特殊的三层神经网络, 其输出等于输入:\(y^{(i)}=x^{(i)}\), 如下图所示: 亦即AutoEncoder想学到的函数为\(f_ ...

  5. C#中2个日期类型相减

    DateTime startTime = Convert.ToDateTime("2017-1-9");DateTime endTime = Convert.ToDateTime( ...

  6. 字符串hash&&对字符串hash的理解

     对字符串hash的一些总结: 1,首先,我们在转化的时候,取底的时候一般是取131这些数,因为要避免不同的字符串对应相同的hash值这种情况的出现.如果卡精度的时候,我们可以采取双模数的方式尽量减少 ...

  7. Django 安装 —Django学习 (一)

    Django Django 是一个python 框架, 采用MTV的模式,模型,模板,视图 注意事项 Django 版本和 python 的版本是一一对应的,安装时一定要注意相应的版本信息. 如下图: ...

  8. 基于Netty4.1.29.Final的helloworld实现.使用idea

    服务端: //服务端 public class Server { public static void main(String[] args) { //创建两个线程组 EventLoopGroup c ...

  9. Add Two Numbers I & II

    Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...

  10. C#.NET调用WSDL接口及方法

    1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...