创建自定义指标


如何显示您的数据作为一个指标

如果您想要在图表上显示一些数据,例如指标,则此处为食用说明。

请遵循以下几个步骤:

  1. 为您的数据创建一个新的ticker,并设置您的服务器返回此ticker有效的SymbolInfo。
  2. 设置服务器以返回此ticker的有效历史数据。
  3. 使用以下指标模板并填写所有占位符(placeholder)的值:名称,说明和代码。 如果需要,还可以修改绘图的默认样式。
  1. {
  2. // 将<study name>替换为您的指标名称
  3. // 它将由图表库内部使用
  4. name: "<study name>",
  5. metainfo: {
  6. "_metainfoVersion": 40,
  7. "id": "<study name>@tv-basicstudies-1",
  8. "scriptIdPart": "",
  9. "name": "<study name>",
  10. // 此说明将显示在指标窗口中
  11. // 当调用createStudy方法时,它也被用作“name”参数
  12. "description": "<study description>",
  13. // 该描述将显示在图表上
  14. "shortDescription": "<short study description>",
  15. "is_hidden_study": true,
  16. "is_price_study": true,
  17. "isCustomIndicator": true,
  18. "plots": [{"id": "plot_0", "type": "line"}],
  19. "defaults": {
  20. "styles": {
  21. "plot_0": {
  22. "linestyle": 0,
  23. "visible": true,
  24. // 绘图线宽度
  25. "linewidth": 2,
  26. // 绘制类型:
  27. // 1 - 直方图
  28. // 2 - 线形图
  29. // 3 - 十字指针
  30. // 4 - 山形图
  31. // 5 - 柱状图
  32. // 6 - 圆圈图
  33. // 7 - 中断线
  34. // 8 - 中断区块
  35. "plottype": 2,
  36. // 显示价格线?
  37. "trackPrice": false,
  38. // 绘制透明度,百分比。
  39. "transparency": 40,
  40. // 以#RRGGBB格式绘制颜色
  41. "color": "#0000FF"
  42. }
  43. },
  44. // 指标输出值的精度
  45. // (小数点后的位数)。
  46. "precision": 2,
  47. "inputs": {}
  48. },
  49. "styles": {
  50. "plot_0": {
  51. // 输出的名字将在样式窗口显示
  52. "title": "-- output name --",
  53. "histogramBase": 0,
  54. }
  55. },
  56. "inputs": [],
  57. },
  58. constructor: function() {
  59. this.init = function(context, inputCallback) {
  60. this._context = context;
  61. this._input = inputCallback;
  62. // 定义要绘制的商品。
  63. // 商品应该是一个字符串。
  64. // 您可以使用PineJS.Std.ticker(this._context)获取所选商品的代码。
  65. // 例,
  66. // var symbol = "AAPL";
  67. // var symbol = "#EQUITY";
  68. // var symbol = PineJS.Std.ticker(this._context) + "#TEST";
  69. var symbol = "<TICKER>";
  70. this._context.new_sym(symbol, PineJS.Std.period(this._context), PineJS.Std.period(this._context));
  71. };
  72. this.main = function(context, inputCallback) {
  73. this._context = context;
  74. this._input = inputCallback;
  75. this._context.select_sym(1);
  76. // 您可以在PineJS.Std对象中使用以下内置函数:
  77. // open, high, low, close
  78. // hl2, hlc3, ohlc4
  79. var v = PineJS.Std.close(this._context);
  80. return [v];
  81. }
  82. }
  83. }
  1. custom_indicators_getter添加到widget构造函数中。它的值是一个方法,返回带有自定义指标列表的Promise对象。

    1. {
    2. custom_indicators_getter: function(PineJS) {
    3. return Promise.resolve([
    4. // *** 您的指标对象,通过模板创建 ***
    5. ]);
    6. },
    7. }

    1.更新widget的初始化代码,以便在图表准备就绪时创建此指标。

  1. 使用custom_indicators_getter选项,将指标添加到图表库。
  2. 更改widget的初始化代码。下面是一个例子。

    1. widget = new TradingView.widget(/* ... */);
    2. widget.onChartReady(function() {
    3. widget.chart().createStudy('<indicator-name>', false, true);
    4. });

请求另一个商品代码的数据

假设您希望在图表上显示用户的收益曲线。你必须做以下事情:

  • 为新的代码创建一个名称。 假设它为 #EQUITY 代码。 您可以使用您想像到的任何名字。
  • 更改服务器的代码以将此代码作为有效商品。 为此返回最小的有效SymbolInfo。
  • 使服务器返回有效的历史记录。 即,服务器可以询问您的数据库的股权历史记录,并返回此数据,就像返回普通商品的历史记录一样(例如 AAPL)。
  • 采用上述指标模板,创建指标文件(或向现有指标文件添加新指标)。

例如:

  1. {
  2. name: "Equity",
  3. metainfo: {
  4. "_metainfoVersion": 40,
  5. "id": "Equity@tv-basicstudies-1",
  6. "scriptIdPart": "",
  7. "name": "Equity",
  8. "description": "Equity",
  9. "shortDescription": "Equity",
  10. "is_hidden_study": true,
  11. "is_price_study": true,
  12. "isCustomIndicator": true,
  13. "plots": [{"id": "plot_0", "type": "line"}],
  14. "defaults": {
  15. "styles": {
  16. "plot_0": {
  17. "linestyle": 0,
  18. "visible": true,
  19. // 使线条变细
  20. "linewidth": 1,
  21. // 绘制类型为线性图
  22. "plottype": 2,
  23. // 显示价格线
  24. "trackPrice": true,
  25. "transparency": 40,
  26. // 为图线设置深红色。
  27. "color": "#880000"
  28. }
  29. },
  30. // 精度为小数点后一位数,如777.7
  31. "precision": 1,
  32. "inputs": {}
  33. },
  34. "styles": {
  35. "plot_0": {
  36. // 输出名字在样式窗口显示
  37. "title": "Equity value",
  38. "histogramBase": 0,
  39. }
  40. },
  41. "inputs": [],
  42. },
  43. constructor: function() {
  44. this.init = function(context, inputCallback) {
  45. this._context = context;
  46. this._input = inputCallback;
  47. var symbol = "#EQUITY";
  48. this._context.new_sym(symbol, PineJS.Std.period(this._context));
  49. };
  50. this.main = function(context, inputCallback) {
  51. this._context = context;
  52. this._input = inputCallback;
  53. this._context.select_sym(1);
  54. var v = PineJS.Std.close(this._context);
  55. return [v];
  56. }
  57. }
  58. }

K线变色

  1. __customIndicators = [
  2. {
  3. name: "Bar Colorer Demo",
  4. metainfo: {
  5. _metainfoVersion: 42,
  6. id: "BarColoring@tv-basicstudies-1",
  7. name: "BarColoring",
  8. description: "Bar Colorer Demo",
  9. shortDescription: "BarColoring",
  10. scriptIdPart: "",
  11. is_price_study: true,
  12. is_hidden_study: false,
  13. isCustomIndicator: true,
  14. isTVScript: false,
  15. isTVScriptStub: false,
  16. defaults: {
  17. precision: 4,
  18. palettes: {
  19. palette_0: {
  20. // 调色板颜色
  21. // 将其更改为您喜欢的默认颜色,
  22. // 但请注意,用户可以在“样式”选项卡中更改它们
  23. // 指标属性
  24. colors: [
  25. { color: "#FFFF00" },
  26. { color: "#0000FF" }
  27. ]
  28. }
  29. }
  30. },
  31. inputs: [],
  32. plots: [{
  33. id: "plot_0",
  34. // plot类型应设置为 'bar_colorer'
  35. type: "bar_colorer",
  36. // 这是定义的调色板的名称
  37. // 在 'palettes' 和 'defaults.palettes' 部分
  38. palette: "palette_0"
  39. }],
  40. palettes: {
  41. palette_0: {
  42. colors: [
  43. { name: "Color 0" },
  44. { name: "Color 1" }
  45. ],
  46. // 值之间的映射
  47. // 返回脚本和调色板颜色
  48. valToIndex: {
  49. 100: 0,
  50. 200: 1
  51. }
  52. }
  53. }
  54. },
  55. constructor: function() {
  56. this.main = function(context, input) {
  57. this._context = context;
  58. this._input = input;
  59. var valueForColor0 = 100;
  60. var valueForColor1 = 200;
  61. // 在这里执行计算并返回其中一个常量
  62. // 在'valToIndex'映射中指定为键
  63. var result =
  64. Math.random() * 100 % 2 > 1 ? // 我们随机选择一个颜色值
  65. valueForColor0 : valueForColor1;
  66. return [result];
  67. }
  68. }
  69. }
  70. ];