如何在TradingView图表导入并绘制交易历史?求PineScript示例
在TradingView上绘制自定义CSV交易点位的PineScript实现
可行性说明
完全可以实现。你碰到的编译错误大多是因为直接用非常量时间值做条件判断,或是没把CSV里的时间和图表K线时间正确匹配。PineScript支持通过time函数匹配自定义时间点,再用绘图函数标记订单点位。
CSV数据格式解析
你的CSV包含两类记录:
e类型:权益记录,格式为e;YYYY-MM-DD HH:MM:SS;权益数值p类型:交易订单,格式为p;YYYY-MM-DD HH:MM:SS;买卖方向(b/s);成交价格;订单编号;交易数量
PineScript v5 代码示例
//@version=5 indicator("自定义交易订单标记", overlay=true) // 初始化订单信息表格 var table ordersTable = table.new(position.top_right, 5, 0) var int orderIndex = 0 // -------------------------- // 替换为你的CSV交易数据 // 格式:"时间字符串,买卖方向,价格,订单号,数量" // -------------------------- tradeRecords = array.new<string>(0) array.push(tradeRecords, "2023-01-03 10:18:00,b,32.86,1,100") array.push(tradeRecords, "2023-01-03 12:24:00,b,32.51,2,100") // -------------------------- // 替换为你的CSV权益数据(可选) // 格式:"时间字符串,权益数值" // -------------------------- equityRecords = array.new<string>(0) array.push(equityRecords, "2023-01-03 09:59:00,16000.00") array.push(equityRecords, "2023-01-03 10:59:00,16014.75") array.push(equityRecords, "2023-01-03 11:59:00,15978.50") array.push(equityRecords, "2023-01-03 12:59:00,15946.50") // 处理交易订单,绘制标记 for i = 0 to array.size(tradeRecords) - 1 record = array.get(tradeRecords, i) splitData = str.split(record, ",") timeStr = splitData[0] direction = splitData[1] price = float.parse(splitData[2]) orderId = splitData[3] qty = int.parse(splitData[4]) // 将时间字符串转为UTC时间戳,匹配图表K线时间 tradeTimestamp = timestamp("UTC", str.extract(timeStr, 0, 4), // 年 str.extract(timeStr, 5, 7), // 月 str.extract(timeStr, 8, 10), // 日 str.extract(timeStr, 11, 13),// 时 str.extract(timeStr, 14, 16),// 分 0) // 秒 if time == tradeTimestamp // 绘制买入标记 if direction == "b" label.new(bar_index, price, "买入\n订单号: " + orderId + "\n数量: " + str.tostring(qty), color=color.green, textcolor=color.white, style=label.style_label_up, size=size.small) // 绘制卖出标记 else if direction == "s" label.new(bar_index, price, "卖出\n订单号: " + orderId + "\n数量: " + str.tostring(qty), color=color.red, textcolor=color.white, style=label.style_label_down, size=size.small) // 将订单信息写入表格 table.cell(ordersTable, 0, orderIndex, timeStr) table.cell(ordersTable, 1, orderIndex, direction == "b" ? "买入" : "卖出") table.cell(ordersTable, 2, orderIndex, str.tostring(price)) table.cell(ordersTable, 3, orderIndex, orderId) table.cell(ordersTable, 4, orderIndex, str.tostring(qty)) orderIndex += 1 // 处理权益记录(可选功能) showEquity = input.bool(true, "显示权益点位") var float[] equityValues = array.new<float>() var int[] equityTimestamps = array.new<int>() if showEquity for i = 0 to array.size(equityRecords) - 1 record = array.get(equityRecords, i) splitData = str.split(record, ",") eqTimeStr = splitData[0] eqValue = float.parse(splitData[1]) eqTimestamp = timestamp("UTC", str.extract(eqTimeStr, 0, 4), str.extract(eqTimeStr, 5, 7), str.extract(eqTimeStr, 8, 10), str.extract(eqTimeStr, 11, 13), str.extract(eqTimeStr, 14, 16), 0) if time == eqTimestamp array.push(equityValues, eqValue) array.push(equityTimestamps, bar_index) // 绘制权益折线(仅在数据窗口显示) plot(array.size(equityValues) > 0 ? array.get(equityValues, array.size(equityValues)-1) : na, color=color.blue, title="权益值", display=display.data_window) // 绘制权益标记点 for i = 0 to array.size(equityTimestamps) - 1 label.new(array.get(equityTimestamps, i), array.get(equityValues, i), str.tostring(array.get(equityValues, i)), color=color.new(color.blue, 80), textcolor=color.black, style=label.style_label_left, size=size.tiny)
代码使用说明
- 数据替换:把代码中
tradeRecords和equityRecords数组里的内容,替换成你CSV文件中的对应数据即可。 - 时区适配:如果你的CSV时间不是UTC,修改
timestamp函数的第一个参数(比如改成"GMT+8"对应北京时间)。 - 标记样式:可以调整
label.new的color、style、size参数,自定义标记的外观。
常见编译错误解决
- 常量相关错误:避免在全局条件判断中使用动态生成的时间值,必须通过循环+时间戳匹配的方式处理,这是PineScript的语法要求。
- 时间不匹配:检查CSV时间格式是否正确,确保转换后的时间戳和图表K线的
time值完全一致(注意时区差异)。
内容的提问来源于stack exchange,提问作者Quant
相关产品推荐
相关产品推荐

