def load_trade_date_series(self, period="D"): """ 下载交易日日期序列 """ from WindPy import w w.start() if period == "2W": today = datetime.today().strftime('%Y-%m-%d') data = w.tdays(self.beg_date, today, "Period=" + "W") data_pd = pd.DataFrame(data.Data, index=['Trade_Date'], columns=data.Times).T data_pd['Trade_Date'] = data_pd['Trade_Date'].map( lambda x: x.strftime('%Y%m%d')) data_pd.index = data_pd.index.map(lambda x: x.strftime('%Y%m%d')) double_loc = list( filter(lambda x: x % 2 == 0, list(range(len(data_pd))))) loc_index = data_pd.index[double_loc] data_pd = data_pd.loc[loc_index, :] else: today = datetime.today().strftime('%Y-%m-%d') data = w.tdays(self.beg_date, today, "Period=" + str(period)) data_pd = pd.DataFrame(data.Data, index=['Trade_Date'], columns=data.Times).T data_pd['Trade_Date'] = data_pd['Trade_Date'].map( lambda x: x.strftime('%Y%m%d')) data_pd.index = data_pd.index.map(lambda x: x.strftime('%Y%m%d')) if period in ['Q', 'S', 'Y']: data_pd = data_pd.iloc[0:-1, :] out_file = os.path.join(self.data_path, self.file_dict[period]) print("Loading Date %s " % out_file) data_pd.to_csv(out_file)
def getDateSeries(self, collection, cmd, **kwargs): """从WIND导入交易日期时间序列""" self.windConn() coll = self.db[collection] if coll.find_one({'exchange': cmd}): queryArgs = {'exchange': cmd} projectionField = ['date'] searchRes = coll.find(queryArgs, projectionField).sort( 'date', pymongo.DESCENDING).limit(1) start_date = list(searchRes)[0]['date'] + timedelta(1) current_year = datetime.today().year end_date = datetime(current_year + 1, 12, 31) # 这里有个问题是每到年末的时候都要重新刷一遍下一年的数据,因为下一年的节假日没有包含在里面,需要去数据库里删除掉 else: start_date = datetime.strptime('2000-01-01', '%Y-%m-%d') current_year = datetime.today().year end_date = datetime(current_year + 1, 12, 31) if start_date > end_date: return if cmd == 'SHSE': res = w.tdays(beginTime=start_date, endTime=end_date) else: res = w.tdays(beginTime=start_date, endTime=end_date, TradingCalendar=cmd) total = len(res.Data[0]) count = 1 print u'更新交易日期数据' self.logger.info(u'共更新了%s个交易日期数据进入到数据库' % total) for r in res.Data[0]: process_str = '>' * int( count * 100. / total) + ' ' * (100 - int(count * 100. / total)) sys.stdout.write('\r' + process_str + u'【已完成%5.2f%%】' % (count * 100. / total)) sys.stdout.flush() res_dict = { 'date': r, 'exchange': cmd, 'update_time': datetime.now() } res_dict.update(kwargs) coll.insert_one(res_dict) count += 1 sys.stdout.write('\n') sys.stdout.flush()
def get_tradeday(self, start_date, end_date, period='M'): ''' 获取指定周期交易日 :param start_date: :param end_date: :param period: ''日,W周,M月,Q季,S半年,Y年 :return: ''' if self.dic_init['data_resource'] == 'wind': data = w.tdays(beginTime=start_date, endTime=end_date, options="Period=%s" % period) if data.ErrorCode != 0: self.logger.error('wind获取交易日期错误,请检查!') return tradeDayList = data.Data[0] tradeDayList = [ tradeDay.strftime('%Y-%m-%d') for tradeDay in tradeDayList ] elif self.dic_init['data_resource'] == 'ifind': ths_data = THS_DateQuery( 'SSE', 'dateType:0,period:%s,dateFormat:0' % period, start_date, end_date) if ths_data['errorcode'] != 0: self.logger.error("同花顺获取交易日期数据错误,请检查:%s" % ths_data['errmsg']) return tradeDayList = ths_data['tables']['time'] return tradeDayList
def get_dataframe(asset_code, start_date, end_date): tdays = w.tdays(start_date, end_date).Times df = pd.DataFrame(index=tdays) df.index.name = 'date' df['pe'] = get_pe(asset_code, start_date, end_date) df['pb'] = get_pb(asset_code, start_date, end_date) * 10 return df
def historytradeday(today_): today = pd.to_datetime(today_).date() data = list(w.tdays("2000-01-01", today_, "").Data[0]) def to_date(dt): return pd.to_datetime(dt).date() def changemonth(mo): if mo == 12: return 1 else: return mo + 1 data = map(to_date, data) y = today.year m = today.month d = today.day historyday = [] while y > 2000: y = y - 1 n = date(y, m, d) dd = d while n not in data: if dd == 30 or dd == 31: dd = 1 mm = changemonth(m) n = date(y, mm, dd) else: dd = dd + 1 n = date(y, m, dd) historyday.append(n) return historyday
def update_etfconstituent_his(): session = DBSession() last_day_in_db = session.query(func.max( ChinaEtfPchRedmList.trade_date)).one()[0] if last_day_in_db is not None: next_trade_date = get_trade_date(last_day_in_db, 1) else: next_trade_date = get_trade_date(today_yyyymmdd, -750) if next_trade_date <= last_date_in_wind: temp = w.tdays(next_trade_date, last_date_in_wind, "") trade_dates = temp.Data[0] for trade_date in trade_dates: temp = w.wset( "etfconstituent", "date={today_yyyymmdd};windcode=510050.SH;" "field=wind_code,volume,cash_substitution_mark," "cash_substitution_premium_ratio,fixed_substitution_amount". format(today_yyyymmdd=trade_date.strftime('%Y%m%d'))) df = pd.DataFrame(temp.Data).T df.columns = temp.Fields df.rename({'wind_code': 'sec_code'}, axis=1, inplace=True) df['etf_sec_code'] = '510050.SH' df['trade_date'] = trade_date df.to_sql(ChinaEtfPchRedmList.__tablename__, engine, index=False, if_exists='append')
def get_seq_w(self, start, end): """ 根据开始时间和结束时间,返回频率为“W"的回测任务确定新仓位时间list. :param start: str, 开始时间. :param end: str, 结束时间. :return: list, 计算下期仓位各时点list。 """ if not w.isconnected(): w.start() sleep(3) # wind的w.tdays函数准确性较差,需要严格检验 # seq = w.tdays(start, end, f"Period=W;TradingCalendar={self.calendar}").Data[0] # 转换为工作日 seq = w.tdays(start, end, "Period=W;Days=Weekdays").Data[0] if seq[-1].weekday() != 4: seq.pop() seq = [dd.strftime("%Y-%m-%d") for dd in seq] month = [] seq_ = [] for ss in seq: if ss[0:7] not in month: # 只有在新月份出现时才可能出现更新 month.append(ss[0:7]) seq_.append((ss, len(month) - 1)) else: seq_.append((ss, 0.5)) return seq_
def risk_parity(close): pct = close.pct_change().dropna() tradedate = w.tdays(pct.index[0], pct.index[-1], "Period=M") tradedate = map(lambda x: str(x).split(' ')[0], tradedate.Data[0]) chicang = dict() for s in close.columns: chicang[s] = 0.0 cash = 1.0 stream = [] weight = pd.DataFrame(index=pct.index[1:], columns=close.columns) for date in pct.index[1:]: iloc = list(pct.index).index(date) if date in tradedate: if iloc >= 20: cov = pct[iloc - 20:iloc].cov() else: cov = pct[:iloc].cov() for s in chicang.keys(): cash += chicang[s] * close.ix[date][s] chicang[s] = 0.0 sig = cov.values.diagonal()**0.5 weight = sig**-1 / (sig**-1).sum() weight = pd.Series(weight, close.columns) cash1 = cash for s in chicang.keys(): chicang[s] += weight[s] * cash1 / close.ix[date][s] cash -= cash1 * weight[s] weight.ix[date] = pd.Series(chicang) stream.append((pd.Series(chicang) * close.ix[date]).sum() + cash) return pd.Series(stream, index=pct.index[1:])
def get_tdays(start_date, end_date, **options): ''' 获取交易日历序列。 Parameters --------------- start_date '20101001' end_date '20171021' options 其他参数 Returns -------- Series[]-= -98 Datetime64,ASC ''' options = dict_2_str(options) start_date = date_format_convert(start_date) end_date = date_format_convert(end_date) t_days = w.tdays(start_date, end_date, options) t_days = pd.Series(t_days.Data[0]) return t_days
def biz_days_list(start_date, end_date, freq): try: dates = w.tdays(start_date, end_date, 'period=' + freq) WindDataProvider.force_throw_err(dates, 'WindDataProvider.biz_days_list') return dates.Data[0] except NameError: pass
def rnn_reg_training(start_date='', end_date='', security_id=''): _t_days = w.tdays(start_date, end_date) t_days = [item.strftime('%Y%m%d') for item in _t_days.Data[0]] try: rnn = torch.load('rnn_5min') except Exception as ex: print('No saved model:{0}'.format(ex)) rnn = RNN() print(rnn) optimizer = torch.optim.Adam(rnn.parameters(), lr=LR) loss_func = torch.nn.MSELoss() h_state = None # 要使用初始hidden state, 可以设成None plt.ion() plt.show() best_loss = 100 n_steps = len(t_days) for step in range(n_steps): train_x_pack, train_y, seq_lengths = get_data_loader(security_id=security_id, date=t_days[step]) prediction, outputs, h_state = rnn(train_x_pack, h_state) # rnn对于每一个step的prediction, 还有最后一个step的h_state h_state = h_state.data # 要把h_state 重新包装一下才能放入下一个iteration,不然会报错 loss = loss_func(outputs, train_y) # cross entropy loss optimizer.zero_grad() # clear gradients for this training step loss.backward() # backprogation, compute gradients optimizer.step() # apply gradients if loss < best_loss: print('Update loss from {0} to {1}'.format(best_loss, loss)) best_loss = loss torch.save(rnn, '{0}_rnn_5min'.format(security_id)) if step % 3 == 0: plt.cla() plt.scatter(range(len(seq_lengths)), train_y[:, 0].data.numpy()) plt.plot(range(len(seq_lengths)), outputs[:, 0].data.numpy(), 'r-', lw=5) plt.text(0.5, 0, 'loss={0},step={1}'.format(loss.data.numpy(), step), fontdict={'size': 20, 'color': 'red'}) plt.pause(0.5)
def get_trading_date_list_by_month_by_day(BACKTEST_START_DATE, BACKTEST_END_DATE, MONTHS, TRADING_DATES_LIST): trading_date_list = [] # 记录备选交易日列表 w.start() date_to_be_selected = w.tdays(BACKTEST_START_DATE, BACKTEST_END_DATE, "").Data[0] # 获取交易日列表 date_to_be_selected = [d.strftime('%Y-%m-%d') for d in date_to_be_selected] i = 0 print('回测开始日期:' + BACKTEST_START_DATE + ',结束日期:' + BACKTEST_END_DATE) while True: print('处理日期:' + str(i)) date_now = date_to_be_selected[i] dates_trading = [ get_trading_date_from_now( date_now.split('-')[0] + '-' + date_now.split('-')[1] + '-' + TRADING_DATE, 0, ql.Days) for TRADING_DATE in TRADING_DATES_LIST ] if date_now in dates_trading: trading_date_list.append(date_now) i += 1 if date_now == BACKTEST_END_DATE: break trading_date_list = [ d for d in trading_date_list if d.split('-')[1] in MONTHS ] # 按照选定的调仓月份进行筛选 return trading_date_list
def get_seq_d(self, start, end): """ 根据开始时间和结束时间,返回频率为“D"的回测任务确定新仓位时间list. :param start: str, 开始时间. :param end: str, 结束时间. :return: list, 计算下期仓位各时点list。 """ if not w.isconnected(): w.start() sleep(3) # 日频级别暂时不变更为工作日日历,对于假期造成的不交易,当天仓位无法变更 seq = w.tdays(start, end, f"TradingCalendar={self.calendar}").Data[0] seq = [dd.strftime("%Y-%m-%d") for dd in seq] month = [] seq_ = [] for ss in seq: if ss[0:7] not in month: # 只有在新月份出现时才可能出现更新 month.append(ss[0:7]) seq_.append((ss, len(month) - 1)) else: seq_.append((ss, 0.5)) return seq_
def get_mkt_mins(self, startdate='', enddate='', sec_codes=[], filter='', orderby='', groupby='', table_name='CUST.EQUITY_PRICEMIN'): ''' Fetch the minute level data from tonglian in oracle Return the rows of values: ['DATADATE', 'TICKER', 'EXCHANGECD', 'SHORTNM', 'SECOFFSET', 'BARTIME', 'CLOSEPRICE', 'OPENPRICE', 'HIGHPRICE', 'LOWPRICE', 'VOLUME', 'VALUE', 'VWAP'] e.g. [(20180605, 600237, 'XSHG', '铜峰电子', 11640, '11:14', 4.46, 4.47, 4.47, 4.46, 68600, 306094.0, 4.462000000000001)] :param startdate: int :param enddate: int :param sec_codes: tuple of str :param filter: :param orderby: :param groupby: :return: rows, col_names; rows: list of tuple from the results; col_names: list of strings of the colume name of the table ''' if not self._dyobj: logger.error("Fail in get_market_mins for empty db_obj") # rows, col_names = self.get_dates_statics(startdate, enddate) # all_trading_dates = [item[1].strftime('%Y%m%d') for item in rows if item[3] == 1] _w_ret = w.tdays(startdate, enddate) # t_months = list(set([item.strftime('%Y%m') for item in _w_ret.Data[0]])) all_trading_dates = list( set([item.strftime('%Y%m%d') for item in _w_ret.Data[0]])) grouped_dates = defaultdict(list) for d in all_trading_dates[:-1]: yymm = d[:6] rows, columns = [], [] grouped_dates[yymm].append(int(d)) total_len = len(grouped_dates) cnt = 0 logger.info( "Start query data in get_market_mins for query_date:{0}".format( len(grouped_dates))) tickers = [item.split('.')[0] for item in sec_codes] ticker_str = ','.join(tickers) for k, v in grouped_dates.items(): cnt += 1 logger.debug("query the {0} th table {1} out of {2}".format( cnt, k, total_len)) v = sorted(v) tab_name = '{0}{1}'.format('CUST.EQUITY_PRICEMIN', k) sqlstr = '''SELECT * FROM {0} WHERE DATADATE >= {1} AND DATADATE <= {2} AND TICKER IN ({3}) AND EXCHANGECD IN ('XSHG','XSHE')'''.format( tab_name, v[0], v[-1], ticker_str) tmp_rows, desc = self._dyobj.execute_query(sqlstr) rows.extend(tmp_rows) logger.info( "Done query data in get_market_mins for query_date:{0}".format( len(grouped_dates))) return rows, desc
def get_mdays(start, end, fn): if os.path.exists(fn): df = pd.read_csv("./" + fn, index_col=0) else: mdays = w.tdays(start, end, "Period=M") df = pd.DataFrame({}) df['mdays'] = list(map(lambda x: str(x)[0:10], mdays.Data[0])) df.to_csv("./" + fn) return df['mdays'].tolist()
def get_list_str_trddate(str_startdate, str_enddate): """ 获得期间自然日期间的交易日,参数为闭区间两端 :param str_startdate: 起始日期,包括 :param str_enddate: 终止日期,包括 :return: list, 指定期间内交易日的列表 """ wtdays = w.tdays(str_startdate, str_enddate) list_str_trddates = [x.strftime('%Y%m%d') for x in wtdays.Data[0]] return list_str_trddates
def tdays(cls, start_date, end_date=None): ''' 获取交易日 ~~~~~~~~~~~~~~~~ l = WindUtil.tdays('2020-03-07') l = WindUtil.tdays('2020-03-07', end_date='2020-04-07') ''' l = w.tdays(cls.get_date_str(start_date), cls.get_date_str(end_date), "").Data return l[0] if l is not None and len(l) > 0 else []
def trade_days(date_start, date_end): """制作正确日期""" trade_days = w.tdays(date_start, date_end) trade_days_list = [] for day in trade_days.Data[0]: strday = datetime.datetime.strftime(day, '%Y-%m-%d') #print(strday) trade_days_list.append([strday]) return trade_days_list
def get_trade_schedule(participant_rate=0.15, target_ratio=0.01, sec_code="002299.SZ", start_date="", end_date="2019-10-15", period=100, target_vol=2150000, target_period=20, price_ratio=0.97): _t_days = w.tdays("2017-01-14", end_date) t_days = [item.strftime('%Y-%m-%d') for item in _t_days.Data[0]] ret = w.wsd(sec_code, "close,volume,turn,open,chg,pct_chg,total_shares", t_days[-period], end_date, "") # print(ret.Data) vols = ret.Data[1] turns = ret.Data[2] total_shares = ret.Data[-1] price_ma5 = np.array(ret.Data[0][-5:]).mean() * price_ratio price_prev = ret.Data[0][-1] * price_ratio # print("price line is :{0}".format(ma5)) ma_vols = list(ta.SMA(np.array(list(vols), dtype=float), timeperiod=5)) # ma_turns = list(ta.SMA(np.array(list(turns), dtype=float), timeperiod=5)) idx = -1 total_vol = 0.0 ret_vols = [] # compute target vol target_vol = target_vol or target_ratio * total_shares[-1] # participant rate testing # p_rates_up = [] # # p_rates_down = [] # # for target_period in [10, 20, 30, 40, 50, 60]: # # p_rates_up.append(100 * (target_vol / (sum(ma_vols[-target_period:]) * 1.05))) # # p_rates_down.append(100 * (target_vol / (sum(ma_vols[-target_period:]) * .95))) # # return p_rates_up, p_rates_down # update the participant_rate according to the target complete period if target_period: participant_rate = target_vol / sum(ma_vols[-target_period:]) print('updated participant rate is:{0}'.format(participant_rate)) while total_vol < target_vol: if target_vol - total_vol <= 100: ret_vols.append(100) break try: _vol = int(ma_vols[idx] * participant_rate / 100) * 100 except: break ret_vols.append(_vol) total_vol += _vol idx -= 1 return ret_vols, [price_prev, price_ma5]
def get_tds_wind(startTime, endTime): ''' 从Wind中获取交易日序列 @param: startTime: 交易日的起始日期,要求为dt.datetime格式或者YYYY-MM-DD格式 endTime: 交易日的终止日期,同上述要求 @return: tds: 交易日列表 ''' check_connection() tds = w.tdays(startTime, endTime) assert tds.ErrorCode == 0, tds.Data[0][0] return tds.Data[0]
def ifNewStock(self, startTime=None, endTime=None, newStockDays=40, tradeDaySource=u'Api'): ''' 对原始日线数据处理,生成是否是新股的boolean值 用到交易所交易日--通过接口获取 :return: 1 #新股(上市日期小于40或未上市), 0 #非新股 ''' volume = self._read_csv(u'volume_stocks', startTime, endTime) timeIndex = volume.index.astype(str) length1 = len(timeIndex) firstDate = timeIndex[0] primaryDate = pd.date_range(end=firstDate, periods=1000, freq=u'D').astype(str)[0] if tradeDaySource == u'Wind': from WindPy import w w.start() prevList = pd.Index( w.tdays(primaryDate, firstDate, u"").Data[0][:-1]).astype(str) w.stop() elif tradeDaySource == u'Api': prevList = pd.Index( self.APIeg.getTradeDate(start=primaryDate, end=firstDate)) else: # 从本地读取交易所交易日 prevList = self._read_csv(u'tradeDay_stocks').index.astype(str) tradedayList = prevList.union(timeIndex) # 整个交易日Index newStockDF = copy.deepcopy(volume) IPODf = self._read_csv(u'ipo_date_stocks') IPOSeries = IPODf.iloc[0] stockName = IPOSeries.index firstDateLoc = tradedayList.get_loc(firstDate) newTimeList = tradedayList[(firstDateLoc - newStockDays):] length2 = len(newTimeList) prevFirstDate = newTimeList[0] for i in range(len(stockName)): stock = stockName[i] ipoDate = IPOSeries[i] tempSeries = copy.deepcopy( pd.Series(np.zeros(length1), index=timeIndex)) if ipoDate > prevFirstDate: newTimeListLoc = newTimeList.get_loc(ipoDate) unNewDateLoc = min( len(newTimeList) - 1, newTimeListLoc + newStockDays) if unNewDateLoc <= length2 - 1 and unNewDateLoc >= newStockDays: tempSeries[:newTimeListLoc] = 1 newStockDF[stock] = pd.Series(tempSeries) newStockDF.to_csv(DataProcessing.CSV_PATH + u'is_new_stock_%s_stocks.csv' % newStockDays)
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 post(self): """ json str:{"begin_time": "2017-3-31", "end_time": "2017-3-31", "options": ""} :return: 返回万得返回数据dict """ args = tdays_parser.parse_args() logger.info('/tdays/ args:%s', args) # 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.tdays(**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('tdays(%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 if len(ret_data.Data) > 0 and len(ret_data.Data[0]) > 0: # date_str = format_datetime_to_str(ret_data.Data[0][0]) # ret_df = pd.DataFrame({'date': [format_datetime_to_str(d) for d in ret_data.Data[0]]}) # ret_df.index = [str(idx) for idx in ret_df.index] # ret_dic = {'date': [format_datetime_to_str(d) for d in ret_data.Data[0]]} ret_dic = [format_2_date_str(d) for d in ret_data.Data[0]] else: logger.warning('tdays(%s) No value return' % args) ret_dic = [] # ret_dic = ret_df.to_dict() # print('tdays:\n', ret_dic) return ret_dic
def QA_fetch_get_trade_date(endDate, exchange): with 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 getTradeDay(self, startdate, endDate, Period=''): ''' 获取指定周期交易日,封装wind接口 :param Period: ''日,W周,M月,Q季,S半年,Y年 :return: ''' # w.start() data = w.tdays(beginTime=startdate, endTime=endDate, options="Period=%s" % Period) if data.ErrorCode!=0: self.PrintInfoDemo.PrintLog('wind获取交易日期错误,请检查!') return tradeDayList = data.Data[0] tradeDayList = [tradeDay.strftime('%Y-%m-%d') for tradeDay in tradeDayList] # w.close() return tradeDayList
def CV_Compute(total_cnt, start_date, end_date, cmt_dict): """ 根据前面得到的所有品种历史合约表,在指定时间段内遍历每个交易日,筛选当日有效的合约, 下载持仓量、收盘价和乘数数据并计算各品种的合约价值。然后再按板块分别归纳统计板块和总 合约价值,最终将这一时间段所有交易日内的CV计算完成 """ # 从wind下载交易日列表 trade_days = w.tdays(start_date, end_date).Data[0] # 初始化品种cv列表,为轴向链接做准备 cnt_value_list = [] for t_date in trade_days: #万得下载的列表为datetime对象,这里生成字符串 target_date = t_date.strftime("%Y-%m-%d") #初始化 cnt_value_pd = pd.DataFrame(columns=cmt_dict["all_cmt"]) effective_cmt_df = total_cnt[ (total_cnt['contract_issue_date'] < t_date) & (total_cnt['last_trade_date'] > t_date)] cnt_list = ','.join(list(effective_cmt_df['wind_code'])) data = w.wss(cnt_list, "contractmultiplier,margin,close,oi", "tradeDate=" + target_date) tmp_df = pd.DataFrame(data.Data, index=data.Fields).T.fillna(0) tmp_df['cmt'] = [Cnt2Cmt(cnt) for cnt in data.Codes] tmp_df[target_date] = tmp_df['CONTRACTMULTIPLIER'] * tmp_df[ 'MARGIN'] / 100 * tmp_df['CLOSE'] * tmp_df['OI'] / 100000000 cnt_value_pd = tmp_df[target_date].groupby(tmp_df['cmt']).sum() cnt_value_list.append(cnt_value_pd) Contract_Value_allcmt = pd.concat(cnt_value_list, axis=1).T Contract_Value_total = Contract_Value_allcmt.sum(axis=1) Contract_Value_chem = Contract_Value_allcmt[cmt_dict["chem_cmt"]].sum( axis=1) Contract_Value_agri = Contract_Value_allcmt[cmt_dict["agri_cmt"]].sum( axis=1) Contract_Value_fmt = Contract_Value_allcmt[cmt_dict["fmt_cmt"]].sum(axis=1) Contract_Value_nfmt = Contract_Value_allcmt[cmt_dict["nfmt_cmt"]].sum( axis=1) Contract_Value_gld = Contract_Value_allcmt[cmt_dict["gld_cmt"]].sum(axis=1) Contract_Value_total.rename("total_CV", inplace=True) Contract_Value_agri.rename("agri_CV", inplace=True) Contract_Value_chem.rename("chem_CV", inplace=True) Contract_Value_fmt.rename("fmt_CV", inplace=True) Contract_Value_nfmt.rename("nfmt_CV", inplace=True) Contract_Value_gld.rename("gld_CV", inplace=True) Contract_Value_sector = pd.concat([Contract_Value_total,Contract_Value_agri,Contract_Value_chem,\ Contract_Value_fmt,Contract_Value_nfmt,Contract_Value_gld],axis=1) return Contract_Value_sector, Contract_Value_allcmt
def get_date(): today = datetime.today().strftime('%Y-%m-%d') last_month = (datetime.today() - timedelta(days=27)).month year = (datetime.today() - timedelta(days=27)).year date = w.tdays("2018-01-05", today, "") date_pd = pd.DataFrame(date.Data, columns=date.Times, index=['date']).T date_pd.index = date_pd.index.map(lambda x: x.strftime('%Y-%m-%d')) date_pd['month'] = date_pd['date'].map(lambda x: x.month) date_pd['year'] = date_pd['date'].map(lambda x: x.year) date_pd_month = date_pd[date_pd['year'] == year] date_pd_month = date_pd_month[date_pd_month['month'] == last_month] begin_date = date_pd_month.index[0] end_date = date_pd_month.index[len(date_pd_month) - 1] return begin_date, end_date
def main(): portfolio_history_holding = pd.read_excel(PORTFOLIO_HOLDING_FILE) # 提取组合所用基金历史净值并计算收益率 funds_returns = get_funds_returns(portfolio_history_holding) # 计算组合收益率 portfolio_return = calculate_daily_return(portfolio_history_holding, funds_returns) # 遍历止盈组合开始日期 data = w.tdays(START_DATE, END_DATE) tdays = pd.to_datetime(data.Data[0]) for target_rate in TARGET_RATES: for least_days, most_days in DATES_RANGE: print(target_rate, least_days, most_days) calculate_earning_portfolio(tdays, portfolio_history_holding, funds_returns, portfolio_return, target_rate, least_days, most_days)
def QA_fetch_get_trade_date(endDate, exchange): from WindPy import w # w.start() w.start() w.isconnected() 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) # print(data.Data) dates = data.Data else: print("exchange name problem") return dates
def post(self): """ json str:{"begin_time": "2017-3-31", "end_time": "2017-3-31", "options": ""} :return: 返回万得返回数据dict """ data_dic = request.json logger.info('/tdays/ data_dic:%s', data_dic) begin_time = data_dic['begin_time'] end_time = data_dic['end_time'] options = data_dic['options'] if not w.isconnected(): w.start() if options == "": options = None ret_data = w.tdays(begin_time, end_time, options) error_code = ret_data.ErrorCode if error_code != 0: msg = ERROR_CODE_MSG_DIC.setdefault(error_code, "") logger.error('tdays("%s", "%s", "%s") ErrorCode=%d %s' % (begin_time, end_time, options, error_code, msg)) if ret_data.ErrorCode == 40521010: w.close() w.start() logger.warning('网络连接超时,端口重新启动') return {'error_code': ret_data.ErrorCode, 'message': msg}, 404 # if ret_data.ErrorCode != 0: # logger.error( # 'tdays("%s", "%s", "%s") ErrorCode=%d' % (begin_time, end_time, options, ret_data.ErrorCode)) # if ret_data.ErrorCode == 40521010: # w.close() # w.start() # logger.warning('网络连接超时,端口重新启动') # return {'error_code': ret_data.ErrorCode}, 404 # 将 Data数据中所有 datetime date 类型的数据转换为 string if len(ret_data.Data) > 0 and len(ret_data.Data[0]) > 0: # date_str = format_datetime_to_str(ret_data.Data[0][0]) # ret_df = pd.DataFrame({'date': [format_datetime_to_str(d) for d in ret_data.Data[0]]}) # ret_df.index = [str(idx) for idx in ret_df.index] # ret_dic = {'date': [format_datetime_to_str(d) for d in ret_data.Data[0]]} ret_dic = [format_2_date_str(d) for d in ret_data.Data[0]] else: logger.warning('tdays(%s, %s, %s) No value return' % (begin_time, end_time, options)) ret_dic = [] # ret_dic = ret_df.to_dict() # print('tdays:\n', ret_dic) return ret_dic
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
__author__ = 'aming.tao' from WindPy import w from datetime import * w.start() data=w.wsd("600000.SH","close,amt","2013-04-30", datetime.today()-timedelta(1))#取浦发银行收盘价等信 data=w.wsd("600000.SH","close,amt", datetime.today()-timedelta(100))# data=w.wsi("600000.SH","close,amt","2015-10-01 9:00:00")#取浦发银行分钟收盘价等信息 data=w.wst("600000.SH","open", datetime.today()-timedelta(0,2*3600), datetime.now())#取浦发银行tick数据信息 data=w.wss("600000.SH,000001.SZ","eps_ttm,orps,surpluscapitalps","rptDate=20121231")#取浦发银行等财务数据信息 data=w.wset("SectorConstituent",u"date=20130608;sector=全部A股")#取全部A 股股票代码、名称信息 w.wset("IndexConstituent","date=20130608;windcode=000300.SH;field=wind_code,i_weight")#取沪深300 指数中股票代码和权重 w.wset("TradeSuspend","startdate=20130508;enddate=20130608;field=wind_code,sec_name,suspend_type,suspend_reason")#取停牌信息 w.wset("SectorConstituent",u"date=20130608;sector=风险警示股票;field=wind_code,sec_name")#取ST 股票等风险警示股票信息 w.tdays("2013-05-01","2013-06-08")#返回5 月1 日到6 月8 日之间的交易日序列 w.tdays("2013-05-01")#返回5 月1 日到当前时间的交易日序列 w.tdaysoffset(-5,"2013-05-01")#返回5 月1 日前推五个交易日的日期,返回2013-4-19 w.tdaysoffset(-5)#返回当前时间前推五个交易日的日期 w.tdayscount("2013-05-01","2013-06-08")#返回5 月1 日到6 月8 日之间的交易日序列长度,为27 w.stop()
import urllib.request import shutil import csv import codecs from datetime import date import sqlite3 import numpy as np import matplotlib.finance as finance import matplotlib.mlab as mlab import sys from WindPy import w w.start() hkTdays = w.tdays("2016-07-01", "2016-07-31", "TradingCalendar=HKEX") server = 'HKI.db' #startdate = date(2016,7,1) #enddate = date(2016,7,6) #fh = finance.fetch_historical_yahoo('^HSI', startdate, enddate)#, cachename=ticker + '.csv' # a numpy record array with fields: date, open, high, low, close, volume, adj_close) #r = mlab.csv2rec(fh); fh.close() #r.sort() #print (r) #r=r[r.volume>0]
''' #!/usr/bin/env python3 # -*- coding: utf-8 -*- from WindPy import w import pymysql w.start(); def saveTradeDate(tradeDates): if tradeDates.ErrorCode!=0: print('error code:'+str(tradeDates.ErrorCode)+'\n'); return(); print('Data length:',len(tradeDates.Data[0])) conn=pymysql.connect(host='192.168.10.222',port='3306',user='******',passwd='cc2718281828',db='AMGMH',charset='utf8') cur=conn.cursor() for i in range(0,len(tradeDates.Data[0])): strTemp='' #if len(tradeDates.Times)>1: # strTemp=str(tradeDates.Times[i])+' ' for k in range(0, len(tradeDates.Fields)): strTemp=strTemp+str(tradeDates.Data[k][i])+' ' sta=cur.execute('insert into TDays values('+'\'SHE\','+str(tradeDates.Data[k][i])+')') print(sta) print(strTemp) tradeDate = w.tdays('2016-03-25', '2016-12-31', 'TradingCalendar=SZSE') saveTradeDate(tradeDate) w.stop();
def tradedate(startDate,endDate): #convert dates to trading dates. d = w.tdays(startDate,endDate,'TradingCalendar=SHFE;') return d.Times