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

Pine Script中动态字符串转时间戳遇常量要求的解决办法咨询

解决Pine Script动态日期时间转换为时间戳的问题

在Pine Script v5中,timestamp()函数的字符串参数版本仅支持常量字符串,无法直接使用动态输入的字符串。你可以通过以下两种方法解决:

方法1:直接使用input.time(推荐)

放弃手动输入日期时间字符串,改用input.time控件直接获取时间戳,这是最简洁的方案:

//@version=5
indicator("Line Date")
// 直接通过时间选择器获取目标时间戳
t1 = input.time(timestamp("2024-06-13T17:25:14"), "Target Time")

// 绘制线条
line.new(t1, close, time, close, xloc.bar_time, extend.none, color.rgb(255,255,0))

方法2:拆分字符串为数值参数

如果必须保留字符串输入,需要将日期、时间字符串拆分为年、月、日、时、分、秒的数值,再调用timestamp()的数值参数版本(该版本支持动态值):

//@version=5
indicator("Line Date")
date1 = input.string("2024-06-13", "Date")
time1 = input.string("17:25:14", "Time")

// 拆分日期字符串为年、月、日数值
date_parts = str.split(date1, "-")
year = str.toint(date_parts[0])
month = str.toint(date_parts[1])
day = str.toint(date_parts[2])

// 拆分时间字符串为时、分、秒数值
time_parts = str.split(time1, ":")
hour = str.toint(time_parts[0])
minute = str.toint(time_parts[1])
second = str.toint(time_parts[2])

// 使用数值参数生成动态时间戳
t1 = timestamp(year, month, day, hour, minute, second)

// 绘制线条
line.new(t1, close, time, close, xloc.bar_time, extend.none, color.rgb(255,255,0))

补充说明

  • 你之前误用的str.format_time()函数作用是将时间戳转换为格式化字符串,和你的需求完全相反,因此无法生效。
  • timestamp(year, month, day, hour, minute, second)是支持动态数值输入的重载版本,完全适配你的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 20:12:35