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 func9(): yesterday = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") df_discount_ratio_M = pd.read_csv('discount_ratio_M.csv') last_date_discount_ratio_M = time.strftime("%Y-%m-%d",time.strptime(str(int(df_discount_ratio_M.iloc[-1,0])),"%Y%m%d")) day_gap_discount_ratio_M = w.tdayscount(last_date_discount_ratio_M, yesterday).Data[0][0]-1 w_wsd_data={} print 9 stock_ls=[] for stock in universe: if stock[0]=='1': stock = stock + '.SZ' else: stock = stock + '.SH' mA_code = w.wsd(stock, 'fund_smfcode', yesterday).Data[0][0] stock_ls.append(mA_code) w_wsd_data[mA_code] = w.wsd(mA_code, 'anal_tdiscountratio', (w.tdaysoffset(1,last_date_discount_ratio_M).Data[0][0]).strftime("%Y-%m-%d"),yesterday) for i in xrange(day_gap_discount_ratio_M): this_day = w.tdaysoffset(i+1,last_date_discount_ratio_M) ls = [] this_day_csv = (this_day.Data[0][0]).strftime("%Y%m%d") for stock in stock_ls: ls.append(w_wsd_data[stock].Data[0][i]) f=open('discount_ratio_M.csv','a') f.write(this_day_csv) for stock_data in ls: f.write(",") f.write(str(stock_data)) f.write("\n") f.close() print 'finish'
def wsd(tickers, fields, startdate, enddate): if isinstance(tickers,str): tickers = tickers.replace(',','').split() if isinstance(fields,str): fields = fields.replace(',','').split() if len(tickers) == 1: tmp = w.wsd(tickers, fields, startdate, enddate) return pd.DataFrame(dict(zip(fields, tmp.Data)),index = tmp.Times) elif len(fields) == 1: tmp = w.wsd(tickers, fields, startdate, enddate) return pd.DataFrame(dict(zip(tickers, tmp.Data)),index = tmp.Times) else: print 'cannot surrport multiple code with multiple fields'
def func11(): yesterday = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") df_industry=pd.read_csv('industry.csv') last_date_industry = time.strftime("%Y-%m-%d",time.strptime(str(int(df_industry.iloc[-1,0])),"%Y%m%d")) day_gap_industry = w.tdayscount(last_date_industry, yesterday).Data[0][0]-1 print 11 w_wsd_data={} stock_ls=[] for stock in list(df_industry.columns.values)[1:] : stock_ls.append(stock) w_wsd_data[stock] = w.wsd(stock, 'close', (w.tdaysoffset(1,last_date_industry).Data[0][0]).strftime("%Y-%m-%d"),yesterday) print w_wsd_data f=open('industry.csv','a') for i in xrange(day_gap_industry): this_day = w.tdaysoffset(i+1,last_date_industry) ls = [] this_day_csv = (this_day.Data[0][0]).strftime("%Y%m%d") for stock in stock_ls: ls.append(w_wsd_data[stock].Data[0][i]) f.write(this_day_csv) for stock_data in ls: f.write(",") f.write(str(stock_data)) f.write("\n") f.close() print 'finish'
def vpup(): '提取持仓与价格变动超过一定幅度的期货合作信息' codes = pw.getCodes() dic = {} for code in codes: data = w.wsd(code, "close,pct_chg,oi,oi_chg", "ED-1TD").Data if isinstance((data[1][-1]), float): price = data[0][-1] price_chg = data[1][-1] oi = data[2][-1] oi_chg = data[3][-1] else: price = data[0][-2] price_chg = data[1][-2] oi = data[2][-2] oi_chg = data[3][-2] if oi != 0: oichg = oi_chg / oi else: oichg = 0 if abs(oichg) > 0.05 and abs(price_chg) > 1.5: dic[code] = ' '.join(['Oichg:%.2f%%' % (100 * oichg), 'Oi:%.0f' % oi, 'Pricechg:%.2f%%' % price_chg, 'Price:%.0f' % price]) else: pass return dic
def GetSWexponent(ExponentID=None,BeginTime=None,EndTime=None): if(ExponentID==None): print("沒有传入申万之一指数代码,数据将返回所有申万一级指数的数据") ExponentID = '801010.SI,801020.SI,801030.SI,\ 801040.SI,801050.SI,801080.SI,801110.SI,\ 801120.SI,801130.SI,801140.SI,801150.SI,\ 801160.SI,801170.SI,801180.SI,801200.SI,\ 801210.SI,801230.SI,801710.SI,801720.SI,\ 801730.SI,801740.SI,801750.SI,801760.SI,\ 801770.SI,801780.SI,801790.SI,801880.SI,\ 801890.SI' if(BeginTime==None): BeginTime = '2010-01-01' if(EndTime==None): EndTime=date.today(); tmp = (ExponentID,'close',BeginTime,EndTime,'Fill=Previous') data = w.wsd(*tmp) Value = [] for i in range(0,len(data.Codes)): for j in range(0,len(data.Data[0])): value = data.Data[i][j]; if isnan(value): value=0.0 Value.append((str(data.Codes[i]),data.Times[j].strftime("%Y-%m-%d"),str(value))) for n in Value: print(n) return Value
def IndustryConfiguration(fundid=None, industry=None, begintime='2010-01-01', endtime=date.today()): if(fundid == None): print("没选择基金,默认为全部基金") fundid = FundID() if(industry == None): print("未选择行业,默认为全部19个新证监会行业指标") industry = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,15,16, 17, 18, 19] Value = [] for k in range(0, len(industry)): param = (fundid, 'prt_stockvalue_industrytoasset2', begintime, endtime, 'industry=' + str(industry[k]), 'Period=Q', 'Fill=Previous') data = w.wsd(*param) if(data.ErrorCode != 0): print("Wind Error") for i in range(0, len(data.Codes)): for j in range(0, len(data.Data[0])): value = data.Data[i][j] if(value is None or isnan(value)): value = 0.0 record = (data.Codes[i], data.Times[j].strftime("%Y-%m-%d"), industry[k], value) Value.append(record) return Value
def getUpdateCode(self,stockCodeList): '''得到直接插入和复权的代码''' reRightCode = [] # 需要復權的股票 # 得到数据库中最新一天的收盘价 closeList = [] for code in stockCodeList: sql = "select close from stockData_day where stockCode = %s order by date desc limit 1" self.cur.execute(sql,code) result = self.cur.fetchone() if not result: closeList.append(0.0) else: closeList.append(result[0]) # 查找今天的數據 fields = ['pre_close'] today = datetime.date.today().strftime('%Y%m%d') #今天的日期 option = 'showblank=0.0;PriceAdj=F' # 要進行前復權處理 wsd_data = w.wsd(stockCodeList,fields,today,today,option) if wsd_data.ErrorCode==0: # 返回成功 stockData = wsd_data.Data[0] ifEqualList = (np.array(stockData) == np.array(closeList)) for i in range(len(ifEqualList)): if not ifEqualList[i]: rate = (stockData[i] - closeList[i]) / closeList[i] reRightCode.append([stockCodeList[i],rate]) print '需要复权的数量为' print len(reRightCode) print '需要复权的股票代码为:' print reRightCode logging.info('需要复权的股票代码为:%s' % reRightCode) return reRightCode
def import_data_from_wind(d_start, d_end, file_folder='C:\\dev\\src\\ktlib\\data3\\', freq='d', contracts=[]): df = load_live_cont(d_start, d_end) contList = [str(inst) for inst in df.index] startDates = df.start_date endDates = df.expiry fields = 'open,close,high,low,volume,oi' if len(contracts) !=0: contList = contracts #w.start() for cont in contList: if cont[1].isalpha(): key = cont[:2] else: key = cont[:1] ex = 'SH' for exch in product_code.keys(): if key in product_code[exch]: ex = exch ticker = cont + '.' + ex mth = int(cont[-2:]) if (key not in contMonth): continue if mth not in contMonth[key]: continue start_d = max(d_start, startDates.ix[cont]) end_d = min(d_end, endDates.ix[cont]) try: if freq == 'm': print "loading min data for ticker = %s" % ticker raw_data = w.wsi(ticker,fields,start_d,end_d) if len(raw_data.Data)>1: outfile = file_folder + cont+'_min.csv' output={'datetime':raw_data.Times, 'open':raw_data.Data[2], 'close':raw_data.Data[3], 'high':raw_data.Data[4], 'low':raw_data.Data[5], 'volume':raw_data.Data[6], 'openInterest':raw_data.Data[7]} dump2csvfile(output,outfile) else: print "no min data obtained for ticker=%s" % ticker else: print "loading daily data for ticker = %s" % ticker raw_data = w.wsd(ticker,fields,start_d,end_d) if len(raw_data.Data)>1: outfile = file_folder + cont+'_daily.csv' output={'datetime':raw_data.Times, 'open':raw_data.Data[0], 'close':raw_data.Data[1], 'high':raw_data.Data[2], 'low':raw_data.Data[3], 'volume':raw_data.Data[4], 'openInterest':raw_data.Data[5]} dump2csvfile(output,outfile) else: print "no daily data obtained for ticker=%s" % ticker except ValueError: pass w.stop() return True
def func2(): yesterday = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") df_openA=pd.read_csv('openA.csv') last_date_openA = time.strftime("%Y-%m-%d",time.strptime(str(int(df_openA.iloc[-1,0])),"%Y%m%d")) day_gap_openA = w.tdayscount(last_date_openA, yesterday).Data[0][0]-1 print 2 w_wsd_data={} stock_ls=[] for stock in universe: if stock[0]=='1': stock = stock + '.SZ' else: stock += '.SH' stock_ls.append(stock) w_wsd_data[stock] = w.wsd(stock, 'open', (w.tdaysoffset(1,last_date_openA).Data[0][0]).strftime("%Y-%m-%d"),yesterday) for i in xrange(day_gap_openA): this_day = w.tdaysoffset(i+1,last_date_openA) ls = [] this_day_csv = (this_day.Data[0][0]).strftime("%Y%m%d") for stock in stock_ls: ls.append(w_wsd_data[stock].Data[0][i]) f=open('openA.csv','a') f.write(this_day_csv) for stock_data in ls: f.write(",") f.write(str(stock_data)) f.write("\n") f.close() print 'finish'
def wsd(self, *args, **kwargs): try: raw_data = w.wsd(*args, **kwargs) if raw_data.ErrorCode < 0: return json.dumps(raw_data.Codes) df = pd.DataFrame(raw_data.Data) df = df.transpose() df.columns = [f.lower() for f in raw_data.Fields] df['date'] = raw_data.Times df.set_index(['date']) return df.to_json() except: return json.dumps({'error': 'something wrong in calculation'})
def run(fund_code): result = w.wsd(fund_code, "NAV_adj,sec_name,fund_fundmanageroftradedate", "2010-01-01", today_date, "Fill=Previous;PriceAdj=F") df = pd.DataFrame(result.Data, columns=result.Times).transpose() fund_name = df.iloc[0, 1] manager = df.iloc[-1, 2] df = df.iloc[:, 0:1] df.dropna(inplace=True) df = del_eql(df) df.columns = [fund_name + '(' + manager + ')'] df.to_csv('D:\\TF\\公募数据\\' + fund_code + ' ' + fund_name + '.csv', encoding='gbk')
def __init__(self, backtest_start_date, backtest_end_date, index_code, threshold=1.5, hold_L=30, mean_L=5): self.backtest_start_date = backtest_start_date # 策略参数设定 self.threshold = threshold self.hold_L = hold_L self.mean_L = mean_L # 提取数据 w.start() start_date = get_trading_date_from_now(backtest_start_date, -mean_L-3, ql.Days) data = w.wsd(index_code, "amt, close", start_date, backtest_end_date, "") self.times = [t.strftime('%Y-%m-%d') for t in data.Times] # 日期序列 self.amt = data.Data[0] # 成交额数据 self.close = data.Data[1] # 价格序列 super().__init__(backtest_start_date, backtest_end_date, index_code)
def get_weights(self): code_list = list_jq2wind(self.code_list) w.start() return_value = np.array( w.wsd(code_list, "pct_chg", "ED-" + str(self.N - 1) + "TD", self.date, "").Data) return_value = return_value.transpose() return_value = pd.DataFrame(return_value, columns=code_list) optimizer = HRPOpt(return_value) hrp_portfolio = optimizer.hrp_portfolio() hrp_portfolio = dict( zip(list_wind2jq(list(hrp_portfolio.keys())), hrp_portfolio.values())) return hrp_portfolio
def update_recorder(endDate=None, recordFile=None, holdListPath=None): endDate = dt.datetime.today().strftime( '%Y%m%d') if endDate is None else endDate recordFile = r'.\backtest_recorder.csv' if recordFile is None else recordFile holdListPath = r'.\holdLists' if holdListPath is None else holdListPath recordDF = pd.read_csv(recordFile) lastUpdt = recordDF['tradeDate'].values[-1] w.start() startDate = w.tdaysoffset(1, str(lastUpdt)).Data[0][0].strftime('%Y%m%d') if startDate > endDate: print('no new data to update') return betweenDays = w.tdays(startDate, endDate).Data[0] betweenDays = [int(tdt.strftime('%Y%m%d')) for tdt in betweenDays] print(betweenDays) outPut = { 'tradeDate': [], 'buyReturn': [], 'holdReturn': [], 'netReturn': [] } cols = ['stkcd', 'buyDate', 'ret', 'flag'] for tdt in betweenDays: retList = pd.read_csv(os.path.join( holdListPath, 'returns_tradeDate_{}.csv'.format(tdt)), encoding='gbk') retList = retList.loc[:, cols] validBuy = np.all( [retList['buyDate'] == tdt, np.isin(retList['flag'], (0, 3, 4))], axis=0) validHold = np.all( [retList['buyDate'] < tdt, np.isin(retList['flag'], (0, 2, 3, 4))], axis=0) outPut['tradeDate'].append(tdt) outPut['buyReturn'].append(retList.loc[validBuy, 'ret'].mean()) outPut['holdReturn'].append(retList.loc[validHold, 'ret'].mean()) outPut['netReturn'].append(retList.loc[validBuy | validHold, 'ret'].mean()) outPut = pd.DataFrame( outPut).loc[:, ['tradeDate', 'buyReturn', 'holdReturn', 'netReturn']] indices = ['000001.SH', '000300.SH', '000905.SH', '000016.SH'] idxRets = pd.DataFrame(w.wsd(','.join(indices), 'pct_chg', startDate, endDate).Data, columns=indices) idxRets = idxRets.loc[:] / 100 outPut = pd.concat([outPut, idxRets], axis=1) outPut.to_csv(recordFile, mode='a+', index=False, header=False) print('recorder updated till {}'.format(endDate))
def Expire(self): today = date.today() tommorrow = today + timedelta(days=1) expire = w.wsd(self.atCode, "exe_enddate", "ED0TD", today, "TradingCalender=DCE").Data[0][0] if self.position == 'flat': return False elif expire == tommorrow: string = "Option will expire tommorrow!" print(string + "Please COVER " + str(self.atCode) + 'and' + str(self.outCode)) return True else: return False
def QA_fetch_get_stock_xueqiu(name, startDate, endDate): with 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 stocks_infomation(): print('合并股票基础数据') codes = pd.read_excel(const.STOCKS_CODE_FILE, skiprows=4, index_col=0) codes.columns = ['股票代码', '股票名称'] hkcodes = pd.read_excel(const.HKSTOCKS_CODE_FILE, skiprows=4, index_col=0) hkcodes.columns = ['股票代码', '股票名称'] codes = codes.merge(hkcodes, on=['股票名称'], how='outer') codes['股票代码'] = [ codes.loc[i, '股票代码_y'] if pd.isna(codes.loc[i, '股票代码_x']) else codes.loc[i, '股票代码_x'] for i in codes.index ] codes = codes[['股票名称', '股票代码']] stocks = pd.read_excel(const.STOCKS_LIST_FILE, converters={ '增速(2020)': utils.percent2float, '增速(2021)': utils.percent2float, '增速(2022)': utils.percent2float }) results = stocks.merge(codes, how='left', on=['股票名称']) # 计算股票动量 data = w.wsd(results['股票代码'].tolist(), 'close', start_date, end_date) # print(data.Data) prices = pd.DataFrame(data.Data, index=data.Codes, columns=data.Times) prices = prices.T returns = prices.pct_change().mean() returns.index = np.arange(results.shape[0]) results['动量'] = returns data = w.wss(','.join(results['股票代码'].dropna().tolist()), "industry_sw,close", "industryType=3;tradeDate=%s;priceAdj=U;cycle=D" % (today)) info = pd.DataFrame(data.Data, index=['细分行业', '收盘价'], columns=data.Codes) info = info.T info['细分行业'] = info['细分行业'].astype(str).map(lambda x: x.rstrip('Ⅲ')) info['股票代码'] = info.index results = results.merge(info, how='left', on=['股票代码']) results['pe(2020)'] = results['收盘价'] / results['eps(2020)'] results['pe(2021)'] = results['收盘价'] / results['eps(2021)'] results['pe(2022)'] = results['收盘价'] / results['eps(2022)'] results_columns = [ '主题行业', '股票名称', '细分行业', '收盘价', '上次预测时间', '短评', '长评', '动量', '增速(2020)', '增速(2021)', '增速(2022)', 'pe(2020)', 'pe(2021)', 'pe(2022)', '内部(40%)', '外部(20%)', '基金重仓(30%)', '外资重仓(10%)', '总分' ] results = results[results_columns] results.to_excel(const.STOCKS_LIST_FILE, index=False)
def get_history(date_end, T): w.start() print('获取指数历史数据......') if type(T) == int: date_start = w.tdaysoffset(-T, date_end, '').Data[0][0].strftime('%Y-%m-%d') else: date_start = T Data = w.wsd(list(SW1.keys()), "pct_chg", date_start, date_end, "") return_d = np.array(Data.Data) date_list = Data.Times date_list = [d.strftime('%Y-%m-%d') for d in date_list] print('指数历史数据获取完毕') return return_d, date_list
def getMultiTimeSeriesDataFrame(codeList, beginDate, endDate, para, period="", tradingCalendar="", priceAdj="", credibility=0): """ para只能是一个参数 get time series from windPy, each code represents one capture 月度合约: trade_hiscode :param credibility: (int) :param codeList: (list) :param beginDate: (date or datetime) :param endDate: (date or datetime) :param para: (string)只能是一个字符参数 :param period: (int) 频率 :param tradingCalendar: (string) 交易日历,选择可以选择银行间:NIB,不选择,则默认交易所日历 :param priceAdj: (string) 价格是否调整,F:前复权,B:后复权 :return: (DataFrame) """ try: w.start() codeListStr = ",".join(codeList) period = ("Period=" + period) if period == "W" else "" tradingCalendar = ( "TradingCalendar=" + tradingCalendar) if tradingCalendar != "" else "" priceAdj = ("priceAdj=" + priceAdj) if priceAdj != "" else "" credibility = ("credibility=" + str(credibility)) if credibility != 0 else "" windData = w.wsd(codeListStr, para, beginDate.strftime("%Y-%m-%d"), endDate.strftime("%Y-%m-%d"), period, tradingCalendar, priceAdj, credibility) if len(windData.Data) == 0: raise BaseException if len(windData.Data[0]) == 0: raise BaseException dataDict = {} for i in range(len(windData.Data)): dataDict[windData.Codes[i].lower() + "_" + para] = windData.Data[i] df = pd.DataFrame(dataDict, index=windData.Times) df.index = pd.to_datetime(df.index) df.index.name = "trade_date" return df except BaseException as e: print(format(e)) raise
def get_sw_industry(code_list, date): """ :param code_list: :param date: :return: sw level one industry date should be trade date code_list can not duplicated """ if len(set(code_list)) != len(code_list): raise ValueError("duplicated value in code_list input") else: sw_industry_code = w.wsd(code_list, "indexcode_sw", date, date, "industryType=1").Data[0] return sw_industry_code
def QA_fetch_get_stock_day_simple(name, startDate, endDate): from WindPy import w #w.start w.start() w.isconnected() if(is_valid_date(endDate)==False): print ("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") print(data.ErrorCode) if (data.ErrorCode==0): print ("Connent to Wind successfully") return data.Data
def data_download(cmt, start_date, end_date): tmp_cnt_list = w.wset( "futurecc", "startdate=" + start_date + ";enddate=" + end_date + ";wind_code=" + cmt + ";field=wind_code") tmp_cnt_list = tmp_cnt_list.Data[0] tmp_download_cl_list = [] tmp_download_open_list = [] tmp_download_vol_list = [] tmp_download_oi_list = [] tmp_download_high_list = [] tmp_download_low_list = [] for cnt in tmp_cnt_list: tmp_download_data = w.wsd(cnt, "close,open,volume,oi,high,low", start_date, end_date, "") time_index = [ datetime.strptime(str(x), "%Y-%m-%d") for x in tmp_download_data.Times ] tmp_cl = pd.Series(tmp_download_data.Data[0], index=time_index, name=cnt) tmp_open = pd.Series(tmp_download_data.Data[1], index=time_index, name=cnt) tmp_vol = pd.Series(tmp_download_data.Data[2], index=time_index, name=cnt) tmp_oi = pd.Series(tmp_download_data.Data[3], index=time_index, name=cnt) tmp_high = pd.Series(tmp_download_data.Data[4], index=time_index, name=cnt) tmp_low = pd.Series(tmp_download_data.Data[5], index=time_index, name=cnt) tmp_download_cl_list.append(tmp_cl) tmp_download_open_list.append(tmp_open) tmp_download_vol_list.append(tmp_vol) tmp_download_oi_list.append(tmp_oi) tmp_download_high_list.append(tmp_high) tmp_download_low_list.append(tmp_low) tmp_update_cl = pd.concat(tmp_download_cl_list, axis=1) tmp_update_open = pd.concat(tmp_download_open_list, axis=1) tmp_update_vol = pd.concat(tmp_download_vol_list, axis=1) tmp_update_oi = pd.concat(tmp_download_oi_list, axis=1) tmp_update_high = pd.concat(tmp_download_high_list, axis=1) tmp_update_low = pd.concat(tmp_download_low_list, axis=1) return tmp_update_cl, tmp_update_open, tmp_update_vol, tmp_update_oi, tmp_update_high, tmp_update_low
def bench_df(trade_days_list): #筛选出沪深300每个交易日的成分股的代码、名字和权重,已保存csv防止wind quota exceed #存在一个bug,wind数据中指数调整没有及时体现,函数取得值跟实际不符 bench_df = pd.DataFrame( columns=['date', 'stock_code', 'stock_name', 'stock_weight']) for day in trade_days_list: strday = day[0] bench_data = w.wset("indexconstituent", "date=" + strday, "windcode=000300.SH") temps_df = pd.DataFrame(bench_data.Data).T temps_df.columns = ['date', 'stock_code', 'stock_name', 'stock_weight'] #保存了当天的样本和权重 bench_df = pd.concat([bench_df, temps_df]) # 将日期格式转化为文本 # 应该可以apply bench_df['date'] = bench_df['date'].map( lambda x: datetime.datetime.strftime(x, '%Y-%m-%d')) #取每个股票的行业信息 bench_stocks = bench_df['stock_code'].drop_duplicates() stocks_list = list(bench_stocks) #print(len(stocks_list)) stocks_code = ','.join(stocks_list) data = w.wss(stocks_code, "industry_sw", "tradeDate=20170901;cycle=D;industryType=1") stock_industry = pd.DataFrame([data.Codes, data.Data[0]]).T stock_industry.columns = ['stock_code', 'stock_industry'] bench_df = pd.merge(bench_df, stock_industry, on='stock_code') # 取出沪深300成分股每天的涨跌幅 bench_yield_df = pd.DataFrame( columns=['date', 'stock_code', 'stock_yield']) for stock in stocks_list: bench_data = w.wsd(stock, "pct_chg,windcode", "2017-09-01", "2017-12-31", "") temps_df = pd.DataFrame( [bench_data.Times, bench_data.Data[0], bench_data.Data[1]]).T temps_df.columns = ['date', 'stock_yield', 'stock_code'] bench_yield_df = pd.concat([bench_yield_df, temps_df]) bench_yield_df['date'] = bench_yield_df['date'].map( lambda x: datetime.datetime.strftime(x, '%Y-%m-%d')) bench_yield_df.to_csv('data_produce/yield.csv') bench_df = pd.merge(bench_df, bench_yield_df, on=['date', 'stock_code']) bench_df = bench_df.sort_values(by=['date', 'stock_code']) return bench_df
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 crawl_benchmark(id): wind.start() # 启动wind all_id = str(id) + '.OF' tmp = wind.wsd(all_id, "NAV_date,nav,NAV_acc", "ED0D", now, "") # 万得API用法 df = pd.DataFrame(tmp.Data) df = df.T if df[0][0] == None: pass else: df.columns = ["statistic_date_wind", "nav_wind", "added_nav_wind"] df["statistic_date_wind"] = df["statistic_date_wind"].apply(lambda x: x.strftime('%Y-%m-%d')) df["fund_id"] = id df["version"] = now to_sql("fund_nv", engine5, df, type="update") print(df)
def get_portfolio_PL(portfolio_dict, start_date, end_date, base_date): # 从start_date日收盘到end_date日收盘间计算投资组合价格变动 # base_date为复权定点日 w.start() stock_names = list(portfolio_dict.keys()) data = w.wsd(stock_names, 'close2', start_date, end_date, 'adjDate=' + ''.join(base_date.split('-')) + ';PriceAdj=T').Data data = np.array(data) price_start = data[:, 0] price_end = data[:, -1] stock_numbers = np.array(list(portfolio_dict.values())) market_value_start = np.sum(price_start * stock_numbers) market_value_end = np.sum(price_end * stock_numbers) print('投资组合的初期市值为:%.2f\n投资组合的到期市值为:%.2f' % (market_value_start, market_value_end))
def QA_fetch_get_stock_risk(name, startDate, endDate): with 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 append_data(codes, fileds): for code in codes: print(code) fname = '%s/%s.xlsx' % (INDEX_DIR, code) if datetime.datetime.now().hour < 15: end_date = datetime.datetime.today() - datetime.timedelta(1) else: end_date = datetime.datetime.today() if os.path.exists(fname): old_df = pd.read_excel(fname, index_col=0) if 'outmessage' in old_df.columns: del old_df['outmessage'] start_date = old_df.index[-1] + datetime.timedelta(1) if start_date > end_date: continue else: data = w.wsd(code, fields, start_date, end_date) df = wind_data.wind2df(data) df = old_df.append(df) df.to_excel(fname) else: data = w.wsd(code, fields, '2001-01-01', end_date) df = wind_data.wind2df(data) df.to_excel(fname)
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_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("Connect to Wind successfully") return pd.DataFrame(np.asarray(data.Data).T, columns=data.Fields, index=data.Times)
def __init__(self, backtest_start_date, backtest_end_date, index_code, N, M, S1=0.7, S2=-0.7): w.start() RSRS_start_date = get_trading_date_from_now(backtest_start_date, -N-M, ql.Days) data = w.wsd(index_code, "high,low,close", RSRS_start_date, backtest_end_date, "") self.RSRS_times = [t.strftime('%Y-%m-%d') for t in data.Times] self.RSRS_data_high = data.Data[0] self.RSRS_data_low = data.Data[1] self.RSRS_data = data.Data[2] self.backtest_start_date = backtest_start_date self.RSRS_cal_start_date = get_trading_date_from_now(backtest_start_date, -M, ql.Days) self.N = N # 计算RSRS指标采样的历史周期长短 self.M = M # 标准化序列的历史周期长短 self.S1 = S1 self.S2 = S2 self.date_list, _, self.signal_list = self._get_data()
def select_hy_contracts(x,y): n=len(x) hy_list=[] date = pd.to_datetime(y) start_date = date - dt.timedelta(20) start_date = start_date.strftime('%Y-%m-%d') d=w.wsd(x,'volume',start_date,y) for i in range(n): #y means test date mean_vol = np.nansum(d.Data[i][-5:])/5.0 #print(mean_vol) if mean_vol > 2e4: stock=d.Codes[i] hy_list.append(stock) return hy_list
def QA_fetch_get_stock_xueqiu(name, startDate, endDate): from WindPy import w # w.start() w.start() w.isconnected() if (is_valid_date(endDate) == False): print("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): print("Connent to Wind successfully") return data.Data
def QA_fetch_get_stock_risk(name, startDate, endDate): from WindPy import w w.start() w.isconnected() if (is_valid_date(endDate) == False): print("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): print("Connent to Wind successfully") return data.Data
def start_market_simulation(self): error, data = w.wsd(self.ticker, "open, close, volume", self.start, self.end, "Fill=Previous;PriceAdj=F", usedf=True) for time, row in data.iterrows(): self.md.add_last_price(time, self.ticker, row["close"], row["volume"]) self.md_add_open_price(time, self.ticker, row["open"]) if not self.event_tick is None: self.event_tick(self.md)
def QA_fetch_get_stock_day(name, startDate, endDate): with w.start(): if (QA_util_date_valid(endDate) == False): QA_util_log_info("wrong date") else: if w.isconnected() is False: w.start() data = w.wsd(name, "sec_name,pre_close,open,high,low,close,volume", startDate, endDate, "PriceAdj=F") 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 __init__(self, backtest_start_date, backtest_end_date, index_code, L=50, N=3, Long=100, Threshold=1.02): self.backtest_start_date = backtest_start_date # 策略参数设定 self.L = L self.N = N self.Long = Long self.Threshold = Threshold # 基本数据获取 w.start() start_date = get_trading_date_from_now(backtest_start_date, -(max(L+N, Long)+100), ql.Days) data = w.wsd(index_code, "close,amt", start_date, backtest_end_date, "") self.times = [t.strftime('%Y-%m-%d') for t in data.Times] # 日期序列 self.close = data.Data[0] # 价格序列 self.amt = data.Data[1] # 成交额序列 self._get_signal('2015-01-27') super().__init__(backtest_start_date, backtest_end_date, index_code)
def __init__(self, backtest_start_date, backtest_end_date, index_code, threshold=0.0, RPS_history=250, max_vol_diff_history=100): self.backtest_start_date = backtest_start_date # 策略参数设定 self.threshold = threshold self.RPS_history = RPS_history self.max_vol_diff_history = max_vol_diff_history # 提取数据 w.start() start_date = get_trading_date_from_now(backtest_start_date, -RPS_history-max_vol_diff_history-30, ql.Days) data = w.wsd(index_code, "open, close, high, low", start_date, backtest_end_date, "") self.times = [t.strftime('%Y-%m-%d') for t in data.Times] # 日期序列 self.open = data.Data[0] self.close = data.Data[1] self.high = data.Data[2] self.low = data.Data[3] super().__init__(backtest_start_date, backtest_end_date, index_code)
def __init__(self, backtest_start_date, backtest_end_date, index_code, llt_d, llt_cal_history=100, llt_threshold=0.0): ''' :param llt_d: 上证综指适合参数39 :param llt_cal_history: :param llt_threshold: ''' w.start() llt_start_date = get_trading_date_from_now(backtest_start_date, -llt_cal_history, ql.Days) data = w.wsd(index_code, "close", llt_start_date, backtest_end_date, "") self.llt_times = [t.strftime('%Y-%m-%d') for t in data.Times] self.llt_data = data.Data[0] self.llt_d = llt_d self.llt_threshold = llt_threshold self.llt_cal_history = llt_cal_history # LLT信号计算的历史长度 self.backtest_start_date = backtest_start_date super().__init__(backtest_start_date, backtest_end_date, index_code)
def column_append(ticker, fields, start_date='2015-01-01', end_date='2017-03-28'): fname = '%s/%s.xlsx' % (const.STOCK_DIR, ticker) old_df = pd.read_excel(fname, index_col=0) for col in fields.split(','): if col in old_df.columns: return print ticker data = w.wsd(ticker, fields, start_date, end_date, "traderType=1") df = wind2df(data) assert (df.shape[0] == old_df.shape[0]) df = old_df.join(df) df.to_excel(fname)
def alert(chg=3.1): '定义警示函数,扫描所有品种的涨幅幅,若超过某个限定的阈值,则发送邮件提醒自己注意!' codes = pw.getActivecodes() dic = {} for code in codes: data = w.wsd(code, "close,pct_chg,oi,oi_chg", "ED-0TD").Data price = data[0][0] price_chg = data[1][0] if price: if price_chg > chg: dic[code] = [price, price_chg] else: pass else: print('没有有效的返回数据!') return dic
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_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 get_wind_nev(windid, fre="日"): """ 获取wind基金数据 """ date = w.wss(windid, "fund_setupdate,NAV_date").Data start_date = date[0][0].strftime('%Y-%m-%d') end_date = date[1][0].strftime('%Y-%m-%d') fund_name = w.wss(windid, "name_official").Data[0] if fre == "周": para = "Period=W;Fill=Previous" else: para = "Period=D;Fill=Previous" data = w.wsd(windid, "NAV_adj", start_date, end_date, para) df = pd.DataFrame(data.Data[0], index=data.Times, columns=fund_name) df.index = pd.to_datetime(df.index) return df
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 ExponentScale(fundid=None, begintime='2010-01-01', endtime=date.today()): if(fundid == None): fundid = FundID() param = (fundid, 'prt_totalasset', begintime, endtime, 'Period=Q', 'Fill=Previous') data = w.wsd(*param) Value = [] for i in range(0, len(data.Codes)): for j in range(0, len(data.Data[0])): stockvalue = data.Data[i][j] print(stockvalue) if(stockvalue is None or isnan(stockvalue)): stockvalue = 0.0 record = (data.Codes[i], data.Times[j].strftime("%Y-%m-%d"), stockvalue) print(record) Value.append(record) return Value
def getDayData(self,stockCodeList,fields,startDate,endDate,option): '''日数据的具体的获取步骤''' for stockCode in stockCodeList: print '添加日数据,代碼%s' % stockCode wsd_data = w.wsd(stockCode,fields,startDate,endDate,option) if wsd_data.ErrorCode==0: # 返回成功 stockDate = wsd_data.Times dateList = [date.strftime("%Y-%m-%d") for date in stockDate] stockDataDict = {'stockCode':stockCode,'date':dateList} for i in range(len(fields)): stockDataDict[fields[i]] = wsd_data.Data[i] stockData = pd.DataFrame(stockDataDict,columns=['stockCode','date']+fields,index=dateList).dropna() # 只要有缺失的数据就删掉这一行,保证数据最为干净 stockData['pct_chg'] = stockData['pct_chg'] / 100 # 让涨幅变为实际涨幅(查询出来的是百分比数据) # 插入到數據庫中 sql = "insert ignore into stockdata_day values(" + "%s,"*(len(fields)+1)+"%s)" self.cur.executemany(sql,tuple(stockData.values.tolist())) self.con.commit() else: logging.info('ERROR-%s-day數據下載失敗,錯誤代碼為:%s' % (stockCode,wsd_data.ErrorCode))
def func6(): yesterday = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") df_closeB=pd.read_csv('closeB.csv') last_date_closeB = time.strftime("%Y-%m-%d",time.strptime(str(int(df_closeB.iloc[-1,0])),"%Y%m%d")) day_gap_closeB = w.tdayscount(last_date_closeB, yesterday).Data[0][0]-1 print 6 w_wsd_data={} stock_ls=[] for stock in universe: if stock =='150073': stock='150075.SZ' else: if stock[0]=='1': stock = str(int(stock[:6])+1) + '.SZ' else: stock = str(int(stock[:6])+1) + '.SH' stock_ls.append(stock) w_wsd_data[stock] = w.wsd(stock, 'close', (w.tdaysoffset(1,last_date_closeB).Data[0][0]).strftime("%Y-%m-%d"),yesterday) # print 1 print stock #if w_wsd_data[stock].Data[0][0]==None: for i in xrange(day_gap_closeB): this_day = w.tdaysoffset(i+1,last_date_closeB) ls = [] this_day_csv = (this_day.Data[0][0]).strftime("%Y%m%d") for stock in stock_ls: ls.append(w_wsd_data[stock].Data[0][i]) f=open('closeB.csv','a') f.write(this_day_csv) for stock_data in ls: f.write(",") f.write(str(stock_data)) f.write("\n") f.close() print 'finish'
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 totalAmount(codes, baseDate='20160706', backwardDays=0): tradeTotal = 0 baseDate = baseDate backDays = backwardDays backwardDays = 'ED-' + str(backwardDays) + "TD" for code in codes: data = w.wsd(code, "open,high,low,close,volume,oi,pct_chg", backwardDays, baseDate) dt = data.Data for i in range(backDays + 1): date = data.Times[i] o = dt[0][i] h = dt[1][i] l = dt[2][i] c = dt[3][i] v = dt[4][i] # pchg=data[5][0] avg = (o + h + l + c) / 4 tradeAmount = v * avg * float(TRADEUNIT[code]) print(str(date.strftime("%Y-%m-%d")) + ":" + code + "的名义成交金额为:", tradeAmount / 10000) tradeTotal += tradeAmount return '名义成交量:' + str(tradeTotal / 10000) + "(万元)"
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)
from WindPy import w import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model import cPickle w.start(); wsd_data=w.wsd("000300.SH", "amt,turn,pct_chg,pe_ttm,pb,roe,yoy_tr,yoy_or,yoyprofit,close", "2005-10-01", "2016-07-31", "Period=M;Days=Alldays;PriceAdj=F") time=wsd_data.Times amt=wsd_data.Data[0] turn=wsd_data.Data[1] pct_chg=wsd_data.Data[2] pe_ttm=wsd_data.Data[3] # pb=wsd_data.Data[4] # roe=wsd_data.Data[5] # yoy_tr=wsd_data.Data[6] # yoy_or=wsd_data.Data[7] # yoyprofit=wsd_data.Data[8] close=wsd_data.Data[9] nrows=len(time) lable=0 # generate train data train_data=[] train_lable=[] for i in range(3,nrows-1): start=close[i-2] end=close[i+1] gain=(float(end)/start)**(float(1)/3)
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来提取各个报告期财务数据 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来取日内成交数据
def get_min_data(filename): file_folder = 'C:\\dev\\src\\ktlib\\data3\\' reader = csv.reader(file(filename, 'rb')) contList = [] fields = 'open,close,high,low,volume,oi' contStart = [] contEnd = [] for line in reader: contList.append(line[0]) contStart.append(line[1]) contEnd.append(line[2]) #w.start() for cont, startD, endD in zip(contList, contStart, contEnd): if cont[1].isalpha(): key = cont[:2] else: key = cont[:1] ex = 'SH' for exch in product_code.keys(): if key in product_code[exch]: ex = exch ticker = cont + '.' + ex startmin = "" endMin = "" if ex == 'CFE': startMin = " 09:14:00" endMin = " 15:15:00" else: startMin = " 08:59:00" endMin = " 15:00:00" if ex == 'CFE': startMin = " 09:14:00" endMin = " 15:15:00" else: startMin = " 08:59:00" endMin = " 15:00:00" try: sdate = datetime.datetime.strptime(startD, "%Y/%m/%d") edate = datetime.datetime.strptime(endD, "%Y/%m/%d") sMin = datetime.datetime.strptime(startD + startMin, "%Y/%m/%d %H:%M:%S") eMin = datetime.datetime.strptime(endD + endMin, "%Y/%m/%d %H:%M:%S") print ticker, sMin, eMin if edate > datetime.date(2009,1,1): raw_data = w.wsi(ticker,fields,sMin,eMin) if len(raw_data.Data)>1: outfile = file_folder + cont+'_min.csv' output={'datetime':raw_data.Times, 'open':raw_data.Data[0], 'close':raw_data.Data[1], 'high':raw_data.Data[2], 'low':raw_data.Data[3], 'volume':raw_data.Data[4], 'openInterest':raw_data.Data[5]} dump2csvfile(output,outfile) else: print "no min data obtained for ticker=%s" % ticker raw_data = w.wsd(ticker,fields,sdate,edate) if len(raw_data.Data)>1: outfile = file_folder + cont+'_daily.csv' output={'datetime':raw_data.Times, 'open':raw_data.Data[0], 'close':raw_data.Data[1], 'high':raw_data.Data[2], 'low':raw_data.Data[3], 'volume':raw_data.Data[4], 'openInterest':raw_data.Data[5]} dump2csvfile(output,outfile) else: print "no daily data obtained for ticker=%s" % ticker except ValueError: pass w.stop() return True
PRIMARY KEY (secid,tradedate) ) """) sql = "INSERT OR REPLACE INTO stockprice VALUES (?, ?, ?, ?, ?, ?, ?, ?)" # 通过wset来取数据集数据 print('\n\n'+'-----通过wset来取数据集数据,获取全部A股代码列表-----'+'\n') wsetdata=w.wset('SectorConstituent','date=20160625;sectorId=a001010100000000;field=wind_code') print(wsetdata) for j in range(0,len(wsetdata.Data[0])): # 通过wsd来提取时间序列数据,比如取开高低收成交量,成交额数据 print( u"\n\n-----第 %i 次通过wsd来提取 %s 开高低收成交量数据-----\n" %(j,str(wsetdata.Data[0][j])) ) wssdata=w.wss(str(wsetdata.Data[0][j]),'ipo_date') wsddata1=w.wsd(str(wsetdata.Data[0][j]), "open,high,low,close,volume,amt", wssdata.Data[0][0], dt, "Fill=Previous") if wsddata1.ErrorCode!=0: continue print (wsddata1) for i in range(0,len(wsddata1.Data[0])): sqllist=[] sqltuple=() sqllist.append(str(wsetdata.Data[0][j])) if len(wsddata1.Times)>1: sqllist.append(wsddata1.Times[i].strftime('%Y%m%d')) for k in range(0, len(wsddata1.Fields)): sqllist.append(wsddata1.Data[k][i]) sqltuple=tuple(sqllist) cursor.execute(sql,sqltuple) conn.commit() conn.close()
# personal version allows 600000-600120 stockStartIndex=600000 for j in range(0,100): stockIndex=stockStartIndex+j print ("j="+str(j)) stock=str(stockIndex)+".sh" oneday=datetime.timedelta(days=1) totaldays=datetime.timedelta(days=numdays) dateEnd=datetime.date.today() dateStr=dateEnd-totaldays strdateStr=str(dateStr).replace("-","").split(" ")[0] strdateEnd=str(dateEnd).replace("-","").split(" ")[0] d=w.wsd(stock,"sec_englishname,open,high,low,close,volume",strdateStr,strdateEnd,"Priceadj=f;tradingcalendar=nib") print (d.Data) stockname=stock #data of each requested entry between the requested dates #will be returned into its own array # in this case i asked for 6 kinds of entries arrSec_Name=d.Data[0] arrOpen=d.Data[1] arrHigh=d.Data[2] arrLow=d.Data[3] arrClose=d.Data[4] arrVolume=d.Data[5]
data = [] if len(outdata.Times) > 1: data.append(str(outdata.Times[i])) for k in range(0, len(outdata.Fields)): data.append(str(outdata.Data[k][i])) x.add_row(data) print(x) if __name__ == '__main__': stock = '600000.SH' # 浦发银行 now = datetime.now() w.start() print('\n' + '获取过去三个季度总股本') wsddata = w.wsd(stock, "total_shares", "ED-3Q", now, "Period=Q;Fill=Previous") printinfo(wsddata) print('\n' + '获取过去三个季度自由流通股本') wsddata = w.wsd(stock, "free_float_shares", "ED-3Q", now, "Period=Q;Fill=Previous") printinfo(wsddata) print('\n' + '获取过去三个季度股东户数') wsddata = w.wsd(stock, "holder_num", "ED-3Q", now, "Period=Q;Fill=Previous") printinfo(wsddata) print('\n' + '获取过去三个季度户均持股数量(流通股本)') wsddata = w.wsd(stock, "holder_avgnum", "ED-3Q", now, "shareType=1;Period=Q;Fill=Previous") printinfo(wsddata) print('\n' + '获取过去三个季度户均持股数量(总股本)')
''' Created on 2016年4月9日 @author: caiwx ''' #!/usr/bin/env python3 # -*- coding:utf-8 -*- from WindPy import w import datetime import pandas as pd w.start(); # 取数据的命令如何写可以用命令生成器来辅助完成 wsd_data=w.wsd("000001.SZ", "open,high,low,close", "2015-12-10", "2015-12-22", "Fill=Previous") #演示如何将api返回的数据装入Pandas的Series open=pd.Series(wsd_data.Data[0]) high=pd.Series(wsd_data.Data[1]) low=pd.Series(wsd_data.Data[2]) close=pd.Series(wsd_data.Data[3]) print('open:/n',open) print('high:/n',high) print('low:/n',low) print('close:/n',close) #演示如何将api返回的数据装入Pandas的DataFrame fm=pd.DataFrame(wsd_data.Data,index=wsd_data.Fields,columns=wsd_data.Times) fm=fm.T #将矩阵转置 print('fm:/n',fm)
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 print "\n.Codes="+str1D(res.Codes) print "\n.Fields="+str1D(res.Fields) print "\n.Times="+str1D([ format(x,'%Y%m%d %H:%M:%S') for x in res.Times]) print "\n.Data="+str2D(res.Data)