def post(self): """ json str:{"codes": "600008.SH, "fields": "ask1,bid1,asize1,bsize1,volume,amt,pre_close,open,high,low,last", "begin_time": "2017-01-04", "end_time": "2017-02-28", "options": ""} :return: 返回万得返回数据dict """ data_dic = request.json logger.info('/wst/ data_dic:%s', data_dic) codes = data_dic['codes'] fields = data_dic['fields'] begin_time = data_dic['begin_time'] end_time = data_dic['end_time'] options = data_dic['options'] if not w.isconnected(): w.start() if options == "": options = None ret_data = w.wst(codes, fields, begin_time, end_time, options) error_code = ret_data.ErrorCode if error_code != 0: msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "") logger.error('wst("%s", "%s", "%s", "%s", "%s") ErrorCode=%d %s' % (codes, fields, begin_time, end_time, options, error_code, msg)) return {'error_code': ret_data.ErrorCode, 'message': msg}, 404 # if ret_data.ErrorCode != 0: # logger.error('wsd("%s", "%s", "%s", "%s", "%s") ErrorCode=%d' % ( # codes, fields, begin_time, end_time, options, ret_data.ErrorCode)) # return {'error_code': ret_data.ErrorCode}, 404 # 将 Data数据中所有 datetime date 类型的数据转换为 string data_len = len(ret_data.Data) for n_data in range(data_len): data = ret_data.Data[n_data] data_len2 = len(data) if data_len2 > 0: # 取出第一个部位None的数据 for item_check in data: if item_check is not None: break # 进行类型检查,如果发现是 datetime, date 类型之一,则进行类型转换 if item_check is not None and type(item_check) in (datetime, date): ret_data.Data[n_data] = [ format_2_datetime_str(dt) for dt in data ] logger.info('%d column["%s"] date to str', n_data, ret_data.Fields[n_data]) # 组成 DataFrame ret_df = pd.DataFrame( ret_data.Data, index=ret_data.Fields, columns=[format_2_datetime_str(dt) for dt in ret_data.Times]) # print(ret_df) ret_dic = ret_df.to_dict() # print('ret_dic:\n', ret_dic) return ret_dic
def get_tick(code, start_time, end_time, dirpath): try: data_wind = w.wst( code, "pre_close,open,high,low,last,ask,bid,volume,amt,vol_ratio,iopv,limit_up,limit_down,ask5,ask4,ask3,ask2,ask1,bid1,bid2,bid3,bid4,bid5, asize5,asize4,asize3,asize2,asize1,bsize1,bsize2,bsize3,bsize4,bsize5", start_time, end_time, "") arr = np.array(data_wind.Data) df = pd.DataFrame(arr.T, index=data_wind.Times, columns=data_wind.Fields) df["code"] = data_wind.Codes[0] df.index.name = "datetime" filepath = dirpath + code + ".csv" df.to_csv(filepath, encoding="utf_8_sig") except: print("{} is not insert".format(code)) return code
def read_data(self): """ Desc: 从wind读取期权和标的期货合约当个交易日的盘口数据并存入一个字典 :return: 装有数据的字典,键是合约代码、值是合约对应的盘口数据 """ # (1) 获取行权价序列 _strike = strike_series(lower_strike=self.lower_strike, upper_strike=self.upper_strike, strike_interval=self.strike_interval) # (2) 构造期货合约代码列表 contract, exchange = self.sec_name.split('.') _contract = contract + str(self.month) future_code = [_contract + '.' + exchange] # (3) 获取期权合约代码列表 if exchange in ["CZC", "SHF"]: call_option_code = [ _contract + 'C' + str(i) + '.' + exchange for i in _strike ] put_option_code = [ _contract + 'P' + str(i) + '.' + exchange for i in _strike ] elif exchange == "DCE": call_option_code = [ _contract + '-C-' + str(i) + '.' + exchange for i in _strike ] put_option_code = [ _contract + '-P-' + str(i) + '.' + exchange for i in _strike ] else: raise Exception('unexpected exchange') sec_code = future_code + call_option_code + put_option_code # (4) 根据上一步中的代码以及起止时间从wind中读取盘口数据 bid_ask_data = {} for _code in sec_code: _data = w.wst(_code, "bid1,ask1,bsize1,asize1", self.start_time, self.end_time, "") if _data.ErrorCode != 0: warn('{} wind API error: error code is {}'.format( _code, _data.ErrorCode)) else: _df = pd.DataFrame(_data.Data).T _df.columns = _data.Fields _df.index = _data.Times bid_ask_data[_code] = _df return bid_ask_data
printpy(wsddata1) # 通过wsd来提取各个报告期财务数据 print('\n\n' + '-----通过wsd来提取各个报告期财务数据-----' + '\n') wsddata2 = w.wsd("600000.SH", "tot_oper_rev,tot_oper_cost,opprofit,net_profit_is", "2008-01-01", "2015-12-22", "rptType=1;Period=Q;Days=Alldays;Fill=Previous") printpy(wsddata2) # 通过wss来取截面数据 print('\n\n' + '-----通过wss来取截面数据-----' + '\n') wssdata = w.wss("600000.SH,600007.SH,600016.SH", "ev,total_shares", "tradeDate=20151222;industryType=1") printpy(wssdata) # 通过wst来取日内成交数据 print('\n\n' + '-----通过wst来取日内成交数据-----' + '\n') wstdata = w.wst("IF.CFE", "last,volume", "2015-12-22 09:00:00", "2015-12-22 14:04:45") printpy(wstdata) # 通过wsi来取日内分钟数据 print('\n\n' + '-----通过wsi来取日内分钟数据-----' + '\n') wsidata = w.wsi("IF.CFE", "open,high,low,close,volume,amt", "2015-12-22 09:00:00", "2015-12-22 14:06:15") printpy(wsidata) # 通过wset来取数据集数据 print('\n\n' + '-----通过wset来取数据集数据,获取沪深300指数权重-----' + '\n') wsetdata = w.wset("IndexConstituent", "date=20151222;windcode=000300.SH;field=date,wind_code,i_weight") printpy(wsetdata)
print('\n\n' + '-----通过wsd来提取各个报告期财务数据-----' + '\n') wsddata2 = w.wsd("600000.SH", "tot_oper_rev,tot_oper_cost,opprofit,net_profit_is", "2008-01-01", "2015-12-22", "rptType=1;Period=Q;Days=Alldays;Fill=Previous") printpy(wsddata2) # 通过wss来取截面数据 print('\n\n' + '-----通过wss来取截面数据-----' + '\n') wssdata = w.wss("600000.SH,600007.SH,600016.SH", "ev,total_shares", "tradeDate=20151222;industryType=1") printpy(wssdata) # 通过wst来取日内成交数据 print('\n\n' + '-----通过wst来取日内成交数据-----' + '\n') wstdata = w.wst("IF.CFE", "last,volume", "2015-12-22 09:00:00", "2015-12-22 14:04:45") printpy(wstdata) # 通过wsi来取日内分钟数据 print('\n\n' + '-----通过wsi来取日内分钟数据-----' + '\n') wsidata = w.wsi("IF.CFE", "open,high,low,close,volume,amt", "2015-12-22 09:00:00", "2015-12-22 14:06:15") printpy(wsidata) # 通过wset来取数据集数据 print('\n\n' + '-----通过wset来取数据集数据,获取沪深300指数权重-----' + '\n') wsetdata = w.wset( "IndexConstituent", "date=20151222;windcode=000300.SH;field=date,wind_code,i_weight") printpy(wsetdata)
# -*- coding: utf-8 -*- """ Created on Wed Jun 07 13:07:33 2017 @author: Think """ from WindPy import w import datetime w.start() data = w.wst("600000.SH", "open", "2017-06-06 09:00:00", "2017-06-06 14:04:45") print data
def post(self): """ json str:{"codes": "600008.SH, "fields": "ask1,bid1,asize1,bsize1,volume,amt,pre_close,open,high,low,last", "begin_time": "2017-01-04", "end_time": "2017-02-28", "options": ""} :return: 返回万得返回数据dict """ args = receive_wst_parser.parse_args() logger.info('/wst/ args:%s', args) # codes = args['codes'] # fields = args['fields'] # begin_time = args['begin_time'] # end_time = args['end_time'] # options = args['options'] if args['options'] == "": args['options'] = None if not w.isconnected(): w.start() ret_data = None for nth in range(2): ret_data = w.wst(**args) error_code = ret_data.ErrorCode if error_code != 0: if nth == 0 and error_code == -40521010: w.stop() w.start() logger.warning('尝试重新登陆成功,再次调用函数') continue msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "") logger.error('wst(%s) ErrorCode=%d %s' % (args, error_code, msg)) raise RequestError(msg, None, error_code) else: break else: if ret_data is None: msg = 'wst(%s) ret_data is None' % args logger.error(msg) raise RequestError(msg, None, 0) # 将 Data数据中所有 datetime date 类型的数据转换为 string data_len = len(ret_data.Data) for n_data in range(data_len): data = ret_data.Data[n_data] data_len2 = len(data) if data_len2 > 0: # 取出第一个部位None的数据 item_check = None for item_check in data: if item_check is not None: break # 进行类型检查,如果发现是 datetime, date 类型之一,则进行类型转换 if item_check is not None and type(item_check) in (datetime, date): ret_data.Data[n_data] = [ format_2_datetime_str(dt) for dt in data ] logger.info('%d column["%s"] date to str', n_data, ret_data.Fields[n_data]) # 组成 DataFrame ret_df = pd.DataFrame( ret_data.Data, index=ret_data.Fields, columns=[format_2_datetime_str(dt) for dt in ret_data.Times]) # print(ret_df) ret_dic = ret_df.to_dict() # print('ret_dic:\n', ret_dic) return ret_dic
print('\n\n' + '-----通过wsd来提取各个报告期财务数据-----' + '\n') wsddata2 = w.wsd("600000.SH", "tot_oper_rev,tot_oper_cost,opprofit,net_profit_is", "2014-01-01", "2015-12-22", "rptType=1;Period=Q;Days=Alldays;Fill=Previous") printpy(wsddata2) # 通过wss来取截面数据 print('\n\n' + '-----通过wss来取截面数据-----' + '\n') wssdata = w.wss("600000.SH,600007.SH,600016.SH", "ev,total_shares", "tradeDate=20151222;industryType=1") printpy(wssdata) # 通过wst来取日内成交数据 print('\n\n' + '-----通过wst来取日内成交数据-----' + '\n') wstdata = w.wst("600001.SH", "last,volume", "2015-12-22 09:00:00", "2015-12-22 14:04:45") printpy(wstdata) # 通过wsi来取日内分钟数据 print('\n\n' + '-----通过wsi来取日内分钟数据-----' + '\n') wsidata = w.wsi("IF.CFE", "open,high,low,close,volume,amt", "2015-12-22 09:00:00", "2015-12-22 14:06:15") printpy(wsidata) # 通过wset来取数据集数据 print('\n\n' + '-----通过wset来取数据集数据,获取沪深300指数权重-----' + '\n') wsetdata = w.wset( "IndexConstituent", "date=20151222;windcode=000300.SH;field=date,wind_code,i_weight") printpy(wsetdata)
__author__ = 'aming.tao' from WindPy import w from datetime import * w.start() data=w.wsd("600000.SH","close,amt","2013-04-30", datetime.today()-timedelta(1))#取浦发银行收盘价等信 data=w.wsd("600000.SH","close,amt", datetime.today()-timedelta(100))# data=w.wsi("600000.SH","close,amt","2015-10-01 9:00:00")#取浦发银行分钟收盘价等信息 data=w.wst("600000.SH","open", datetime.today()-timedelta(0,2*3600), datetime.now())#取浦发银行tick数据信息 data=w.wss("600000.SH,000001.SZ","eps_ttm,orps,surpluscapitalps","rptDate=20121231")#取浦发银行等财务数据信息 data=w.wset("SectorConstituent",u"date=20130608;sector=全部A股")#取全部A 股股票代码、名称信息 w.wset("IndexConstituent","date=20130608;windcode=000300.SH;field=wind_code,i_weight")#取沪深300 指数中股票代码和权重 w.wset("TradeSuspend","startdate=20130508;enddate=20130608;field=wind_code,sec_name,suspend_type,suspend_reason")#取停牌信息 w.wset("SectorConstituent",u"date=20130608;sector=风险警示股票;field=wind_code,sec_name")#取ST 股票等风险警示股票信息 w.tdays("2013-05-01","2013-06-08")#返回5 月1 日到6 月8 日之间的交易日序列 w.tdays("2013-05-01")#返回5 月1 日到当前时间的交易日序列 w.tdaysoffset(-5,"2013-05-01")#返回5 月1 日前推五个交易日的日期,返回2013-4-19 w.tdaysoffset(-5)#返回当前时间前推五个交易日的日期 w.tdayscount("2013-05-01","2013-06-08")#返回5 月1 日到6 月8 日之间的交易日序列长度,为27 w.stop()