数据序列

内置价格数据、时间序列、K线状态和标的信息

PineTS 提供内置数据序列,无需导入即可在每个指标中使用。这些数据代表当前标的的 OHLCV 数据、时间信息和 K线状态。

价格数据 (OHLCV)

序列类型描述
openseries<number>每根K线的开盘价
highseries<number>每根K线的最高价
lowseries<number>每根K线的最低价
closeseries<number>每根K线的收盘价
volumeseries<number>每根K线的成交量

计算价格序列

序列公式描述
hl2(high + low) / 2中间价
hlc3(high + low + close) / 3典型价格
ohlc4(open + high + low + close) / 4平均价格
// Use any price series in calculations
const typicalPrice = hlc3;
const sma = ta.sma(typicalPrice, 20);
plot(sma, 'SMA of Typical Price');

时间序列

序列类型描述
openTimeseries<number>K线开盘时间(毫秒)
closeTimeseries<number>K线收盘时间(毫秒)

K线信息

变量类型描述
bar_indexseries<number>当前K线的索引(从0开始)
last_bar_indexseries<number>数据集中最后一根K线的索引
last_bar_timeseries<number>最后一根K线的时间戳
// Only plot on the last 100 bars
if (bar_index > last_bar_index - 100) {
  plot(close, 'Recent Close');
}

K线状态

barstate 对象提供关于当前K线在数据集中位置的标志:

属性类型描述
barstate.isfirstboolean在首根K线上为 true
barstate.islastboolean在最后一根K线上为 true
barstate.ishistoryboolean对于历史K线为 true
barstate.isrealtimeboolean对于实时/流式K线为 true
barstate.isnewboolean在新K线的第一个tick上为 true
barstate.isconfirmedboolean当K线确认(收盘)时为 true
barstate.islastconfirmedhistoryboolean在最后一根已确认的历史K线上为 true

品种信息

Studio 使用提供的 OHLCV K 线运行 PineTS,不会填充 syminfo。访问 syminfo.mintick 或 syminfo.ticker 等属性会报错。对于最小计算增量等所需的品种专用值,请使用显式指标输入。

特殊值

值描述
na表示“不可用” — 类似于 null。用于检查值是否存在
nz(value, replacement?)如果 value 为 na,则返回 replacement(默认 0)
const prev = close[1];
// na check
if (!na(prev)) {
  const change = close - prev;
  plot(change, 'Change');
}

// nz shorthand — default to 0 if na
const safeChange = nz(close - close[1], 0);

全局常量

命名空间描述
timeframe当前时间周期信息(timeframe.period、timeframe.multiplier)
order排序顺序常量(order.ascending、order.descending)
currency货币代码常量(59种货币,包括 currency.BTC、currency.USD)
display显示模式常量
shape用于 plotshape() 的形状类型常量
location位置常量(location.abovebar、location.belowbar 等)
size尺寸常量(size.tiny、size.small、size.normal、size.large、size.huge)
format格式常量(format.price、format.volume、format.percent)
dayofweek星期常量

所有数据序列都支持使用括号表示法访问历史数据:close[1] 返回上一根K线的收盘价,close[10] 返回10根K线之前的值。