### 寻找相似的历史k线

Tushare提供的沪深股票日线行情数据包含以下字段：

``````名称	        描述
ts_code	        股票代码
open	        开盘价
high	        最高价
low	        最低价
close	        收盘价
pre_close	昨收价
change	        涨跌额
pct_change	涨跌幅
vol	        成交量（手）
amount	        成交额（千元）``````

code="002351.SZ"//漫步者

num=exec count(*) from edifier

1.使用k线的距离衡量相似度

def square_sum(x,y):sum2(x-y)
corrTable1=select ts_code,move(date(trade_date),num-1)as beginDate,date(trade_date) as endDate,moving(square_sum{edifier.pct_change},pct_change,num) as distance from stock context by ts_code

mostCorrelated1=select * from corrTable1 where isValid(distance),rank(distance,true) between 0:10 order by distance 1:10
``````ts_code	        beginDate	endDate	        distance
300073.SZ	2016.08.02	2016.08.29	67.024
600995.SH	2017.07.06	2017.08.02	70.8713
600549.SH	2010.12.17	2011.01.14	73.514
600627.SH	2008.07.07	2008.08.07	73.59
600367.SH	2010.12.17	2011.01.14	76.447
600867.SH	2011.07.28	2011.08.24	76.5449
002253.SZ	2010.05.21	2010.06.22	79.2845
002382.SZ	2015.01.06	2015.02.02	79.5101
300266.SZ	2014.07.07	2014.09.17	80.1772
600706.SH	2017.06.05	2017.07.14	80.5079``````

def getReturn(t, row): exec pct_change/100 from t where ts_code= row.ts_code, date(trade_date) between row.beginDate : row.endDate

retMatrix1 = each(getReturn{stock}, mostCorrelated1).rename!(mostCorrelated1.ts_code)
plot(retMatrix1,,"使用最短k线距离找出相似历史k线")

2.使用股票日回报相关性衡量相似度

corrTable2=select ts_code,move(date(trade_date),num-1)as beginDate,date(trade_date) as endDate,moving(corr{edifier.pct_change},pct_change,num) as corr from stock context by ts_code

mostCorrelated2=select * from corrTable2 where rank(corr,false) between 0:10 order by corr desc
``````ts_code	        beginDate	endDate	        corr
600367.SH	2010.12.17	2011.01.14	0.8824
600549.SH	2010.12.17	2011.01.14	0.8806
300073.SZ	2016.08.02	2016.08.29	0.8749
002294.SZ	2014.02.26	2014.03.25	0.8729
600995.SH	2017.07.06	2017.08.02	0.8723
600486.SH	2010.12.17	2011.01.14	0.8721
002382.SZ	2015.01.06	2015.02.02	0.8718
002253.SZ	2010.05.21	2010.06.22	0.8708
000939.SZ	2008.03.21	2008.04.18	0.8706
600627.SH	2008.07.07	2008.08.07	0.8692``````

retMatrix2 = each(getReturn{stock}, mostCorrelated2).rename!(mostCorrelated2.ts_code)
plot(retMatrix,,"使用股票日回报相关性找出相似历史k线")

3.性能分析