def Get_5_10_20_data(self, code, start, end): data_5_data = ts.get_hist_data(code, start=start, end=end) #防止过多拉取数据源 网站屏蔽IP cons = ts.get_apis() # 增加前复权 adj='qfq' 后复权 adj='hfq'(bob_jie : 2017-11-01) data_5_data = ts.bar(code, conn=cons, start_date=start, adj='qfq', end_date=end, ma=[5, 10, 20], factors=['vr', 'tor']) try: data_5_data.values[0][0] except Exception as e: time.sleep(2) #防止过多拉取数据源 网站屏蔽IP cons = ts.get_apis() # 增加前复权 adj='qfq' 后复权 adj='hfq'(bob_jie : 2017-11-01) data_5_data = ts.bar(code, conn=cons, start_date=start, adj='qfq', end_date=end, ma=[5, 10, 20], factors=['vr', 'tor']) ts.get_h_data('002337', autype='qfq') Mysql().SaveMySqlTWO(data_5_data, 'Stock_Basics_Info_All', code + 'stock_basics', code)
def tushare_bar(code, start, end, freq, **kwargs): df = ts.bar(code=code, conn = ts.get_apis(), start_date = start, end_date = end, \ freq = freq, asset='E', adj = 'qfq', **kwargs) if isinstance(df, pd.DataFrame): return df else: raise TypeError('df is unknown type %s' % type(df))
def stock_plot(): left, width = 0.1, 0.8 rect_vol = [left, 0.1, width, 0.3] rect_main = [left, 0.4, width, 0.5] fig = plt.figure() conn = ts.get_apis() df = ts.bar('300333', conn=conn, start_date='2018-01-01') del df['code'] df.reset_index(inplace=True) dates = df['datetime'].values df['datetime'] = df['datetime'].map(mdates.date2num) # dates=df['datetime'].values vol = df['vol'].values ax_vol = fig.add_axes(rect_vol) ax_vol.fill_between(dates, vol, color='y') # 横坐标时间旋转 plt.setp(ax_vol.get_xticklabels(), rotation=30, horizontalalignment='right') ax_main = fig.add_axes(rect_main) candlestick_ochl(ax_main, df.values, width=0.6, colordown='g', colorup='r') ax_main.axes.get_xaxis().set_visible(False) ax_main.set_title("STOCK LINE") ts.close_apis(conn) plt.show()
def stock_plot(): left, width = 0.1, 0.8 rect_vol = [left, 0.1, width, 0.3] rect_main = [left, 0.4, width, 0.5] fig = plt.figure() conn = ts.get_apis() df = ts.bar('300333', conn=conn, start_date='2018-01-01') del df['code'] df.reset_index(inplace=True) dates=df['datetime'].values df['datetime'] = df['datetime'].map(mdates.date2num) # dates=df['datetime'].values vol = df['vol'].values ax_vol = fig.add_axes(rect_vol) ax_vol.fill_between(dates, vol,color = 'y') # 横坐标时间旋转 plt.setp(ax_vol.get_xticklabels(), rotation=30, horizontalalignment='right') ax_main = fig.add_axes(rect_main) candlestick_ochl(ax_main,df.values,width=0.6,colordown='g',colorup='r') ax_main.axes.get_xaxis().set_visible(False) ax_main.set_title("STOCK LINE") ts.close_apis(conn) plt.show()
def fetch_kdata_lazy(code, asset, freq, end_date=''): # global在第一行声明,养成好习惯 global cons start_date = get_date_max(code, asset, freq) + relativedelta(minutes=1) if isinstance(end_date, datetime.datetime): end_date_str = end_date.strftime(DT_FMT).replace( "11:30:00", "13:00:00") end_date = datetime.datetime.strptime(end_date_str, DT_FMT) kdata = ts.bar(code, conn=cons, asset=asset, adj='qfq', freq=freq, start_date=start_date, end_date=end_date) result = 0 try: result = insert_kdata(asset, freq, kdata) except pymongo.errors.BulkWriteError as err: logger.error(err) except StockError as err: logger.error(err) except TypeError as e: cons = ts.get_apis() logger.error(e) except Exception as e: logger.error(e) return result
def __bar(self): self.data_frame = ts.bar(code=self.code, conn=TushareConn.conn, start_date=self.start, end_date=self.end, freq=self.freq, asset=self.asset)
def plot_stock_line(code, name, table_name, start='2017-10-01', save=False): today = datetime.datetime.now().strftime('%Y-%m-%d') title = u'{} {} {} {}'.format(today, code, name, table_name) if os.path.exists(title + '.png'): return engine = get_engine('db_stock', local=True) fig = plt.figure(figsize=(10, 8)) base_info = pd.read_sql('tb_basic_info', engine, index_col='index') # fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10)) ax = fig.add_axes([0, 0.3, 1, 0.55]) ax2 = fig.add_axes([0, 0.1, 1, 0.25]) if code is None and name is not None: code = base_info[base_info['name'] == name]['code'].values[0] print code df = None for _ in range(4): api = ts.get_apis() try: df = ts.bar(code, conn=api, start_date=start) break except Exception, e: print e ts.close_apis(api) time.sleep(random.random() * 3) continue
def write_all_stock(allAshare, lib=None): """ :param allAshare: List,所有股票 :param lib: arctic.store.version_store.VersionStore :return: succ: List, written stocks; fail: List, failed written stocks """ succ = [] fail = [] cons = ts.get_apis() if not os.path.exists(hqdir): os.mkdir(hqdir) for symbol in allAshare: try: df = ts.bar(symbol, cons, freq='D', start_date='', end_date='').sort_index(ascending=True) if MONGO and lib: lib.write(symbol, df, metadata={'source': 'Tushare'}) if CSV: df.to_csv(os.path.abspath("{}/{}.csv".format(hqdir, symbol))) print(symbol + '写入完成') succ.append(symbol) except Exception as e: fail.append(symbol) print("Failed for ", symbol, str(e)) sleep(0.1) if SFL: write_list(succ, os.path.abspath('./hq_succ_list.txt')) write_list(fail, os.path.abspath('./hq_fail_list.txt'))
def downMinuteBarBySymbol(symbol): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 cons = ts.get_apis() df = ts.bar(symbol, cons, start_date='2018-01-10', end_date='2018-04-09', freq='1min', asset='X') df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost)
def downMinuteBarBySymbol(symbol, freq): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = ts.bar(symbol, ts.get_apis(), freq=freq, asset='X') #df = ts.bar(symbol, ktype='1min') df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ #有的合约字母变成大写,可以自己放开这个地方 #d['symbol'] = d['symbol'].lower() flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s数据下载完成%s - %s,耗时%s毫秒' % (symbol, df.index[0], df.index[-1], cost)
def get_close_price(code, start, end=datetime.today(), freq='D'): if (asset_category(code) == 'FUND_TRADED' or asset_category(code) == 'FUND_UNTRADED'): try: f = fp_prefix + code + '.csv' if (asset_category(code) == 'FUND_TRADED'): col_name = '收盘价' if (asset_category(code) == 'FUND_UNTRADED'): col_name = '净值' close_price = pd.read_csv(f, header=0, sep=',', converters={'交易时间': lambda x: datetime.strptime(x, '%Y-%m-%d')}, index_col='交易时间', usecols=['交易时间', col_name]) except: print('Cannot find {}.\n'.format(f)) else: if (asset_category(code) == 'A'): asset = 'E' if (asset_category(code) == 'HK'): asset = 'X' if (asset_category(code) == 'XSB'): asset = 'X' if (asset_category(code) == 'INDEX'): asset = 'INDEX' try: #cons = ts.get_apis() s = ts.bar(code, conn=cons, freq='D', start_date=start, end_date=end, asset=asset) close_price = s['close'] except: print('Error when get price of {}.\n'.format(code)) close_price = close_price.sort_index(ascending='ascending') if (freq == 'Y' or freq == 'END'): close_price = close_price.iloc[[0, -1]] return close_price
def collect_single_index_daily_from_ts(code, table_name='index_k_data_60m', conn=None): start = (datetime.now() - timedelta(5)).strftime( datetime_utils.DATE_FORMAT) try: data = ts.bar(conn=conn, code=code, freq='60min', asset="INDEX", start_date=start, retry_count=10) data.rename(columns={'vol': 'volume'}, inplace=True) data = data.drop(columns=['amount']) data['date'] = data.index data['pre_close'] = data['close'].shift(-1) data = data.head(4) data.to_sql(table_name, dataSource.mysql_quant_engine, if_exists='append', index=False) except Exception as e: logger.error(e)
def fetch_code(code='000001'): df = ts.bar(code=code, conn=cons, freq=freq, start_date=start_date, end_date=end_date, ma=ma) columns = ['close', 'high', 'low', 'open', 'vol', 'amount', 'ma5', 'ma10', 'ma20', 'ma30', 'ma60'] df = df.loc[:, columns] df = df.sort_index(axis=0, ascending=True) df.to_csv('data/code.csv') return df
def plot_stock_line(code,start): fig = plt.figure(figsize=(10,15)) # fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10)) ax=fig.add_axes([0,0.2,1,0.5]) ax2=fig.add_axes([0,0,1,0.2]) df = ts.bar(code,conn=api,start_date=start) # df=df.sort_values(by='datetime') df = df.sort_index() df =df.reset_index() # df = ts.get_k_data('300141',start='2018-03-01') # df['date']=df['date'].dt.strftime('%Y-%m-%d') df['datetime']=df['datetime'].dt.strftime('%Y-%m-%d') sma5=talib.SMA(df['close'].values,5) sma20=talib.SMA(df['close'].values,20) # ax.set_xticks(range(0,len(df),20)) # # ax.set_xticklabels(df['date'][::5]) # ax.set_xticklabels(df['datetime'][::20]) candlestick2_ochl(ax,df['open'],df['close'],df['high'],df['low'],width=0.5,colorup='r',colordown='g',alpha=0.6) # ax.set_title(code) ax.plot(sma5) ax.plot(sma20) # df['vol'].plot(kind='bar') volume_overlay(ax2,df['open'],df['close'],df['vol'],width=0.5,alpha=0.8,colordown='g',colorup='r') ax2.set_xticks(range(0,len(df),20)) # ax.set_xticklabels(df['date'][::5]) ax2.set_xticklabels(df['datetime'][::20]) # ax2.grid(True) # plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right') # plt.grid(True) # plt.subplots_adjust(hspace=0) plt.show()
def insert_kdata(code, asset, freq, start, end): sheet = get_sheet(freq) cons = ts.get_apis() k_data = ts.bar(code, conn=cons, freq='5min', start_date='2016-01-01', end_date='', asset=asset) # 对DataFrame的个别列进行重命名 k_data = k_data.rename(columns={'code': 'code_origin'}) # 代码 k_data['code'] = code # 周期 k_data['freq'] = freq # 是否指数 k_data['asset'] = asset k_data = k_data[[ 'code', 'index', 'date', 'ktype', 'open', 'close', 'high', 'low', 'volume', 'code_origin' ]] # inplace默认为False,如果该值为False,那么原来的pd顺序没变,只是返回的是排序的 k_data.sort_values("date", ascending=False, inplace=True) records = pandas.io.json.loads(k_data.T.to_json()).values() global insert_result insert_result = sheet.insert_many(records, ordered=False) # except BulkWriteError as e: # print("insert_many出现重复,忽略错误") return insert_result
def __ma(self): self.data_frame_ma = ts.bar(code=self.code, conn=TushareConn.conn, start_date=self.start, end_date=self.end, freq=self.freq, ma=self.ma)
def get_bar_realtime_code(): ap = ts.get_apis() data = ts.bar('600570', ap, start_date='20190516', end_date='20190517', freq='1min') # if os.path.exists(filename): # data.to_csv( # filename, # mode='a', # header=False, # encoding='utf-8', # na_rep='NA') # else: # data.to_csv(filename, encoding='utf-8', na_rep='NA') # pcolumns = data.columns # pindex = data.index # pdata = data[data.columns[0:10]] # pdata2 = data['首字符'] = data['code'].str[0:1] data['datetime'] = data.index # ps = data['time'] # print(ps) # print(type(data.index)) # data['time'] = pd.to_datetime(data['time'], format='%d.%m.%Y') # pddata = data.loc[-1] = ptime # data.loc[data['首字符'] == '6', '市场'] = '1' # ss = data.to_json(orient='split') content = data_to_json(data) # resp = Response_headers(content) return content
def GetstockData(code,start,end=None): api=ts.get_apis() if not end: end = datetime.datetime.now().strftime('%Y-%m-%d') print(end) df = ts.bar(code,conn=api,freq='D',start_date=start,end_date=end) df.to_excel('data/'+code+'.xls')
def fetch(code_list): tsconn = ts.get_apis() sqlconn = db.connect() cursor = sqlconn.cursor() try: for code in code_list: try: data = ts.bar(code, conn=tsconn, start_date="1970-01-01", freq="5min") count = data.shape[0] i = 0 for index, row in data.iterrows(): cursor.execute( "insert into stock_price (code, time, price) values (%s, %s, %s)", (code, str(index), float(row["close"]))) i += 1 if i % 1000 == 0: print("{0} {1}%".format(code, int(i / count * 100))) sqlconn.commit() print("done") except IOError as err: print(code) print(err) finally: sqlconn.close() ts.close_apis(tsconn)
def downBarBySymbol(symbol, start_date=None, end_date=None, freq='D'): """下载某一合约的分钟线数据""" start = time() db_freq = freq #if freq[0] in ['0', '1', '2','3','4','5','6','7','8','9']: #db_freq = freq + 'MIN' db = mc[DB_NAME_DICT[db_freq]] # 数据库 cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 if generateExchange(symbol)=="QH": asset = 'X' else: asset = 'E' asset = 'INDEX' df = ts.bar(symbol, conn=ts.get_apis(), freq=freq, asset=asset, start_date=start_date, end_date=end_date, adj='qfq') #df = ts.get_hist_data(symbol,start=start_date, end=end_date, ktype=freq) df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print u'合约%s 周期%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, freq, df.index[0], df.index[-1], cost)
def get_of_bar(code, conn, freq, start, end): df = ts.bar(code=code, conn=conn, freq=freq, start_date=start, end_date=end) return df
def get_k_frame(cls, con, code): k_df = tushare.bar(code, con, adj='qfq') k_df_size = k_df.index.size if not k_df_size: return k_df k_df['high'] = pandas.to_numeric(k_df['high']) k_df['low'] = pandas.to_numeric(k_df['low']) k_df['open'] = pandas.to_numeric(k_df['open']) k_df['close'] = pandas.to_numeric(k_df['close']) k_df['date'] = k_df.index k_df['date'] = k_df['date'].apply(lambda x: datetime.datetime.strftime(x, "%Y-%m-%d")) k_df = k_df.drop(['vol', 'amount'], axis=1) k_df = k_df.reset_index(drop=True) k_df = k_df.sort_values(by='date').reset_index(drop=True) k_df['k_pos'] = k_df.index def _get_per_change(x): if x == 0: _pc = (k_df.at[0, "close"] - k_df.at[0, "open"]) / k_df.at[0, "open"] * 100 else: _pc = (k_df.at[x, "close"] - k_df.at[x-1, "close"]) / k_df.at[x-1, "close"] * 100 return float("%0.2f" % _pc) k_df["per_change"] = k_df.k_pos.apply(_get_per_change) return k_df
def isXR(code=""): global cons shi_jian = gsd.get_code_min_shi_jian(code) if shi_jian == "None": return False data = gsd.get_stock_data_daily_df_time(code=code, start=shi_jian, end=shi_jian) price = data["price"].values[0] tsFaile = 1 while tsFaile != 0: try: # print(code,shi_jian) # if tsFaile >=2 : # cons = ts.get_apis() data = ts.bar(code, conn=cons, adj='qfq', start_date=shi_jian, end_date=shi_jian, retry_count=2) # print("data",data) ''' if len(data["close"].values)<1 : tsFaile += 1 else: tsFaile = 0 ''' tsFaile = 0 if len(data["close"].values) < 1: return False except Exception as e: #获取连接备用 ts.close_apis(conn=cons) cons = ts.get_apis() print(code, shi_jian, "isXR fail:", tsFaile, e) time.sleep(5) if tsFaile <= 3: tsFaile += 1 else: return False # print(code,shi_jian) # data = ts.bar(code, conn=cons, adj='qfq', start_date=shi_jian, end_date=shi_jian,retry_count = 20) # print("data",data) close = data["close"].values[0] if price == close: return False else: return True
def store_data(): code = '600050' conn = ts.get_apis() file_name = os.path.join(data_path, code + '.xls') df = ts.bar(code, conn=conn, start_date='2015-01-01') # print(df.head(10)) print(file_name) df.to_excel(file_name)
def get_index(symbol): cons = ts.get_apis() return ts.bar(symbol, cons, freq='D', start_date='', end_date='', asset='INDEX').sort_index(ascending=True)
def get_hist_data(self,code,name,start_data): try: start_data = datetime.datetime.strptime(str(start_data), '%Y%m%d').strftime('%Y-%m-%d') df = ts.bar(code,conn=conn,start_date=start_data,adj='qfq') print df except Exception,e: print e return
def get_hist_data(self, code, name, start_data): try: start_data = datetime.datetime.strptime(str(start_data), '%Y%m%d').strftime('%Y-%m-%d') df = ts.bar(code, conn=conn, start_date=start_data, adj='qfq') print df except Exception, e: print e return
def download(): cons = ts.get_apis() df = ts.bar('ifl9', conn=cons, asset='X', start_date='2017-6-24', end_date='') df1 = df[['close']] df1.to_excel('c:/if.xlsx')
def get_hs300_last_date_online(freq='d'): conn = ts.get_apis() benchmark = ts.bar('000300', conn=conn, asset='INDEX', freq=freq) ts.close_apis(conn) if benchmark is None: return None, None last_date0 = benchmark.index[0].strftime('%Y-%m-%d') last_date1 = benchmark.index[1].strftime('%Y-%m-%d') return last_date0, last_date1
def get_trading_days(cls, start=None, end=None, ndays=None, ascending=True): """ 取得交易日列表,分三种方式取得 (1)指定开始、结束日期,即start和end不为None,此时忽略参数ndays (2)指定开始日期和天数,即start和ndays不为None,而end为None (3)指定结束日期和天数,即end和ndays为None,而start为None -------- :param start: datetime-like or str 开始日期,格式:YYYY-MM-DD :param end: datetime-like or str 结束日期,格式:YYYY-MM-DD :param ndays: int 交易日天数 :param ascending: bool,默认True 是否升序排列 :return: -------- Series of pandas.Timestamp,交易日列表,默认按交易日升序排列 """ if len(Utils.utils_trading_days) == 0: ts_conn = ts.get_apis() df_SZZS = ts.bar(code='000001', conn=ts_conn, asset='INDEX') ts.close_apis(ts_conn) Utils.utils_trading_days = Series(df_SZZS.index).sort_values() if start is not None: start = cls.to_date(start) if end is not None: end = cls.to_date(end) if start is not None and end is not None: trading_days = Utils.utils_trading_days[ (Utils.utils_trading_days >= start) & (Utils.utils_trading_days <= end)] elif start is not None and ndays is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days >= start].iloc[:ndays] elif end is not None and ndays is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days <= end].iloc[-ndays:] elif start is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days >= start] elif end is not None: trading_days = Utils.utils_trading_days[ Utils.utils_trading_days <= end] elif ndays is not None: trading_days = Utils.utils_trading_days.iloc[-ndays:] else: trading_days = Utils.utils_trading_days trading_days = trading_days.reset_index(drop=True) if not ascending: trading_days = trading_days.sort_values(ascending=False) return trading_days
def store(self, code, start_date): #print code,start_date try: print code print start_date df = ts.bar(code=code, conn=self.cons,start_date=start_date) except Exception,e: print e return
def fetch_kdata(code, asset, freq): start_date = get_start_date(freq) kdata = ts.bar(code, conn=cons, asset='INDEX', adj='qfq', freq=freq, start_date=start_date, end_date='') return insert_kdata(asset, freq, kdata)
def _data(self, symbol, start_date='', end_date=''): try: conn = self._get_server() df = ts.bar(symbol, conn=conn, freq='5min') df = df.reset_index(inplace=True) df = df[['datetime', 'open', 'close', 'high', 'low', 'volume', 'code']] self.data = self.data.append(df, ignore_index=True) return df except: return None
def PreClose(L): ClosePrice = OrderedDict() for i in L: ClosePrice[i] = ts.bar(i, conn=cons, freq='D', asset='E', start_date=Today, end_date='').close return ClosePrice
def store(self, code, start_date): #print(code,start_date) try: df = ts.bar(code=code, conn=self.cons,start_date=start_date) except Exception as e: print(e) return try: df.to_sql(code, self.engine) except Exception as e: print(e) return
def stock_line(): api = ts.get_apis() df = ts.bar('300141', conn=api, start_date='2018-01-01') # print(df) del df['code'] df.reset_index(inplace=True) print(df) df['datetime'] = df['datetime'].map(mdates.date2num) ax1 = plt.subplot2grid((6, 1), (0, 0), rowspan=5) ax2 = plt.subplot2grid((6, 1), (5, 0), rowspan=5, sharex=ax1) ax1.xaxis_date() candlestick_ochl(ax1, df.values, width=1, colorup='r', colordown='g') # ax2.fill_between(ax1,df['vol'].values,0) plt.show() ts.close_apis(api)
def get_hist_data(self, code, name, start_data): try: start_data = datetime.datetime.strptime(str(start_data), '%Y%m%d').strftime('%Y-%m-%d') df = ts.bar(code, conn=conn, start_date=start_data, adj='qfq') print(df) except Exception as e: print(e) return df.insert(1, 'name', name) df = df.reset_index() try: df.to_sql(code, engine, if_exists='append') except Exception as e: print(e)
def get_hist_data(code, name, start_data): try: # start_data = datetime.datetime.strptime(str(start_data), '%Y%m%d').strftime('%Y-%m-%d') df = ts.bar(code, conn=conn, start_date=start_data, adj='qfq') except Exception as e: print(e) return hist_con = get_engine('history') df.insert(1, 'name', name) df = df.reset_index() #print(df) df2=pd.read_sql_table(code,hist_con,index_col='index') try: new_df = pd.concat([df,df2]) new_df = new_df.reset_index(drop=True) new_df.to_sql(code, engine, if_exists='replace') except Exception as e: print(e) return
def downMinuteBarBySymbol(symbol): """下载某一合约的分钟线数据""" start = time() cl = db[symbol] cl.ensure_index([('datetime', ASCENDING)], unique=True) # 添加索引 df = ts.bar(symbol, freq='1min') df = df.sort_index() for ix, row in df.iterrows(): bar = generateVtBar(row) d = bar.__dict__ flt = {'datetime': bar.datetime} cl.replace_one(flt, d, True) end = time() cost = (end - start) * 1000 print(u'合约%s数据下载完成%s - %s,耗时%s毫秒' %(symbol, df.index[0], df.index[-1], cost))
def ban_share(code,name): conn =ts.get_apis() year_2017 = [2629.218,3970.902,2083.032,1720.327,1999.456,1771.074,2417.082,2904.992,2910.946,2971.483,2350.122,3874.328] df = ts.bar(code, conn=conn, freq='M', start_date='2016-12-30', end_date='2017-11-01', asset='INDEX') series = df['close'] ''' diff_series=[] print series l = len(series) for i in range(l-1): print series[i] d= series[i]-series[i+1] diff_series.append(d) #print len(diff_series) ''' #s2=Series(series) s2=series[:len(series)-1] s3 = s2.sort_index(ascending=True) #print s3 s1 = Series(year_2017[0:len(s3)]) s3=s3.reset_index(drop=True) #print s3 #print s1 cor = s3.corr(s1) #print len(s3) #print len(s1) print cor plt.figure() plt.subplot(2,1,1) s1.plot() plt.subplot(2,1,2) s3.plot(title=name) plt.show() if abs(cor) >0.5: print 'Great factor: ',code
import json import pandas as pd import pymongo import time from ggplot import * import tushare as ts cons = ts.get_apis() hs300 = ts.bar('000300', conn=cons, asset='INDEX', start_date='', end_date='') hs300 = hs300.reset_index(); #print(hs300.head(5)); firstopen = hs300.iloc[-1].values[2]; #print(firstopen); hs300['value'] = hs300['open']/firstopen; hs300['time'] = hs300['datetime']; del hs300['datetime']; del hs300['open']; del hs300['close']; del hs300['high']; del hs300['low']; del hs300['vol']; del hs300['amount']; #print(hs300.head(5)); client = pymongo.MongoClient('localhost', 27017) #基金 fddb = client.fddb;
def plot_stock_line(api,code, name, table_name, current, start='2017-10-01', save=False): title = '{} {} {} {}'.format(current, code, name, table_name) title = title.replace('*', '_') if os.path.exists(title + '.png'): return if code is None and name is not None: code = base_info[base_info['name'] == name]['code'].values[0] df = None for _ in range(4): try: df = ts.bar(code, conn=api, start_date=start) except Exception as e: logger.info(e) ts.close_apis(api) time.sleep(random.random() * 30) api = ts.get_apis() else: break if df is None: return df = df.sort_index() if name is None: name = base_info[base_info['code'] == code]['name'].values[0] df = df.reset_index() df['datetime'] = df['datetime'].dt.strftime('%Y-%m-%d') sma5 = talib.SMA(df['close'].values, 5) sma20 = talib.SMA(df['close'].values, 10) # ax.set_xticks(range(0,len(df),20)) # # ax.set_xticklabels(df['date'][::5]) # ax.set_xticklabels(df['datetime'][::20]) fig = plt.figure(figsize=(10, 8)) # fig,(ax,ax2)=plt.subplots(2,1,sharex=True,figsize=(16,10)) ax = fig.add_axes([0, 0.3, 1, 0.50]) ax2 = fig.add_axes([0, 0.1, 1, 0.20]) candlestick2_ochl(ax, df['open'], df['close'], df['high'], df['low'], width=1, colorup='r', colordown='g', alpha=0.6) ax.grid(True) ax.set_title(title) ax.plot(sma5, label='MA5') ax.legend() ax.plot(sma20, label='MA20') ax.legend(loc=2) ax.grid(True) # df['vol'].plot(kind='bar') volume_overlay(ax2, df['open'], df['close'], df['vol'], width=1, alpha=0.8, colordown='g', colorup='r') ax2.set_xticks(range(0, len(df), 5)) # ax.set_xticklabels(df['date'][::5]) ax2.set_xticklabels(df['datetime'][::5]) plt.setp(ax2.get_xticklabels(), rotation=30, horizontalalignment='right') ax2.grid(True) plt.subplots_adjust(hspace=0.3) if save: # path = os.path.join(os.path.dirname(__file__),'data',today) fig.savefig(title + '.png') else: plt.show() plt.close()
#中证500 #dt = ts.get_zz500s() #print(dt); #上证50 #print(ts.get_sz50s()); #沪深300 #print(ts.get_hs300s()); #获取连接备用 cons = ts.get_apis() df = ts.bar('000300', conn=cons, asset='INDEX', start_date='', end_date='') df = df.reset_index(); df['value'] = df['open']/2000; print(df.head(5)); print(df.dtypes); df1 = ts.bar('000016', conn=cons, asset='INDEX', start_date='', end_date='') df1 = df1.reset_index(); df1['value'] = df1['open']/2000; df1['odc'] = df1['open']>df1['close']; df = pd.concat([df,df1]); ''' df1 = ts.bar('000905', conn=cons, asset='INDEX', start_date='2016-01-01', end_date='') df1 = df1.reset_index(); df1['odc'] = df1['open']>df1['close'];
import tushare as ts # 0. 获取连接备用 # [](http://mp.weixin.qq.com/s/RhGgTEVHqm8aDh2ZzpLviA) cons = ts.get_apis() # 1. 股票日线行情 df = ts.bar('600000', conn=cons, freq='D', start_date='2016-01-01', end_date='') df.head(5) # 2. 带因子的行情 # ma=[5, 10, 20, 60] 表示一次性获得5/10/20/60日均线 # factors=[‘vr’, ‘tor’],vr表示量比,tor表示换手率 df = ts.bar('600000', conn=cons, start_date='2016-01-01', end_date='', ma=[5, 10, 20], factors=['vr', 'tor']) df.head(5) # 3. 复权行情 # adj=qfq(前复权), hfq(后复权),默认None不复权 df = ts.bar('600000', conn=cons, adj='qfq', start_date='2016-01-01', end_date='') df.head(5) # 4. 分钟数据 # 设置freq参数,分别为1min/5min/15min/30min/60min,D(日)/W(周)/M(月)/Q(季)/Y(年) df = ts.bar('600000', conn=cons, freq='1min', start_date='2016-01-01', end_date='') df.head(15)