def collect_basic_data(): basic_data_path = os.path.join(BASE_FOLDER, 'basics') if not os.path.exists(basic_data_path): os.makedirs(basic_data_path) path = os.path.join(basic_data_path, 'basic.csv') if not os.path.exists(path): ts.get_stock_basics().to_csv(path) return path
def load_file(): db_full_path = get_db_file_name() if os.path.exists(db_full_path): if os.path.getsize(db_full_path) == 0: stock_list = ts.get_stock_basics() save_file(stock_list) else: stock_list = ts.get_stock_basics() save_file(stock_list) f = open(db_full_path, 'rb') objects = pickle.load(f) f.close return objects
def download_stock_basic_info(): try: df = ts.get_stock_basics() print df.columns df[KEY_CODE] = df.index df = df[[KEY_CODE,KEY_NAME, KEY_INDUSTRY, KEY_AREA, KEY_TimeToMarket]] print df.columns print df.head() sql = 'select code from {}'.format(STOCK_BASIC_TABLE) df_code = pd.read_sql(sql, engine) df = df[df['code'].apply(lambda x : not x in df_code['code'].get_values())] print df.head() print len(df) if len(df): df.to_sql(STOCK_BASIC_TABLE, engine, if_exists='append', index=False) # 添加指数 # indexs = [('sh', '上证指数', '指数','全国','19910715'), # ('sz', '深圳成指', '指数','全国','19940720'), # ('hs300', '沪深300指数', '指数','全国','20050408'), # ('sz50', '上证50指数', '指数','全国','20040102'), # ('zxb', '中小板指数', '指数','全国','20050607'), # ('cyb', '创业板指数', '指数','全国','20100531'),] # df = pd.DataFrame(indexs, columns=[KEY_CODE,KEY_NAME, KEY_INDUSTRY, KEY_AREA, KEY_TimeToMarket]) # print df # df.to_sql(STOCK_BASIC_TABLE, engine, if_exists='append', index=False) except Exception as e: print str(e)
def update_basics(): basics = ts.get_stock_basics() f = os.path.join(DATA_DIR, 'basics.h5') basics.to_hdf(f, 'basics') length = 4 * 5 year, season = last_report_season() for i in range(length): f = os.path.join(DATA_DIR, 'basics-{0}-{1}.h5'.format(year, season)) if os.path.exists(f): continue report = ts.get_report_data(year, season) report.to_hdf(f, 'report') profit = ts.get_profit_data(year, season) profit.to_hdf(f, 'profit') operation = ts.get_operation_data(year, season) operation.to_hdf(f, 'operation') growth = ts.get_growth_data(year, season) growth.to_hdf(f, 'growth') debtpaying = ts.get_debtpaying_data(year, season) debtpaying.to_hdf(f, 'debtpaying') cashflow = ts.get_cashflow_data(year, season) cashflow.to_hdf(f, 'cashflow') season -= 1 if season == 0: season = 4 year -= 1
def QA_save_stock_day_all(client=DATABASE): df = ts.get_stock_basics() __coll = client.stock_day __coll.ensure_index('code') def saving_work(i): QA_util_log_info('Now Saving ==== %s' % (i)) try: data_json = QA_fetch_get_stock_day(i, start='1990-01-01') __coll.insert_many(data_json) except Exception as e: print(e) QA_util_log_info('error in saving ==== %s' % str(i)) for i_ in range(len(df.index)): QA_util_log_info('The %s of Total %s' % (i_, len(df.index))) QA_util_log_info( 'DOWNLOAD PROGRESS %s ' % str(float(i_ / len(df.index) * 100))[0:4] + '%' ) saving_work(df.index[i_]) saving_work('hs300') saving_work('sz50')
def sync_stock_list(): ''' get stock list only ''' global stocklist df = ts.get_stock_basics() stocklist = df.index.tolist()
def dataframe2list_test(): ''' 把dataframe的index列保存到一个list里面 ''' data = tushare.get_stock_basics() data_list = data.index.tolist() print(data_list)
def check_data_integrity(data_path): ''' 对比data_path对应的文件夹中所有csv文件和basic_info.csv中code是否对应 Parameters ------ data_path: 指定文件夹路径 return ------- missing_code_list: 缺失的code列表 ''' data_code_list = sfu.get_code_list_in_dir(data_path) missing_code_list = [] basic_data = pd.DataFrame() try: basic_data = tushare.get_stock_basics() except Exception as exception: slog.StlDmLogger().error('tushare.get_stock_basics() excpetion, args: %s' % exception.args.__str__()) if basic_data is None: slog.StlDmLogger().warning('tushare.get_stock_basics() return none') return [] else: code_list = basic_data.index for code in code_list: found = False for tmp_code in data_code_list: if tmp_code == code: found = True break if found != True: missing_code_list.append(code) return missing_code_list
def ggdr(year1,month1,day1,year2,month2,day2): gongsilist = ts.get_stock_basics().index a = len(gongsilist) for gongsi in gongsilist: basic_info_insert(gonggao_dict(gongsi,year1,month1,day1,year2,month2,day2)) a -= 1 print '还剩',a,'家'
def save_data_excel(self): df = ts.get_stock_basics() df.to_csv(self.today + '.csv', encoding='gbk') df_x = pd.read_csv(self.today + '.csv', encoding='gbk') df_x.to_excel(self.today + '.xls', encoding='gbk') os.remove(self.today + '.csv')
def get_all_index_K_yangbaoyin(data): good = {} stock_list = ts.get_stock_basics() for code in stock_list.index: hist = ts.get_hist_data(code) good[code] = get_index_K_yangbaoyin(hist) return good
def save_stock_basics_to_sql(): back = ts.get_stock_basics() back['code'] = back.index back.index = range(back.shape[0]) engine = create_engine('mysql://*****:*****@localhost/stock?charset=utf8') filename = "stock_basics" back.to_sql(filename, engine, if_exists='append',index_label=['index'])
def basic_info(self,retry=5): engine = get_engine('db_stock') # 需要添加异常处理 重试次数 count = 0 while count < retry: try: df = ts.get_stock_basics() except Exception as e: logger.info(e) time.sleep(10) count+=1 continue else: if df is not None: df=df.reset_index() df['更新日期']=datetime.datetime.now() df.to_sql('tb_basic_info',engine,if_exists='replace') logger.info('入库成功') break else: count+=1 time.sleep(10) continue
def today_df_filter0(today_df): #""" today_df = ts.get_today_all() today_df = today_df[today_df.amount>0] today_df_high_open = today_df[today_df.open>today_df.settlement*1.005] all_trade_code = today_df['code'].values.tolist() all_a_code = ps.get_all_code(hist_dir="C:/中国银河证券海王星/T0002/export/") all_stop_code = list(set(all_a_code).difference(set(all_trade_code))) print('\n') print('all_stop_code=%s' % all_stop_code) print(len(all_stop_code)) high_open_code_str = today_df_high_open['code'].values.tolist() print('all_trade_code = %s'%all_trade_code) print(len(all_trade_code)) print('today_df_high_open = %s'%high_open_code_str) today_df['star'] = ((today_df['trade']-today_df['open'])/(today_df['high']-today_df['low'])).round(3) today_df['star_h'] = np.where(today_df['star']>=0, ((today_df['high']-today_df['trade'])/(today_df['high']-today_df['low'])).round(3), ((today_df['high']-today_df['open'])/(today_df['high']-today_df['low'])).round(3)) today_df['atr'] = np.where((today_df['high']-today_df['low'])<(today_df['high']-today_df['settlement']), today_df['high']-today_df['settlement'],today_df['high']-today_df['low']) #temp_df['close'].shift(1)-temp_df['low']) today_df['atr'] = np.where(today_df['atr']<(today_df['settlement']-today_df['low']), (today_df['settlement']-today_df['low']),today_df['atr']) today_df['atr_r'] = ((today_df['atr']/today_df['settlement']).round(3))*100.0 today_df['star_chg'] = today_df['star'] * today_df['changepercent'] #del today_df['atr'] describe_df = today_df.describe().round(3) #print(type(describe_df)) lt_describe = describe_df.loc['25%']#.iloc[3].values mean_chg = describe_df.loc['mean','changepercent'] most_chg = describe_df.loc['75%','changepercent'] least_chg = describe_df.loc['25%','changepercent'] most_atr_r = describe_df.loc['75%','atr_r'] least_atr_r = describe_df.loc['25%','atr_r'] print(mean_chg,most_chg,least_chg,most_atr_r,least_atr_r) print(describe_df) great_rate = 2.0 gt_today_df = today_df[today_df.changepercent> great_rate] great_atd_df = today_df[today_df['atr_r']>11] stock_basic_df=ts.get_stock_basics() #stock_basic_df['outstanding'] = stock_basic_df['outstanding'] * 0.0001 #stock_basic_df['totals'] = stock_basic_df['totals'] * 0.0001 lt_outstanding_df = stock_basic_df[stock_basic_df.outstanding<100000] #流通股本小于10亿 print(lt_outstanding_df) today_df['real'] = 0.00000001 *today_df['amount']/today_df['turnoverratio'] min_atr_df = today_df[today_df.real<10.0] min_atr_df = min_atr_df[min_atr_df.atr_r<least_atr_r] lt_outstanding_df_list = lt_outstanding_df.index.values.tolist() print(lt_outstanding_df_list) min_atr_df = min_atr_df.set_index('code') min_atr_df_list = min_atr_df.index.values.tolist() print(min_atr_df_list) inter_list = list(set(min_atr_df_list).intersection(set(lt_outstanding_df_list))) print(len(inter_list)) filter_df = min_atr_df[min_atr_df.index.isin(inter_list)] #print(filter_df) #print(type(filter_df)) min_atr_df = filter_df.sort_values(axis=0, by='atr_r', ascending=True) min_atr_df = filter_df.sort_values(axis=0, by='star', ascending=False) print(min_atr_df)
def get_code_list(): stock_list_csv_path = STOCK_BASICS_DATA_PATH if os.path.isfile(stock_list_csv_path): stock_list_df = pd.read_csv(stock_list_csv_path, dtype={"code":"object"}) else: stock_list_df = ts.get_stock_basics() return stock_list_df["code"]
def importDb(): flag = 0 try: #首先取出所有股票的代码,然后取得股票的上市时间,根据上市时间按年增加数据,一直进行循环 conn = MySQLdb.connect(host='localhost',user='******',passwd='123456',db='stock',charset="utf8") engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8') cursor = conn.cursor() sql ="select * from stock_basic" cursor.execute(sql) stockDf =ts.get_stock_basics() startImport = time.localtime(time.time()) #通过循环获得股票的基本信息并写入到数据库中,并实现定义好股票的相关基本信息与数据库的字段相对应 #通过循环获取当前股票的最大时间,然后利用最大时间与与当前时间设置差值,进行导入数据 for row in cursor.fetchall(): stockno = str(row[0]) print stockno currentDay = time.strftime('%Y-%m-%d',time.localtime(time.time())) end = currentDay #dbutil.get_day_data(engine,stockno,end,end) dbutil.get_bfq_data(engine,conn,stockno,end,end) dbutil.get_day_data(engine,conn,stockno,end,end) print "sdfsdf" #dbutil.get_five_data(stockno,start,end) #ssYear = ssYear+1 #取得当前的年月,并按年进行循环,插入数据相关数据 print "import stock data success" return flag except Exception,e: print e.message
def get_stock_basics(): try: df = ts.get_stock_basics() df.reset_index(level=0, inplace=True) return json.loads(df.to_json(orient='records')) except IOError: pass
def freshBasicStockInfo(): import tushare as ts td = ts.get_stock_basics() ntd = td.reset_index().loc[:, ['code', 'name', 'industry', 'area', 'timeToMarket', 'totals', 'outstanding']] df = pd.read_sql_query("select * from stock_basic where flag=0", db.engine) tdf = pd.merge(ntd, df, on='code', how='left') def fixNoneTime(x): return pd.to_datetime('1900-01-01') if pd.isnull(x) else x def fixNoneTime2Now(x): return datetime.now() if pd.isnull(x) else x df2 = pd.DataFrame({ 'code': tdf['code'], 'name': tdf['name_x'], 'industry': tdf['industry_x'], 'area': tdf['area_x'], 'zgb': tdf['totals'], 'ltgb': tdf['outstanding'], 'launch_date': tdf['timeToMarket'].apply(lambda x: pd.to_datetime('1900-01-01').date() if x==0 else x), 'latest_report': tdf['latest_report'].apply(lambda x: pd.to_datetime('1900-01-01').date() if pd.isnull(x) else x), 'holder_updated_time': tdf['holder_updated_time'].apply(fixNoneTime), 'trade_updated_time': tdf['trade_updated_time'].apply(fixNoneTime), 'finance_updated_time': tdf['finance_updated_time'].apply(fixNoneTime), 'created_time': tdf['created_time'].apply(fixNoneTime2Now), 'flag': '0' }) db.session.execute('update stock_basic set flag="-1"') df2.to_sql('stock_basic', db.engine, if_exists='append', index=False, chunksize=1000)
def downloadStockBasics(): stockBasics = ts.get_stock_basics() # stockBasics.insert(0,"update_date",time.strftime( conf.ISO_DATE_FORMAT, time.localtime()),True) executeSQL("delete from t_stock_basics") stockBasics.to_sql(name="t_stock_basics", con=getEngine(), if_exists="append") return stockBasics
def get_stock_base(config): ''' get the stock basic info :param data_root_path: :param date_format_str: :return: ''' data_root_path = config.dataConfig.data_root_path date_format_str = config.dataConfig.date_format_str stock_base_path = os.path.join(data_root_path, 'base').lower() if not os.path.isdir(stock_base_path): os.mkdir(stock_base_path) today_date_str = datetime.strftime(datetime.now(), date_format_str) stock_startwords = 'stockbase' stock_base_file_name = stock_startwords + '_' + today_date_str + '.csv' for file_name in os.listdir(stock_base_path): if file_name.startswith(stock_startwords): stock_base_file_name = file_name stock_base_full_file_name = os.path.join(stock_base_path, stock_base_file_name) if stock_base_file_name.find(today_date_str) > -1: pass else: stock_base_data = tushare.get_stock_basics() stock_base_data.to_csv(stock_base_full_file_name.lower()) return stock_base_full_file_name
def download_stock_basic_info(): """ 获取股票基本信息 :return: """ try: df = ts.get_stock_basics() print(df.columns) df['code'] = df.index print(df.head()) if len(df): engine = db.get_w_engine() to_sql(STOCK_BASIC_TABLE, engine, df, type='replace') # df.to_sql(STOCK_BASIC_TABLE, engine, if_exists='append', index=False) # 添加指数 indexs = [('sh', '上证指数', '指数','全国','19910715'), ('sz', '深圳成指', '指数','全国','19940720'), ('hs300', '沪深300指数', '指数','全国','20050408'), ('sz50', '上证50指数', '指数','全国','20040102'), ('zxb', '中小板指数', '指数','全国','20050607'), ('cyb', '创业板指数', '指数','全国','20100531'),] df = pd.DataFrame(indexs, columns=[KEY_CODE,KEY_NAME, KEY_INDUSTRY, KEY_AREA, KEY_TimeToMarket]) print(df) to_sql(STOCK_BASIC_TABLE, engine, df, type='replace') except Exception as e: print(str(e))
def QA_save_stock_day_with_fqfactor(client=DATABASE): df = ts.get_stock_basics() __coll = client.stock_day __coll.ensure_index('code') def saving_work(i): QA_util_log_info('📝Now Saving ==== %s' % (i)) try: data_hfq = QA_fetch_get_stock_day( i, start='1990-01-01', if_fq='02', type_='pd') data_json = QA_util_to_json_from_pandas(data_hfq) __coll.insert_many(data_json) except: QA_util_log_info('error in saving ==== %s' % str(i)) for i_ in range(len(df.index)): QA_util_log_info('The %s of Total %s' % (i_, len(df.index))) QA_util_log_info('⏳DOWNLOAD PROGRESS %s ' % str( float(i_ / len(df.index) * 100))[0:4] + '%') saving_work(df.index[i_]) saving_work('hs300') saving_work('sz50') QA_util_log_info('Saving Process has been done !') return 0
def process(self): fo = open('shrink_code', 'w') oversold = open('oversold', 'w') week_shrink = open('week_shrink','w') #raw_data = TS.memchaced_data(ts.get_stock_basics,'get_stock_basics') raw_data = ts.get_stock_basics() raw_data['earn_ratio'] = raw_data['esp'] / raw_data['bvps'] for code in raw_data.index: if code in bad_container: continue try: daydata = ts.get_k_data(code, ktype='D') ratio = judge_oversold(daydata) if ratio < -0.45: oversold.write("{0}\t{1}\n".format(code, ratio)) weekdata = ts.get_k_data(code, ktype='W') if judge_week_shrinkage(code, 0.02): week_shrink.write(code + '\n') if ratio > -0.3: continue #if raw_data.ix[code]['earn_ratio'] < 0.05: # continue totals = raw_data.ix[code]['totals'] close = np.array(daydata['close'])[-1] if close < 6.5: continue if close * totals > 360: continue flag,mean = judge_shrinkage(daydata,0.02) if ratio > -0.3: continue if flag != -1: fo.write("{0}\t{1}\t{2:.1%}\n".format(flag,code, mean)) except Exception, e: print e
def collect_hist_data(start=None, end=None, type='D', exclude_cyb=True, sample=0, persist=False): folder_name = 'hist-{}-{:%y%m%d}-{:%y%m%d}'.format( type, datetime.datetime.strptime(start, '%Y-%m-%d'), datetime.datetime.today()) storage_path = os.path.join(BASE_FOLDER, folder_name) # if target storage path already exists, consider the data has been collected already if not os.path.isdir(storage_path): os.makedirs(storage_path) if not os.listdir(storage_path): basics = ts.get_stock_basics() codes = basics.index if not exclude_cyb else [x for x in basics.index if not x.startswith('300')] hist_data = {} codes_selected = codes if sample == 0 else random.sample(codes, sample) for code in codes_selected: try: df = ts.get_hist_data(code, start, end, ktype=type) hist_data[code] = df if persist: df.to_csv(os.path.join(storage_path, '%s.csv' % code)) print('retrieved hist data for %s' % code) except Exception as ex: try: print('error occurred in retrieving {}: {}'.format(code, ex)) except Exception as innerex: print('exception: {}'.format(innerex)) # return pd.Panel(hist_data) return storage_path
def stock_list(): df = ts.get_stock_basics() data = df.to_dict('index') for code, value in sorted(data.items()): # print(code) # print(value['name']) Stock.objects(code=code).update_one(code=code, name=value['name'], upsert=True)
def process(self): week_shrink = open('week_shrink','w') raw_data = ts.get_stock_basics() raw_data['earn_ratio'] = raw_data['esp'] / raw_data['bvps'] container = defaultdict(list) for code in raw_data.index: if code in bad_container: continue weekdata = ts.get_k_data(code, ktype='W') try: info = compute_foundation_info(code, weekdata, 36) if info['ratio'] > 0.2: continue if not info['cross_flag'] and not info['nearcross_flag']: continue if info['macdmean'] > 0.3: continue assment = info['close'] * raw_data.ix[code]['totals'] if info['week_shrink'] < 2 or info['close'] < 6 or assment > 1800: continue for feature in features: feature_value = info[feature] container[feature].append(feature_value) except Exception,e: print "ERROR:{0}".format(code)
def load_comp_basic(session): log_file = (os.getcwd() + os.sep + "logs" + os.sep + "load_comp_basic_%s.log") % ( dt.datetime.now().strftime('%Y-%m-%d')) logger = get_logger(log_file,'basic_load') logger.info('Daily company basic information load begin') logger.info("##########################################") logger.info("Begin load data from Tushare") # 获得上市公司的基本数据 code_list = ts.get_stock_basics() code_list = code_list.sort_index() logger.info(("Get total %d stocks") % (code_list.index.__len__())) try: logger.info("Truncate table stock.comp_basic") session.execute(str("truncate table stock.comp_basic")) session.commit() logger.info("Insert data into table stock.comp_basic") code_list.insert(0,'code',code_list.index) code_list.timeToMarket =code_list.timeToMarket.apply(lambda x:dt.datetime.strptime(x.__str__(),"%Y%m%d") if x !=0 else None) engine = session.get_bind() code_list.to_sql('comp_basic', engine, schema='stock', index=False, if_exists='append') logger.info("Load successfully") return code_list except Exception as e: logger.error("Error when get basic data") return pd.DataFrame() finally: session.close()
def stocks_clawer(save=True): logging.info("Fetch from remote!") df = ts.get_stock_basics() logging.info("Fetch %s stocks from remote!"% len(df.index)) if save is True: _save_db(df, db_stocks_table, if_exists="replace", index_label="code") return df
def QA_fetch_get_stock_info(name): data = QATs.get_stock_basics() data_json = QA_util_to_json_from_pandas(data) for i in range(0, len(data_json) - 1, 1): data_json[i]['code'] = data.index[i] return data_json
def get_stock_his_day_Data(code, startDay, endDay):###generator for the stock data share by year df = ts.get_stock_basics() tmDate = df.ix[code]['timeToMarket'] if '-' in startDay: _d = startDay.split('-') startDay = _d[0]+_d[1]+_d[2] if '-' in endDay: _d = endDay.split('-') endDay = _d[0]+_d[1]+_d[2] if not isinstance(startDay, np.int64): startDay = np.int64(startDay) if not isinstance(endDay, np.int64): endDay = np.int64(endDay) if startDay < tmDate: startDay = tmDate today = np.int64( str(datetime.date.today()).replace('-','') ) if endDay > today: endDay = today #search by year, for the reliability nyears = endDay/10000 - startDay/10000 + 1 sstartDay, sendDay = str(startDay), str(endDay) for nyear in xrange(startDay/10000,endDay/10000+1): tmpStart = sstartDay[0:4]+'-'+sstartDay[4:6]+'-'+sstartDay[6:8] if nyear==startDay/10000 else str(nyear)+'-01-01' tmpEnd = sendDay[0:4]+'-'+sendDay[4:6]+'-'+sendDay[6:8] if nyear==(endDay/10000) else str(nyear)+'-12-31' logging.debug("get code:%s history data from %s to %s" %(code, tmpStart, tmpEnd)) tmpdata = ts.get_h_data(code, start=tmpStart, end=tmpEnd) yield(tmpdata)
def get_stock_basics(engine): df = ts.get_stock_basics() df.to_sql('stock_basics', engine, if_exists='append')
import tushare as ts import pandas as pd import time date = time.strftime('%Y-%m-%d', time.localtime(time.time())) hs300 = ts.get_hs300s() sz50 = ts.get_sz50s() basic = ts.get_stock_basics() hs300.describe() print('ok1') roe2017_4 = ts.get_profit_data(2017, 4) roe2017_3 = ts.get_profit_data(2017, 3) roe2017_2 = ts.get_profit_data(2017, 2) roe2017_1 = ts.get_profit_data(2017, 1) roe2017_4 = roe2017_4.loc[:, ['code', 'name', 'roe']] roe2017_3 = roe2017_3.loc[:, ['code', 'name', 'roe']] roe2017_2 = roe2017_2.loc[:, ['code', 'name', 'roe']] roe2017_1 = roe2017_1.loc[:, ['code', 'name', 'roe']] hs300_2017_4 = pd.merge(hs300, roe2017_4, on=['code', 'name']) hs300_2017_3 = pd.merge(hs300, roe2017_3, on=['code', 'name']) hs300_2017_2 = pd.merge(hs300, roe2017_2, on=['code', 'name']) hs300_2017_1 = pd.merge(hs300, roe2017_1, on=['code', 'name']) print('ok3') hs300_2017_4 = hs300_2017_4.loc[:, ['code', 'name', 'roe']]
def code_issue(): base = ts.get_stock_basics() base.to_excel('111.xls')
import tushare as ts dftest = ts.get_k_data('000001', start='2019-04-01', end='2019-04-10') #Index = True """dftest date open close high low volume code 58 2019-04-01 12.83 13.18 13.55 12.83 1951401.0 000001 59 2019-04-02 13.28 13.36 13.48 13.23 1100384.0 000001 60 2019-04-03 13.21 13.44 13.45 13.15 792915.0 000001 61 2019-04-04 13.43 13.86 14.00 13.43 2034365.0 000001 62 2019-04-08 13.90 13.96 14.43 13.72 1743176.0 000001 """ df_code = ts.get_stock_basics() ''' name industry area pe ... profit gpr npr holders code ... 300768 N迪普 软件服务 浙江 32.18 ... 30.53 70.69 28.55 77596.0 300158 振东制药 中成药 山西 72.75 ... -74.66 64.12 2.58 20263.0 600794 保税科技 仓储物流 江苏 178.89 ... 115.68 11.39 2.60 83309.0 000607 华媒控股 广告包装 浙江 84.60 ... -32.61 26.85 5.20 40359.0 600178 东安动力 汽车配件 黑龙江 579.92 ... -86.97 11.35 0.43 40965.0 000862 银星能源 新型电力 宁夏 107.47 ... 129.65 38.13 4.45 82454.0 600710 苏美达 工程机械 江苏 19.77 ... 27.19 6.14 0.56 34400.0 000966 长源电力 火力发电 湖北 26.80 ... 193.83 10.78 3.59 50684.0 002210 飞马国际 仓储物流 深圳 59.72 ... -32.20 0.50 0.30 55232.0 000410 沈阳机床 机床制造 辽宁 0.00 ... 77.58 26.60 -4.17 116387.0 300356 光一科技 电气设备 江苏 139.62 ... 249.69 32.98 8.21 22929.0 000958 东方能源 火力发电 河北 62.56 ... 29.74 13.48 4.70 76397.0
import pandas as pd import numpy as np import tushare, talib from datetime import datetime, timedelta import json pastNdays = 100 # 根据过去多少天判断 basics = tushare.get_stock_basics() def analyse(code): result = {"code": code, "name": basics.name[code]} # clean df = tushare.get_hist_data(code).sort_values(by='date') df.reset_index(inplace=True) startDate = datetime.today() - timedelta(days=pastNdays) df = df[df['date'] > str(startDate)] df.index = pd.to_datetime(df.date) # basics volume = df.volume.values close = df.close.values # indicators ## Bollinger Bands #upper, middle, lower = talib.BBANDS(close, matype=MA_Type.T3) upper, middle, lower = talib.BBANDS(close, 20, 2, 2) chanceToRise = (middle[-1] - close[-1]) / (upper[-1] - lower[-1]) * 100 result["BollingerBands"] = upper[-1], middle[-1], lower[-1], close[ -1], chanceToRise ## RSI
from sqlalchemy import create_engine import tushare as ts stock_basics = ts.get_stock_basics() #所有股票列表 stock_basic print(stock_basics)