如何用Pine Script获取标的IPO日期及指定日期开盘价?
在TradingView中获取标的IPO日期及指定日期开盘价的方法
背景说明
我需要获取NVDA的IPO日期(TradingView显示为1999-01-22),以及NVDA和SPY在当日的open价格(分别为0.0438和122.13)。注意:查看时open价格可能因拆股或分红已被调整。同时要注意TradingView免费用户仅能获取5000根K线,图表首根K线可能并非标的历史首条数据。
问题解答
1. 获取标的的IPO日期
由于免费用户存在5000根K线的限制,直接通过图表首根K线判断IPO日期可能不准确,推荐两种可行方法:
- 方法一:基本面信息查询
直接进入标的详情页的「基本面」板块,这里会显示标的准确的IPO日期,该信息不受K线数量限制,是最可靠的方式。 - 方法二:Pine Script遍历记录(限可加载K线范围内)
如果需要通过脚本获取图表加载的最早K线日期(仅当标的历史K线≤5000根时等于IPO日期),可以用以下代码://@version=5 indicator("记录最早K线日期", overlay=true) var earliest_date = timestamp("1970-01-01") if bar_index == 0 earliest_date := time if barstate.islast label.new(bar_index, high, str.tostring(earliest_date, "yyyy-MM-dd"), color=color.blue)
2. 获取标的在指定日期的open价格
可以通过Pine Script编写脚本匹配目标日期,提取对应开盘价,支持跨标的查询:
- 单标的指定日期开盘价查询
//@version=5 indicator("获取指定日期开盘价", overlay=true) target_date = timestamp("1999-01-22") var float target_open = na if time == target_date target_open := open if barstate.islast label.new(bar_index, high, str.tostring(target_open), color=color.red) - 跨标的同时查询(如NVDA和SPY)
使用request.security函数调用其他标的数据,注意目标日期需在该标的可加载的K线范围内:
注意:如果目标日期超出免费用户可加载的K线范围,脚本会返回//@version=5 indicator("跨标的指定日期开盘价", overlay=true) target_date = timestamp("1999-01-22") nvda_open = request.security("NASDAQ:NVDA", "D", open[time == target_date ? 0 : na]) spy_open = request.security("NYSEARCA:SPY", "D", open[time == target_date ? 0 : na]) if barstate.islast label.new(bar_index, high, "NVDA开盘价: " + str.tostring(nvda_open) + "\nSPY开盘价: " + str.tostring(spy_open), color=color.green)na,此时需确认日期有效性或升级账户获取更多K线权限。
内容的提问来源于stack exchange,提问作者Xaree Lee
相关产品推荐
相关产品推荐

