@Channelchan
2018-02-27T11:11:22.000000Z
字数 4691
阅读 21947
写类的顺序
1. 初始化与参数设置
2. self传递
3. 方法汇总在类里
4. 写执行类里方法的方法
from jaqs.data.dataapi import DataApi
from jaqs.data import DataView
from _datetime import datetime
from datetime import timedelta
import pandas as pd
import numpy as np
import alphalens as al
import warnings
warnings.filterwarnings("ignore")
class get_group():
def __init__(self, index, start, end):
self.api = DataApi(addr='tcp://data.tushare.org:8910')
self.api.login("13662241013",
"eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTc2NDQzMzg5MTIiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTM2NjIyNDEwMTMifQ.sVIzI5VLqq8fbZCW6yZZW0ClaCkcZpFqpiK944AHEow"
)
self.index = index
self.start = start
self.end = end
self.classify = None
def get_classify(self):
df_index, msg_index = self.api.query(
view="lb.indexCons",
fields="",
filter="index_code={}&start_date={}&end_date={}".format(self.index, self.start, self.end),
data_format='pandas')
df_classify, msg_classfy = self.api.query(
view="lb.secIndustry",
fields="",
filter="industry_src=SW&symbol=%s" % ",".join(df_index.symbol),
data_format='pandas')
columns = ["in_date", "industry1_code", "out_date", "symbol"]
classify = df_classify[columns]
classify_dict = {'480000': 'Bank', '430000': 'Real_Estate', '460000': 'Leisure_Service',
'640000': 'Mechanical_Equipment', '240000': 'Nonferrous_Metals',
'510000': 'Synthesis', '410000': 'Public_Utility', '450000': 'Commercial_Trade',
'730000': 'Communication', '330000': 'Household_Appliances', '720000': 'Media',
'630000': 'Electrical_Equipment', '270000': 'Electronic_Engineering',
'490000': 'Non_Bank_Finance', '370000': 'Medical_Biology', '710000': 'Computer',
'280000': 'Car', '340000': 'Food_Beverage', '220000': 'Chemical_Engineering',
'210000': 'Digging', '230000': 'Steel', '650000': 'Military',
'110000': 'Agriculture_Fishing', '420000': 'Transportation',
'620000': 'Architectural_Ornament', '350000': 'Textile_Garment',
'610000': 'Building_Materials', '360000': 'Light_Manufacturing'}
classify.industry1_code = classify.industry1_code.replace(classify_dict)
self.classify = classify
return classify
def get_datetime(self):
trade_date, msg_date = self.api.query(
view="jz.secTradeCal",
fields="trade_date,istradeday",
filter="start_date={}&end_date={}".format(self.start, self.end),
data_format='pandas')
trade_date = trade_date["trade_date"][trade_date.istradeday == "T"]
date_index = pd.Index(map(lambda x: datetime.strptime(str(x),"%Y%m%d") , trade_date))
return date_index
def convert(self, time):
return datetime.strptime(time, "%Y%m%d")
def classify_df(self, classify, index_date):
try:
out_date = self.convert(classify.out_date)
except:
idx = index_date
else:
idx = index_date[index_date<out_date]
finally:
return classify.symbol, pd.Series(classify.industry1_code, idx)
def process_group(self):
self.get_classify()
date_index = self.get_datetime()
group_name = pd.DataFrame(dict(self.classify_df(row, date_index) for name, row in self.classify.iterrows()))
group_name.columns.name="symbol"
group_name.index.name="trade_date"
group = group_name.rename_axis(lambda s: s.year*10000+s.month*100+s.day)
return group
group = get_group('000001.SH', 20170101, 20171229).process_group()
print(group.tail(5))
symbol 600000.SH 600004.SH 600005.SH 600006.SH 600007.SH \
trade_date
20171225 Bank Transportation Steel Car Real_Estate
20171226 Bank Transportation Steel Car Real_Estate
20171227 Bank Transportation Steel Car Real_Estate
20171228 Bank Transportation Steel Car Real_Estate
20171229 Bank Transportation Steel Car Real_Estate
symbol 600008.SH 600009.SH 600010.SH 600011.SH \
trade_date
20171225 Public_Utility Transportation Steel Public_Utility
20171226 Public_Utility Transportation Steel Public_Utility
20171227 Public_Utility Transportation Steel Public_Utility
20171228 Public_Utility Transportation Steel Public_Utility
20171229 Public_Utility Transportation Steel Public_Utility
symbol 600012.SH ... 603987.SH 603988.SH \
trade_date ...
20171225 Transportation ... Medical_Biology Electrical_Equipment
20171226 Transportation ... Medical_Biology Electrical_Equipment
20171227 Transportation ... Medical_Biology Electrical_Equipment
20171228 Transportation ... Medical_Biology Electrical_Equipment
20171229 Transportation ... Medical_Biology Electrical_Equipment
symbol 603989.SH 603990.SH 603991.SH \
trade_date
20171225 Electronic_Engineering Computer Chemical_Engineering
20171226 Electronic_Engineering Computer Chemical_Engineering
20171227 Electronic_Engineering Computer Chemical_Engineering
20171228 Electronic_Engineering Computer Chemical_Engineering
20171229 Electronic_Engineering Computer Chemical_Engineering
symbol 603993.SH 603996.SH 603997.SH \
trade_date
20171225 Nonferrous_Metals Household_Appliances Car
20171226 Nonferrous_Metals Household_Appliances Car
20171227 Nonferrous_Metals Household_Appliances Car
20171228 Nonferrous_Metals Household_Appliances Car
20171229 Nonferrous_Metals Household_Appliances Car
symbol 603998.SH 603999.SH
trade_date
20171225 Medical_Biology Media
20171226 Medical_Biology Media
20171227 Medical_Biology Media
20171228 Medical_Biology Media
20171229 Medical_Biology Media
[5 rows x 1386 columns]