def GetTodayRealTimePrice( id, needNew): if needNew: g_pdReport = ts.get_day_all() else: if g_pdReport == None: g_pdReport = ts.get_day_all() return None
def GetTodayRealTimePrice( id, needNew = False): global g_pdReport if needNew: g_pdReport = ts.get_day_all("2018-03-20") else: if g_pdReport.empty: g_pdReport = ts.get_today_all() try: ret = g_pdReport.loc[g_pdReport['code'] == id] except: return None return {"open":ret["open"].values[0],"high":ret["high"].values[0], "low":ret["low"].values[0], "price":ret["trade"].values[0], "volume":ret["volume"].values[0]*100, "preclose":ret["settlement"].values[0]}
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) KTF.set_session(sess) data_dir = './data/netease/hist_ma/' code = 600082 #code = 600169 #code = 600815 #code = 600036 #code = 300104 #code = 600201 #code = '002608' #stock_codes = [600082, 600169, 600036, 600201, 600400, 600448, 600536, 600339, 600103, 600166] df = ts.get_day_all() if df is None or df.empty: print('failed to get codes') exit(-1) stock_codes = df['code'].tolist() pick_index = -1 snapshot_dir = './snapshots_pick/pick_cnn_netease_all_clf_hkhsi' if not os.path.exists(snapshot_dir): print('snapshot dir not exists:%s' % snapshot_dir) exit(-1) timestr = str(datetime.now()).replace(' ', '@').replace(':', '_')
def update_db_day_all(date=''): df = ts.get_day_all(date) df.to_csv('%s/%s.csv' % (day_all_path, date), encoding='utf-8')
def update_all_today_online(self, last_date=None, code_set=None, autype='D'): ''' update all stock data today, using ts.get_day_all(date). NOTE: 1. not hfq data, only use during 9:30am - 15:00pm for speed. must use update_stock_data() to overwrite. 2. if one stock ting2 pai2 in last_date, this function will not conclude this stock. 3. the reference day to compute adj is *before* and *the nearest to* param "last_date" details see the building of code_set --- return None ''' today_date = datetime.datetime.now().strftime('%Y-%m-%d') today_all_df = ts.get_day_all(today_date) lastday_date = last_date if lastday_date > today_date: raise OSError( '"last_date" is later than today. please check again.') if lastday_date is None: lastday_date = today_date lastday_date, lastday_all_df = self._get_nearest_trade_date_and_df_before( lastday_date) # 根据昨天的数据和本地csv数据,计算出后复权的权重比例 # 再计算今天对应的后复权股价,写进csv文件里 today_all_df = today_all_df[today_all_df['volume'] > 10] lastday_all_df = lastday_all_df[lastday_all_df['volume'] > 10] new_code_set = (set(today_all_df['code']) & set(lastday_all_df['code']) ) if code_set is None else set(code_set) for code in new_code_set: lastday_code_df = lastday_all_df[lastday_all_df['code'] == code] old_df = self.get_stock_data(code, way='fs') if lastday_date not in set(old_df['date']): print( 'Warning: ', code, ' the last trade date in filesystem is not match with it in web.' ) print( ' jump to next stock code. if all code makes warning, change the param "last_date".' ) else: old_last_series = old_df[old_df['date'] == lastday_date] adj = self._comput_adj_with(old_last_series, lastday_code_df) new_df = today_all_df[today_all_df['code'] == code] new_df['date'] = today_date new_df = new_df[[ 'date', 'open', 'price', 'high', 'low', 'volume', 'amount', 'turnover' ]] new_df.columns = [ 'date', 'open', 'close', 'high', 'low', 'volume', 'amount', 'tor' ] new_df['open'] = round(new_df['open'] * adj, 2) new_df['close'] = round(new_df['close'] * adj, 2) new_df['high'] = round(new_df['high'] * adj, 2) new_df['low'] = round(new_df['low'] * adj, 2) old_df = old_df[old_df['date'] != today_date] df = pd.concat([old_df, new_df]) df.to_csv(FS_PATH_OL + '/' + code + '.csv', index=False)
val = int(val) if val > 0: color = 'red' elif val < 0: color = 'green' else: color = 'black' #color = 'red' if val > 0 elif < 0 'green' else 'black' return 'color: %s' % color if __name__ == "__main__": #file = sys.argv[1] #stock_list = get_stock_list(file) df = ts.get_day_all() my_chioce = df[(df.pe <= 30) & (df.interval3 <= -15) & (df.open >= 50 ) & (df.pe != 0) & (df.price < df.avgprice)] stock_list = [ code[0] for code in my_chioce[['code']].values ] stock_basics=get_stock_basics() num4days = 90 cycle_time = 30 day_list = [i for i in range(cycle_time)] #stock_code = sys.argv[1] now = datetime.date.today() end_day = get_end_day(now) start_day = get_start_day(now,num4days) cpus = multiprocessing.cpu_count()
import sys import hdfs from hdfs.client import Client import tushare as ts import pandas import csv test_trade_data = ts.get_day_all('2019-03-29') client = Client("http://172.23.253.80:50070/", root="/", timeout=10000, session=False) def upload(hdfs_path='', local_file_path='', cleanup=True): return client.upload(hdfs_path, local_file_path, cleanup) def delete_hdfs_file(hdfs_path): return client.delete(hdfs_path) def read_csv_from_hdfs(hdfs_path): with client.read(hdfs_path, encoding='utf-8') as reader: csv = pandas.read_csv(reader) return csv if __name__ == '__main__':
def update_all(self, start_date, end_date=None, code_set=None, processors=10): if end_date is None: end_date = self.utils.get_logdate('today') self.utils.logger.info("开始日期 %s, 结束日期 %s" % (start_date, end_date)) # 每天的code基本面数据,写入基本面表 date_list = self.utils.get_date_list(start_date, end_date) for date in date_list: try: self.utils.logger.info('日期 %s' % date) cur_code_df = ts.get_day_all(date) if cur_code_df is not None and len(cur_code_df) > 0: self.utils.update_daily_df(date, cur_code_df, 'basic', 'dayall') except HTTPError as err: if err.msg == 'Not Found': self.utils.logger.warning( 'date: %s, ts.get_day_all return no data.' % date) else: raise err self.utils.logger.info("基本面数据写入完成...") # 更新天级k线hfq数据,写入周级表 def process_one_code_d(code, start_date, end_date): utils = Utils() utils.logger.info("处理%s日k线数据..." % code) df = ts.get_k_data(code, start_date, end_date, autype='hfq', ktype='D') # 计算复权因子,写入基本面表 records = utils.find_code_date('basic', 'dayall', code, start_date, end_date) if len(df) <= 0: return df['tor'] = df['date'].apply(lambda x: records[x]['turnover'] if x in records.keys() else None) utils.update_kdata_df(code, df, 'kdata', 'D') for date in df.date: if date not in records.keys(): utils.logger.warning( "股票代码%s 在%s 没有基本面数据,且存在日k线数据,待人工确认..." % (code, date)) return nfq_open = records[date]['open'] hfq_open = df[df.date == date].open.values[0] aufactor = hfq_open / nfq_open docs.append({'code': code, 'date': date, 'aufactor': aufactor}) utils.update_doc(docs, 'basic', 'dayall') if code_set is None: code_set = self.utils.get_all_code_list(start_date, end_date) docs = [] pool = ThreadPool(processors) for code in code_set: pool.apply_async(process_one_code_d, args=(code, start_date, end_date)) pool.close() pool.join() self.utils.logger.info("日k线数据写入完成,后复权因子写入基本面表完成...") # 更新周级k线hfq数据,写入周级表 def process_one_code_w(code, start_date, end_date): utils = Utils() utils.logger.info("处理%s周k线数据..." % code) df = ts.get_k_data(code, start_date, end_date, autype='hfq', ktype='W') if len(df) <= 0: return records = utils.find_code_date('basic', 'dayall', code, start_date, end_date) daily_date_list = sorted(records.keys()) weekly_date_set = set(df['date']) if len(weekly_date_set - set(daily_date_list)) > 0: utils.logger.warning( '在股票代码%s中,以下日期存在周k线数据但不存在基本面对应数据,待人工确认...%s' % (code, weekly_date_set - set(daily_date_list))) tor_dic = {} cur_tor = 0 for date in daily_date_list: cur_tor += records[date]['turnover'] if date in weekly_date_set: tor_dic[date] = cur_tor cur_tor = 0 df['tor'] = df['date'].apply(lambda x: tor_dic[x] if x in tor_dic.keys() else 0) df = df[[ 'date', 'open', 'close', 'high', 'low', 'volume', 'tor', 'code' ]] utils.update_kdata_df(code, df, 'kdata', 'W') if code_set is None: code_set = self.utils.get_all_code_list(start_date, end_date) pool = ThreadPool(processors) for code in code_set: pool.apply_async(process_one_code_w, args=(code, start_date, end_date)) pool.close() pool.join() self.utils.logger.info("周k线数据写入完成...") # 更新分钟k线数据 (可能是多个分钟级k线list) def process_one_code_Mn(code, start_date, end_date, ktype): utils = Utils() utils.logger.info("处理%s %s分钟k线数据..." % (code, ktype)) df = ts.get_k_data(code, start_date, end_date, autype='hfq', ktype=ktype) if len(df) == 0: return df = df[(df['date'] >= start_date) & (df['date'] <= (end_date + '-'))] if len(df) == 0: return # 读每天的复权因子字段,使用复权因子更新价格 records = utils.find_code_date('basic', 'dayall', code, start_date, end_date) #TODO: 如果没有,应该保持和前一天一样的aufactor cur_date_set = set(df['date'].apply(lambda x: x[0:10])) basic_date_set = set(records.keys()) if len(cur_date_set - basic_date_set) > 0: utils.logger.warning( '在股票代码%s中,以下日期存在k线数据但不存在基本面对应数据,待人工确认... %s' % (code, cur_date_set - basic_date_set)) df['aufactor'] = df['date'].apply(lambda x: records[x[0:10]][ 'aufactor'] if x[0:10] in records.keys() else 1) for col in ('open', 'close', 'high', 'low'): df[col] = round(df[col] * df['aufactor'], 3) # 使用全天交易量和换手率更新换手率 for _, r in records.items(): if r['volume'] > 0: r['tor_p_v'] = r['turnover'] / r['volume'] else: r['tor_p_v'] = int(ktype) / 240.0 df['tor'] = df.apply( lambda x: records[x['date'][0:10]]['tor_p_v'] * x['volume'] if x['date'][0:10] in records.keys() else 0, axis=1) df = df[[ 'date', 'open', 'close', 'high', 'low', 'volume', 'tor', 'code' ]] utils.update_kdata_df(code, df, 'kdata', 'M' + ktype) if code_set is None: code_set = self.utils.get_all_code_list(start_date, end_date) for ktype in ('60', ): pool = ThreadPool(processors) for code in code_set: pool.apply_async(process_one_code_Mn, args=(code, start_date, end_date, ktype)) pool.close() pool.join() self.utils.logger.info("%s分钟k线数据写入完成..." % ktype)
print("请先调用getCodeHist()方法获得code历史价格") sys.exit() RSI6 = talib.RSI(self.codeHistPrice["close"], timeperiod=6) RSI12 = talib.RSI(self.codeHistPrice["close"], timeperiod=12) RSI24 = talib.RSI(self.codeHistPrice["close"], timeperiod=24) # def toCsv(self): if __name__ == "__main__": # 时间为收盘时间 Stime = "2020-08-13" #选择上一个交易日,因为使用实时预测是将今天的实时价格进行拼接计算的,如果收盘了就用收盘价格进行拼接了 startClock = time.time() nowClock = time.time() today_all = ts.get_day_all() choosePara = ChoosePara(endTime=Stime, allDataFrame=today_all) fBuy1 = open("../BuyMACD接近交叉.txt", 'w') fBuy2 = open("../BuyMACD梯度为0.txt", 'w') jd = tqdm.tqdm(total=len(today_all["code"])) print(len(today_all["code"]), len(today_all["name"])) for index in range(len(today_all["code"])): code, name = today_all["code"][index], today_all["name"][index] if ((name[:2] != "ST") & (name[:3] != "*ST")): # if choosePara.getSingleCodeRealTimeByAPI(code): # if choosePara.getSingleCodeHistByAPI(code): if choosePara.getSingleCodeSQLByAPI(code): flag = choosePara.BSdic[choosePara.getCodeMACD()]
#!/bin/python # -*- coding:utf-8 -*- import time import price as prc import tushare as ts import storage as sv all_stocks = ts.get_day_all() print("get data done") sv.saveData(all_stocks) print(all_stocks) # all_stocks=pd.read_json(sv.readData(), convert_axes=False, convert_dates=False) today = time.strftime('%Y-%m-%d', time.localtime(time.time())) start = time.time() for index in all_stocks.index: stock_code = all_stocks.code[index] if all_stocks.abvalues[index] > 50: continue if not prc.istradeable(all_stocks.high[index], all_stocks.low[index]): continue if all_stocks.volratio[index] < 1.5: continue if not all_stocks.preprice[index] < all_stocks.price[ index] < all_stocks.preprice[index] * 1.05: continue
# -*- coding:utf-8 -*- __author__ = 'Administrator' #coding: utf-8 def run(): print("hello") if __name__ == "__main__": run() import tushare as ts hq = ts.get_day_all() shares = ts.get_stock_basics() hq = hq.set_index('code') basics = shares[['resevedPerShare', 'esp', 'timeToMarket']] df = hq.merge(basics, left_index=True, right_index=True) print df
#开发目标 # 1.取每天股票数据,按规则取前30条 # 2.市值递增排序(动态、静态) # 3.市盈率递增排序 # 4.股价递增排序 # 5.近五个交易日涨幅排序 import tushare as ts from pandas import DataFrame #获取当日行情数据 df = ts.get_day_all('2019-04-23')[[ 'code', 'name', 'pe', 'floats', 'fvalues', 'industry', 'change', 'price', 'p_change' ]] #对股票进行条件排序 dfSort = df.sort_values(by=['pe', 'fvalues', 'price'], ascending=[True, True, True]) #取前n行数据,并重置索引,删除原索引 dfData = dfSort[1:100].reset_index(drop=True) lst = [] #新股列表 listBanstock = [] #st板块列表 listCreate = [] #创业板 #遍历dfsort中数据,获取市值为0的新股数据,并对st与*st股的索引,并对相应数据进行打标签 for index, row in dfData.iterrows(): if row["fvalues"] == 0: lst.append(index) elif row["name"].find('ST') >= 0: listBanstock.append(index)
import tushare as ts import pandas as pd import time import time, datetime import calendar a=ts.get_day_all('2017-10-20') b=ts.get_day_all('2018-07-06') print(a.columns) # print(b)