You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script调用timestamp报错series string需const string的解决办法

Pine Script v5 timestamp函数传入动态日期报错解决方案

报错根因

你首先要明确类型转换的可行性边界:

  • const string是脚本编译阶段就完全固定的常量字符串,不能在运行时动态生成
  • 你通过ta.valuewhen计算得到的date_bluex5是每根K线都会更新的series string(序列字符串),二者本质不同,不存在互相转换的方法
    此外你的原有代码存在低级逻辑错误:你把year()的结果赋值给了dax5、dayofmonth()的结果赋值给了yrx5,拼接出来的日期格式本身就是错误的。

解决方法

直接使用timestamp函数支持序列参数的重载版本,不需要拼接字符串:
timestamp(year, month, day, hour, minute, second)
这个重载允许传入series int类型的时间参数,完美适配你用ta.valuewhen得到的动态时间值。

修正后的完整代码

//@version=5
indicator("My Script")
box_plot_found_blue=ta.crossover(close,ta.sma(close,200))
// 修正年月日赋值对应关系
int year_x5 = ta.valuewhen(box_plot_found_blue, year(time), 5)
int month_x5 = ta.valuewhen(box_plot_found_blue, month(time), 5)
int day_x5 = ta.valuewhen(box_plot_found_blue, dayofmonth(time), 5)
// 直接传年月日参数调用timestamp,时分秒传0即可
targetTime = timestamp(year_x5, month_x5, day_x5, 0, 0, 0)
bgcolor(targetTime == time ? color.silver : na)

内容的提问来源于stack exchange,提问作者TJalam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:27:07