Pine Script技术问询:调用FRVP指标与获取历史5分钟周期数据
Pine Script 相关问题解答
一、Fixed Range Volume Profile(FRVP)的实现方式
Pine Script v5 没有内置的FRVP直接调用函数,需要自行编写核心逻辑:
- 核心思路:先指定目标时间/价格范围,遍历该范围内的K线,统计每个价格水平的累计成交量,最终输出成交量分布。
- 简化代码示例:
//@version=5 indicator("自定义FRVP", overlay=false) // 定义时间范围(示例:最近20根日线覆盖的区间) startBar = ta.barssince(time >= timestamp(year, month, day - 20, 0, 0)) endBar = 0 // 存储价格水平与对应成交量 var float[] priceLevels = array.new_float() var float[] volProfiles = array.new_float() if barstate.islast // 遍历目标区间内的所有K线 for i = startBar to endBar currentPrice = close[i] currentVol = volume[i] // 查找当前价格是否已在统计列表中 idx = array.indexof(priceLevels, currentPrice) if idx != -1 // 累加成交量 array.set(volProfiles, idx, array.get(volProfiles, idx) + currentVol) else // 添加新的价格水平及初始成交量 array.push(priceLevels, currentPrice) array.push(volProfiles, currentVol) // 绘制成交量分布标签(简单展示) for i = 0 to array.size(priceLevels) - 1 label.new(bar_index, array.get(priceLevels, i), str.tostring(array.get(volProfiles, i)), color=color.blue, textcolor=color.white)
二、获取过去n天的特定5分钟周期数据
可以结合request.security和时间条件筛选实现,步骤如下:
- 在5分钟周期上下文里,筛选出符合指定时间的K线;
- 提取这些K线的数据,并限制在过去n天范围内。
- 代码示例(以获取过去9天每天9:35的5分钟收盘价为例):
//@version=5 indicator("获取特定5分钟周期数据", overlay=true) // 可配置参数 nDays = input.int(9, "过去天数") targetHour = input.int(9, "目标小时") targetMinute = input.int(35, "目标分钟") // 5分钟周期内的目标Bar筛选逻辑 f_getTargetData() => // 判断当前5分钟K线是否是目标时间点 isTargetBar = hour == targetHour and minute == targetMinute // 仅在目标Bar返回收盘价,其余返回空值 isTargetBar ? close : na // 收集过去n天的目标数据 var float[] pastTargetData = array.new_float() if barstate.islast for i = 1 to nDays // 计算目标日期的时间范围 targetDateStart = timestamp(year, month, day - i, 0, 0) targetDateEnd = timestamp(year, month, day - i + 1, 0, 0) // 调用5分钟数据,获取目标时间点的收盘价 targetClose = request.security(syminfo.tickerid, "5", f_getTargetData(), lookahead=barmerge.lookahead_on, gaps=barmerge.gaps_on) // 过滤空值,添加到数据数组 if not na(targetClose) array.push(pastTargetData, targetClose) // 输出结果展示 if barstate.islast label.new(bar_index, high, "过去9天9:35的5分钟收盘价:\n" + str.tostring(array.join(pastTargetData, ", ")), color=color.green, textcolor=color.white)
内容的提问来源于stack exchange,提问作者Hoghul
相关产品推荐
相关产品推荐

