def hist_line(pepbs, start=origin): h = pd.DataFrame() for date in pd.DatetimeIndex(jqdata.get_trade_days(start_date=start)): h[date] = pd.Series(pos_hist(pepbs, dt.datetime.strftime(date, '%Y-%m-%d')), index=['pe', 'pb']) return h.transpose()
def hist_data(code, start_date=None, end_date=None): # 参数检查 today_str = (datetime.now().date() - timedelta(1)).strftime('%Y-%m-%d') if start_date is None or start_date < _start_date: start_date = _start_date if end_date is None or end_date < _start_date or end_date > today_str: end_date = today_str # 交易日 trade_days = get_trade_days(start_date=start_date, end_date=end_date) col = _codes[code].get('position') name = _codes[code].get('name') def data_list(): for day in trade_days: datas = [] for page in [2, 3, 4]: #链接 url = '%stype=zy%s&date=%s' % (_url_base, page, day) #页面请求 content = requests.get(url).text soup = BeautifulSoup(content, 'html.parser') data = soup.select('tbody[class="tc"] td') if len(data) == 0: break datas.append(data[col].get_text()) print '\r数据更新:板块%s %s %s ' % (code, name, day), yield [day] + datas # 组织数据(迭代数据生成器) df = pd.DataFrame(data=[item for item in data_list()], columns=['date', 'pe', 'pb', 'dyr']) df.set_index('date', inplace=True) df.index.name = None df.index = pd.to_datetime(df.index, format='%Y-%m-%d') return df.astype('float')
def pl_load_change_pct_data(context,pl_codes): pl_change_pct_dict_list = [] # 计算涨跌幅需要用到前一日收盘价,所以需要多加载一天的数据, # 计算前一个交易日收盘后的数据,所以需要再多加载一天 # 使用固定的25个交易日,而非25个bar计算涨跌幅 pl_count = PL_CHANGE_PCT_DAY_NUMBER + 1 # 获取25个交易日的日期 pl_pre_25_dates = jqdata.get_trade_days(start_date=None, end_date=context.current_dt, count=pl_count) pl_pre_25_date = pl_pre_25_dates[0] pl_pre_1_date = pl_pre_25_dates[-1] for pl_code in pl_codes: pl_pre_25_data = get_price(pl_code, start_date=None, end_date=pl_pre_25_date, frequency='daily', fields=['close'], skip_paused=True, fq='post', count=1) pl_pre_1_data = get_price(pl_code, start_date=None, end_date=pl_pre_1_date, frequency='daily', fields=['close'], skip_paused=True, fq='post', count=1) pl_pre_25_close = None pl_pre_1_close = None if str(pl_pre_25_date) == str(pl_pre_25_data.index[0])[:10]: pl_pre_25_close = pl_pre_25_data['close'][0] if str(pl_pre_1_date) == str(pl_pre_1_data.index[0])[:10]: pl_pre_1_close = pl_pre_1_data['close'][0] if pl_pre_25_close != None and pl_pre_1_close != None and not math.isnan(pl_pre_25_close) and not math.isnan(pl_pre_1_close): pl_change_pct = (pl_pre_1_close - pl_pre_25_close) / pl_pre_25_close pl_item = {'code':pl_code, 'change_pct': pl_change_pct} pl_change_pct_dict_list.append(pl_item) return pl_change_pct_dict_list
def get_pepb_range(index_code, start=origin, end=today, weight='C'): pebs = pd.DataFrame() for date in pd.DatetimeIndex( jqdata.get_trade_days(start_date=start, end_date=end)): s = get_pepb(index_code, dt.datetime.strftime(date, '%Y-%m-%d'), weight) if s != [0, 0]: pebs[dt.datetime.strftime(date, '%Y-%m-%d')] = pd.Series( s, index=['pe', 'pb']) return pebs.dropna()
def update(self, codes): """ 更新数据,子类可重载 codes:代码列表,list 返回:无 """ # 更新数量 n = 0 # 更新日期 update_date = None # 最近一个交易日 trade_date = get_trade_days(end_date=pd.datetime.today(), count=10)[-2].strftime('%Y-%m-%d') # 数据保存模式 append = False # 遍历代码列表 for code in codes: print '\r数据更新:%s' % (self.pool.name(code)), # 判断数据文件是否存在 # 不存在则初次获取,存在则追加数据 try: # 读取数据 temp_df = self.read(code, years=1) # 获取上次更新日期 update_date = temp_df.index[-1].date() # 本次更新日期 clac_date = (update_date + timedelta(1)).strftime('%Y-%m-%d') # 本次更新日期大于最近交易日期,则无需更新 if clac_date > trade_date: continue # 增量获取 df = self.get_data(code, start_date=clac_date) # 追加模式 append = True except IOError: # 替换模式 append = False # 初次获取 df = self.get_data(code) except Exception as e: #print '%s:%s'%(self.pool.name(code),e) continue if not df is None: # 保存数据 self.save(code, df, append=append) # 获取更新日期 #update_date=df.index[-1].date() print code n += 1 if n > 0: print '\r数据更新:%s,已更新 %s 个' % (self.project.note, n) else: print '\r数据更新:%s,无需更新' % (self.project.note)
def get_current_tradeday(context=None, end_date=None, days: int = 0, ret_first=True): """ 获取最近N个交易日 :return: """ if context: end_date = context.current_dt.date() trade_days = jq.get_trade_days(end_date=end_date, count=days + 1) return trade_days[0] if ret_first else trade_days
def load_change_pct_data(context, codes): ''' 计算标的的25日涨跌幅。 Args: context 上下文 codes 标的的代码列表 Returns: 标的的涨跌幅列表。列表中的每一项数据时一个字典: code:标的代码 change_pct: 标的的涨跌幅 ''' change_pct_dict_list = [] # 计算涨跌幅需要用到前一日收盘价,所以需要多加载一天的数据, # 而这里在第二日的开盘前运行,计算前一个交易日收盘后的数据,所以需要再多加载一天 # 使用固定的25个交易日,而非25个bar计算涨跌幅 count = CHANGE_PCT_DAY_NUMBER + 1 # 获取25个交易日的日期 pre_25_dates = jqdata.get_trade_days(start_date=None, end_date=context.current_dt, count=count) pre_25_date = pre_25_dates[0] pre_1_date = pre_25_dates[-1] for code in codes: pre_25_data = get_price(code, start_date=None, end_date=pre_25_date, frequency='daily', fields=['close'], skip_paused=True, fq='post', count=1) pre_1_data = get_price(code, start_date=None, end_date=pre_1_date, frequency='daily', fields=['close'], skip_paused=True, fq='post', count=1) pre_25_close = None pre_1_close = None if str(pre_25_date) == str(pre_25_data.index[0])[:10]: pre_25_close = pre_25_data['close'][0] if str(pre_1_date) == str(pre_1_data.index[0])[:10]: pre_1_close = pre_1_data['close'][0] if pre_25_close != None and pre_1_close != None and not math.isnan( pre_25_close) and not math.isnan(pre_1_close): change_pct = (pre_1_close - pre_25_close) / pre_25_close item = {'code': code, 'change_pct': change_pct} change_pct_dict_list.append(item) return change_pct_dict_list