TradingView 自定义指标
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 自定义指标的更多相关文章
- AWS EC2 复制实例后,自定义指标无法显示数据
从一个实例创建了一个AMI,然后通过这个AMI创建新的EC2实例,结果发票自定义指标不会显示: 系统一直在邮件中提示: print() on closed filehandle MDATA at Cl ...
- kubernetes学习笔记之十二:资源指标API及自定义指标API
第一章.前言 以前是用heapster来收集资源指标才能看,现在heapster要废弃了从1.8以后引入了资源api指标监视 资源指标:metrics-server(核心指标) 自定义指标:prome ...
- k8s-资源指标API及自定义指标API-二十三
一. 原先版本是用heapster来收集资源指标才能看,但是现在heapster要废弃了. 从k8s v1.8开始后,引入了新的功能,即把资源指标引入api: 在使用heapster时,获取资源指标是 ...
- Kubernetes 学习23 kubernetes资源指标API及自定义指标API
一.概述 1.上集中我们说到,官方文档提示说从k8s 1.11版本开始,将监控体系指标数据获取机制移向新一代的监控模型.也就意味着对于我们的k8s来讲现在应该有这样两种资源指标被使用.一种是资源指标, ...
- k8s之自定义指标API部署prometheus
1.自定义指标-prometheus node_exporter是agent;PromQL相当于sql语句来查询数据; k8s-prometheus-adapter:prometheus是不能直接解析 ...
- k8s系列---资源指标API及自定义指标API
不得不说千万不要随意更改版本,我用的1.13的版本,然后学到这一步时,还因yaml文件不同,卡住了很久,然后各种google才找到解决办法 https://www.linuxea.com/2112. ...
- 简单4步,利用Prometheus Operator实现自定义指标监控
本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...
- Prometheus自定义指标
1. 自定义指标 为了注册自定义指标,请将MeterRegistry注入到组件中,例如: public class Dictionary { private final List<String ...
- Kubernetes 监控:Prometheus Adpater =》自定义指标扩缩容
使用 Kubernetes 进行容器编排的主要优点之一是,它可以非常轻松地对我们的应用程序进行水平扩展.Pod 水平自动缩放(HPA)可以根据 CPU 和内存使用量来扩展应用,前面讲解的 HPA 章节 ...
随机推荐
- 51 nod 1243 排船的问题
1243 排船的问题http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1243 题目来源: Codility 基准时间限制:1 ...
- stl空间配置器简介
1. 符合STL标准的空间配器接口 STL是c++中使用非常广泛的一个标准库,它包含各种有用的容器.而空间配置器作为STL各种容器的背后的核心,负责容器内部内存的分配和释放.不过空间配置器可以分配的也 ...
- [转载]js 程序执行与顺序实现详解
http://www.jb51.net/article/36755.htm JavaScript是一种描述型脚本语言,由浏览器进行动态的解析与执行,浏览器对于不同的方式有不同的解析顺序,详细介绍如下, ...
- Python练习-内置函数的应用
说真的,我感觉这几天egon没有睡好,或者是egon心里有事儿,练习给留的太过简单了 # 编辑者:闫龙 # 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb #name=['al ...
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- 编写shell脚本一键启动zookeeper集群!!
踩了一个多小时坑终于解决了: 这里分享给大家,更主要的目的是记住这些坑,避免以后重复走!!! 首先,这里采用ssh秘钥方式进行集群主机之间免密登录执行启动命令 这里简单说下原理: 通过ssh去另外一台 ...
- 基于Netty4.1.29.Final的helloworld实现.使用idea
服务端: //服务端 public class Server { public static void main(String[] args) { //创建两个线程组 EventLoopGroup c ...
- Linux USB驱动框架分析(2)【转】
转自:http://blog.chinaunix.net/uid-23046336-id-3243543.html 看了http://blog.chinaunix.net/uid-11848011 ...
- Unity3D Instantiate慢的问题
1.NGUI直接打开界面卡 2.角色放技能的时候卡 3.载入模型的时候卡 http://www.xuanyusong.com/archives/2925
- 使用html+css+js实现3D相册
使用html+css+js实现3D相册,快来上传的照片吧 效果图: 代码如下,复制即可用: <!DOCTYPE html> <html lang="en"> ...