TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS(customIndex.js) 源文件,放在图表库 static 文件夹下。自定义指标 JS 源代码官网模板如下:
{// 将<study name>替换为您的指标名称// 它将由图表库内部使用name: "<study name>",metainfo: {"_metainfoVersion": 40,"id": "<study name>@tv-basicstudies-1","scriptIdPart": "","name": "<study name>",// 此说明将显示在指标窗口中// 当调用createStudy方法时,它也被用作“name”参数"description": "<study description>",// 该描述将显示在图表上"shortDescription": "<short study description>","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": 2,// 绘制类型:// 1 - 直方图// 2 - 线形图// 3 - 十字指针// 4 - 山形图// 5 - 柱状图// 6 - 圆圈图// 7 - 中断线// 8 - 中断区块"plottype": 2,// 显示价格线?"trackPrice": false,// 绘制透明度,百分比。"transparency": 40,// 以#RRGGBB格式绘制颜色"color": "#0000FF"}},// 指标输出值的精度// (小数点后的位数)。"precision": 2,"inputs": {}},"styles": {"plot_0": {// 输出的名字将在样式窗口显示"title": "-- output name --","histogramBase": 0,}},"inputs": [],},constructor: function() {this.init = function(context, inputCallback) {this._context = context;this._input = inputCallback;// 定义要绘制的商品。// 商品应该是一个字符串。// 您可以使用PineJS.Std.ticker(this._context)获取所选商品的代码。// 例,// var symbol = "AAPL";// var symbol = "#EQUITY";// var symbol = PineJS.Std.ticker(this._context) + "#TEST";var symbol = "<TICKER>";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);// 您可以在PineJS.Std对象中使用以下内置函数:// open, high, low, close// hl2, hlc3, ohlc4var v = PineJS.Std.close(this._context);return [v];}}
}
1.将指标保存到具有以下结构的自定义指标文件中:
__customIndicators = [*** 您的指标对象,由模板创建 ()***
];
请注意,该指标文件是一个JavaScript源文件,它定义了一个指标对象数组。因此,您可以在其中放置多个指标,或者将它们与我们为您编译的指标组合起来。
2.在 new TradingView.widget() 中使用 indicators_file_name: 'customIndex.js'引入
widget = new TradingView.widget({***indicators_file_name:'customIndex.js', /**自定义指标文件 */***
});
完整代码(自定义MACD红绿)
// 自定义指标文件,可以写多个指标
__customIndicators = [{name: "自定义(MACD)",metainfo: {"_metainfoVersion": 40,"id": "macd-custom@tv-basicstudies-1","scriptIdPart": "","name": "自定义(MACD)","description": "自定义(MACD)","shortDescription": "MACD_CUSTOM","is_price_study": false,"isCustomIndicator": true,defaults: {styles: {plot_0: {linestyle: 0,linewidth: 4,//红色plottype: 1,trackPrice: !1,transparency: 35,visible: !0,color: "#da1155"},plot_1: {linestyle: 0,linewidth: 1,plottype: 0,trackPrice: !1,transparency: 0,visible: !0,color: "#FFFFFF"},plot_2: {linestyle: 0,linewidth: 1,plottype: 0,trackPrice: !1,transparency: 0,visible: !0,color: "#FFFF00"},plot_3: {linestyle: 0,linewidth: 4,//绿色plottype: 1,trackPrice: !1,transparency: 35,visible: !0,color: "#33FF33"}},precision: 2, /**保留小数点 */inputs: {in_0: 12,in_1: 26,in_3: "close",in_2: 9}},plots: [{id: "plot_0",type: "line"},{id: "plot_1",type: "line"},{id: "plot_2",type: "line"},{id: "plot_3",type: "line"}],styles: {plot_0: {title: "Histogram",histogramBase: 0,joinPoints: !1},plot_1: {title: "MACD",histogramBase: 0,joinPoints: !1},plot_2: {title: "Signal",histogramBase: 0,joinPoints: !1},plot_3: {title: "Histogram",histogramBase: 0,joinPoints: !1}},inputs: [{id: "in_0",name: "fastLength",defval: 12,type: "integer",min: 1,max: 2e3,},{id: "in_1",name: "slowLength",defval: 26,type: "integer",min: 1,max: 2e3},{id: "in_3",name: "Source",defval: "close",type: "source",options: "open high low close hl2 hlc3 ohlc4".split(" ")},{id: "in_2",name: "signalLength",defval: 9,type: "integer",min: 1,max: 50}],id: "Moving Average Convergence Custom/Divergence@tv-basicstudies-1",scriptIdPart: "",name: "MACDCUSTOM"},constructor: function() {this.f_0 = function(a, b) {return a - b},this.main = function(a, b) {var c, k, f, n, r, l, u, p, q, e, g, v, i;this._context = a;this._input = b;c = PineJS.Std[this._input(2)](this._context);k = this._input(0);f = this._input(1);n = this._input(3);r = this._context.new_var(c);l = PineJS.Std.ema(r, k, this._context);u = this._context.new_var(c);p = PineJS.Std.ema(u, f, this._context);q = this.f_0(l, p);e = this._context.new_var(q);g = PineJS.Std.sma(e, n, this._context);v = this.f_0(q, g);if (v >= 0) {i = 0;} else {i = v;v = 0}return [v, q, g, i];}}}
];
结果显示效果跟自带MACD一样
具体内容可添加QQ(1765156144)备注来意