编写常用代码集锦(草稿)

常用代码
标签: #<Tag:0x00007f73e89f3f40>

(iQuant) #1

股票池中去除获取创业板代码
all_ins = D.instruments(start_date='2017-09-07', end_date='2017-09-07')
# 除去创业板股票代码,创业板代码一般以3开头
ins = [k for k in all_ins if k[0] != '3']  


获取指数成分股股票代码
date='2017-05-23'
df = D.history_data(D.instruments(date, date),date,date,fields=['in_csi300'])
# 获取沪深300的成分股股票列表  中证500:in_csi500,中证800:in_csi800  参考文档说明
hs300 = set(df[df['in_csi300']==1]['instrument'])
 

获取指定时间段内某个交易市场的交易日历
D.trading_days(market='CN',start_date='2015-01-01', end_date='2016-01-01')


获取指定时间段内有效的股票代码
D.instruments(start_date='2015-01-01', end_date='2016-01-01', market='CN_STOCK_A')


获取指定时间段内的基金代码
D.instruments(start_date='2015-01-01', end_date='2016-01-01', market='CN_FUND')


获取交易数据
D.history_data(instruments=['000001.SZA'], start_date='2017-01-01', end_date='2017-01-07',
               fields=['open', 'high', 'low', 'close'])
          

获取指定行业的数据
instruments = D.instruments()   #获取所有股票列表
df = D.history_data(instruments, start_date='2017-05-19', end_date='2017-05-19',
                    fields=['industry_sw_level1'])  #获取一天历史数据
df[df['industry_sw_level1']== 710000]   #获取计算机行业的股票数据


获取指数成分股
instruments = D.instruments()   #获取股票列表
df = D.history_data(instruments, start_date='2017-05-19', end_date='2017-05-19',
                    fields=['in_csi300'])   #获取一天历史数据
list(df[df['in_csi300']== 1]['instrument']) #沪深300指数成份股


获取板块名称列表
instruments = D.instruments()   #获取股票列表
df = D.history_data(instruments, start_date='2017-05-19', end_date='2017-05-19',
                    fields=['concept']) #获取一天历史数据
set([y for x in df['concept'] if x for y in x.split(';')])  #获取一天包含的所有板块名称


获取指定板块数据
instruments = D.instruments()   #获取股票列表
df = D.history_data(instruments, start_date='2017-05-19', end_date='2017-05-19',
                    fields=['concept'])     #获取一天历史数据
df[df['concept'].apply(lambda x:'智能家居' in x.split(';')
                       if x else False)] #获取智能家居概念包含的股票


获取沪深300的数据:
D.history_data(instruments=['000300.SHA'], start_date='2017-01-01', end_date='2017-01-07',
               fields=['open', 'high', 'low', 'close', 'volume', 'amount'])
               

获取财报数据
D.financial_statements(instruments=['000001.SZA'], start_date='2010-01-01', end_date='2017-01-07',
                       fields=['date', 'instrument', 'fs_publish_date', 'fs_quarter', 'fs_eps', 'fs_roe'])
                       

获取宏观经济数据
D.macro_data(start_date='2015-01-01', end_date='2017-07-01', fields=['gdp','ppi'])


获取特征数据
D.features(instruments=['000001.SZA'], start_date='2017-01-01', end_date='2017-01-07', fields=['close_1'])


绘制基于Highcharts的可交互图表
T.plot(df[['close']], title='收盘价', chart_type='line')


数据过滤
# 查看最新版本和定义
M.filter.m_latest_version

# 数据过滤
def gen_history_data(instruments, start_date, end_date):
    df = D.history_data(instruments, start_date, end_date, ['close', 'open'])
    return Outputs(data = DataSource.write_df(df))

m1 = M.cached.v2(run=gen_history_data, kwargs={
    'instruments': ['000001.SZA', '600000.SHA'],
    'start_date': '2017-05-01',
    'end_date': '2017-05-20'
})

# 按时间过滤
m2 = M.filter.v2(data=m1.data, expr='"2017-05-08" < date < "2017-05-10"')
m2.data.read_df().head()

# 按价格过滤
m3 = M.filter.v2(data=m1.data, expr='open > 110 and close < 600')
m3.data.read_df().head()


计算股票实际价格
df = D.history_data(['000001.SZA'],'2017-09-10','2017-09-22',['open','high','low','close','volume','amount','adjust_factor'])
for price in ['open','high','low','close']:
    df[price] /= df['adjust_factor']