You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PineScript中收集测量工具的多空仓位数据并以表格展示?

在PineScript中收集并展示多空仓位数据(模拟表格)

PineScript没有原生的表格渲染功能,但可以通过数据存储+自定义可视化/日志输出的方式实现类似需求,以下是具体实现方案:

1. 用数组存储仓位数据

首先通过var定义持久化数组,用来记录预测/测量工具触发的多空仓位关键信息(如入场价、方向、时间、目标价等):

//@version=5
indicator("仓位数据收集", overlay=true)

// 定义存储仓位数据的数组
var string[] longData = array.new_string()
var string[] shortData = array.new_string()

// 模拟预测工具触发信号(示例:RSI超买超卖触发)
rsiValue = ta.rsi(close, 14)
longCondition = rsiValue < 30
shortCondition = rsiValue > 70

// 记录多头仓位数据
if (longCondition)
    entryInfo = str.tostring(bar_index) + " | 多头 | 入场价: " + str.tostring(close) + " | 时间: " + str.tostring(time, "yyyy-MM-dd HH:mm")
    array.push(longData, entryInfo)

// 记录空头仓位数据
if (shortCondition)
    entryInfo = str.tostring(bar_index) + " | 空头 | 入场价: " + str.tostring(close) + " | 时间: " + str.tostring(time, "yyyy-MM-dd HH:mm")
    array.push(shortData, entryInfo)

2. 用自定义标签模拟表格展示

通过label.new在图表固定位置生成文本块,拼接数组中的数据形成表格样式:

// 在图表左上角绘制多头数据表格
if (barstate.islast)
    // 表头
    label.new(bar_index, high * 1.05, "=== 多头仓位记录 ===", color=color.new(color.green, 0), textcolor=color.white, style=label.style_label_left, size=size.small)
    // 遍历数组填充内容
    for i = 0 to array.size(longData) - 1
        label.new(bar_index, high * 1.05 - (i+1)*0.01*ta.tr, array.get(longData, i), color=color.new(color.green, 90), textcolor=color.black, style=label.style_label_left, size=size.small)

    // 在图表右上角绘制空头数据表格
    label.new(bar_index, high * 1.05, "=== 空头仓位记录 ===", color=color.new(color.red, 0), textcolor=color.white, style=label.style_label_right, size=size.small)
    for i = 0 to array.size(shortData) - 1
        label.new(bar_index, high * 1.05 - (i+1)*0.01*ta.tr, array.get(shortData, i), color=color.new(color.red, 90), textcolor=color.black, style=label.style_label_right, size=size.small)

3. 输出到控制台查看结构化数据

如果不需要在图表上展示,可通过print将数据输出到PineScript控制台:

if (barstate.islast)
    print("=== 多头仓位数据 ===")
    for i = 0 to array.size(longData) - 1
        print(array.get(longData, i))
    print("\n=== 空头仓位数据 ===")
    for i = 0 to array.size(shortData) - 1
        print(array.get(shortData, i))

4. 利用策略测试器报表(策略脚本适用)

如果是策略类型脚本,可通过strategy.entry/strategy.close记录仓位,然后在TradingView的策略测试器 → List of Trades中查看自带的表格化交易记录,包含入场/出场价、盈利、时间等完整信息。

内容的提问来源于stack exchange,提问作者ImAnGuey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 12:40:50