掘金社区
"""
引入需要的库
"""
from gmsdk import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
md.init('SSID', '')
'''
按指定代码获取数据,并转成DataFrame
sym: 掘金代码,eg. SHSE.60000
bar_type: 周期,用秒做单位,300表示5分钟
start_date: 开始日期
end_date: 结束日期
'''
def bar_dataframe(sym, bar_type, start_date, end_date):
def fill_date_str(date):
return "{} 00:00:00".format(date)
def local_time(utc_time):
import arrow
return arrow.get(utc_time).to('local').datetime
bars = md.get_bars(sym, bar_type, fill_date_str(start_date), fill_date_str(end_date))
data = []
for b in bars:
data.append([b.open, b.high, b.low, b.close, local_time(b.utc_time)])
df = pd.DataFrame(data, columns=['open', 'high', 'low', 'close', 'timestamp'])
df.index = df.timestamp
return df
au1706 = bar_dataframe('SHFE.au1706', 900, '2016-11-01', '2017-03-30')
au1706.head()
----------
open high low close timestamp
timestamp
2016-11-01 00:00:00+08:00 282.200012 282.250000 282.100006 282.100006 2016-11-01 00:00:00+08:00
2016-11-01 00:15:00+08:00 282.100006 282.149994 282.000000 282.049988 2016-11-01 00:15:00+08:00
2016-11-01 00:30:00+08:00 282.000000 282.100006 282.000000 282.100006 2016-11-01 00:30:00+08:00
2016-11-01 00:45:00+08:00 282.100006 282.100006 281.899994 281.950012 2016-11-01 00:45:00+08:00
2016-11-01 01:00:00+08:00 282.000000 282.100006 281.950012 281.950012 2016-11-01 01:00:00+08:00
ag1706 = bar_dataframe('SHFE.ag1706', 900, '2016-11-01', '2017-03-30')
''' 分析价格比值
'''
ratio = au1706.close/ag1706.close
ratio.describe()
----------
count 3693.000000
mean 0.065855
std 0.001031
min 0.062681
25% 0.065294
50% 0.065950
75% 0.066615
max 0.067704
Name: close, dtype: float64
ratio.skew()
----------
-0.8714719462945193
ratio.quantile()
----------
0.06594973269610546
ratio.sem()
----------
1.6970587904306822e-05
ratio.diff(3).describe()
----------
count 3.690000e+03
mean -4.521236e-07
std 1.296759e-04
min -9.134190e-04
25% -5.599278e-05
50% 7.237222e-07
75% 5.429794e-05
max 1.539378e-03
Name: close, dtype: float64
from statsmodels.tsa.stattools import adfuller
X = ratio.values
result = adfuller(X)
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
print('Critical Values:')
for key, value in result[4].items():
print(' %s: %.3f' % (key, value))
----------
ADF Statistic: -2.324358
p-value: 0.164232
Critical Values:
5%: -2.862
1%: -3.432
10%: -2.567
from statsmodels.tsa.stattools import kpss
result = kpss(X)
result
----------
(2.2059498591207585,
0.01,
30,
{'1%': 0.739, '10%': 0.347, '2.5%': 0.574, '5%': 0.463})
''' 如果出现数据缺少,填充,可bfill()或ffill()
'''
> ratio.plot()
> ag1706.close.plot()
> ratio.bfill().plot()
暂无评论
相关阅读
掘金动态【公告】掘金绩效分析服务隆重上线
策略研究教你如何用Python预测股票价格
掘金动态【新功能】掘金3.6发布上线,新增仿真实盘绩效分析功能
策略研究《利用Python进行数据分析》PDF电子书下载