def QA_fetch_get_stock_day(name, startDate, endDate, if_fq='01'): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: if if_fq in ['00', 'bfq']: data = w.wsd(name, "sec_name,pre_close,open,high,low,close,volume", startDate, endDate) elif if_fq in ['01', 'qfq']: data = w.wsd(name, "sec_name,pre_close,open,high,low,close,volume", startDate, endDate, "PriceAdj=F") elif if_fq in ['02', 'hfq']: data = w.wsd(name, "sec_name,pre_close,open,high,low,close,volume", startDate, endDate, "PriceAdj=B") else: QA_util_log_info('wrong fq factor! using qfq') data = w.wsd(name, "sec_name,pre_close,open,high,low,close,volume", startDate, endDate, "PriceAdj=B") if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def QA_fetch_get_stock_financial(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() pass
def GetDatas(a_ifId, a_begin, a_end, a_max): client = MongoClient('mongodb://localhost:27017/') col = client.Test[a_ifId] colRange = client.Test["range_%s" % a_ifId] if not col.count == 0: #检测是否给定的时间范围 #否则重置col,colRange if colRange.count() == 0: col.drop() else: range = [ x[u'_id'] for x in colRange.find()] if not (range[0] == a_ifId and range[1] == a_begin and range[2] == a_end): colRange.drop() col.drop() if col.count() == 0: w.start(waitTime=10) wdatas = w.wsi(a_ifId,"open,close",a_begin,a_end, 'Fill=Previous') datasOpen = wdatas.Data[0] datasClose = wdatas.Data[1] docs =[ {'_id':x[0],'open':x[1],'close':x[2] } for x in zip(wdatas.Times, datasOpen, datasClose) ] col.insert(docs) colRange.insert( [ {'_id':a_ifId} , {'_id':a_begin} , {'_id':a_end}] ) else: datasClose = [ x[u'close'] for x in col.find()] if a_max == -1: return datasClose return datasClose[:a_max]
def QA_fetch_get_stock_list(date): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(date) == False): QA_util_log_info("wrong date") else: awgs = 'date=' + date + ';sectorid=a001010100000000' data = w.wset("sectorconstituent", awgs) return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def QA_fetch_get_stock_list_special(date, id): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(date) == False): QA_util_log_info("wrong date") else: if id in ['big', 'small', 'cixin', 'yujing', 'rzrq', 'rq', 'yj', 'st', 'sst']: awgs = 'date=' + date + ';sectorid=' + \ data_list.wind_stock_list_special_id[id] data = w.wset("sectorconstituent", awgs) return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def start(self): '''the main function to perform all operations''' print('开始程序') w.start(60) #if cannot connect to wind, just stop if not w.isconnected(): print('无法连接万德') print('程序结束') w.stop() return #otherwise print('万德已连接') print('开始处理数据') #期货行情 today = datetime.now() print('今天是', today.strftime('%Y-%m-%d')) loop = asyncio.get_event_loop() exchanges = self.config['futures']['exchanges'] for eid in dict.keys(exchanges): instruments = exchanges[eid] instrument = None for instrument in instruments: print('处理品种 ' + instrument + ' 交易所 ' + eid) items = [] i = 0 for i in range(today.month, today.month + 13, 1): iid = instrument if i > 12: iid = iid + self.fix_instrument(str(today.year + 1)[2:] + \ pad_month(i % 12), eid) else: iid = iid + self.fix_instrument(str(today.year)[2:] + \ pad_month(i), eid) iid = iid + '.' + eid items.append(iid) tasks = [] for item in items: tasks.append(asyncio.ensure_future(self.process_item(w, item))) try: loop.run_until_complete(asyncio.wait(tasks)) except: print('循环产生异常:处理品种' + instrument + '期货价格数据') finally: print('完成品种 ' + instrument + ' 交易所 ' + eid) loop.close() print('程序结束') w.stop()
def __init__(self): try: #wind不会重复启动,也无需特别关闭 w.start() if w.isconnected: pass else: print('打开Wind失败') except: print('打开Wind时异常') #启动交易帐户 if VendorWind._tradeaccid[0]==0: self.tradelogon()
def QA_fetch_get_stock_shape(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: # history_low近期创历史新低,stage_high近期创阶段新高,history_high近期创历史新高,stage_low近期创阶段新高,up_days连涨天数,down_days连跌天数,breakout_ma向上有效突破均线,breakdown_ma向下有效突破均线,bull_bear_ma均线多空排列看涨看跌 data = w.wsd(name, "history_low,stage_high,history_high,stage_low,up_days,down_days,breakout_ma,breakdown_ma,bull_bear_ma", startDate, endDate, "n=3;m=60;meanLine=60;N1=5;N2=10;N3=20;N4=30;upOrLow=1") if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def QA_fetch_get_stock_xueqiu(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: data = w.wsd(name, "xq_accmfocus,xq_accmcomments,xq_accmshares,\ xq_focusadded,xq_commentsadded,xq_sharesadded,\ xq_WOW_focus,xq_WOW_comments,xq_WOW_shares", startDate, endDate, "") if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def QA_fetch_get_stock_day_simple(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: data = w.wsd(name, "sec_name,preclose,open,high,low,close,volume", startDate, endDate, "Fill=Previous;PriceAdj=F") #data=w.wsd("000002.SZ", "open,high,low,close,volume", "2017-03-03", "2017-04-01", "PriceAdj=B") QA_util_log_info(data.ErrorCode) if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return data.Data
def QA_fetch_get_stock_risk(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: data = w.wsd(name, "annualyeild_100w,annualyeild_24m,annualyeild_60m,\ annualstdevr_100w,annualstdevr_24m,annualstdevr_60m,beta_100w,\ beta_24m,beta_60m,avgreturn,avgreturny,stdevry,stdcof,\ risk_nonsysrisk1,r2,alpha2,beta,sharpe,treynor,jensen,jenseny,betadf", startDate, endDate, "period=2;returnType=1;index=000001.SH;yield=1") if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def main(): w.start() func1() func2() func3() func4() func5() func6() func7() func8() func9() func10() func11() #a=w.wsd('150074.SZ', 'nav','2015-12-11','2015-12-22') #print a #a=w.wsd('502022.SH', 'nav','2015-12-11','2015-12-22') #print a '''
def QA_fetch_get_trade_date(endDate, exchange): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() supportExchanges = ["SSE", "SZSE", "CFFEX", "SHFE", "DCE", "CZCE"] if (exchange in supportExchanges): #"SSE","SZSE","CFFEX","SHFE","DCE","CZCE" # 上海股票交易所,深圳股票交易所,中国金融期货交易所,上海期货交易所,大连商品交易所,郑州期货交易所 exchanges = "TradingCalendar=" + exchange data = w.tdays("1990-01-01", endDate, exchanges) # QA_util_log_info(data.Data) dates = pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times) else: QA_util_log_info("exchange name problem") return dates
def QA_fetch_get_stock_info(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() # get the all stock list on the endDate # judge the vaild date if(QA_util_date_valid(endDate) is False): QA_util_log_info("wrong date") else: # tempStr='date='+endDate+";sectorid=a001010100000000" # data=w.wset("sectorconstituent",tempStr) data = w.wsd(name, "sec_name,sec_englishname,ipo_date,exch_city,mkt,\ sec_status,delist_date,issuecurrencycode,curr,RO,parvalue,\ lotsize,tunit,exch_eng,country,concept,marginornot,SHSC,\ parallelcode,sec_type,backdoor,backdoordate,windtype", startDate, endDate) # QA_util_log_info(data) if (data.ErrorCode != 0): QA_util_log_info("Connent to Wind successfully") return data.Data
def QA_fetch_get_stock_indicator(name, startDate, endDate): try: from WindPy import w except: QA_util_log_info('No WindPY Module!') w.start() if(QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: # ADTM动态买卖气指标,ATR真实波幅,BBI多空指数,BBIBOLL多空布林线,BIAS乖离率,BOLL布林带,CCI顺势指标,CDP逆势操作,DMA平均线差, # DMI趋向标准,DPO区间震荡线,ENV,EXPMA指数平滑移动平均,KDJ随机指标,slowKD慢速kd,MA简单移动平均,MACD指数平滑移动平均,MIKE麦克指数, # MTM动力指标,PRICEOSC价格震荡指标,PVT量价趋势指标,RC变化率指数,ROC变动速率,RSI相对强弱指标,SAR抛物转向,SI摆动指标,SOBV能量潮, # SRMI MI修正指标,STD 标准差,TAPI 加权指数成交值,TRIX 三重指数平滑平均,VHF纵横指标,VMA量简单移动平均,VMACD量指数平滑移动平均, # VOSC成交量震荡,WVAD威廉变异离散量,vol_ratio量比 data = w.wsd(name, "ADTM,ATR,BBI,BBIBOLL,BIAS,BOLL,CCI,CDP,\ DMA,DMI,DPO,ENV,EXPMA,KDJ,slowKD,MA,MACD,\ MIKE,MTM,PRICEOSC,PVT,RC,ROC,RSI,SAR,SI,\ SOBV,SRMI,STD,TAPI,TRIX,VHF,VMA,VMACD,VOSC,\ WVAD,vol_ratio", startDate, endDate, "ADTM_N1=23;ADTM_N2=8;ADTM_IO=1;ATR_N=14;ATR_IO=1;\ BBI_N1=3;BBI_N2=6;BBI_N3=12;BBI_N4=24;BBIBOLL_N=10;\ BBIBOLL_Width=3;BBIBOLL_IO=1;BIAS_N=12;BOLL_N=26;\ BOLL_Width=2;BOLL_IO=1;CCI_N=14;CDP_IO=1;DMA_S=10;\ DMA_L=50;DMA_N=10;DMA_IO=1;DMI_N=14;DMI_N1=6;\ DMI_IO=1;DPO_N=20;DPO_M=6;DPO_IO=1;ENV_N=14;ENV_IO=1;\ EXPMA_N=12;KDJ_N=9;KDJ_M1=3;KDJ_M2=3;KDJ_IO=1;SlowKD_N1=9;\ SlowKD_N2=3;SlowKD_N3=3;SlowKD_N4=5;SlowKD_IO=1;MA_N=5;\ MACD_L=26;MACD_S=12;MACD_N=9;MACD_IO=1;MIKE_N=12;MIKE_IO=1;\ MTM_interDay=6;MTM_N=6;MTM_IO=1;PRICEOSC_L=26;PRICEOSC_S=12;\ RC_N=50;ROC_interDay=12;ROC_N=6;ROC_IO=1;RSI_N=6;SAR_N=4;\ SAR_SP=2;SAR_MP=20;SRMI_N=9;STD_N=26;TAPI_N=6;TAPI_IO=1;\ TRIX_N1=12;TRIX_N2=20;TRIX_IO=1;VHF_N=28;VMA_N=5;VMACD_S=12;\ VMACD_L=26;VMACD_N=9;VMACD_IO=1;VOSC_S=12;VOSC_L=26;WVAD_N1=24;\ WVAD_N2=6;WVAD_IO=1;VolumeRatio_N=5") if (data.ErrorCode == 0): QA_util_log_info("Connent to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
""" Created on Wed Jan 2 10:18:35 2019 @author: Administrator """ # -*- coding:utf-8 -*- #Python 3.5.0 from WindPy import w import pandas as pd import datetime import matplotlib.pyplot as plt import numpy as np import csv w.start(); future_name = 'T1812' start_date = '2018-08-11' end_date = "2018-11-11" #csvfile = open(future_name + '_day.csv', 'w') #writer = csv.writer(csvfile) # 取数据的命令如何写可以用命令生成器来辅助完成 #wsd_data=w.wsi(future_name+".CFE", "open,high,low,close,chg,volume,amt,oi", "2018-07-11", end_date, "Fill=Previous","BarSize=5") wsd_data=w.wsd(future_name+".CFE", "open,high,low,close,chg", "2018-07-11", end_date, "Fill=Previous")
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
######## coding=gbk __author__ = 'henry' import site import sys reload(sys) sys.setdefaultencoding( "utf-8" ) from WindPy import w from Utility import str1D from Utility import str2D if (w.isconnected() != True): w.start(); #pwd:607387 stock_code = sys.argv[1] start_date = sys.argv[2] end_date = sys.argv[3] # res= w.wset("TradeSuspend","startdate=20150122;enddate=20150122") fields = "sec_name,trade_status,susp_reason" res = w.wsd(stock_code, fields, start_date, end_date, "Fill=Previous") #1.List getStocks print "WindStockReuslt:" print res
from futureimporter import FutureImporter # from spotimporter import SpotImporter # from indeximporter import IndexImporter # from volumeimporter import VolumeImporter if __name__ == '__main__': '''main function''' print('开始程序') config = None with open('config.json', encoding='utf-8') as config_file: config = json.load(config_file, encoding='utf-8') if config is None: print('无法读取配置文件config.json') #move on to perform the actual job w.start(60) if not w.isconnected(): print('无法连接万德') else: print('万德已连接') print('开始处理数据') #grab the main loop loop = asyncio.get_event_loop() #future quotes upload future_importer = FutureImporter(loop, w, config) future_importer.run() # spot_importer = SpotImporter(loop, w, config) # spot_importer.run()
def __init__(self): w.start()
def wrap(data: pd.DataFrame, group_target: str, **kwargs): w.start() data = data.groupby(by=group_target, group_keys=False).apply( partial(fun, **kwargs)).reset_index(drop=True) w.close() return data
# -*- coding: utf-8 -*- """ Created on Wed Mar 25 14:17:37 2020 @author: 029046 """ from WindPy import w from DiscreteKalmanFilter import * from Functions import * from DynamicFactorModel import * w.start(waitTime=60) w.isconnected() plt.rcParams['figure.figsize'] = (9, 6) data = vintage_PPI data_sm = DataInterpolation(data, 0, len(data.index), 'linear').dropna(axis=0, how='any') data_dev = data_sm - data_sm.mean() dfm_em = DFM_EMalgo(data_sm, 10, 2, 3) #error = pd.DataFrame(data=rand_Matrix(len(data.index), 2),columns=['shock1','shock2'],index=data.index) error = pd.DataFrame(data=np.zeros(shape=(len(data.index), 2)), columns=['shock1', 'shock2'], index=data.index) kf = KalmanFilter(Z=data.iloc[6:] - data_sm.mean(), U=error, A=dfm_em.A, B=dfm_em.B,
def calRatio(index1, index2, startDate, endDate): w.start() #wsddata1 = w.wsd("000905.SH,000016.SH", "close", "2007-01-01", "2016-06-21", "") wsddata1 = wind_data = w.wsd("204001.SH", "close", "ED-3M", endDate, "") ret = getReturn(wsddata1.Times, wsddata1.Data[0])
def updatePal(palPath=None): start = time.time() md.init('18201141877', 'Wqxl7309') if not w.isconnected(): w.start() palPath = r'E:\bqfcts\bqfcts\data\Paltest' if palPath is None else palPath tempFilePath = os.path.join(palPath,'temp_files') if not os.path.exists(tempFilePath): os.mkdir(tempFilePath) matName = 'data_20150701_now.mat' savedPal = h5py.File(os.path.join(palPath,matName)) # print(read_cell(savedPal,'sec_names')) nextTrd = dt.datetime.strptime(str(int(savedPal['nexttrd'][0][0])),'%Y%m%d') nextTrdStr = nextTrd.strftime('%Y-%m-%d') updateTime = dt.datetime(nextTrd.year,nextTrd.month,nextTrd.day,15,30,0) if updateTime > dt.datetime.now(): print('not update time yet') return else: availableDateStr = md.get_last_dailybars('SHSE.000001')[0].strtime[:10] if int(availableDateStr.replace('-','')) <= int(nextTrdStr.replace('-','')): print('new data not avaliable yet') return else: print('will update from {0} to {1}'.format(nextTrdStr,availableDateStr)) betweenDays = [tdt.strtime[:10] for tdt in md.get_calendar('SHSE',nextTrdStr,availableDateStr)] if nextTrdStr!=availableDateStr: # 避免同一日期重复 betweenDays.append(availableDateStr) betweenDaysNumber = [int(tdt.replace('-','')) for tdt in betweenDays] newDateNum = len(betweenDaysNumber) # 更新前 先备份数据 backupPath = os.path.join(palPath,'backup') cpResult = os.system(r'COPY {0} {1} /Y'.format(os.path.join(palPath,matName),os.path.join(backupPath,matName))) assert cpResult==0,'backup failed' gmDateFmt = 'yyyy-mm-dd' # update indice indiceNames = ['sh','hs300','zz500','sz50'] indiceCodes = ['000001','000300','000905','000016'] symbols = ','.join(['SHSE.{}'.format(sbl) for sbl in indiceCodes]) indiceBars = md.get_dailybars(symbols,nextTrdStr,availableDateStr) for dumi,idx in enumerate(indiceNames): bars = indiceBars[dumi::4] idxret = np.array([bar.close for bar in bars])/np.array([bar.pre_close for bar in bars]) - 1 idxArray = np.array([betweenDaysNumber, [bar.open for bar in bars], [bar.high for bar in bars], [bar.low for bar in bars], [bar.close for bar in bars], [bar.volume for bar in bars], [bar.amount for bar in bars], idxret ]) # newIndex = np.column_stack([savedPal['index_{}'.format(idx)][:], idxArray]) pd.DataFrame(np.transpose(idxArray)).to_csv(os.path.join(tempFilePath,'index_{}.csv'.format(idx)),index=False,header=False) # update stock info nCut = savedPal['N_cut'][0][0] # 6000 nEnd = savedPal['N_end'][0][0] # last end date id ex.6732 stockNames = read_cell(savedPal, 'stockname') savedStkcdsGM = ['.'.join([stk[-2:]+'SE',stk[:6]]) for stk in stockNames] savedStkNum = len(stockNames) listedStkcdsWind = w.wset('sectorconstituent','date={};sectorid=a001010100000000'.format(availableDateStr)).Data[1] newStkcdsWind = sorted(list(set(listedStkcdsWind) - set(stockNames))) if newStkcdsWind: stockNames.extend( newStkcdsWind ) newStkIpos = [int(tdt.strftime('%Y%m%d')) for tdt in w.wss(newStkcdsWind, 'ipo_date').Data[0]] newIpoIds = [(w.tdayscount(nextTrd,str(ipo)).Data[0][0]+nEnd) for ipo in newStkIpos] newStockip = pd.DataFrame([[int(newStkcdsWind[dumi][:6]), newStkIpos[dumi], newIpoIds[dumi],0,0,0,0,0] for dumi in range(len(newStkcdsWind))]) newStockip.to_csv( os.path.join(tempFilePath,'stockip.csv'),index=False,header=False ) else: pd.DataFrame([]).to_csv(os.path.join(tempFilePath, 'stockip.csv'), index=False, header=False) newStkcdsGm = ['.'.join([stk[-2:]+'SE',stk[:6]]) for stk in newStkcdsWind] allStkcdsGM = savedStkcdsGM + newStkcdsGm # 全体股票包含已退市 与pal行数相同 # allSecNames = pd.DataFrame(w.wss(stockNames,'sec_name').Data[0]) allInstruments = md.get_instruments('SZSE', 1, 0) + md.get_instruments('SHSE', 1, 0) allInstrumentsDF = pd.DataFrame([[inds.symbol, inds.sec_name] for inds in allInstruments],columns=['symbol','sec_name']).set_index('symbol') allSecNames = allInstrumentsDF.loc[allStkcdsGM,'sec_name'] allSecNames.to_csv( os.path.join(tempFilePath, 'sec_names.csv'), index=False, header=False ) pd.DataFrame(newStkcdsWind).to_csv( os.path.join(tempFilePath, 'stockname.csv'), index=False, header=False ) # update trade info pages = ['date','open','high','low','close','volume','amount','pctchg','flow_a_share','total_share','adjfct','adjprc','isst'] newPal = {} for page in pages: newPal[page] = pd.DataFrame(np.zeros([len(allStkcdsGM), newDateNum]),index=allStkcdsGM,columns=betweenDays) lastPal = pd.DataFrame(savedPal['Pal'][:,-1,:],columns=savedStkcdsGM) barsDaily = md.get_dailybars(','.join(allStkcdsGM), nextTrdStr, availableDateStr) for bar in barsDaily: tdt = bar.strtime[:10] stk = '.'.join([bar.exchange,bar.sec_id]) newPal['date'].loc[stk, tdt] = int(tdt.replace('-','')) newPal['open'].loc[stk, tdt] = bar.open newPal['high'].loc[stk, tdt] = bar.high newPal['low'].loc[stk, tdt] = bar.low newPal['close'].loc[stk, tdt] = bar.close newPal['volume'].loc[stk, tdt] = bar.volume newPal['amount'].loc[stk, tdt] = bar.amount newPal['pctchg'].loc[stk, tdt] = bar.close/bar.pre_close - 1 # 计算自算复权因子 : 前一日收盘价*(1+当日收益率)/当日收盘价 s.t. (当日收盘价*当日复权因子)/前一日收盘价 = 1+ret # 若当日没有交易 : 沿用前一日 复权因子 循环外处理 # 若前一日没有交易 前一日收盘价 特殊处理: # 当日有交易 : 取停牌前最后一个交易日的 收盘价 # 当日没交易 没有退市 : 沿用前一日复权因子 循环外处理 # 当日没交易 已经退市 : 沿用前一日复权因子 循环外处理 # 若新股上市第一天 : 复权因子为1 if stk in newStkcdsGm: newPal['adjfct'].loc[stk, tdt] = 1 else: noTrdLast = (lastPal.loc[0, stk] == 0) if tdt == nextTrdStr else (newPal['date'].loc[stk, betweenDays[betweenDays.index(tdt) - 1]] == 0) if noTrdLast: # 前一日没交易 今日有交易(否则不应出现在bars里面) lastBar = md.get_last_n_dailybars(stk, 2, end_time=tdt)[-1] newPal['adjfct'].loc[stk, tdt] = lastPal.loc[15, stk] * lastBar.close * (1 + newPal['pctchg'].loc[stk, tdt]) / bar.close else: preClose = lastPal.loc[4,stk] if tdt==nextTrdStr else newPal['close'].loc[stk,betweenDays[betweenDays.index(tdt)-1]] newPal['adjfct'].loc[stk, tdt] = lastPal.loc[15, stk] * preClose * (1 + newPal['pctchg'].loc[stk, tdt]) / bar.close for dumi,tdt in enumerate(betweenDays): idx = newPal['adjfct'].loc[:,tdt]==0 idx = idx.values if tdt==nextTrdStr: newPal['adjfct'].loc[idx[:savedStkNum], tdt] = lastPal.loc[15,:].values[idx[:savedStkNum]] else: newPal['adjfct'].loc[idx, tdt] = newPal['adjfct'].loc[idx, betweenDays[dumi-1]] newPal['adjprc'] = newPal['adjfct']*newPal['close'] shareBar = md.get_share_index(','.join(allStkcdsGM), nextTrdStr, availableDateStr) for bar in shareBar: tdt = bar.pub_date stk = bar.symbol newPal['flow_a_share'].loc[stk, tdt] = bar.flow_a_share newPal['total_share'].loc[stk, tdt] = bar.total_share isST = np.array([int('ST' in sn) for sn in allSecNames.values]) newPal['isst'] = pd.DataFrame(np.repeat(np.reshape(isST,(isST.shape[0],1)),len(betweenDays),axis=1), index=allStkcdsGM, columns=betweenDays) for page in newPal: newPal[page].to_csv(os.path.join(tempFilePath,'{}.csv'.format(page)),index=False,header=False ) print('Pal temp files update finished with {0} stocks and {1} days in {2} seconds '.format(len(newStkcdsWind),len(betweenDays),time.time() - start))
def _windPyStart(): w.start() print("w.isconnected: {}".format(w.isconnected()))
def QA_fetch_get_stock_financial(name, startDate, endDate): with w.start(): pass
def AllStockHistoryData(self, symbols, start_date, end_date, step=0): print(self.getCurrentTime(), ": start to download stock data:") for symbol in symbols: w.start() try: stock = w.wsd( symbol, "trade_code,open_price,high_price,\ low_price,close_price,volumn,\ pre_close", start_date, end_date) index_data = pd.DataFrame() index_data['trade_code'] = stock.Times stock.Data[0] = symbol index_data['stock_code'] = stock.Data[0] index_data['open_price'] = stock.Data[1] index_data['high_price'] = stock.Data[2] index_data['low_price'] = stock.Data[3] index_data['close_price'] = stock.Data[4] index_data['volumn'] = stock.Data[5] index_data['pre_close'] = stock.Data[6] index_data['data_source'] = 'Wind' index_data['created_date'] = time.strftime('%Y-%m-%d %H:%M:%S'\ ,time.localtime(time.time())) index_data['updated_date'] = time.strftime('%Y-%m-%d %H:%M:%S'\ ,time.localtime(time.time())) index_data = index_data[index_data['open_price' > 0]] try: index_data.to_sql('stock_daily_data',\ engine,if_exists='append') except Exception as e: error_log = pd.DataFrame() error_log['trade_date'] = stock.Times error_log['stock_code'] = stock.Data[0] error_log['strat_date'] = start_date error_log['end_date'] = end_date error_log['status'] = None error_log['table'] = 'stock_daily_data' error_log['args'] = 'Symbol' + symbol + 'From'\ + start_date + 'To' + end_date error_log['error_info'] = e error_log['created_date'] = time.strftime(\ '%Y-%m-%d %H:%M:%S',time.localtime(time.time())) error_log.to_sql('stock_error_log',\ engine,if_exists='append') print(self.getCurrentTime(), ":SQL Exception: %s" % (e)) continue w.start() except Exception as e: error_log = pd.DataFrame() error_log['trade_date'] = stock.Times error_log['stock_code'] = stock.Data[0] error_log['strat_date'] = start_date error_log['end_date'] = end_date error_log['status'] = None error_log['table'] = 'stock_daily_data' error_log['args'] = 'Symbol' + symbol + 'From'\ + start_date + 'To' + end_date error_log['error_info'] = e error_log['created_date'] = time.strftime(\ '%Y-%m-%d %H:%M:%S',time.localtime(time.time())) error_log.to_sql('stock_error_log',\ engine,if_exists='append') print (self.getCurrentTime(),":index_data %s : Exception :%s" \ % (symbol,e) ) time.sleep(sleep_time) w.start() continue print(self.getCurrentTime(),": Downloading [",symbol,"] From "\ +start_date+" to "+end_date) print(self.getCurrentTime(), ": Symbols have been downloaded.")
def get_info_wind(code, fields, date): w.start() data = w.wss(code, fields, "tradeDate=%s"%(date)) df = pd.DataFrame(np.array(data.Data).T, index=[date], columns=data.Fields) return df
def dmpStkMin1FromWind(self, folder: str, sdate: int = 19900101, edate: int = 0, codes: list = None, isIndex: bool = False): from WindPy import w if edate == 0: edate = int(datetime.datetime.now().strftime('%Y%m%d')) if codes is None: if isIndex: codes = self.__indice__.keys() else: codes = self.__stocks__.keys() total = len(codes) endtime = str(edate) endtime = endtime[0:4] + "-" + endtime[4:6] + "-" + endtime[ 6:] + " 15:30:00" begintime = str(sdate) begintime = begintime[0:4] + "-" + begintime[4:6] + "-" + begintime[ 6:] + " 09:00:00" count = 0 w.start() for code in codes: count += 1 rawcode = code code = code[2:] + "." + code[:2] exchg = "" if rawcode[:2] == "SH": exchg = "SSE" else: exchg = "SZSE" print("正在拉取%s[%d/%d]的1分钟线数据[%d-%d]……" % (rawcode, count, total, sdate, edate)) if not os.path.exists(folder + 'min/' + exchg + "/"): create_dirs(folder + 'min/' + exchg + "/") csvpath = (folder + 'min/%s/%s.csv') % (exchg, rawcode[2:]) err, df_bars = w.wsi(code, fields="open,high,low,close,volume", beginTime=begintime, endTime=endtime, options="BarSize=1;Fill=Previous", usedf=True) if err != 0: print("拉取出错") print(df_bars) continue if len(df_bars) == 0: print("历史数据为空,跳过处理") continue print("正在写入数据文件") isEmpty = is_file_empty(csvpath) f = open(csvpath, 'a') if isEmpty: f.write("date, time, open, high, low, close, volumn\n") df_bars.fillna(0, inplace=True) content = "" for idx in range(0, len(df_bars)): curBar = df_bars.iloc[idx] bartime = df_bars.index[idx] curMin = int(bartime.strftime("%H%M")) if curMin == 1130 or curMin == 1500: continue if curMin == 1129 or curMin == 1459: nextBar = df_bars.iloc[idx + 1] if curBar["volume"] != 0: if nextBar["volume"] != 0: curBar["volume"] += nextBar["volume"] curBar["close"] = nextBar["close"] curBar["high"] = max(curBar["high"], nextBar["high"]) curBar["low"] = min(curBar["low"], nextBar["low"]) else: curBar = nextBar bartime = bartime + datetime.timedelta(minutes=1) line = "" line += (bartime.strftime("%Y/%m/%d") + ", ") line += (bartime.strftime("%H:%M:%S") + ", ") line += (str(curBar["open"]) + ",") line += (str(curBar["high"]) + ",") line += (str(curBar["low"]) + ",") line += (str(curBar["close"]) + ",") line += (str(curBar["volume"]) + "\n") content += line f.write(content) f.close() w.stop()
from WindPy import w import numpy as np from matplotlib import pyplot as plt w.start() # 默认命令超时时间为120秒,如需设置超时时间可以加入waitTime参数,例如waitTime=60,即设置命令超时时间为60秒 w.isconnected() # 判断WindPy是否已经登录成功 a = w.wsd("600030.SH", "CLOSE", "20190101", "20200101") # # closePriceList = a.Data[0] # print(closePriceList) # plt.plot(closePriceList) # plt.show() # wssdata=w.wss("600000.SH,600007.SH,600016.SH", "ev,total_shares","tradeDate=20151222;industryType=1") # print(wssdata) # # a=w.edb("M5567876", "2019-04-01", "2020-05-23","Fill=Previous") # print(a) # # w.edb("M5567876", "2019-04-01", "2020-05-23","Fill=Previous") # # b = w.edb("M0009805", "2019-06-21", "2020-06-20","Fill=Previous") b = w.edb("M5567876,M5567877,M5567878,M5567879,M0000612", "2019-07-20", "2020-07-19", "Fill=Previous") print(b) # dict1={} # # wind api # b = w.wss("000001.OF,000005.OF,000009.OF",
PlatformCode = '0000' #交易/模拟平台(经纪商)代码 Account = 'W109839501202' #交易/模拟账户名 Passwd = '000000' #交易/模拟账户名密码 Kind = 'RB1901.SHF' #交易的商品品种 monthlist = [4, 9, 12] #每年哪几个月的第二个星期五需要强制平仓 ##################### ### 初始化 ### AccountInfo = [PlatformCode, Account, Passwd] #模拟账户平台/账号/* LogFileName = GetDetailFileName( ) #根据月份获取log文件的文件名,格式为yyyymm_Everyday_Detail.txt DayFileName = "Day_Close.txt" #记录每天摘要结果的文件名 LastAct = 0 #上次操作的标识,1为开仓,-1为平仓 ActFlag = 0 #当天操作标识,1为开仓,-1为平仓,-2为强制全部平仓,-3为强制平仓后的休整期,2为休整后的开仓 timelist2 = TransTimeList(timelist) #转换时间节点列表为datetime格式的列表 w.start() #打开Wind-Python接口 Money, Pos, LastPX, LastAct, OriginMAFlag = ReadDayData( DayFileName, OriginAsset, Kind) #获取昨日收盘后信息:资金,持仓,收盘价,上次操作标识,均值交叉标识 LogFile = CheckLogFile(LogFileName) #获取Log文件 TodayMoney = Money #记录当天初始资金 TodayPos = Pos #记录当天初始仓位 NumAct = 0 #每天交易次数 PreMA5, PreMA10, PreMA20 = GetClosePX(DayFileName, Kind) #前4/9/19天收盘价预先求和 ### 开始当天操作,检查日期 ### TodayDate = str(datetime.date(datetime.now())) #获取当天时间,格式为yyyy-mm-dd #TodayDate = "2018-09-15" ActFlag = CheckFriday(TodayDate, monthlist) #检查当天是否强制平仓(-2),或在休整期(-3),或继续(0) print("%s Records of %s %s" % ('-' * 10, TodayDate, '-' * 10)) #分割线 ##for i in range(5): #测试运行n次
def crawl(cls): wind.start() result = cls._fetch_swstype() cls.store(result)
def initialize(self): w.start() self.initLogging() self.con = Connection().getConnection() self.cur = self.con.cursor()
def main(): product_gain = 0 date = input("输入日期\n") chart = "估值表_" + date file_dir = ".\\lists" # file directory all_csv_list = os.listdir(file_dir) # get csv list for single_csv in all_csv_list: if chart in single_csv: filename = single_csv # 将所有债券名称存到bonds中 # print(single_csv) dataset = pd.DataFrame(pd.read_excel(file_dir + "\\" + filename)) shishouziben = 0 bonds = [] wenxin = '' for index, row in dataset.iterrows(): # 找出债券名称 if str(row[0])[-2:] == 'SH' or str(row[0])[-2:] == 'SZ' or str( row[0])[-2:] == 'IB' and str( row[0])[-9] != '6' and not (row[14] != row[14]): bonds.append(row[1]) # 找出稳鑫表 elif "OTC" in str(row[0]): wenxin = row[1] elif str(row[0]) == "实收资本": shishouziben = float(row[6]) # print(bonds) # 得到稳鑫表名 temp = list(wenxin) temp.pop(-1) temp.pop(-3) temp.pop(-3) temp.pop(-11) temp.pop(-11) temp.pop(-11) temp.pop(-11) wenxin = ''.join(temp) wenxinname = wenxin + '_' + '[' + date[0:4] + '-' + date[ 4:6] + '-' + date[6:8] + ']' #print(wenxinname) for single_csv1 in all_csv_list: if wenxin in single_csv1: filename = single_csv1 break # 读取温馨表 dataset = pd.DataFrame(pd.read_excel(file_dir + "\\" + filename)) for index, row in dataset.iterrows(): # 找出债券名称 if len(str(row[0])) == 14: # print(row) if (row[1][0] >= '0' and row[1][0] <= '9'): bonds.append(row[1]) # 去除重复债券,all_bonds为债券名字集合 all_bonds = sorted(set(bonds), key=bonds.index) print(bonds) print(all_bonds) # 读取所有对账单 duizhangdan = merge_duizhang() # print(duizhangdan) # 遍历债券 w.start() #从wind提取所有债券名称和代码 #everything = (w.wset("sectorconstituent","date=2021-06-07;sectorid=a101010801000000;field=wind_code,sec_name")).Data everything = pd.read_excel("bonds.xlsx", encoding='gbk') for bond in all_bonds: for index, row in everything.iterrows(): if row[1] == bond: print(row) code = row[0] product_gain += bond_gain(bond, duizhangdan, code) break #计算产品绝对收益 print(product_gain) product_gainper = product_gain / shishouziben print(product_gainper)
######## coding=gbk __author__ = 'henry' import site import sys reload(sys) sys.setdefaultencoding( "utf-8" ) from WindPy import w from Utility import str1D from Utility import str2D if (w.isconnected() != True): w.start(); #pwd607387 stock_code = sys.argv[1] start_date = sys.argv[2] end_date = sys.argv[3] bar_size = sys.argv[4] fields = "open,close,high,low,volume,amt" print "WindStockReuslt:" #3.getKData #·ÖÖÓÐòÁÐKÏß # print w.wsi("000001.SZ", "open,close,high,low,volume,amt","2014-11-10 09:00:00", "2014-11-10 02:50:46", "") # print w.wsi("000001.SZ", "open,close,high,low,volume,amt","2014-11-10 09:00:00", "2014-11-10 11:50:46", "BarSize=30") # print w.wsd("000001.SZ", fields, "2014-11-17", "2014-11-20", "Fill=Previous") res = w.wsi(stock_code,fields,start_date,end_date,bar_size)
# -*- coding:utf-8 -*- from __builtin__ import str # Python 3.0 以上版本的用户请注释掉这一句 from WindPy import w # from datetime import * w.start(); # 命令如何写可以用命令生成器来辅助完成 # 定义打印输出函数,用来展示数据使用 def printpy(outdata): if outdata.ErrorCode != 0: print('error code:' + str(outdata.ErrorCode) + '\n'); return (); for i in range(0, len(outdata.Data[0])): strTemp = '' if len(outdata.Times) > 1: strTemp = str(outdata.Times[i]) + ' ' for k in range(0, len(outdata.Fields)): strTemp = strTemp + str(outdata.Data[k][i]) + ' ' print(strTemp) # 通过wsd来提取时间序列数据,比如取开高低收成交量,成交额数据 print('\n\n' + '-----通过wsd来提取时间序列数据,比如取开高低收成交量,成交额数据-----' + '\n') wsddata1 = w.wsd("000001.SZ", "open,high,low,close,volume,amt", "2015-11-22", "2015-12-22", "Fill=Previous") printpy(wsddata1) # 通过wsd来提取各个报告期财务数据
def __init__(self, rep, pub): super(WindServer, self).__init__(rep, pub) self.register(self.wsd) self.register(self.wsi) w.start()
def post(self): """ json str:{"tablename": "sectorconstituent", "options": "date=2017-03-21;sectorid=1000023121000000"} :return: 返回万得返回数据dict """ args = receive_wset_parser.parse_args() logger.info('/wset/ args:%s' % args) # print('args:%s' % args) # table_name = args['table_name'] # 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.wset(**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('wset(%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_count = len(ret_data.Data) # if data_count > 0: # print('ret_data.Fields\n', ret_data.Fields) # ret_data.Data[0] = [format_2_date_str(dt) for dt in ret_data.Data[0]] # print('ret_data.Data\n', ret_data.Data) for n_data in range(data_count): 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_date_str(dt) for dt in data ] logger.info('%d column["%s"] date to str', n_data, ret_data.Fields[n_data]) ret_df = pd.DataFrame(ret_data.Data, index=ret_data.Fields, columns=ret_data.Codes) # print('ret_df\n', ret_df) ret_dic = ret_df.to_dict() # print('ret_dic:\n', ret_dic) return ret_dic
def __init__(self): self.logger = mylog.set_log() mysql_con_demo = MysqlCon() self.engine = mysql_con_demo.getMysqlCon(flag='engine') w.start()
class Future(Security): ''' 期货类,继承了Security类 子类包括: ''' class IndexFuture(Future): ''' 股指期货,继承了Future类 ''' class Index(WindProduct): ''' 指数类,继承了WindProduct 子类包括:Sector(板块类),Industry(行业类) ''' class Sector(Index): ''' 板块类,继承了Index类。板块由行业组成 ''' class Industry(Index): 'Class for Industry. Child class of Index class.' # def query_weight(self, date): # 'return weight of constituent ' w.start(waitTime=120, showmenu=False)
def post(self): """ json str:{"codes": "M0017126,M0017127,M0017128", "begin_time": "2016-11-10", "end_time": "2017-11-10", "options": "Fill=Previous"} :return: 返回万得返回数据dict """ args = receive_edb_parser.parse_args() logger.info('/edb/ args:%s', args) codes = args['codes'] # 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.edb(**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_date_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=[xx.strip() for xx in codes.split(',')], columns=[format_2_date_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 import_smfund_daily(): w.start() today = date.today().strftime('%Y-%m-%d') engine = get_db_engine() sql_str = """select wind_code, ifnull(trade_date_max, fund_setupdate) date_start, class_a_code, class_b_code from wind_smfund_info fi left outer join (select code_p, adddate(max(trade_date), 1) trade_date_max from wind_smfund_daily group by code_p) smd on fi.wind_code = smd.code_p where fund_setupdate is not null and class_a_code is not null and class_b_code is not null""" df = pd.read_sql(sql_str, engine) df.set_index('wind_code', inplace=True) df_count = df.shape[0] print('df_count', df_count) index_start = 0 for i, code in enumerate(df.index): # 可调整 # [100:min([df_count, 200])] if i < index_start: continue print('%d/%d) %s start to import' % (i, df_count, code)) if type(df.loc[code, 'date_start']) not in (date, datetime, Timestamp): print('%d %s has no fund_setupdate will be ignored' % (i, code)) # print(df.iloc[i, :]) continue beginTime = df.loc[code, 'date_start'].strftime('%Y-%m-%d') field = "open,high,low,close,volume,amt,pct_chg" df_p = wsd_cache(w, code, field, beginTime, today, "") df_p.rename(columns=lambda x: x.swapcase(), inplace=True) df_p['code_p'] = code code_a = df.loc[code, 'class_a_code'] if code_a is None: print('%d %s has no code_a will be ignored' % (i, code)) # print(df.iloc[i, :]) continue df_a = wsd_cache(w, code_a, field, beginTime, today, "") df_a.rename(columns=lambda x: x.swapcase() + '_a', inplace=True) code_b = df.loc[code, 'class_b_code'] df_b = wsd_cache(w, code_b, field, beginTime, today, "") df_b.columns = df_b.columns.map(lambda x: x.swapcase() + '_b') new_df = pd.DataFrame() for d in df_p.index: time = d.date().strftime('%Y-%m-%d') field = "date=%s;windcode=%s;field=a_nav,b_nav,a_fs_inc,b_fs_inc,cur_interest,next_interest,tm_type,ptm_year,anal_pricelever,anal_navlevel,t1_premium,t2_premium,next_pcvdate,dq_status" % ( time, code) temp = wset_cache(w, "leveragedfundinfo", field) temp['date'] = d new_df = new_df.append(temp) new_df['next_pcvdate'] = new_df['next_pcvdate'].map(lambda x: x.date() if x is not None else x) new_df.set_index('date', inplace=True) one_df = pd.concat([df_p, df_a, df_b, new_df], axis=1) one_df.reset_index(inplace=True) # one_df['date'] = one_df['date'].map(lambda x: x.date()) one_df.rename(columns={'date': 'trade_date'}, inplace=True) one_df.set_index(['code_p', 'trade_date'], inplace=True) one_df.to_sql('wind_smfund_daily', engine, if_exists='append', index_label=['code_p', 'trade_date'], dtype={ 'code_p': String(20), 'trade_date': Date, 'next_pcvdate': Date, 'a_nav': Float, 'b_nav': Float, 'a_fs_inc': Float, 'b_fs_inc': Float, 'cur_interest': Float, 'next_interest': Float, 'ptm_year': Float, 'anal_pricelever': Float, 'anal_navlevel': Float, 't1_premium': Float, 't2_premium': Float, 'dq_status': String(50), 'open': Float, 'high': Float, 'low': Float, 'close': Float, 'volume': Float, 'amt': Float, 'pct_chg': Float, 'open_a': Float, 'high_a': Float, 'low_a': Float, 'close_a': Float, 'volume_a': Float, 'amt_a': Float, 'pct_chg_a': Float, 'open_b': Float, 'high_b': Float, 'low_b': Float, 'close_b': Float, 'volume_b': Float, 'amt_b': Float, 'pct_chg_b': Float, }) print('%d/%d) %s import success' % (i, df_count, code))
# %% mysql连接和配置 connect = pymysql.Connect( host='localhost', port=3306, user='******', passwd='123456', db='stock_data', charset='utf8' ) cursor = connect.cursor() # 获取游标 CreateTableInMysql() # %% mongodb连接和配置 conn = MongoClient('localhost', 27017) db = conn.stock_data # 连接mydb数据库,没有则自动创建 # %% wind连接和配置 field_list = ['open', 'high', "low", "close", "volume", "amt", "vwap"] w.start() # 启动连接 last_date = GetLastTradeDate() start_time = time.clock() UpdateAllStockDataFromWind() # 关闭连接 cursor.close() connect.close() end_time = time.clock() print('Running time: %s Seconds' % (end_time - start_time))
class WindDataLive(DataBase): from WindPy import w w.start() params = ( ('nocase', True), ('dataname', 'None'), ('dataname_live', 'None'), ('datetime', None), ('open', -1), ('high', -1), ('low', -1), ('close', -1), ('volume', -1), ('frequency', 'day'), ('security', 'None'), ('sleep', 1), ) def islive(self): '''Returns ``True`` to notify ``Cerebro`` that preloading and runonce should be deactivated''' return True def live(self): return True def __init__(self): super().__init__() self.backtest = True def start(self): super().start() self._idx = -1 s = self.p.dataname start_date = str(self.p.fromdate.date()) frequency = self.p.frequency user = config['user'] sql_password = config['password'] host = config['host'] port = config['port'] c = 'd_' + frequency + ',open,high,low,close,volume' if self.p.security == 'stock': if self.p.frequency == 'day': sql = "select "+c+" from md_day where stock = '"+ s[2:] +"' and d_day > '"+start_date+"'" self.security = 'stock' else: sql = "select "+c+" from md_" + frequency + "_" + s +" where d_"+ frequency+">'"+start_date+"'" self.security = 'stock' elif self.p.security == 'future': if self.p.frequency == 'day': sql = "select "+c+" from md_day where contract = '"+ s +"' and d_day > '"+start_date+"'" self.security = 'future' else: sql = "select "+c+" from md_" + frequency + "_" + s +" where d_"+ frequency+">'"+start_date+"'" self.security = 'future' elif self.p.security == 'index': if self.p.frequency == 'day': sql = "select "+c+" from md_day where index = '"+ s[2:] +"' and d_day > '"+start_date+"'" self.security = 'index' else: sql = "select "+c+" from md_" + frequency + "_" + s +" where d_"+ frequency+">'"+start_date+"'" self.security = 'index' self.conn = pymysql.connect(host = host, user = user, password = sql_password, database = 'md_' + self.security, port = port) self.cursor = self.conn.cursor() self.cursor.execute(sql) self.description = self.cursor.description def _load(self): self._idx += 1 if self.backtest: try: bar = self.cursor.fetchone() while None in bar: bar = self.cursor.fetchone() self.lines.datetime[0] = date2num(bar[0]) self.lines.open[0] = bar[1] self.lines.high[0] = bar[2] self.lines.low[0] = bar[3] self.lines.close[0] = bar[4] self.lines.volume[0] = bar[5] self.temp = bar[0] return True except TypeError: self.backtest = False self.conn.close() if self.p.sleep == 1: if datetime.datetime.now().time() > datetime.time(15,1,0): market_opentime = datetime.datetime.combine(datetime.date.today()+datetime.timedelta(days=1),datetime.time(9,30,0)) print('wait for market opening') time.sleep((market_opentime-datetime.datetime.now()).seconds) elif datetime.datetime.now() < datetime.datetime.combine(datetime.date.today(),datetime.time(9,30,0)): market_opentime = datetime.datetime.combine(datetime.date.today(),datetime.time(9,30,0)) print('wait for market opening') time.sleep((market_opentime-datetime.datetime.now()).seconds) else: if self.p.frequency == 'day': time.sleep(19800) else: print('等待数据') time.sleep(60*int(self.p.frequency[0:-3])) elif self.p.sleep == 0: pass s = self.p.dataname frequency = self.p.frequency now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') if self.p.security == 'stock': if frequency != 'day': data_wind = w.wsi(s, "open,high,low,close,volume", str(self.temp), now_time, "BarSize="+frequency+";periodstart=09:00:00;periodend="+datetime.datetime.now().strftime('%H:%M:%S')) else: data_wind = w.wsd(s, "open,high,low,close,volume", str(self.temp), now_time, "PriceAdj=B") elif self.p.security == 'future': if frequency != 'day': data_wind = w.wsi(s, "open,high,low,close,volume", str(self.temp), now_time, "BarSize="+frequency+";periodstart=09:00:00;periodend="+datetime.datetime.now().strftime('%H:%M:%S')) else: data_wind = w.wsd(s, "open,high,low,close,volume", str(self.temp), now_time, "PriceAdj=B") elif self.p.security == 'index': if frequency != 'day': data_wind = w.wsi(s, "open,high,low,close,volume", str(self.temp), now_time, "BarSize="+frequency+";periodstart=09:00:00;periodend="+datetime.datetime.now().strftime('%H:%M:%S')) else: data_wind = w.wsd(s, "open,high,low,close,volume", str(self.temp), now_time, "PriceAdj=B") self.times = data_wind.Times self.data = data_wind.Data if len(self.times) < 2: #print('中午休市,等待数据') return None bar = [self.times[1]] for i in self.data: if np.isnan(i[1]) == True: #print('停牌了') return None bar.append(i[1]) self.lines.datetime[0] = date2num(bar[0]) self.lines.open[0] = bar[1] self.lines.high[0] = bar[2] self.lines.low[0] = bar[3] self.lines.close[0] = bar[4] self.lines.volume[0] = bar[5] self.temp = bar[0] return True
def AStockHisData(self,symbols,start_date,end_date,step=0): ''' 逐个股票代码查询行情数据 wsd代码可以借助 WindNavigator自动生成copy即可使用;时间参数不设,默认取当前日期,可能是非交易日没数据; 只有一个时间参数时,默认作为为起始时间,结束时间默认为当前日期;如设置两个时间参数则依次为起止时间 ''' print(self.getCurrentTime(),": Download A Stock Starting:") for symbol in symbols: w.start() try: #stock=w.wsd(symbol,'trade_code,open,high,low,close,volume,amt',start_date,end_date) ''' wsd代码可以借助 WindNavigator自动生成copy即可使用; 时间参数不设,默认取当前日期,可能是非交易日没数据; 只有一个时间参数,默认为起始时间到最新;如设置两个时间参数则依次为起止时间 ''' stock=w.wsd(symbol, "trade_code,open,high,low,close,pre_close,volume,amt,dealnum,chg,pct_chg,vwap, adjfactor,close2,turn,free_turn,oi,oi_chg,pre_settle,settle,chg_settlement,pct_chg_settlement, lastradeday_s,last_trade_day,rel_ipo_chg,rel_ipo_pct_chg,susp_reason,close3, pe_ttm,val_pe_deducted_ttm,pe_lyr,pb_lf,ps_ttm,ps_lyr,dividendyield2,ev,mkt_cap_ard,pb_mrq,pcf_ocf_ttm,pcf_ncf_ttm,pcf_ocflyr,pcf_nflyr,trade_status", start_date,end_date) index_data = pd.DataFrame() index_data['trade_date']=stock.Times stock.Data[0]=symbol index_data['stock_code']=stock.Data[0] #index_data['stock_code'] =symbol index_data['open'] =stock.Data[1] index_data['high'] =stock.Data[2] index_data['low'] =stock.Data[3] index_data['close']=stock.Data[4] index_data['pre_close']=stock.Data[5] index_data['volume']=stock.Data[6] index_data['amt']=stock.Data[7] index_data['dealnum']=stock.Data[8] index_data['chg']=stock.Data[9] index_data['pct_chg']=stock.Data[10] #index_data['pct_chg']=index_data['pct_chg']/100 index_data['vwap']=stock.Data[11] index_data['adj_factor']=stock.Data[12] index_data['close2']=stock.Data[13] index_data['turn']=stock.Data[14] index_data['free_turn']=stock.Data[15] index_data['oi']=stock.Data[16] index_data['oi_chg']=stock.Data[17] index_data['pre_settle']=stock.Data[18] index_data['settle']=stock.Data[19] index_data['chg_settlement']=stock.Data[20] index_data['pct_chg_settlement']=stock.Data[21] index_data['lastradeday_s']=stock.Data[22] index_data['last_trade_day']=stock.Data[23] index_data['rel_ipo_chg']=stock.Data[24] index_data['rel_ipo_pct_chg']=stock.Data[25] index_data['susp_reason']=stock.Data[26] index_data['close3']=stock.Data[27] index_data['pe_ttm']=stock.Data[28] index_data['val_pe_deducted_ttm']=stock.Data[29] index_data['pe_lyr']=stock.Data[30] index_data['pb_lf']=stock.Data[31] index_data['ps_ttm']=stock.Data[32] index_data['ps_lyr']=stock.Data[33] index_data['dividendyield2']=stock.Data[34] index_data['ev']=stock.Data[35] index_data['mkt_cap_ard']=stock.Data[36] index_data['pb_mrq']=stock.Data[37] index_data['pcf_ocf_ttm']=stock.Data[38] index_data['pcf_ncf_ttm']=stock.Data[39] index_data['pcf_ocflyr']=stock.Data[40] index_data['pcf_ncflyr']=stock.Data[41] index_data['trade_status']=stock.Data[42] index_data['data_source']='Wind' index_data['created_date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) index_data['updated_date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) index_data = index_data[index_data['open'] > 0] #index_data.fillna(0) try: index_data.to_sql('stock_daily_data',engine,if_exists='append'); except Exception as e: #如果写入数据库失败,写入日志表,便于后续分析处理 error_log=pd.DataFrame() error_log['trade_date']=stock.Times error_log['stock_code']=stock.Data[0] error_log['start_date']=start_date error_log['end_date']=end_date error_log['status']=None error_log['table']='stock_daily_data' error_log['args']='Symbol: '+symbol+' From '+start_date+' To '+end_date error_log['error_info']=e error_log['created_date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) error_log.to_sql('stock_error_log',engine,if_exists='append') print ( self.getCurrentTime(),": SQL Exception :%s" % (e) ) continue w.start() except Exception as e: #如果读取处理失败,可能是网络中断、频繁访问被限、历史数据缺失等原因。写入相关信息到日志表,便于后续补充处理 error_log=pd.DataFrame() error_log['trade_date']=stock.Times error_log['stock_code']=stock.Data[0] error_log['start_date']=start_date error_log['end_date']=end_date error_log['status']=None error_log['table']='stock_daily_data' error_log['args']='Symbol: '+symbol+' From '+start_date+' To '+end_date error_log['error_info']=e error_log['created_date']=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) error_log.to_sql('stock_error_log',engine,if_exists='append') print ( self.getCurrentTime(),":index_data %s : Exception :%s" % (symbol,e) ) time.sleep(sleep_time) w.start() continue print(self.getCurrentTime(),": Downloading [",symbol,"] From "+start_date+" to "+end_date) print(self.getCurrentTime(),": Download A Stock Has Finished .")
def QA_fetch_get_stock_financial(name, startDate, endDate): from WindPy import w # w.start() w.start() w.isconnected() pass
def __enter__(self): w.start() return self
from WindPy import w as wind import re import time from engine import * wind.start() # 启动wind now = time.strftime("%Y-%m-%d") def jrbbb(jr): yy = "'J172220.OF','JR132245','XT1703499.XT','JR135238', 'XT1703378.XT','JR136323', 'XT1703377.XT','JR058300', 'XT1703375.XT','JR122820', 'XT1703264.XT','JR049705', 'XT1703262.XT','JR040919', 'XT1703182.XT','JR126682', 'XT1703070.XT','JR002194', 'XT1702907.XT','JR131724', 'XT1702906.XT','JR126710', 'XT1702905.XT','JR020944', 'XT1702776.XT','JR001403', 'XT1702709.XT','JR126360', 'XT1702606.XT','JR132113', 'XT1702605.XT','JR126669', 'XT1702604.XT','JR126636', 'XT1702603.XT','JR126638', 'XT1702601.XT','JR126998', 'XT1702600.XT','JR127004', 'XT1702599.XT','JR127005', 'XT1702598.XT','JR131917', 'XT1702461.XT','JR131815', 'XT1702455.XT','JR128119', 'XT1702452.XT','JR136259', 'XT1702349.XT','JR101947', 'XT1702344.XT','JR101954', 'XT1702274.XT','JR132029', 'XT1702273.XT','JR029247', 'XT1702258.XT','JR131780', 'XT1702249.XT','JR131754', 'XT1702248.XT','JR126478', 'XT1702228.XT','JR132189', 'XT1702227.XT','JR132190', 'XT1702226.XT','JR132191', 'XT1702225.XT','JR132192', 'XT1702224.XT','JR132193', 'XT1702223.XT','JR132194', 'XT1702222.XT','JR132195', 'XT1702174.XT','JR131838', 'XT1702146.XT','JR132177', 'XT1702113.XT','JR134563', 'XT1702012.XT','JR132185', 'XT1701960.XT','JR132221', 'XT1701959.XT','JR132222', 'XT1701899.XT','JR043956', 'XT1701847.XT','JR132186', 'XT1701815.XT','JR126562', 'XT1701814.XT','JR126442', 'XT1701786.XT','JR132213', 'XT1701731.XT','JR132215', 'XT1701698.XT','JR101951', 'XT1701691.XT','JR132214', 'XT1701498.XT','JR101950', 'XT1701154.XT','JR073347', 'XT1700828.XT','JR128135', 'XT1700697.XT','JR118653', 'XT1700618.XT','JR029899', 'XT1700294.XT','JR015847', 'XT1700249.XT','JR041459', 'XT1700120.XT','JR068530', 'XT1623847.XT','JR132196', 'XT1623554.XT','JR125796', 'XT1623386.XT','JR116547', 'XT1623286.XT','JR116589', 'XT1623158.XT','JR125840', 'XT1622941.XT','JR113824', 'XT1622776.XT','JR073511', 'XT1622764.XT','JR084324', 'XT1622122.XT','JR115408', 'XT1622062.XT','JR115578', 'XT1621773.XT','JR000797', 'XT1621631.XT','JR065550', 'XT1620582.XT','JR103587', 'XT1620312.XT','JR103204', 'XT1619594.XT','JR108594', 'XT1619418.XT','JR103298', 'XT1618352.XT','JR074575', 'XT1617384.XT','JR116102', 'XT1617247.XT','JR104643', 'XT1616417.XT','JR105052', 'XT1615601.XT','JR087048', 'XT1615364.XT','JR001260', 'XT1615214.XT','JR119390', 'XT1615213.XT','JR035874', 'XT1615212.XT','JR036435', 'XT1614258.XT','JR092151', 'XT1613828.XT','JR051081', 'XT1611709.XT','JR087127', 'XT1610612.XT','JR109365', 'XT1609628.XT','JR062545', 'XT1608951.XT','JR002320', 'XT1608950.XT','JR041339', 'XT1607512.XT','JR026903', 'XT1607427.XT','JR116380', 'XT1606781.XT','JR000526', 'XT1606160.XT','JR011122', 'XT1605966.XT','JR026200', 'XT1604822.XT','JR010904', 'XT1604180.XT','JR010903', 'XT1603670.XT','JR010655', 'XT1600738.XT','JR027830', 'XT1600692.XT','JR009760', 'XT1600476.XT','JR107450', 'XT1600414.XT','JR009421', 'XT1600413.XT','JR009420', 'XT1600412.XT','JR009419', 'XT1600411.XT','JR009418', 'XT1600410.XT','JR009417', 'XT1600409.XT','JR009416', 'XT1600408.XT','JR009415', 'XT1600407.XT','JR009414', 'XT1600406.XT','JR009413', 'XT1600405.XT','JR009412', 'XT1525337.XT','JR011083', 'XT1524968.XT','JR126271', 'XT1522783.XT','JR022444', 'XT1522350.XT','JR022385', 'XT1521929.XT','JR020288', 'XT1519863.XT','JR022269', 'XT1519278.XT','JR018950', 'XT1519072.XT','JR020384', 'XT1518369.XT','JR020643', 'XT1517794.XT','JR019083', 'XT1517586.XT','JR007242', 'XT1517561.XT','JR019124', 'XT1517263.XT','JR019646', 'XT1517221.XT','JR019256', 'XT1515811.XT','JR015323', 'XT1515581.XT','JR019349', 'XT1515440.XT','JR016357', 'XT1515324.XT','JR002175', 'XT1513445.XT','JR016251', 'XT1513444.XT','JR017082', 'XT1513353.XT','JR014481', 'XT1513304.XT','JR013684', 'XT1513280.XT','JR014275', 'XT1513261.XT','JR014199', 'XT1513249.XT','JR014200', 'XT1512441.XT','JR013298', 'XT1512014.XT','JR014458', 'XT1511967.XT','JR016358', 'XT1511852.XT','JR015442', 'XT1511548.XT','JR016088', 'XT1511072.XT','JR015471', 'XT1510999.XT','JR030565', 'XT1510648.XT','JR014228', 'XT1510413.XT','JR014934', 'XT1510001.XT','JR007052', 'XT1509966.XT','JR013850', 'XT1509529.XT','JR045951', 'XT1508962.XT','JR016502', 'XT1507419.XT','JR013774', 'XT1507236.XT','JR034917', 'XT1506866.XT','JR027330', 'XT1505930.XT','JR005469', 'XT1505805.XT','JR012667', 'XT1505483.XT','JR009930', 'XT1502896.XT','JR012985', 'XT1502703.XT','JR027296', 'XT1502335.XT','JR012762', 'XT1501436.XT','JR001174', 'XT1501413.XT','JR078633', 'XT1501316.XT','JR012836', 'XT1501269.XT','JR012778', 'XT1501232.XT','JR027284', 'XT1501144.XT','JR109419', 'XT1501143.XT','JR134483', 'XT1500941.XT','JR101000', 'XT149321.XT','JR022936', 'XT145167.XT','JR004463', 'XT145163.XT','JR131631', 'XT144700.XT','JR112536', 'XT143128.XT','JR048971', 'XT142961.XT','JR002609', 'XT1414339.XT','JR005236', 'XT1412185.XT','JR027394', 'XT1411346.XT','JR080054', 'XT141040.XT','JR027187', 'XT1410225.XT','JR018705', 'XT134338.XT','JR105182', 'XT134245.XT','JR005661', 'XT130923.XT','JR000048', 'XT125375.XT','JR003680', 'XT125374.XT','JR058618', 'XT125311.XT','JR082302', 'XT125269.XT','JR003746', 'XT123370.XT','JR004154', 'XT122359.XT','JR006874', 'XT1205897.XT','JR116035', 'XT115084.XT','JR018948', 'XT113162.XT','JR100579', 'XT112030.XT','JR100188', 'XT102636.XT','JR003655', 'XT102211.XT','JR121387', 'XT090745.XT','JR003110', 'XT090712.XT','JR003140', 'XT090384.XT','JR003097', 'XT080123.XT','JR099734', 'XT071130.XT','JR054799', 'XT070415.XT','JR001759', 'XT0300891.XT','JR078639', 'XT1702235.XT','JR123230', 'XT1702116.XT','JR132804', 'XT1701704.XT','JR043391', 'XT1701495.XT','JR126811', 'XT1700771.XT','JR126938', 'XT1700557.XT','JR132238', 'XT1623578.XT','JR117844', 'XT1621451.XT','JR132333', 'XT1621450.XT','JR132330', 'XT1620974.XT','JR103141', 'XT1620558.XT','JR116971', 'XT1618884.XT','JR116052', 'XT1616801.XT','JR105303', 'XT1616655.XT','JR119273', 'XT1616112.XT','JR132408', 'XT1611791.XT','JR075406', 'XT1611366.XT','JR098521', 'XT1606638.XT','JR132580', 'XT1606493.XT','JR099775', 'XT1606054.XT','JR060675', 'XT1606053.XT','JR054983', 'XT1606052.XT','JR049455', 'XT1606051.XT','JR077940', 'XT1600552.XT','JR100444', 'XT1525557.XT','JR079803', 'XT1520281.XT','JR099591', 'XT1520280.XT','JR078088', 'XT1520279.XT','JR099590', 'XT1517768.XT','JR027598', 'XT1517493.XT','JR101642', 'XT1516243.XT','JR128744', 'XT1515180.XT','JR100026', 'XT1512611.XT','JR073308', 'XT1511633.XT','JR027554', 'XT1511527.XT','JR118121', 'XT1509045.XT','JR027497', 'XT1508043.XT','JR101641', 'XT1506310.XT','JR100923', 'XT1501578.XT','JR005819', 'XT146866.XT','JR051689', 'XT146373.XT','JR012217', 'XT1411550.XT','JR099763', 'XT1410032.XT','JR099762', 'XT140108.XT','JR118746', 'XT135521.XT','JR044807', 'XT134420.XT','JR003961', 'XT131714.XT','JR099756', 'XT131241.XT','JR099755', 'XT131121.XT','JR101148', 'XT1306936.XT','JR034104', 'XT115236.XT','JR041724', 'XT101617.XT','JR079854', 'XT090908.XT','JR078054', 'XT070393.XT','JR003135', 'XT070222.XT','JR003134'," y = re.findall(r'{}\',\'(\w+\d+)\''.format(jr), yy) return y def crawl_benchmark(id): # 定义方法 tmp = wind.wsd(id, "NAV_date,nav,NAV_acc", "ED", now, "") # 万得API用法 df = pd.DataFrame(tmp.Data) # 用结果(list)去构造一个DataFrame二维表 df = df.T # 把表转置 print(df) # ID1=jrbbb(id) # ID=str(ID1[0]) ID = id A = str(df.iloc[0, 0]) if A == "None": R = "None" B = str(df.iloc[0, 1]) C = str(df.iloc[0, 2]) return (ID, R, B, C)
import sys import pandas as pd from WindPy import w as wind wind.start() _PREVIOUS_DAY = 0 _CODE_MAP = { "000300.SH": "hs300", "000905.SH": "csi500", "000016.SH": "sse50", "000002.SH": "ssia", "H11011.CSI": "cbi", "NH0100.NHF": "nfi", # "M1001940.NHF": "y1_treasury_rate", "M1001940": "y1_treasury_rate" } _CODE_MAP = { "000300.SH": "hs300", "000905.SH": "csi500", "000016.SH": "sse50", "000002.SH": "ssia", "H11011.CSI": "cbi", "NH0100.NHF": "nfi", # "M1001940.NHF": "y1_treasury_rate", "M1001940": "y1_treasury_rate" } def sql_cols(df, usage="sql"): cols = tuple(df.columns)
import os,time from WindPy import w import PerformanceAnalytics ###设置工作路径 path = os.getcwd() os.chdir(path) os.chdir("E:\\Github\\AdvancedPython\\PythonInAction\\Task1") ###读取数据 #托管产品数据 AllNetValueData = pd.read_excel('副本产品净值.xlsx','SQL Results',index_col = None, na_values=['NA']) #沪深300指数数据 HS300Data = pd.read_csv('沪深300指数.csv') """ 从Wind中提取沪深300指数数据 w.start() HS300 = w.wsd("000300.SH","close",'20100101','20161223') HS300Data = HS300.Data HS300Data = HS300Data[0] HS300Times = HS300.Times data = pd.DataFrame({'date_line':HS300Times,'index_capital_line':HS300Data}) data.to_csv('沪深300指数.csv',index=None) """ ###计算产品评价指标 AllProduct = np.unique(AllNetValueData['VC_CPMC']) result = pd.DataFrame(AllProduct,columns = ['产品名称'],index = AllProduct) #数据清洗(处理日期数据) for i in range(len(HS300Data.index)):
# -*- coding: utf-8 -*- """ Created on Mon Jun 27 15:31:50 2016 @author: gsyuan """ import PyWind as pw from WindPy import w import smtplib from email.mime.text import MIMEText from email.header import Header w.start() TRADEUNIT = {'A.DCE': '10', 'MA.CZC': '10', 'RI.CZC': '20', 'RM.CZC': '10', 'FG.CZC': '20', 'J.DCE': '100', 'PM.CZC': '50', 'CU.SHF': '5', 'LR.CZC': '20', 'P.DCE': '10', 'BB.DCE': '500', 'AL.SHF': '5', 'HC.SHF': '10', 'OI.CZC': '10', 'JM.DCE': '60', 'AG.SHF': '15',
import datetime from pandas import Series from pandas import DataFrame import pandas as pd from WindPy import w index_code = "399006.SZ" benchmark_code = index_code w.start(showmenu=False) wsd_data = w.wsd(index_code, "close, pct_chg", "2011-01-01", "2016-08-23", "") dates = wsd_data.Times index_df = DataFrame(wsd_data.Data, index=wsd_data.Fields, columns=wsd_data.Times).T index_df['nav'] = (index_df['PCT_CHG'] / 100 + 1).cumprod() pe_df = DataFrame() for date in dates: date = datetime.datetime.strftime(date, '%Y-%m-%d') cons_code = w.wset("sectorconstituent", "date=%s;windcode=%s" % (date, index_code)).Data[1] code_str = ','.join(cons_code) wsd_data = w.wsd(code_str, 'pe_ttm', date, date, "") print(wsd_data) pe_df[date] = (DataFrame(wsd_data.Data, index=[date], columns=wsd_data.Codes).T).loc[:, date] print(pe_df)