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 download_kline_by_date_range(code, date_start, date_end): """ 根据日期范围下载股票行情 :param code:股票代码,即6位数字代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板) :param date_start: :param date_end: :return: """ try: if len(code)==6: df_qfq = ts.get_k_data(str(code), start=date_start, end=date_end, autype='qfq') # 前复权 df_hfq = ts.get_k_data(str(code), start=date_start, end=date_end, autype='hfq') # 后复权 else: df_qfq = ts.get_k_data(str(code), start=date_start, end=date_end) if len(df_qfq)==0 or (len(code)==6 and len(df_hfq)==0): return pd.DataFrame() if len(code)==6: df_qfq['close_hfq'] = df_hfq['close'] else: df_qfq['close_hfq'] = df_qfq['close'] # 指数后复权=前复权 print(df_qfq.head()) return df_qfq except Exception as e: print(str(e)) return pd.DataFrame()
def tang_process(self): badarea = set(['黑龙江','辽宁','吉林']) raw_data = self.foundmental_data month_writer = open('month_health', 'w') week_writer = open('week_health', 'w') day_writer = open('day_health', 'w') for code in raw_data.index: earn_ratio = raw_data.ix[code]['earn_ratio'] if raw_data.ix[code]['area'] in badarea: continue monthdata = ts.get_k_data(code, ktype='M') weekdata = ts.get_k_data(code, ktype='W') daydata = ts.get_k_data(code, ktype='D') if tang_method(monthdata, -0.8, 1.5): month_writer.write(code + '\t' + str(earn_ratio) + '\n') if tang_method(weekdata, -0.5, 0.8): week_writer.write(code + '\t' + str(earn_ratio) + '\n') if tang_method(daydata,-0.2, 0.6): day_writer.write(code + '\t' + str(earn_ratio) + '\n') print code sys.stdout.flush() month_writer.close() week_writer.close() day_writer.close()
def baseAPI(): # 通过tushare获取股票信息 df = ts.get_k_data('300580', start='2017-01-12', end='2017-05-26') # 提取收盘价 closed = df['close'].values # 获取均线的数据,通过timeperiod参数来分别获取 5,10,20 日均线的数据。 ma5 = talib.SMA(closed, timeperiod=5) ma10 = talib.SMA(closed, timeperiod=10) ma20 = talib.SMA(closed, timeperiod=20) # 打印出来每一个数据 print(closed) print(ma5) print(ma10) print(ma20) # 通过plog函数可以很方便的绘制出每一条均线 plt.plot(closed) plt.plot(ma5) plt.plot(ma10) plt.plot(ma20) # 添加网格,可有可无,只是让图像好看点 plt.grid() # 记得加这一句,不然不会显示图像 plt.show()
def ma_type_test(): # MA_Type: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TEMA, 5=TRIMA, 6=KAMA, 7=MAMA, 8=T3 (Default=SMA) df = ts.get_k_data('300580', start='2017-01-12', end='2017-05-26') closed = df['close'].values sma = talib.MA(closed, timeperiod=10, matype=0) ema = talib.MA(closed, timeperiod=10, matype=1) wma = talib.MA(closed, timeperiod=10, matype=2) dema = talib.MA(closed, timeperiod=10, matype=3) tema = talib.MA(closed, timeperiod=10, matype=4) trima = talib.MA(closed, timeperiod=10, matype=5) kma = talib.MA(closed, timeperiod=10, matype=6) mama = talib.MA(closed, timeperiod=10, matype=7) t3 = talib.MA(closed, timeperiod=10, matype=8) # ouput=talib.MA(closed,timeperiod=5,matype=0) print(closed) plt.ylim([0, 40]) plt.plot(sma, 'r--') plt.plot(ema, 'g-*') plt.plot(wma) plt.plot(dema) plt.plot(tema) plt.plot(trima) plt.plot(kma) plt.plot(mama) plt.plot(t3) plt.grid() plt.text(7, 30, 'BST') plt.show()
def prepare_stock_data(stock): stock_data = ts.get_k_data(stock) stock_data = stock_data.sort_values("date") stock_data = stock_data.set_index(["date"]) stock_data = calculate_macd(stock_data) stock_data = calculate_change_aver(stock_data) return stock_data
def QA_fetch_get_stock_day(name, startDate='', endDate='', if_fq='01', type_='json'): if (len(name) != 6): name = str(name)[0:6] if str(if_fq) in ['qfq', '01']: if_fq = 'qfq' elif str(if_fq) in ['hfq', '02']: if_fq = 'hfq' elif str(if_fq) in ['bfq', '00']: if_fq = 'bfq' else: QA_util_log_info('wrong with fq_factor! using qfq') if_fq = 'qfq' data = QATs.get_k_data(str(name), startDate, endDate, ktype='D', autype=if_fq, retry_count=200, pause=0.005).sort_index() data['date_stamp'] = data['date'].apply(lambda x: QA_util_date_stamp(x)) data['fqtype'] = if_fq if type_ in ['json']: data_json = QA_util_to_json_from_pandas(data) return data_json elif type_ in ['pd', 'pandas', 'p']: data['date'] = pd.to_datetime(data['date']) data = data.set_index('date',drop=False) data['date'] = data['date'].apply(lambda x: str(x)[0:10]) return data
def _load_data(self, code, dt_start, dt_end): dts = _process_ts_dt(dt_start) dte = _process_ts_dt(dt_end) data = ts.get_k_data(code, start=dts, end=dte) data.set_index('date',drop=True,inplace=True) data.index.names = ['datetime'] return data.iloc[::-1]
def k_15min_one_stock(_stock_id, _db): # log_info("k_15min_one_stock begin") # get from web(by tushare) begin = get_micro_second() try: df = ts.get_k_data(_stock_id, ktype='15', autype='qfq') except Exception: log_error("warn:error: %s get_k_data exception!", _stock_id) return -4 # calc cost time log_info("get_k_data [%s] costs %d ms", _stock_id, (get_micro_second()-begin)/1000) if df is None : log_error("warn: stock %s is None, next", _stock_id) return -1 if df.empty: log_error("warn: stock %s is empty, next", _stock_id) return -2 df.sort_index(ascending=False, inplace=True) begin = get_micro_second() k_15min_one_to_db(_stock_id, df, _db) log_info("one_to_db costs %d ms", (get_micro_second() - begin)/1000) # log_info("function k_15min_one_stock end") return
def getk_handler(args,parser): for s in args.subnargs: res = ts.get_k_data(s,ktype=args.ktype,autype=args.autype,index=args.indexmode,start=args.startdate,end=args.enddate) print('%s for %s %s:%s'%(s,args.ktype,args.startdate,args.enddate)) print('%s'%(res)) sys.exit(0) return
def calc_open_by_percent(self,code): cont=100000000 #total_vol=self.bases[self.bases['code']==code]['totals'].values[0] acutal_vol=self.bases[self.bases['code']==code]['outstanding'].values[0] all_vol= acutal_vol*cont df1=ts.get_k_data(code) i=1 while 1: s=df1.ix[i] if s['high']!=s['low']: #date=s['date'] break i=i+1 j=i-1 date_end=df1.ix[j]['date'] date_start=df1.ix[0]['date'] df3=df1[(df1['date']>=date_start) & (df1['date']<=date_end)] v_total_break=df3['volume'].sum() l=len(df3) print(l) print(v_total_break) rate=v_total_break*100*100.00/all_vol #手和股 注意 print(round(rate,6)) return rate,l
def relation(self): sh_index=ts.get_k_data('000001',index=True,start='2012-01-01') sh=sh_index['close'].values print sh vol_close=sh_index.corr() print vol_close '''
def macd(self): # df=self.fetch_new_ipo(writeable=True) # all_code=df['code'].values # all_code=self.get_all_code() # print all_code result = [] for each_code in self.all_code: print each_code try: df_x = ts.get_k_data(code=each_code, start='2017-03-01') # 只找最近一个月的,所以no item的是停牌。 except: print "Can't get k_data" continue if len(df_x) < 11: # return print "no item" continue ma5 = df_x['close'][-5:].mean() ma10 = df_x['close'][-10:].mean() if ma5 > ma10: # print "m5>m10: ",each_code," ",self.base[self.base['code']==each_code]['name'].values[0], "ma5: ",ma5,' m10: ',ma10 temp = [each_code, self.base[self.base['code'] == each_code]['name'].values[0]] print temp result.append(temp) print result print "Done" return result
def calc_open_day(self,code): cont=100000000 #total_vol=self.bases[self.bases['code']==code]['totals'].values[0] acutal_vol=self.bases[self.bases['code']==code]['outstanding'].values[0] all_vol= acutal_vol*cont #df= ts.get_hist_data(code) df1=ts.get_k_data(code) if len(df1)<3: return None #print(df1.info()) #df1=df.reset_index() #print(df1) start=df1['date'].values[0] print('Start day:', start) df2= df1[(df1['close']==df1['low']) & (df1['high']==df1['low'])] print(self.bases[self.bases['code']==code]['name'].values[0]) end=df2['date'].values[-1] print("Break day" , end) df3=df1[(df1['date']>=start) & (df1['date']<=end)] v_total_break=df3['volume'].sum() l=len(df3) print(l) print(v_total_break) rate=v_total_break*100*100.00/all_vol #手和股 注意 print(round(rate,6)) return rate,l
def _getFundDaysFromTuShare(self, code, startDate, endDate, fields, name=None): """ 从tushare获取基金(ETF)日线数据。 # !!!TuShare没有提供换手率,复权因子和成交额,所以只能假设。 # 策略针对ETF的,需要注意。 """ tuShareCode = code[:-3] try: # 以无复权方式从腾讯获取OHCLV,成交量是手(整数化过) # 此接口支持ETF日线数据 df = ts.get_k_data(tuShareCode, startDate, endDate, autype=None).sort_index() except Exception as ex: self._info.print("从TuShare获取{}({})日线数据[{}, {}]失败: {}".format(code, name, startDate, endDate, ex), DyLogData.error) return None df['volume'] = df['volume']*100 # !!!TuShare没有提供换手率,复权因子和成交额,所以只能假设。 # 策略针对ETF的,需要注意。 df['turnover'] = 0 df['factor'] = 1 df['amount'] = 0 df.index.name = None # change to Wind's indicators df.rename(columns={'date': 'datetime', 'amount': 'amt', 'turnover': 'turn', 'factor': 'adjfactor'}, inplace=True) # 把日期的HH:MM:SS转成 00:00:00 df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d') # select according @fields df = df[['datetime'] + fields] return df
def gen_zz800_market_index_price_and_ratio(start, end): zz800_market_ratio = ts.get_k_data('000906', start=start, end=end, index=True) zz800_market_ratio.columns = zz800_market_ratio.columns.str.upper() file_path = '/data/gen_data/' + start + '_' + end + '_zz800_market_ratio' + '.csv' zz800_market_ratio['RATIO'] = zz800_market_ratio['CLOSE'].rolling(window=2).apply(lambda x: (x[1] - x[0]) / x[0]) zz800_market_ratio[['DATE', 'CLOSE', 'RATIO']].to_csv(config.rootPath + file_path, index=False, header=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 k_day_one_stock(_stock_id, _db): # log_info("k_day_one_stock begin") # get max-date from table, as start date max_date = k_day_get_max_date(_stock_id, _db) end_date = get_date_by(0) # log_debug("[%s, %s]", max_date, end_date) # qfq if max_date is None: start_date = '2016-01-01' start_date = '2017-07-01' start_date = '2014-01-01' start_date = '2017-01-03' log_debug("it's first time: [%s]", _stock_id) else: start_date = str(max_date) # log_debug("a old friend: [%s]", _stock_id) log_debug("[%s, %s]", start_date, end_date) # get from web(by tushare) begin = get_micro_second() try: df = ts.get_k_data(_stock_id, autype='qfq', start=start_date, end=end_date) # reject by tencent since 2018-7-16 # df = ts.get_h_data(_stock_id, autype='qfq', start=start_date, end=end_date, retry_count=5, pause=6) # df = ts.get_h_data(_stock_id, start='2016-08-20', end='2016-10-30') # df = ts.get_h_data(_stock_id, autype='qfq') except Exception: log_error("warn:error: %s get_k_data exception!", _stock_id) return -4 # calc cost time log_info("get_k_data [%s] costs %d us", _stock_id, get_micro_second()-begin) if df is None : log_error("warn: stock %s is None, next", _stock_id) return -1 if df.empty: log_error("warn: stock %s is empty, next", _stock_id) return -2 df.sort_index(ascending=True, inplace=True) # df = df.sort_index(ascending=True) # df = df.reindex(index=range(0, len(df))) # log_debug("df: \n%s", df) begin = get_micro_second() k_day_one_to_db(_stock_id, df, max_date, _db) log_info("one_to_db costs %d us", get_micro_second() - begin) # log_info("function k_day_one_stock end") return
def profit(code,start,end): # conn=ts.get_apis() try: # df=ts.bar(code,conn=conn,start_date=start,end_date=end) df=ts.get_k_data(code,start=start,end=end) except Exception,e: print e return None
def price_moment(): df = ts.get_k_data('300580', start='2017-01-12', end='2017-05-26') closed = df['close'].values ouput = talib.MOM(closed, timeperiod=5) print(closed) print(ouput) plt.plot(ouput) plt.grid() plt.show()
def make_k_data(): df = ts.get_k_data('600000', start='2015-01-01') datastr = '' for idx in df.index: rowstr = '[\'%s\',%s,%s,%s,%s]' % (df.ix[idx]['date'], df.ix[idx]['open'], df.ix[idx]['close'], df.ix[idx]['low'], df.ix[idx]['high']) datastr += rowstr + ',\n' print(datastr)
def return_risk(stocks,startdate='2005-01-01'): close=pd.DataFrame() for stock in stocks.values(): close[stock]=ts.get_k_data(stock,ktype='D', autype='qfq', start=startdate)['close'] tech_rets = close.pct_change()[1:] rets = tech_rets.dropna() ret_mean=rets.mean()*100 ret_std=rets.std()*100 return ret_mean,ret_std
def amplitude_compute(code): ''' 计算一些指数的日均振幅信息 ''' rawdata = ts.get_k_data(code,start='2011-10-01', end='2014-05-31') rawdata['delta'] = rawdata['high'] - rawdata['low'] length = len(rawdata.index) - 1 base = rawdata[rawdata.index < length]['close'] delta = rawdata[rawdata.index > 0]['delta'] ave_percent = np.mean(delta / base) return ave_percent
def profit(self,start,end,code): start_price=ts.get_k_data(start=start,end=start,code=code) if len(start_price)==0: print("Not open for start day") #采用日期操作,往前移动上一个开盘日. s_price=start_price['close'].values[0] print("Start price: ",s_price) end_price=ts.get_k_data(start=end,end=end,code=code) if len(end_price)==0: print("Not open for end day") e_price=end_price['close'].values[0] print("End price: ",e_price) earn_profit=(e_price-s_price)/s_price*100 print("Profit: ",) print(round(earn_profit,2))
def download_stock_history(data): symbol = data["symbol"] is_index = data["is_index"] df = ts.get_k_data(symbol, index=is_index, start="2000-01-01") if df.empty: return path = os.path.join(stock_dir, symbol) exsymbol = stock.utils.symbol_util.symbol_to_exsymbol(symbol, is_index) redis_store = get_store("redis_store") file_store = get_store("file_store") redis_store.save(exsymbol, df) file_store.save(exsymbol, df)
def computeYield(code, market, date): ds = ts.get_k_data(code, date) hs300 = ts.get_hist_data(market, date) close = ds['close'].values[:len(ds)] hs300_close = hs300['close'].values[:len(ds)] ri = yieldOf(close) rm = yieldOf(hs300_close) # print("股票" + code + "收益率", ri) # print("市场收益率", rm) covi = np.cov(ri, rm) print(covi) varm = np.var(rm) # print("协方差", covi) csvwriter.writerow([code, covi[0][1] / varm])
def data_recieve(name, start, data_type='normal', ktype='D'): if data_type not in {'normal', 'carg', 'grow'}: raise ValueError('Invalid data_type "%s"' % data_type) data = ts.get_k_data(name, start=start, ktype=ktype) data_var = data.drop(data.columns, axis=1) date = data.pop('date').values code = data.pop('code') #print('获得原始数据:\t %s' % code[0]) print('起始时间:\t %s' % str(date[0])) print('结束时间:\t %s' % str(date[-1])) print('数据个数:\t %s\n' % len(date)) print data.head(5) if data_type == 'normal': for i in data.columns: data_var[i] = data[i] if data_type == 'carg': for i in data.columns: data_var[i] = (data[i] - data[i].shift(1)) / data[i].shift(1) if data_type == 'grow': for i in data.columns: data_var[i] = data[i] / data[i].shift(1) open = data_var.open.values high = data_var.high.values close = data_var.close.values low = data_var.low.values volume = data_var.volume.values new_data = data.drop(data.columns, axis=1) new_data.index = date new_data['open'] = open new_data['high'] = high new_data['close'] = close new_data['low'] = low new_data['volume'] = volume new_data = new_data.dropna() print('\n查看最新数据:\n') print new_data.head(5) return new_data
def check_k_data(): each_code = '300333' # 如果当天还没收盘,就获取昨天的收盘 df_x = ts.get_k_data(code=each_code, start='2017-03-01') print(df_x) if len(df_x) < 11: print("Error") exit() print(df_x) ma5 = df_x['close'][-5:].mean() ma10 = df_x['close'][-10:].mean() print(ma5) print(ma10) print(df_x['volume'])
def work_one(_stock_id, _row, _db): rs = False content = "" begin = get_micro_second() try: df = ts.get_k_data(_stock_id, ktype='15', autype='qfq') except Exception: log_error("warn:error: %s get_k_data exception!", _stock_id) return -4 # calc cost time log_info("get_k_data [%s](%d) costs %d us", _stock_id, len(df), get_micro_second()-begin) if df is None : log_error("warn: stock %s is None, next", _stock_id) return -1 if df.empty: log_error("warn: stock %s is empty, next", _stock_id) return -2 rt_u15m_format_ref(_stock_id, df) # log_debug("+++++++++++++++++\n%s", df) rv = rt_u15m_case1(_stock_id, df, _db) if rv == 0: rs = True log_info("nice1: %s single pivot", _stock_id) return rs log_debug("-----------------------------------------") """ rv = rt_u15m_case2(_stock_id, df, _db) if rv == 0: rs = True log_info("nice2: %s six-cross", _stock_id) return rs log_debug("-----------------------------------------") """ return rs
def downloadEquityDailyBarts(self, symbol): """ 下载股票的日行情,symbol是股票代码 """ print u'开始下载%s日行情' %symbol # 查询数据库中已有数据的最后日期 cl = self.dbClient[DAILY_DB_NAME][symbol] cx = cl.find(sort=[('datetime', pymongo.DESCENDING)]) if cx.count(): last = cx[0] else: last = '' # 开始下载数据 import tushare as ts if last: start = last['date'][:4]+'-'+last['date'][4:6]+'-'+last['date'][6:] data = ts.get_k_data(symbol,start) if not data.empty: # 创建datetime索引 self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)], unique=True) for index, d in data.iterrows(): bar = VtBarData() bar.vtSymbol = symbol bar.symbol = symbol try: bar.open = d.get('open') bar.high = d.get('high') bar.low = d.get('low') bar.close = d.get('close') bar.date = d.get('date').replace('-', '') bar.time = '' bar.datetime = datetime.strptime(bar.date, '%Y%m%d') bar.volume = d.get('volume') except KeyError: print d flt = {'datetime': bar.datetime} self.dbClient[DAILY_DB_NAME][symbol].update_one(flt, {'$set':bar.__dict__}, upsert=True) print u'%s下载完成' %symbol else: print u'找不到合约%s' %symbol
import numpy as np import pandas as pd from pandas import Series, DataFrame import json import time import sys from sqlalchemy import create_engine with open('/home/code/shares/block/industry.json', 'r') as f: datas = json.load(f) startTime = '2001-01-01' endTime = time.strftime('%Y-%m-%d', time.localtime(time.time())) engine = create_engine('mysql://*****:*****@127.0.0.1/shares?charset=utf8') for data in datas: code = data.get('code') singles = ts.get_k_data(code, start=startTime, end=endTime) if singles['open'] + singles['close'] == 23.393: singles['one_point'] = 1 elif singles['open'] + singles['close'] == 23.915: singles['one_point'] = 2 else: singles['one_point'] = 3 print(singles) sys.exit() # for single in singles['date'] # print(single) # die() # singles.to_sql(code+'_history', engine, if_exists='append')
def download_d(self, code, start='2000-01-01', end='2023-01-01'): """docstring for download""" print(f"Downloading {code}...") df = ts.get_k_data(code, start=start, end=end) df.to_csv(f"{self.DATA_FOLDER}/{code}.csv", index=False)
warnings.simplefilter('ignore') # 忽略警告信息 # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题 plt.style.use('seaborn') mpl.rcParams['font.family'] = 'serif' np.sign(-0.02) # 重要函数 date = '2014-01-20' type(date) data = pd.to_datetime('2014-01-20') type(data) # 转换成为时间数据 # 数据准备 & 回测准备 data = ts.get_k_data('hs300', start='2010-01-01', end='2017-06-30')[['date', 'close']] data.rename(columns={'close': 'price'}, inplace=True) data.set_index('date', inplace=True) # 策略开发思路 data['returns'] = np.log(data['price'] / data['price'].shift(1)) data['position'] = np.sign(data['returns']) # 关键语句,np.sign()很多地方用到;向量化 data['strategy'] = data['position'].shift(1) * data['returns'] # 计算Momentum策略收益;避免未来函数 # 策略可视化 data[['returns', 'strategy' ]].cumsum().apply(np.exp).plot(figsize=(10, 6)) # 计算出策略的最终的累计收益 plt.show()
def generateline(stocknumber,Type,startdate,enddate,interval): startdata = startdate.encode("ascii").replace("/","-").replace("\n","") #convert to tushare readable date enddata = enddate.encode("ascii").replace("/","-").replace("\n","") #print startdata #print enddata current_time = time.strftime("%Y/%m/%d") if Type == "分笔".decode("utf-8"): if startdate!=current_time: array = ts.get_tick_data(stocknumber, date = startdata)#分笔 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() price = array["price"].tolist() flag = ["bar" for i in date] for idx,val in enumerate(atype):#if卖盘,交易变成负数 if val == "卖盘": amount[idx] = -amount[idx] if val == "中性盘":#if中性盘,则忽略. Might have a problem with this part?? amount[idx] = 0 returnarray = zip(date,amount,flag,price) return returnarray else: array = ts.get_today_ticks(stocknumber)#Tushare里今日分笔和历史分笔需要分别对待 if array is None: return array = array.sort_values("time") date = array["time"].tolist() amount = array["amount"].tolist() atype = array["type"].tolist() flag = ["bar" for i in date] for idx, val in enumerate(atype): if val == "卖盘".decode("utf-8"): amount[idx] = -amount[idx] if val == "中性盘".decode("utf-8"): amount[idx] = 0 returnarray = zip(date, amount, flag) return returnarray if Type=="季度饼图".decode("utf-8"): datestr = startdate.split("/") thisyear = datestr[0] df2 = ts.top10_holders(code=stocknumber, gdtype="1") test = df2[1]["quarter"].tolist() df_ready = df2[1] idxlist = [] for idx, val in enumerate(test): a = val.split("-") if a[0] == thisyear: # print a[0],idx idxlist.append(idx) thing = df_ready.loc[idxlist] thing = thing.sort_values(["quarter", "name"]) # print a[0],id name = thing["name"].tolist() value = thing["hold"].tolist() quarter = thing["quarter"].tolist() namearray = [name[i:i + 10] for i in xrange(0, len(name), 10)] valuearray = [value[j:j + 10] for j in xrange(0, len(value), 10)] quarterarray = [quarter[k:k + 10] for k in xrange(0, len(quarter), 10)] flag = ["pie" for i in namearray] num = [len(value) for k in namearray] returnarray = zip(namearray,valuearray,quarterarray,flag,num) return returnarray if interval!="qfq" and interval!="hfq": if interval=="1min" or interval=="5min" or interval=="15min" or interval=="30min" or interval=="60min": df = ts.get_tick_data(stocknumber, date=startdata) df.sort_values("time") a = startdata + " " + df["time"] df["time"] = a df["time"] = pd.to_datetime(a) df = df.set_index("time") price_df = df["price"].resample(interval).ohlc() price_df = price_df.dropna() vols = df["volume"].resample(interval).sum() #relevant data processing algorithm taken from Jimmy, Creator of Tushare vols = vols.dropna() vol_df = pd.DataFrame(vols, columns=["volume"]) amounts = df["amount"].resample(interval).sum() amounts = amounts.dropna() amount_df = pd.DataFrame(amounts, columns=["amount"]) newdf = price_df.merge(vol_df, left_index=True, right_index=True).merge(amount_df, left_index=True, right_index=True) if Type != "Kline": Type1 = firstletter(Type).encode("ascii") target = newdf[Type1].tolist() date = newdf.index.format() returnarray = zip(date, target) return returnarray else: Date = newdf.index.format() Open = newdf["open"].tolist() Close = newdf["close"].tolist() High = newdf["high"].tolist() Low = newdf["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick #正常历史k线 if Type == "差价图".decode("utf-8"): global array300 if array300 is None: array300 = ts.get_k_data("399300", start=startdata, end=enddata, ktype=interval) array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval, autype='qfq') if array is None or array300 is None: return None target = [] benchmark_list = [] try: close_list = array["close"].tolist() date = array["date"].tolist() date300 = array300["date"].tolist() close300 = array300["close"].tolist() if len(close_list) is 0: return None data_price_300_map = dict() benchmark300 = 100 / (close300[0]) benchmark = 100 / (close_list[0]) for index, day in zip(range(0, len(date300)), date300): data_price_300_map[day] = close300[index] for index, close in zip(range(1, len(close_list)), close_list): price300 = data_price_300_map[date[index]] * benchmark300 target.append(round(close * benchmark - price300, 3)) benchmark_list.append(0) returnarray = zip(date, target, benchmark_list) if checkValid(returnarray) is False: return None return returnarray except Exception as e: print e return None elif Type == "价格指数".decode("utf-8"): global array300 if array300 is None: array300 = ts.get_k_data("399300", start=startdata, end=enddata, ktype=interval) array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval, autype='qfq') if array is None or array300 is None: return None price_list = [] price300_list = [] try : close_list = array["close"].tolist() date = array["date"].tolist() close300_list = array300["close"].tolist() if len(close_list) is 0: return None benchmark300 = 100 / (close300_list[0]) benchmark = 100 / (close_list[0]) for close in close_list: price_list.append(close * benchmark) for close300 in close300_list: price300_list.append(close300 * benchmark300) returnarray = zip(date, price_list, price300_list) if checkValid(returnarray) is False: return None return returnarray except Exception as e: print e return None elif Type!="Kline": array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Type1 = firstletter(Type).encode("ascii") target = array[Type1].tolist() date = array["date"].tolist() returnarray = zip(date,target) return returnarray else: array = ts.get_k_data(stocknumber, start=startdata, end=enddata, ktype=interval) if array is None: return Date = array["date"].tolist() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date,Open,Close,Low,High]) return Candlestick else: if Type!="Kline": # 复权 array = ts.get_h_data(stocknumber, start = startdata, end = enddata, autype= interval) if array is None: return Type1 = firstletter(Type).encode("ascii") array = array.sort_index() target = array[Type1].tolist() date = array.index.format() returnarray = zip(date, target) return returnarray else : array = ts.get_h_data(stocknumber, start=startdata, end=enddata, autype=interval) if array is None: return array = array.sort_index() Date = array.index.format() Open = array["open"].tolist() Close = array["close"].tolist() High = array["high"].tolist() Low = array["low"].tolist() Candlestick = zip(*[Date, Open, Close, Low, High]) return Candlestick
def show_trend(stock_id, stock_name, end_date=(datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y%m%d'), working_days=200, step=10): end_date = datetime.datetime.strptime(end_date, '%Y%m%d') start_date = end_date while get_working_days(start_date, end_date) < working_days: start_date = start_date + datetime.timedelta(days=-1) start_date = start_date + datetime.timedelta(days=-(step - 1)) k_dict = {} k_index = ts.get_k_data(stock_id, start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) for i in range(0, len(k_index)): k_dict[list(k_index['date'])[i]] = list(k_index['close'])[i] trend_dict = wssrdb.get_trend(stock_id, start_date, end_date) date_list = sorted(list(k_dict.keys()) + [item for item in trend_dict.keys() if item not in k_dict.keys()]) k_list = [] trend_list = [] for r in date_list: if r in k_dict.keys(): k_list.append(k_dict[r]) elif len(k_list) > 0: k_list.append(k_list[-1]) else: k_list.append(0.0) if r in trend_dict.keys(): trend_list.append(trend_dict[r]) else: trend_list.append(0) ##################################################### if step > 1: # 大于1天 date_list_2 = [] k_list_2 = [] trend_list_2 = [] k_avg = 0.0 trend_sum = 0 for i in range(0, len(date_list)): if (i + 1) % step > 0: k_avg += k_list[i] trend_sum += trend_list[i] else: date_list_2.append(date_list[i]) k_list_2.append(k_avg / step) trend_list_2.append(trend_sum) k_avg = 0.0 trend_sum = 0 date_list = date_list_2 k_list = k_list_2 trend_list = trend_list_2 ##################################################### date_list = [d.replace("-", "\n") for d in date_list] fig, pl1 = pl.subplots() pl1.plot(date_list, k_list, label=u"收盘价", color="blue", linewidth=2.5) pl1.set_ylabel(u"收盘价", fontsize=18, color="blue") for label in pl1.get_yticklabels(): label.set_color("blue") pl2 = pl1.twinx() pl2.plot(date_list, trend_list, label=u"研报推荐", color="red", linewidth=2.5) pl2.set_ylabel(u"研报推荐", fontsize=18, color="red") for label in pl2.get_yticklabels(): label.set_color("red") pl2.set_xlabel(u'时间', fontsize=18, color="black") pl.title(stock_name + "[" + stock_id + "]") pl.legend() pl.show()
import tushare as ts import json import time from sqlalchemy import create_engine todayTime = time.strftime('%Y-%m-%d', time.localtime(time.time())) single = ts.get_k_data('000026', start='2018-05-25', end='2018-05-25') engine = create_engine('mysql://*****:*****@127.0.0.1/shares?charset=utf8') #print(single) single.to_sql('000026_history', engine, if_exists='append')
import talib as ta import tushare as ts import matplotlib.pyplot as plt from matplotlib import rc rc('mathtext', default='regular') import seaborn as sns sns.set_style('white') from matplotlib import dates import matplotlib as mpl %matplotlib inline # myfont = mpl.font_manager.FontProperties( # fname=r"c:\windows\fonts\simsun.ttc", size=14) plt.rcParams["figure.figsize"] = (20, 10) dw = ts.get_k_data("600600") dw = dw[300:] dw.index = range(len(dw)) close = dw.close.values dw["rsi"] = ta.RSI(close, timeperiod=14) #dw[["close","rsi"]].plot() fig = plt.figure(figsize=(20, 10)) fig.set_tight_layout(True) ax1 = fig.add_subplot(111) #fig.bar(dw.index, dw.volume, align='center', width=1.0) ax1.plot(dw.index, dw.close, '-', color='g') ax2 = ax1.twinx() ax2.plot(dw.index, dw.rsi, '-', color='r') ax2.plot(dw.index, [70] * len(dw), '-', color='r') ax2.plot(dw.index, [30] * len(dw), '-', color='r')
def _download_data(self, code, index): start_day_date = date_to_str(self.test_date - timedelta(40)) start_week_date = date_to_str(self.test_date - timedelta(25) * 7) #取日线行情: data_day = ts.get_k_data(code, ktype='d', autype='qfq', index=index, start=start_day_date, end=self.buy_date_str) #检查够不够20个交易日 if (data_day.index.size < 20): data_day = ts.get_k_data( code, ktype='d', autype='qfq', index=index, start=date_to_str(self.test_date - timedelta(180)), end=self.buy_date_str) #考虑有停牌的情况,祖国不够20个,再往前追加3个月 #检查是否仍然空。如果还空,要么停牌超过半年,要么服务器出错。 if (data_day.empty): print("数据无效:停牌超过半年,或者服务器出错:" + code + ": " + self.buy_date_str) self.data_valid = False return None, None # 检查buydate是否存在 if (data_day.iloc[-1, 0] != self.buy_date_str): print("数据无效:目标买入日不是交易日:" + code + ": " + self.buy_date_str) self.data_valid = False return None, None if (data_day.index.size < 20): print("数据无效:股票上市日子不够,计算不出日MA20:" + code + ": " + self.buy_date_str) self.no_week_ma = True self.data_valid = False return None, None #取周线行情: data_week = ts.get_k_data(code, ktype='w', autype='qfq', index=index, start=start_week_date, end=self.buy_date_str) #检查够不够20个交易日 if (data_week.index.size < 20): data_week = ts.get_k_data(code, ktype='w', autype='qfq', index=index, start=date_to_str(self.test_date - timedelta(50) * 7), end=self.buy_date_str) if (data_week.index.size < 20): print("数据无效:股票上市日子不够,计算不出周MA20:" + code + ": " + self.buy_date_str) self.no_week_ma = True self.data_valid = False return None, None #当日price price = [data_day.iloc[-1, 2]] #日均值price price += compute_ma(data_day['close']) #周均值price price += compute_ma(data_week['close']) if (self._check_data(price) is False): print("数据无效:股价太小" + code + ": " + self.buy_date_str) self.data_valid = False return None, None #当日volume volume = [data_day.iloc[-1, 5]] #日均值volume volume += compute_ma(data_day['volume']) #周均值volume volume += compute_ma(data_week['volume']) if (self._check_data(volume) is False): print("数据无效:成交量太小" + code + ": " + self.buy_date_str) self.data_valid = False return None, None return price, volume
first, last = lookback_trade_cal(last, LOOKBACKDAYS) ########## Log记录信息########## logger.info('开始日期: ' + first) logger.info('结束日期: ' + last) ################################ # 通过tushare获取前lookback_days 个交易日数据,并筛选满足收盘价序列的股票 stockList = totList # stockList = ['300706'] targetDict = {} fd = open('./log.txt', 'w') i = 0 for stock in stockList: data = ts.get_k_data(code=stock, start=first, end=last) logger.info("start analysis code=%s" % stock) records = len(data) if (records == 0): continue logger.info("len=%d" % records) print("start analysis code=%s" % stock) #print data #if len(data) == LOOKBACKDAYS: c_data = data.close.values v_data = data.volume.values open_data = data.open.values high_data = data.high.values low_data = data.low.values
#!/usr/bin/python # coding: UTF-8 import tushare as ts import pandas as pd import matplotlib.pyplot as plt import numpy as np import talib df = ts.get_k_data('600600') df['MA10_rolling'] = pd.rolling(df['close']).mean(10) close = [float(x) for x in df['close']] # 调用talib计算10日移动平均线的值 df['MA10_talib'] = talib.MA(np.array(close), timeperiod=10) df.tail(12)
import tushare as ts df = ts.get_k_data('sz50', start='2010-01-01', end='2017-12-31') df.to_csv('./test/sz50.csv')
@author: Lu.yipiao ''' import pandas as pd import numpy as np import matplotlib.pyplot as plt import tensorflow as tf import tushare as ts import pandas as pd base_path="./" #——————————————————导入数据—————————————————————— f=open('dataset_1.csv') get_k_data = ts.get_k_data("000001", start='1990-12-19') df = pd.DataFrame(get_k_data); data=np.array(df['close']) #获取最高价序列 df=pd.read_csv(f) #读入股票数据 data=np.array(df['close']) #获取最高价序列 data=data[::-1] #反转,使数据按照日期先后顺序排列 print(data) #以折线图展示data # plt.figure() # plt.plot(data) # plt.show() normalize_data=(data-np.mean(data))/np.std(data) #标准化 normalize_data=normalize_data[:,np.newaxis] #增加维度
def getData(code): df = ts.get_k_data(code=code) hd = df.head(1) q.put(hd) pbar.update(1) return hd
# -*- coding:utf-8 -*- import matplotlib as mpl import tushare as ts import matplotlib.pyplot as plt import mpl_finance as mpf from matplotlib.pylab import date2num import datetime import matplotlib.dates as mdates import numpy as np wdyx = ts.get_k_data('000001', '2017-01-01') #wdyx.info() print(wdyx[:3]) def date_to_num(dates): num_time = [] for date in dates: date_time = datetime.datetime.strptime(date, '%Y-%m-%d') #print("date_time",date_time) num_date = date2num(date_time) #print("num_date", num_date) num_time.append(num_date) return num_time # dataframe转换为二维数组 mat_wdyx = wdyx.values #print(mat_wdyx[:,0])
def dmpStkDaysFromTS(self, folder: str, sdate: str = "1990-01-01", edate: str = "", codes: list = None, bNeedAdj: bool = True, isIndex: bool = False): ''' 从tushare导出日线数据,慢!!!\n @sdate 开始日期,格式如2020-06-09\n @edate 结束日期,格式同上\n @codes 代码列表,为空时自动获取\n @bNeedAdj 是否除权,默认为True\n @isIndex 是否指数数据,默认为False\n ''' if edate == "": edate = datetime.datetime.now().strftime('%Y-%m-%d') if not os.path.exists(folder + 'day/'): create_dirs(folder + 'day/') if not os.path.exists(folder + 'min5/'): create_dirs(folder + 'min5/') if codes is None: if isIndex: codes = self.__indice__.keys() else: codes = self.__stocks__.keys() total = len(codes) count = 0 for code in codes: count += 1 print("正在拉取%s的日线[%d/%d]……" % (code, count, total)) exchg = "" if code[:2] == "SH": exchg = "SSE" else: exchg = "SZSE" thisFolder = folder + 'day/' + exchg + "/" if not os.path.exists(thisFolder): create_dirs(thisFolder) thisFolder = folder + 'min5/' + exchg + "/" if not os.path.exists(thisFolder): create_dirs(thisFolder) df_bars = None try: # get_h_data貌似也可以 df_bars = ts.get_k_data(code[2:], start=sdate, end=edate, ktype='D', autype='qfq' if bNeedAdj else None, index=isIndex) csvpath = (folder + 'day/%s/%s%s.csv') % ( exchg, code[2:], "Q" if bNeedAdj and not isIndex else "") isEmpty = is_file_empty(csvpath) f = open(csvpath, 'a') if isEmpty: f.write("date, time, open, high, low, close, volumn\n") for idx, row in df_bars.iterrows(): f.write( datetime.datetime.strptime(str( row["date"]), '%Y-%m-%d').strftime("%Y/%m/%d") + ", ") f.write("0, ") f.write(str(row["open"]) + ", ") f.write(str(row["high"]) + ", ") f.write(str(row["low"]) + ", ") f.write(str(row["close"]) + ", ") f.write(str(row["volume"]) + "\n") f.close() except: print("正在拉取%s的日线异常" % (code)) print("正在拉取%s的5分钟线[%d/%d]……" % (code, count, total)) df_bars = None try: df_bars = ts.get_k_data(code[2:], sdate, edate, ktype='5', autype='qfq' if bNeedAdj else None) csvpath = (folder + 'min5/%s/%s%s.csv') % ( exchg, code[2:], "Q" if bNeedAdj and not isIndex else "") f = open(csvpath, 'w') f.write("date, time, open, high, low, close, volumn\n") for idx, row in df_bars.iterrows(): curDt = datetime.datetime.strptime(str(row["date"]), '%Y-%m-%d %H:%M') f.write(curDt.strftime("%Y/%m/%d") + ", ") f.write(curDt.strftime("%H:%M") + ", ") f.write(str(row["open"]) + ", ") f.write(str(row["high"]) + ", ") f.write(str(row["low"]) + ", ") f.write(str(row["close"]) + ", ") f.write(str(row["volume"]) + "\n") f.close() except: print("正在拉取%s的5分钟线异常" % (code))
def get_data(gid, start=None, end=None): gdata = tushare.get_k_data(gid, start, end) return gdata
from scipy import stats import statsmodels.api as sm import numpy as np import pandas as pd import matplotlib.pyplot as plt import arch import tushare as ts IndexData = ts.get_k_data(code='sh', start='2014-01-01', end='2016-08-01'); IndexData.index = pd.to_datetime(IndexData.date); close = IndexData.close; rate = (close - close.shift(1)) / close.shift(1) data = pd.DataFrame(); data['rate'] = rate; data = data.dropna(); data1 = np.array(data['rate']) # data['rate'].plot(figsize=(15, 5)) # t = sm.tsa.stattools.adfuller(data1) # print("p-vaule:", t[1]) # fig = plt.figure(figsize=(20, 5)) # ax1 = fig.add_subplot(111) # fig = sm.graphics.tsa.plot_acf(data1, lags=20, ax=ax1) # order = (8, 0) # model = sm.tsa.ARMA(data1, order).fit() # print(model) # at = data1 - model.fittedvalues # at2 = np.square(at) # plt.figure(figsize=(10, 6)) # plt.plot(at, label='at') # plt.legend();
'''当前交易所有股票的行情数据''' realTimeIndex = ts.get_index() '''当前大盘的行情数据''' realtime_tran_data = ts.get_realtime_quotes(target_stock_code) '''实时分笔数据''' #ts.get_realtime_quotes(['600848','000980','000981']) #symbols from a list #ts.get_realtime_quotes('sh')#上证指数 #ts.get_realtime_quotes(['sh','sz','hs300','sz50','zxb','cyb'])#上证指数 深圳成指 沪深300指数 上证50 中小板 创业板 #ts.get_realtime_quotes(['sh','600848'])#或者混搭 realday_deal_data = ts.get_today_ticks(target_stock_code) '''当日历史分笔''' big_deal_data = ts.get_sina_dd(target_stock_code, date=transaction_day) '''获取大单交易数据,默认为大于等于400手''' #df = ts.get_sina_dd('600848', date='2015-12-24', vol=500) #指定大于等于500手的数据 M_recent_k_data = ts.get_k_data(target_stock_code, ktype='M') '''最近月k线''' W_recent_k_data = ts.get_k_data(target_stock_code, ktype='W') '''最近周k线''' D_recent_k_data = ts.get_k_data(target_stock_code, ktype='D') '''最近日k线''' recent_k60_data = ts.get_k_data(target_stock_code, ktype='60') '''最近小时线''' recent_k30_data = ts.get_k_data(target_stock_code, ktype='30') '''最近30min线''' recent_k15_data = ts.get_k_data(target_stock_code, ktype='15') '''最近15min线''' recent_k5_data = ts.get_k_data(target_stock_code, ktype='5') '''最近5min线''' #==============================================================================指数成分============================================================================== '''获取指数成分'''
# encoding=utf_8 """ 本脚本用于研究stk价格在一定时期内所处的位置 """ import tushare as ts stk = ts.get_k_data('300183', start='2012-01-01').reset_index() win_size = 180 for idx in stk.index: price_now = stk.loc[idx, 'close'] se = stk.loc[idx - win_size:idx, 'close'] price_expensive = list(filter(lambda x: x < price_now, se)) stk.loc[idx, 'price_scale'] = len(price_expensive) / len(se) * 100.0 # 越高越好 stk.loc[win_size:, :].plot('date', ['close', 'price_scale'], subplots=True) end = 0 # stk.rooling()
codes = ['000001.SZ', '603999.SH', '600000.SH'] # or set() -> list() inital_cash = 1e5 begt = dt.datetime(2013, 1, 1) endt = dt.datetime(2018, 1, 1) cerebro = bt.Cerebro() # cerebro.addsizer(bt.sizers.FixedSize, stake=1000) # 默认买入一手 cerebro.broker.setcash(inital_cash) # 初始资金 cerebro.addstrategy(ManmStrategy, codes=codes) # Add Analyzer cerebro.addanalyzer(bt.analyzers.Returns, _name='returns') cerebro.addanalyzer(bt.analyzers.DrawDown, _name='drawdown') # Feed Data for each, code in enumerate(codes): tohlcva = ts.get_k_data(code.split('.')[0], start=str(begt.date()), end=str(endt.date()), autype='qfq') tohlcva.drop('code', axis=1, inplace=True) tohlcva['date'] = pd.to_datetime(tohlcva['date']) tohlcva.set_index('date', inplace=True) tohlcva = tohlcva[['open', 'high', 'low', 'close', 'volume']] tohlcva['openinterest'] = 0.0 # print(tohlcva.head(10)) data = bt.feeds.PandasData(dataname=tohlcva, fromdate=begt, todate=endt) cerebro.adddata(data, name=code) cerebro.run() cerebro.plot()
def predict_tomorrow(stk_code, label, N_STEPS=N_STEPS, feature_cols=feature_cols, HIDDEN_SIZE=HIDDEN_SIZE, NUM_LAYERS=NUM_LAYERS): """ :param stk_code: 例子 '300508' :param label: 例子 'high' :param N_STEPS: :param feature_cols: :param HIDDEN_SIZE: :param NUM_LAYERS: :return: """ """ ---------------------- 读取json中存储的极值 ---------------------- """ with open(rootPath + '\LSTM\AboutLSTM\stk_max_min.json', 'r') as f: max_min_info = json.load(f) """ ---------------------- 获取实时数据 ---------------------- """ data_now = ts.get_k_data(stk_code)[-(N_STEPS + 30):] # 增加M9 Rank data_now['m9'] = data_now['close'].rolling(window=9).mean() data_now['diff_m9'] = data_now.apply(lambda x: (x['close'] - x['m9']) / x['close'], axis=1) data_now['rank'] = data_now.apply(lambda x: relativeRank( max_min_info[stk_code]['m9_history'], x['diff_m9']), axis=1) # rootPath = 'C:/Users\paul\Desktop\软件代码\Git-Clone' for c in ['close', 'high', 'low', 'open']: data_now[c] = (data_now[c].values - max_min_info[stk_code]['p_min'] ) / (max_min_info[stk_code]['p_max'] - max_min_info[stk_code]['p_min']) data_now['volume'] = ( data_now['volume'].values - max_min_info[stk_code]['v_min']) / ( max_min_info[stk_code]['v_max'] - max_min_info[stk_code]['v_min']) # 进行归一化 input_normal = data_now.loc[:, feature_cols].tail(20).values tf.reset_default_graph() """ ---------------------- 创建模型 ---------------------- """ predictions, loss, train_op, X, y = lstm_model(n_steps=N_STEPS, n_inputs=len(feature_cols), HIDDEN_SIZE=HIDDEN_SIZE, NUM_LAYERS=NUM_LAYERS) # 创建保存器用于模型 saver = tf.train.Saver() # 初始化 sess = tf.Session() model_name = stk_code + '_' + label model_dir = rootPath + '\LSTM\AboutLSTM\modelDir/' if os.path.exists(model_dir + model_name + '/' + model_name + '.ckpt.meta'): saver = tf.train.import_meta_graph(model_dir + model_name + '/' + model_name + '.ckpt.meta') saver.restore(sess, tf.train.latest_checkpoint(model_dir + model_name + '/')) # graph = tf.get_default_graph() # 防报错 tf.reset_default_graph() r_rela = sess.run([predictions], feed_dict={X: [input_normal]})[0][0][0] return max_min_info[stk_code]['p_min'] + ( max_min_info[stk_code]['p_max'] - max_min_info[stk_code]['p_min']) * r_rela else: print('加载模型' + model_name + '失败!') return -1
def get(code): df = ts.get_k_data(code, start='2017-01-01') result.append(df) print(len(result), code)
continue elif float(stock_info.loc[stock_info.index==code].perundp) < 0: continue elif float(stock_info.loc[stock_info.index==code].rev) < -20: continue elif float(stock_info.loc[stock_info.index==code].profit) < -20: continue # elif float(stock_info.loc[stock_info.index==code].gpr) < 15: # continue # elif float(stock_info.loc[stock_info.index==code].npr) < 0: # continue try: if TEST=='True': stock=ts.get_k_data(code, start=Tstart, end=Tend) else: stock=ts.get_k_data(code) except: time.sleep(1) if TEST=='True': stock=ts.get_k_data(code, start=Tstart, end=Tend) else: stock=ts.get_k_data(code) if len(stock) < 250: continue stock.index=stock.iloc[:,0] stock.index=pd.to_datetime(stock.index,format='%Y-%m-%d') stock=stock.iloc[:,1:-1]
import tushare as ts import talib from pylab import * from DataSource.Data_Sub import my_pro_bar from Experiment.Constraint.Constraint import calBSReseau from SDK.MyTimeOPT import minus_date_str, get_current_date_str from HuiCe.Sub import bs_opt, plot_op_result if __name__ == '__main__': # 登录datasource from DataSource.auth_info import * stk_code = '000001' df = ts.get_k_data(stk_code, start='2017-01-12', end='2019-05-26') record_info = { 'floor_last': 0, 'money_remain': 20000, 'amount_remain': 1500, 'M_last': -1, # 用以记录上次的均线值,在反向操作中(本次操作与上次不同的情况)使用上次均值! 'BS_last': 'init', # 上次是买还是卖 "buy" "false" "init" 'price_last': df.head(1)['close'].values[0], # 上次价格 'BS_trend_now': 'init', 'BS_real': 'NO_OPT', # 本次实际操作 'Price_now': 12, 'last_opt_date': '2018-12-15', 'time_span_from_last': 1, # 当前距离上次操作的时间间隔 'B_continue': 1, 'S_continue': 1,
import matplotlib.pyplot as plt import tushare as ts import futuquant as ft import time datas = ts.get_k_data('hs300', ktype='D', start='2007-01-01', end='2018-07-01') #datas = datas[['code','close']].sort_index() #把列变成时间 datas['date'] = pd.to_datetime(datas['date']) #把时间对角例,变成索引 datas = datas.set_index('date') datas['无风险利率'] = (4.0 / 100 + 1)**(1.0 / 250) - 1 datas['无风险收益净值'] = (datas['无风险利率'] + 1).cumprod() #取每个月的第一个交易日 by_month = datas.resample('M', kind='period').first() trade_log = pd.DataFrame(index=by_month.index) trade_log['基金净值'] = by_month['close'] / 1000 #每月月初投入1000元 trade_log['投入资金'] = 1000 #购买份额 trade_log['基金份额'] = trade_log['投入资金'] / trade_log['基金净值'] trade_log['总基金份额'] = trade_log['基金份额'].cumsum() trade_log['累计投入资金'] = trade_log['投入资金'].cumsum() trade_log['理财份额'] = trade_log['投入资金'] / by_month['无风险收益净值'] trade_log['理财总份额'] = trade_log['理财份额'].cumsum() #重新采样转换为日历日 tem = trade_log.resample('D').ffill() datas = datas.to_period('D') #计算每个交易日的基金净值和资产
from sqlalchemy import create_engine from sqlalchemy.types import VARCHAR import pymysql pymysql.install_as_MySQLdb() # 创建数据连接 conn = create_engine('mysql+mysqldb://root:111111@localhost:3306/stock?charset=utf8') # 股票代码列表 # stock_list = ['002218', '600206', '300349', '002493', '600600', '600667', '603225', '600508', '000729', '002079', '000633'] stock_list = ['600035'] # 定义get_k_data接口返回数据的字段类型 k_data_dtype = { 'date': VARCHAR(64), 'code': VARCHAR(64) } # 循环获取股票日K线数据 for stock in stock_list: df = ts.get_k_data(code=stock, ktype='D', autype='qfq', start='1990-01-01') table = 'kd_d_' + stock pd.io.sql.to_sql(frame=df, name=table, con=conn, if_exists='replace', dtype=k_data_dtype) # 循环获取股票周K线数据 for stock in stock_list: df = df = ts.get_k_data(code=stock, ktype='W', autype='qfq', start='1990-01-01') table = 'kd_w_' + stock pd.io.sql.to_sql(frame=df, name=table, con=conn, if_exists='replace', dtype=k_data_dtype)
def fenxi(request): if request.method == "GET": dict = { "title": '用户注册' } return render(request, 'register.html', context=dict) elif request.method == "POST": daima = request.POST.get("daima") if daima == '': daima = '600519' dict = { 'daima': daima } listX = [] listy = [] data = ts.get_k_data(code=daima, ktype='15') data.index = data['date'] data.pop('date') # 基于n日均线计算boll n = 26 boll = create_boll(data, n) # 涨跌幅度 boll['zdfu'] = boll['close'].pct_change() # 涨跌标签:1,-1 boll['zd'] = np.sign(boll['zdfu']) boll['fen'] = boll['close'] - boll['bm'] # 去掉含空值行 con = boll.notnull().all(axis=1) boll = boll[con] lmc = pd.DataFrame(index=boll.index) lmc['zd'] = boll['zd'] lmc['bm'] = boll['bm'] lmc['close'] = boll['close'] lmc['fengy'] = boll[['fen']].apply(guiyi) lmc['liang'] = boll['volume'] lmc['liangfu'] = (boll['fen']) * lmc['liang'] lmc['liangfugy'] = lmc[['liangfu']].apply(guiyi) # 量脉冲限制条件 for i in range(0, len(lmc)): if (boll['close'][i] > boll['bl'][i]) and boll['zdfu'][i] < 0: lmc['liangfugy'][i] = lmc['fengy'][i] if (boll['close'][i] < boll['bm'][i]) and boll['zdfu'][i] > 0: lmc['liangfugy'][i] = 0 figure = plt.figure(figsize=(14, 7)) ax1 = figure.add_subplot(111) ax1.plot((boll['close'][-200:]), 'k', alpha=0.8) ax1.plot(lmc['liangfugy'][-200:] * np.min(boll['close'][-200:])/10 + np.min(boll['close'][-200:]), 'b', alpha=0.5) ax1.set_xticks(range(0, len(boll['close'][-200:]), 20)) ax1.set_xticklabels(boll['close'][-200:].index[::20], rotation=30) ax1.grid(b=True, linestyle="--") ax1.set_title("{}".format(daima), fontsize=20, color="black") figure.savefig("./static/img_gupiao/{}01.jpg".format(daima), facecolor="w", pad_inches=5) X, y = fun_y(lmc) listX.append(X) listy.append(y) return render(request, 'fenxi.html', context= dict)
return None, None, None except Exception: pass if __name__ == '__main__': #init_db() df = ts.get_hs300s() #df['code'].values for code in [ '603630', ]: try: #print(code) data = ts.get_k_data(code, start='2017-01-01') data["mavol30"] = cal_mavol30(data) data["ma30"] = cal_ma30(data) data["ma60"] = cal_ma60(data) data['pre_close'] = data['close'].shift() #data['p_change'] = ((data['close'] - data['pre_close']) / data['pre_close']) data = data.tail(30) #print(data[['date','close','ma30', 'volume', 'mavol30']]) code, date, volume = abnormal_val(data, code) #code = cal_singal_stock('002661') if code is None: continue
# -*- coding: utf-8 -*- import tushare as ts window_size = 5 # 过程演示用,待改为55 code = "000001" temp = ts.get_k_data(code, start="2018-03-01", ktype="D", autype="qfq") temp.index = temp.pop("date") df = temp.loc[:, ["high", "close"]] df["hhv"] = df["high"].rolling(window_size).max() df["pre_hhv"] = df["hhv"].shift(1) # df["signals"] = df["close"] > df["pre_hhv"] df["signals"] = (df["close"].shift(1) <= df["pre_hhv"].shift(1)) & \ (df["close"] > df["pre_hhv"]) print(df) results = df[df["signals"]] print(list(results.index))
net_pct_s 小单净占比(%) 小单净占比 = 小单净额 / 成交额 """ df_flow = get_money_flow('300183.XSHE', '2018-05-01', '2019-06-10') df_flow_plot = df_flow.loc[:, [x not in ['sec_code'] for x in df_flow.columns.values]] df_flow_plot['net_total'] = df_flow.apply(lambda x: np.sum([ x['net_amount_main'], x['net_amount_xl'], x['net_amount_l'], x['net_amount_m'], x['net_amount_s']]), axis=1) df_flow_plot['net_m'] = df_flow_plot['net_total'].rolling(window=5).sum() df_flow_plot['date_str'] = df_flow_plot.apply(lambda x: str(x['date'])[:10], axis=1) df_flow_plot = df_flow_plot.set_index(keys='date_str') # 下载tushare数据 df_t = ts.get_k_data('300183', start='2018-05-01') df_t = df_t.set_index(keys='') end = 0 """ df_flow_plot.plot('date', list(df_flow.columns.values).remove('date'), subplots=True) df_flow_plot.plot('date', ['change_pct', 'net_total'], subplots=True) """