获取近5根K线最高值时报integer变量无法赋值series[float]如何解决
报错原因
你遇到的是Pine Script的类型不匹配问题:
Variable 'target' was declared with 'integer' type. Cannot assign it expression of type 'series[float]'.
- 你声明
target变量时用的初始值是整数0,系统会自动把target的类型判定为整数(integer) - 而
highest(high,5)函数返回的是浮点型序列(series[float]),两种类型不兼容,无法直接赋值
修复方案
根据你的使用场景选择对应写法即可:
场景1:仅需实时获取最新5根K线最高值(最常用)
不需要额外声明,直接调用函数即可,返回值就是当前K线对应的最近5根最高值:
target = highest(high, 5)
场景2:需要用var关键字持久化存储变量
显式指定变量类型为浮点型,初始值设为浮点格式的0即可:
var float target = 0.0 target := highest(high, 5)
补充说明
highest(high, 5)默认返回当前bar对应的值,不需要额外加[0]索引,加了也不会报错但属于冗余写法。
内容的提问来源于stack exchange,提问作者tyforcode
相关产品推荐
相关产品推荐

