Files
playbook/skills/tsl-api-reference/references/codegen/dotnet/datawarehouse/time_series_statistics.md
T
csh 9010829c6d feat(tsl-api-reference): expand and reorganize api catalog
Flatten builtin pages, add third-party and platform scopes, and import financial and data warehouse references.

Keep the existing generated index without rebuilding after signature normalization.
2026-08-10 18:26:24 +08:00

24 KiB

数据仓库 / 时间序列统计

count(exp, n, cache_string)

声明:function

统计N天中表达式Exp值为真的天数

参数 类型 说明
exp t_expression 表达式类型
n integer 整数,交易日天数,如果为-1表示从上市第一天起统计
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:integer

示例

范例01:调用函数

// 上证指数在最近100天内的上涨天数;
setSysParam(pn_stock(), 'SH000001');
setSysParam(pn_date(), intToDate(20140122));
return count(isUp(), 100);
// 输出:47。

cross(a, b)

声明:function

A线从下面穿越B线时返回1,从A上面向下穿越B时返回-1,否则返回0。与系统参数(股票、时间、周期等相关)

参数 类型 说明
a t_expression TExpression型表达式
b t_expression TExpression型表达式

返回:integer

示例

范例01:万科在指定区间内的均线穿越情况;

// 万科在指定区间内的均线穿越情况;

setsysparam(pn_stock(), 'SZ000002');

end_date := inttodate(20110829);

beg_date := IncWeek(end_date, -1); // end_date 前推一个

r := array();

i := 0;

for d := end_date downto beg_date do

begin

    if not istradeday(d) then continue;

    setsysparam(pn_date(), d);

    r[i]['date'] := datetostr(d);

    case cross(close(), ma(close(), 5)) of

        1:begin

            r[i]['cross_state'] := '突破5日均线';

        end

        -1:begin

            r[i]['cross_state'] := '跌破5日均线';

        end

        0:begin

            r[i]['cross_state'] := '';

        end

    end;

    i++;

end;

return r;

ema(exp, n, cache_string)

声明:function

返回N日Exp的移动平均,与系统参数(股票、时间、周期等相关)

参数 类型 说明
exp t_expression TExpression型表达式;
n integer 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

示例

范例01:调用函数

// 获得万科A(SZ000002)在2011-09-09日的10日平滑移动均价
setSysParam(pn_stock(), 'SZ000002');
setSysParam(pn_date(), intToDate(20110909));
n := 10;
return ema(close(), n);
// 输出:8.1158

hhv(exp, n, cache_string)

声明:function

返回N日最高的Exp的值,与系统参数(股票、时间、周期等相关),查看相关行情数据说明

参数 类型 说明
exp t_expression TExpression型表达式
n integer 整数,天数。N为0时,从上市日算起;注意,计算上市以来的最高/最低时,0必须用常量。比如hhv(high(),0)。不能以变量的方式传递:N:=0; hhv(high(),N),会报错
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

示例

范例01:获取万科A(SZ000002)截止2011-09-09日14个交易日内的最高,最低的收盘价以及

// 获取万科A(SZ000002)截止2011-09-09日14个交易日内的最高,最低的收盘价以及

// 发生日期。

setsysparam(pn_stock(), 'SZ000002');

setsysparam(pn_date(), inttodate(20110909));

N := 14;

r := array('h_value':hhv(close(), N),

    'h_date':datetostr(GetSysParam('hhvtime')),

    'l_value':llv(close(), N),

    'l_date':datetostr(GetSysparam('llvtime'))

);

return r;

llv(exp, n, cache_string)

声明:function

返回N日最低的Exp的值,与系统参数(股票、时间、周期等相关),查看相关行情数据说明

参数 类型 说明
exp t_expression TExpression型表达式
n integer 整数,天数。N为0时,从上市日算起;N为负数时无效。注意,计算上市以来的最高/最低时,0必须用常量。比如llv(close(),0)。不能以变量的方式:N:=0; llv(close(),N),会报错
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

ma(exp, n, cache_string)

声明:function

返回N日Exp的平均值,与系统参数(股票、时间、周期等相关)

参数 类型 说明
exp t_expression TExpression型表达式
n integer 整数,天数。N为0时,返回当天值;N为-1时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

示例

范例01:获得万科A(SZ000002)在2011-09-09日的10日均价

// 获得万科A(SZ000002)在2011-09-09日的10日均价

setsysparam(pn_stock(), 'SZ000002');

setsysparam(pn_date(), inttodate(20110909));

N := 10;

return ma(close(), N);
// 输出:8.103

范例02:获取万科A一段时间的10日均价

// 获取万科A一段时间的10日均价

setsysparam(pn_stock(), "SZ000002");

setsysparam(pn_date(), strtodate("2014-01-15"));

t := nday(100, 'time', sp_time(), 'close', close(), 'ma10', ma(close(), 10), 'ma20', ma(close(), 20));

return t;

sma(exp, n, m, cache_string)

声明:function

求Exp的N日平滑移动平均,M为权重

参数 类型 说明
exp t_expression TExpression型表达式
n integer 整数,天数
m integer 整数,权重
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

示例

范例01:调用函数

// 获得万科A(SZ000002)在2011-09-09日的10日平滑移动均价,3为权重
setSysParam(pn_stock(), 'SZ000002');
setSysParam(pn_date(), intToDate(20110909));
n := 10;
m := 3;
return sma(close(), n, m);
// 输出:8.0914

sp_avedev(exp, n, cache_string)

声明:function

估算N天的TExpression的平均绝对偏差。TExpression是与取时间序列数值的表达式,例如close函数,函数的含义是根据连续N天的当天的EXP的值计算平均绝对偏差

参数 类型 说明
exp t_expression TExpression表达式
n integer 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

sp_devsq(exp, n, cache_string)

声明:function

估算N天的TExpression的偏差平方和。TExpression是与取时间序列数值的表达式,例如close函数,函数的含义是根据连续N天的当天的EXP的值计算偏差平方和

参数 类型 说明
exp t_expression TExpression表达式
n integer 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

sp_geoMean(exp, n)

声明:function

移动几何平均数。表达式前N个交易日的几何平均数

参数 类型 说明
exp t_expression 表达式
n integer 交易日天数

返回:float

示例

范例01:调用 SP_GeoMean

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(),

    "几何平均价格", Specall(SP_geomean(Close(), 4),

    Array(Pn_Cycle():Cy_60m(), pn_date():SP_time()+0.99)));

sp_harMean(exp, n)

声明:function

移动调和平均数。表达式前N个交易日的调和平均数

参数 类型 说明
exp t_expresssion 表达式
n integer 交易日天数

返回:float

示例

范例01:调用 SP_HarMean

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "调和平均价格", Specall(SP_Harmean(Close(), 4800), Array(Pn_Cycle():Cy_3s(), pn_date():SP_time()+0.99)));

sp_kurtosis(exp, n)

声明:function

移动峰度(Excel兼容)。表达式数据最近N个交易日的峰度,算法与excel兼容

参数 类型 说明
exp t_expression 表达式
n integer 天数

返回:float

示例

范例01:调用 SP_Kurtosis

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "峰度", Specall(SP_kurtosis(Close(), 4800), array(pn_date():SP_time()+0.99, pn_Cycle():Cy_3s())));

sp_kurtosis2(exp, n)

声明:function

移动峰度(Matlab兼容)。表达式最近N个交易日的峰度,算法与matlab兼容

参数 类型 说明
exp t_expression 表达式
n integer 天数

返回:float

示例

范例01:调用 SP_Kurtosis2

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "峰度", Specall(SP_kurtosis2(Close(), 4800), array(pn_date():SP_time()+0.99, pn_Cycle():Cy_3s())));

sp_large(exp, m, n)

声明:function

移动最大排位值。指定表达式最近N天中的第M个最大值

参数 类型 说明
exp t_expression 指定表达式,和系统当前时间有关
m integer 最大值位数
n integer 交易日天数

返回:float

示例

范例01:调用 SP_Large

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "分钟线最大成交量", Specall(SP_Large(Vol(), 1, 240), Array(Pn_Cycle():Cy_1m(), pn_date():SP_time()+0.99)));

sp_percentile(exp, m, n)

声明:function

移动百分位数。表达式最近N个交易日第M个百分比对应的数据

参数 类型 说明
exp t_expression 表达式
m float 百分比,0~1之间
n integer 交易日天数

返回:float

示例

范例01:调用 SP_Percentile

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

Return Nday(10, "日期", SP_time(), "价格", SpecAll(SP_percentile(close(), 0.8, 240), Array(pn_Cycle():Cy_1m(), pn_date():SP_time()+0.99)));

sp_percentrank(exp_1, exp_2, n)

声明:function

移动百分比排名。指标数据的当前数据在最近N个交易日中的百分比排名

参数 类型 说明
exp_1 t_expression 表达式,N个交易日的表达式
exp_2 t_expression 表达式,当前值的表达式
n integer 交易日天数

返回:float

示例

范例01:调用 SP_Percentrank

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

Return Nday(10, "日期", SP_time(), "百分比", SpecAll(SP_percentRank(close(), close(), 240), Array(pn_Cycle():Cy_1m(), pn_date():SP_time()+0.99)));

sp_quartile(exp, m, n)

声明:function

移动四分位数。表达式最近N个交易日集中四分位对应的数据

参数 类型 说明
exp t_expression 表达式
m integer 四分位值,0~4的整数
n integer 交易日天数

返回:float

示例

范例01:调用 sp_quartile

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "价格", Specall(SP_quartile(Close(), 2, 4800), Array(Pn_Cycle():Cy_3s(), pn_date():SP_time()+0.99)));

sp_rank(arg_1, arg_2, arg_3)

声明:function

移动排名。表达式当前数据在最近N个交易日的排名(从大到小)

参数 类型 说明
arg_1 t_expression 表达式
arg_2 t_expression 表达式
arg_3 integer 交易日天数

返回:array,table_array

示例

范例01:调用 SP_Rank

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

Return Nday(10, "日期", SP_time(), "排名", SpecAll(SP_Rank(close(), close(), 240), Array(pn_Cycle():Cy_1m(), pn_date():SP_time()+0.99)));

sp_skewness(exp, n)

声明:function

移动偏度(Excel兼容)。表达式数据最近N个交易日的偏度,算法与excel兼容

参数 类型 说明
exp t_expression 表达式
n integer 天数

返回:float

示例

范例01:调用 SP_Skewness

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "偏度", Specall(SP_skewness(Close(), 4800), array(pn_date():SP_time()+0.99, pn_Cycle():Cy_3s())));

sp_skewness2(exp, n)

声明:function

移动偏度(Matlab兼容)。表达式数据最近N个交易日的偏度,算法与matlab兼容

参数 类型 说明
exp t_expression 表达式
n integer 天数

返回:float

示例

范例01:调用 SP_Skewness2

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "偏度", Specall(SP_skewness2(Close(), 4800), array(pn_date():SP_time()+0.99, pn_Cycle():Cy_3s())));

sp_small(exp, m, n)

声明:function

移动最小排位值。指标数据最近N个交易日的第M个最小值

参数 类型 说明
exp texpression 表达式
m integer 对应的最小值位数
n integer 交易日天数;备注:指定表达式与系统当前时间相关,M的取值数据小于N

返回:float

示例

范例01:调用 SP_Small

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

Setsysparam(pn_Cycle(), cy_Month());

return Nday(12, "日期", SP_time(), "月末5日最小价格", Specall(SP_Small(Close(), 1, 5), Array(Pn_Cycle():Cy_day())));

sp_std(exp, n, cache_string)

声明:function

估算N天的TExpression的标准差。TExpression是与时间序列数据相关的函数或表达式,例如close函数,函数的含义是根据连续N天的当天的EXP的值计算标准差

参数 类型 说明
exp t_expression TExpression表达式
n integer 整数,交易日天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

示例

范例01:计算万科A截止20110909前30个交易日股价的统计信息

// 计算万科A截止20110909前30个交易日股价的统计信息

setsysparam(pn_stock(), 'SZ000002');

setsysparam(pn_date(), inttodate(20110909));

_stat := array();

_stat := array('标准差':sp_std(close(), 30),

    '总体标准差':sp_stdp(close(), 30),

    '方差':sp_var(close(), 30),

    '总体方差':sp_varp(close(), 30),

    '平均绝对偏差':sp_avedev(close(), 30),

    '偏差平方和':sp_devsq(close(), 30));

return _stat;

sp_stdp(exp, n, cache_string)

声明:function

估算N天的TExpression的总体标准差。TExpression是与取时间序列数值的表达式,例如close函数,函数的含义是根据连续N天的EXP的值计算总体标准差

参数 类型 说明
exp t_expression TExpression表达式
n intege 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

示例

范例01:计算万科截止20110909前30个交易日股票的总体标准差

// 计算万科截止20110909前30个交易日股票的总体标准差

setsysparam(pn_stock(), 'SZ000002');

setsysparam(pn_date(), inttodate(20110909));

return sp_stdp(close(), 30);
// 输出:0.1456

sp_totalVariance(exp, n)

声明:function

移动总体偏差。表达式数据最近N个交易日的离差平方和(总体偏差)

参数 类型 说明
exp t_expression 表达式
n integer 天数

返回:float

示例

范例01:调用 SP_TotalVariance

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "偏差", Specall(SP_totalvariance(Close(), 4800), array(pn_date():SP_time()+0.99, pn_Cycle():Cy_3s())));

sp_trimmean(exp, m, n)

声明:function

移动去首尾平均值。表达式最近N个交易日去掉首尾百分比后的平均值

参数 类型 说明
exp t_expression 表达式
m integer 百分比数,0~1之间
n integer 交易日天数

返回:float

示例

范例01:调用 SP_Trimmean

Setsysparam(pn_Stock(), "SZ000002");

Setsysparam(pn_date(), 20181030T);

return Nday(10, "日期", SP_time(), "平均价格", Specall(SP_trimmean(Close(), 0.8, 4800), Array(Pn_Cycle():Cy_3s(), pn_date():SP_time()+0.99)));

sp_var(exp, n, cache_string)

声明:function

估算N天的TExpression的样本方差。TExpression是与取时间序列数值的表达式,例如close函数,函数的含义是根据连续N天的当天的EXP的值计算样本方差

参数 类型 说明
exp t_expression TExpression表达式
n integer 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

示例

范例01:计算万科截止20110909前30个交易日股票的样本方差

// 计算万科截止20110909前30个交易日股票的样本方差

setsysparam(pn_stock(), 'SZ000002');

setsysparam(pn_date(), inttodate(20110909));

return sp_var(close(), 30);
// 输出:0.0219

sp_varp(exp, n, cache_string)

声明:function

估算N天的TExpression的总体样本方差。TExpression是与取时间序列数值的表达式,例如close函数,函数的含义是根据连续N天的当天的EXP的值计算总体样本方差

参数 类型 说明
exp t_expression TExpression表达式
n integer 整数,天数;N为-1,0时无效
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:float

sumN(exp, n, cache_string)

声明:function

求Exp的N日累加和

参数 类型 说明
exp t_expression TExpression型表达式
n integer 整数,交易日天数,如果为-1表示从上市第一天起统计
cache_string string 可选参数,字符串类型,缓存标志串。参见:时间序列统计缓存标志与性能加速

返回:real

示例

范例01:调用函数

// 获得万科A(SZ000002)在2011-09-09日的5日总成交量
setSysParam(pn_stock(), 'SZ000002');
setSysParam(pn_date(), intToDate(20110909));
return sumN(vol(), 5);
// 输出:152502152