1分钟图表下如何用单个request.security获取1440分钟区间指定收盘价?
解决Pine Script单次获取1分钟序列数据的问题
你当前代码的核心错误是:request.security(symbol_name, "1440", close)请求的是日K线(1440分钟)的收盘价序列,所以last_1440m_price[360]取的是360个交易日之前的收盘价,完全不符合你要的「1分钟框架下360分钟前的价格」需求。
修正方案(两种实现方式)
方式1:用数组批量存储1分钟价格(灵活扩展)
这种方式适合需要提取多个时间点价格的场景,数组会自动维护最近1440个1分钟bar的收盘价:
//@version=5 indicator("FREC", overlay=true) // 初始化数组,存储最近1440个1分钟收盘价(var确保只初始化一次) var float[] min_prices = array.new_float(1440) // 单次请求1分钟时间框架数据,更新数组 request.security(syminfo.tickerid, "1", array.shift(min_prices) array.push(min_prices, close) ) // 提取目标时间点价格:0=最新,360=6小时前,720=12小时前 current_price = array.get(min_prices, 0) price_6h_ago = array.get(min_prices, 360) price_12h_ago = array.get(min_prices, 720) // 创建展示表格 my_table = table.new(position.bottom_center, columns=5, rows=5, bgcolor=color.white, frame_color=color.black, border_color=color.red, frame_width=20, border_width=20) table.cell_set_text_color(my_table, 0, 0, color.black) table.cell_set_text_color(my_table, 1, 0, color.black) table.cell_set_text_color(my_table, 2, 0, color.black) table.cell_set_text(my_table, 0, 0, str.tostring(current_price, "#.##")) table.cell_set_text(my_table, 1, 0, str.tostring(price_6h_ago, "#.##")) table.cell_set_text(my_table, 2, 0, str.tostring(price_12h_ago, "#.##"))
方式2:直接返回目标价格(简洁高效)
如果只需要固定三个时间点的价格,可以在request.security中直接返回目标值,单次调用完成获取:
//@version=5 indicator("FREC", overlay=true) // 单次请求1分钟数据,同时获取三个目标价格 [cur_p, p6h, p12h] = request.security(syminfo.tickerid, "1", [close, close[360], close[720]] ) // 创建展示表格 my_table = table.new(position.bottom_center, columns=5, rows=5, bgcolor=color.white, frame_color=color.black, border_color=color.red, frame_width=20, border_width=20) table.cell_set_text_color(my_table, 0, 0, color.black) table.cell_set_text_color(my_table, 1, 0, color.black) table.cell_set_text_color(my_table, 2, 0, color.black) table.cell_set_text(my_table, 0, 0, str.tostring(cur_p, "#.##")) table.cell_set_text(my_table, 1, 0, str.tostring(p6h, "#.##")) table.cell_set_text(my_table, 2, 0, str.tostring(p12h, "#.##"))
关键说明
- 两种方式都请求的是**1分钟时间框架("1")**的数据,而非日K,确保拿到的是分钟级序列
- 方式1通过数组维护最近1440个值,可随时提取任意位置的价格;方式2直接返回目标值,代码更精简
内容的提问来源于stack exchange,提问作者ok1
相关产品推荐
相关产品推荐

