实现:
使用价格分段算法对历史K线分割,得到每个分割点的日期时间和价格,用这个数据作为拐点,在K线图上绘制出折线图。
在天软终端上绘制折线图需要给出线段上每个点的坐标,下面的范例在仅有折线图拐点的情况下,以相邻的两个拐点作为线段的起点和终点,通过计算线段的斜率,得到每个点的坐标。
其中价格分段算法参见:FAQ:
2021-02-02-应用专题-价格分段:常见价格分段算法及其实现
Function linechart();
Begin
//对历史K线图分段,提取每段图形之间的拐点
Setsysparam(pn_Stock(),"SZ000002");
Setsysparam(Pn_Cycle(),cy_day());
begt:=20200801T;
Endt:=20200930T;
PriceDivide:=StockPriceDivideByKLine(begt,Endt,0);
//提取绘制K线图所需的数据
setsysparam(pn_date(),Endt);
setsysparam(pn_nday(),tradedays(begt,endt));
t1:=NDay2(
gfopen(), Open() ,
gfclose(), close(),
gfhigh(), high(),
gflow(), low(),
gftime(), sp_time(),
gfColor(), IfThen3(Close(),Open(),ClRed(),clYellow(),ClLime()),
gffill(),ifThen3(Close(), Open(), flClear(), flSolid(),flSolid())
);
//K线图数据与分段拐点数据合并
t2:=select [1].['time'],[2].['价格'] from t1 left join PriceDivide
on [1].['time']=[2].['截止日'] end;
//填充拐点之间的价格数据,用于绘制折线图
datafill(t2,'价格');
t2:=select datetimetostr(['time']) as 'time',['价格'] from t2 end;
//图形绘制
g1:=graph(gtKline(),'KLine',t1);
g2:=graph(gtline(),'价格',t2,gfColor(),clYellow());
return graphgroup(g2,g1);
end
function datafill(table,colname)
begin
{
以相邻的两个拐点作为线段的起点和终点,通过计算线段的斜率得到
线段上每个点的坐标(这个范例的坐标为每个时间点对应的价格);
}
last1:=-1;
for i:=0 to length(table)-1 do
begin
if ifnil(table[i][colname]) then continue;
if last1=-1 then
begin
last1:=i;
continue;
end
//计算斜率
table[last1:i,'k']:=(table[i][colname]-table[last1][colname])/(i-last1);
table[last1:i,'y0']:=table[last1][colname];
table[last1:i,'x0']:=last1;
last1:=i;
end;
//计算线段上每个时间点对应的价格
update table set [colname]=(['k']*(thisrowindex-['x0'])+['y0'])
where ifnil([colname]) and not ifnil(['x0']) end;
end
返回结果: