Pine Script新手求助:如何获取昨日数据并实现突破预警?
问题描述
我是Pine Script及编程新手,想要编写一个简单指标,但难度超出预期。我尝试用request.security()同时获取昨日数据和当前timeframe数据,但未能成功。我希望绘制昨日高低点的水平线,并在这些线被突破时用plotshape标记。
错误代码
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © roddysweg //@version=5 indicator("My Test allert1", overlay = true) [d_high, d_low] = request.security(syminfo.tickerid,"D",[high[0],low[0]]) newDay = ta.change(time('D')) var line LastHigh = na var line LastLow = na if newDay LastHigh := line.new(bar_index, d_high, bar_index+1, d_high) LastLow := line.new(bar_index, d_low, bar_index+1, d_low) LastHigh.set_x2(bar_index) LastLow.set_x2(bar_index) [chart_high, chart_low, chart_open, chart_close] = request.security(syminfo.tickerid, timeframe.period, [high, low, open, close]) Cond1 = chart_close > LastHigh Cond2 = chart_open <= LastLow BreakLine1 = Cond1 and Cond2 plotshape(BreakLine1, 'Break', shape.arrowup, location.abovebar, color.green, 0, 'Breaking\nLine', textcolor = color.green)
报错信息
if语句的condition参数需要布尔值,请传入布尔类型的值或表达式,避免意外结果。- 无法对参数'expr1'='LastLow'使用运算符'<=': 传入了'字符串字面量'类型,但需要'simple float'类型。
解决方案及修正代码
错误原因分析
- 布尔条件错误:
ta.change(time('D'))返回的是时间变化的差值(数值类型),而if语句需要布尔值作为判断条件,需改为判断该值是否非零,或直接使用ta.newbar('D')检测新交易日的开始。 - 对象与数值混淆:
LastHigh和LastLow是line绘图对象,不能直接与价格数值比较,需通过line.get_y1()方法获取线对应的价格值。 - 昨日数据获取错误:原代码中
request.security取的是当日日K的高低点,要获取昨日数据需改为high[1]和low[1]。 - 冗余数据请求:当前时间框架的
high/low/open/close无需通过request.security获取,直接使用全局变量即可。
修正后的代码
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © roddysweg //@version=5 indicator("昨日高低点突破标记", overlay = true) // 获取昨日日K的高低点 [yesterday_high, yesterday_low] = request.security(syminfo.tickerid, "D", [high[1], low[1]]) // 检测新交易日(返回布尔值) is_new_day = ta.newbar("D") // 初始化线对象 var line last_high_line = na var line last_low_line = na // 新交易日开始时,更新昨日高低点水平线 if is_new_day // 删除旧线避免画面混乱 if not na(last_high_line) line.delete(last_high_line) if not na(last_low_line) line.delete(last_low_line) // 创建新线,x1设为新交易日第一根K线,x2后续动态延伸 last_high_line := line.new(bar_index, yesterday_high, bar_index, yesterday_high, color=color.red, width=2) last_low_line := line.new(bar_index, yesterday_low, bar_index, yesterday_low, color=color.green, width=2) // 动态更新线的终点到当前K线 if not na(last_high_line) line.set_x2(last_high_line, bar_index) if not na(last_low_line) line.set_x2(last_low_line, bar_index) // 获取线对应的价格值用于突破判断 current_high_level = line.get_y1(last_high_line) current_low_level = line.get_y1(last_low_line) // 定义突破条件:收盘价突破昨日高点,且开盘价低于等于昨日低点 break_condition = close > current_high_level and open <= current_low_level // 绘制突破标记 plotshape(break_condition, title="突破标记", shape=shape.arrowup, location=location.abovebar, color=color.green, size=size.small, text="突破", textcolor=color.white)
代码说明
- 用
ta.newbar("D")准确检测新交易日开始,返回的布尔值符合if条件要求。 - 通过
request.security的high[1]和low[1]获取昨日日K的高低点数据。 - 新交易日自动删除旧线并创建新线,避免画面残留过多无效线条。
- 用
line.get_y1()提取线的价格数值,实现与当前K线价格的突破逻辑判断。 - 直接使用全局
open/close变量,省去冗余的当前时间框架数据请求。
内容的提问来源于stack exchange,提问作者Roddy
相关产品推荐
相关产品推荐

