def fetch_fund_history_data(self, fund_types, path, start_day = '2011-09-11', end_day = None): ''' Get the fund history information and save it to a csv file per fund. Parameters ---------- fund_types : list of string the fund types For example: ['all'] ['all', 'equity', 'mix', 'bond', 'monetary'] ... path : string the path to save the funds files start_day : string, default is '2011-09-11' the start day of the fund information end_day : string, default is today end day for fund history Returns ------- None ''' if end_day is None: end_day = datetime.date.today().strftime("%Y-%m-%d") fund_symbols = list() for fund_type in fund_types: fund_symbols += self._get_fund_symbols(fund_type) # Remove duplicated symbols fund_symbols = list(set(fund_symbols)) for fund_symbol in tqdm(fund_symbols[:]): try: # print('Getting %s history infortation...' %(fund_symbol)) his_df = nav.get_nav_history(fund_symbol, start = start_day, end = end_day, retry_count = 5, timeout = 20) file_spec = path + fund_symbol +'.csv' his_df.to_csv(file_spec) print('\n Saved %s' %(file_spec)) except Exception as e: print(e) pass
def test_nav_history(self): self.set_data() lst = ['164905', '161005', '380007', '000733', '159920', '164902', '184721', '165519', '164302', '519749', '150275', '150305', '150248'] for _, item in enumerate(lst): print '\n=============\nget %s nav\n=============' % item fund_df = nav.get_nav_history(item, self.start, self.end) if fund_df is not None: print '\n', 'nums=', len(fund_df), '\n', fund_df[:self.disp]
def generate_fund_history_data(fund_type, start_day = None, end_day = None): ''' Get the fund history information and save it to a csv file per fund. Inputs: fund_type - one of ['all', 'equity', 'mix', 'bond', 'monetary'] start_day - '2011-09-11' if not set. end_day - today if not set. ''' if start_day is None: start_day = '2011-09-11' if end_day is None: end_day = datetime.date.today().strftime("%Y-%m-%d") if fund_type == 'all': fund_type_list = ['all', 'equity', 'mix', 'bond', 'monetary'] else: fund_type_list = [fund_type] for fund_type in fund_type_list: fund_symbols = get_fund_symbols(fund_type) for fund_symbol in fund_symbols[:]: try: # print('Getting %s history infortation...' %(fund_symbol)) fund_his_data = nav.get_nav_history(fund_symbol, start = start_day, end = end_day, retry_count = 5, timeout = 20) fund_info_file = utility.check_path(path_name + 'fundhistory' + dy.sep + fund_type + 'fund') + dy.sep + fund_symbol +'.csv' print('Saving %s history infortation to %s ...' %(fund_symbol, fund_info_file)) fund_his_data.to_csv(fund_info_file) except Exception as e: print(e) pass
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'MFC' __time__ = '2019-03-12 22:26' from tushare.fund import nav # def test_get_nav_open(): # # lst = ['all', 'equity', 'mix', 'bond', 'monetary', 'qdii'] # lst = ['monetary'] # print('get nav open................\n') # for item in lst: # print('=============\nget %s nav\n=============' % item) # fund_df = nav.get_nav_open(item) # print('\nnums=%d' % len(fund_df)) # print(fund_df[:2]) # 返回的数据 return pd.concat(fund_dfs, ignore_index=True) # # test_get_nav_open() # TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame" # get fund history info ret = nav.get_nav_history("160716", start="2019-03-01", end="2019-03-12") print(type(ret)) print(ret) # get fund detail info ret2 = nav.get_fund_info("160716") print(type(ret2)) print(ret2)