Python tushare 模块,get_h_data() 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用tushare.get_h_data()

项目:stockUtils    作者:caiyue    | 项目源码 | 文件源码
def loop_all_stocks():
    days = 60
    count =0
    for EachStockID in info.index:
        end_day=datetime.date(datetime.date.today().year,datetime.date.today().month,datetime.date.today().day)
        days=days*7/5
        start_day=end_day-datetime.timedelta(days)
        start_day=start_day.strftime("%Y-%m-%d")
        end_day=end_day.strftime("%Y-%m-%d")
        df= ts.get_h_data(EachStockID,start=start_day,end=end_day)
        if not(df is None):
            period_high=df['high'].max()
            today_high=df.iloc[0]['high']
            if today_high>=period_high:
                highlist.append(df,ignore_index=True)
                count+=1
项目:strategy    作者:kanghua309    | 项目源码 | 文件源码
def _save(stock, conn):
    try:
        print "save ----- :", stock
        marketday = stocksinfo.at[stock, 'timeToMarket']
        startday = pd.Timestamp(str(marketday))
        # print marketday,startday,str(startday)[:10]
        # df = ts.get_h_data(code, start=str(startday)[:10], retry_count = 5)
        df = ts.get_h_data(stock, start=str(startday)[:10], retry_count=5, pause=1)
        df = df.sort_index(ascending=True)
        # ma_list = [5,10,20,60]
        # for ma in ma_list:
        #    df['MA_' + str(ma)] = pd.rolling_mean(df.close, ma)
        # print df[['open','high','close','low','volume']].head(2)
        df[['open', 'high', 'close', 'low', 'volume']].to_sql(stock, conn, if_exists='append')
    except Exception, arg:
        print "exceptions:", stock, arg
        errorlist.append(stock)
项目:strategy    作者:kanghua309    | 项目源码 | 文件源码
def _update(stock, conn):
    try:
        print "update ----- :", stock
        query = "select * from '%s' order by date" % stock
        df = pd.read_sql(query, conn)
        df = df.set_index('date')

        print "sql saved:", df.tail(1),df.ix[-1],df.ix[-1].name
        if dt.now().weekday() == 5:
            today = str(pd.Timestamp(dt.now()) - pd.Timedelta(days=1))[:10]
        elif dt.now().weekday() == 6:
            today = str(pd.Timestamp(dt.now()) - pd.Timedelta(days=2))[:10]
        else:
            today = str(pd.Timestamp(dt.now()))[:10]
        print "today:",today
        if today != df.ix[-1].name[:10]:
            df = ts.get_h_data(stock, start=df.ix[-1].name[:10], retry_count=5, pause=1)
            print "read from tu:",df.head(1)
            df[['open', 'high', 'close', 'low', 'volume']].to_sql(stock, conn, if_exists='append')
            import time
            time.sleep(10)
    except Exception, arg:
        print "exceptionu:", stock, arg
        errorlist.append(stock)
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def main():
    # ?????? ?3????
    history = ts.get_hist_data(id)

    print u"??3????"

    print history.head(10)

    history_all = ts.get_h_data(id, '20015101', '20160101')

    print u'???????'
    print history_all



    # print type(stockInfo)
    # print stockInfo.head()
    # print stockInfo.dtypes
    # df = ts.get_stock_basics()
    #data = df.ix[id]['timeToMarket']
    #print data
    #ts.get_today_all()
项目:python_data_tools    作者:king3366ster    | 项目源码 | 文件源码
def get_h_data(self, code, start = None, end = None, autype = None, index = False):
        if end is None:
            end = dataTime.datetimeRelative(delta = 1)
        if start is None:
            start = dataTime.datetimeRelative(base = end, delta = -20)
        df = ts.get_h_data(code, start, end, autype = autype, index = index)
        df = self.format_date_index(df)
        df = df.sort_values(['date'], ascending=[True])
        return df

# ????
    # ?????
        # code???????6?????
        # date??????YYYY-MM-DD
        # retry_count : int, ??3,??????????????
        # pause : int, ?? 0,??????????????????????????????

    # ??????
        # time???
        # price?????
        # change?????
        # volume????
        # amount?????(?)
        # type????????????????
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def get_high_test():
    df = ts.get_h_data('300141', start=day30, end=day0)

    #???????????????

    #print df
    #current= df[:1]
    #current=df.iloc[0]
    print df
    current = df.ix['2016-07-15']
    print current
    current_high = current['high'].values[0]
    print current_high
    highest = df['high']
    lowest = df['low']

    price_30_max = highest.max()
    price_30_min = lowest.min()

    print df[df.high >= price_30_max]

    #???????????
    print df[df.low <= price_30_min]
    #???????????


    print price_30_max
    print price_30_min
    #oneData= df.ix['2016-07-11']
    #print oneData.iloc[0,1]
    #print type(oneData)
    #for i in highest.len:
    #    print i

    #print type(t)
    if current_high >= price_30_max:
        print stock_info.ix['300141']['name'].decode('utf-8')
项目:strategy    作者:kanghua309    | 项目源码 | 文件源码
def _update(stock,begin, conn):
    print "update ----- :", stock
    try:
        df = ts.get_h_data(stock, start=begin, retry_count=5, pause=1)
        if len(df) != 0:
           print df.head(2)
           df[['open', 'high', 'close', 'low', 'volume']].to_sql(stock, conn, if_exists='append')
        import time
        time.sleep(10)
    except Exception, arg:
        print "exception:", stock, arg
        raise SystemExit(-1)
        #errorlist.append(stock)
项目:silverstrategy    作者:silverhandy    | 项目源码 | 文件源码
def is_break_high(self, stockId, days):
        (start_day, end_day) = self.pick_date_from_days(days)
        #print("high: stockId", stockId, "days", days, "start_day", start_day, "end_day", end_day)
        df = ts.get_h_data(stockId, start=start_day, end=end_day)
        if df is None:
            return False
        period_high = df['high'].max()
        today_high = df.iloc[0]['high']
        if today_high >= period_high:
            return True
        else:
            return False
项目:silverstrategy    作者:silverhandy    | 项目源码 | 文件源码
def add_toprank_IR(self, stockId):
        (start_day, end_day) = self.pick_date_from_days(self.rankDays)
        #print("increase: stockId", stockId, "start_day", start_day, "end_day", end_day)
        df = ts.get_h_data(stockId, start=start_day, end=end_day)
        #print("Dataframe: \n", df)
        if df is None:
            return
        perl = toprank_perl(stockId, self.d2i)
        for i in range(0, self.rankDays):
            if i >= len(df):
                return
            IR = self.get_IR_from_df(df, i)
            perl.set_IR(i, IR)
        self.rankCrown.append(perl)
项目:silverstrategy    作者:silverhandy    | 项目源码 | 文件源码
def dummy_run(self):
        (start_day, end_day) = self.pick_date_from_days(self.rankDays)
        print("daysBefore:", self.daysBefore, "pick date:", start_day, "-", end_day)
        #df = ts.get_h_data('000063', start_day, end_day)
        #print("Dataframe: \n", df)
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def main():
    now = time.strftime("%Y-%m-%d")
    # print now
    token = '60517739976b768e07823056c6f9cb0fee33ed55a1709b3eaa14a76c6a1b7a56'
    sb = StockBox()
    # sb.looper(id)
    id = '300333'
    # sb.realtime(id)
    sb.base_function("300333")
    # pandas_test=Pandas_test()
    # pandas_test.test_function()
    # sb.longhuban('2016-04-05')
    # sb.getNews()
    # sb.fund()
    #sb.get_stock_chengfeng()
    #sb.date_store()
    #sb.profit_test()
    #sb.daily_longhu()


    # ?????? ?3????
    history = ts.get_hist_data(id)

    print u"??3????"

    print history.head(10)

    history_all = ts.get_h_data(id, '20015101', '20160101')

    print u'???????'
    print history_all



    # print type(stockInfo)
    # print stockInfo.head()
    # print stockInfo.dtypes
    #df = ts.get_stock_basics()
    #data = df.ix[id]['timeToMarket']
    #print data
    #ts.get_today_all()
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_AMOUNT_DATA_DAY(self, context, security,isLastest=True,data={},dataCount=20):
        #TODO get_k_data has no amount data, get_h_data no support index
        try:
            df_data = ts.get_h_data(security, index=False)
        except Exception,e:
            print e
            df_data = None
        if df_data is None or df_data.empty == True:
            print "security:%s in context:%s NO GET_AMOUNT_DATA_DAY!" %(str(security),str(context))
            return np.array([np.nan])
        df_data = df_data.iloc[::-1].tail(dataCount)
        amountData = df_data['amount'].values
        try:
            dfreal = ts.get_realtime_quotes(security)
        except Exception,e:
            print Exception,":",e
            dfreal = None
        if not dfreal is None:
            data = dfreal.date[0]
            time = dfreal.time[0]
            dateStr = data + ' ' + time
            current_dt = self.__getdatetime__(dateStr)
            runtime = SecurityDataSrcBase.GET_RUN_MINUTES(TsContext(current_dt))
            if runtime < 240:
                amountLast = float(dfreal.amount[0])
                amountData = np.append(amountData, amountLast)
        return amountData

    # overide
项目:tushare-data-sync    作者:HUMANAMUH    | 项目源码 | 文件源码
def fetch_history(stock, start, end):
    """
    History data forward answer authority
    """
    disable_stdout()
    with timer(logtime("ts.get_h_data('%s', autype=None, start='%s', end='%s', drop_factor=False)" % (stock, start, end))):
        df = await wait_concurrent(event_loop, proc_pool, ts.get_h_data, stock, autype=None, start=start, end=end, drop_factor=False, pause=0.05)
    if df is None:
        logging.debug("no history data for stock: ts.get_h_data('%s', autype=None, start='%s', end='%s')" % (stock, start, end))
        return
    df['stock'] = stock
    ans = df.reset_index()
    return history_buffer.proc_data(ans)
项目:tushare-data-sync    作者:HUMANAMUH    | 项目源码 | 文件源码
def fetch_history_index(code, start, end):
    disable_stdout()
    info = "ts.get_h_data('%s', start='%s', end='%s', pause=0.05, index=True)" % (code, start, end)
    with timer(logtime(info)):
        df = await wait_concurrent(event_loop, proc_pool, ts.get_h_data, code, start=start, end=end, pause=0.05, index=True)
    if df is None:
        logging.debug("no history index data for code: %s", info)
        return
    df['code'] = code
    ans = df.reset_index()
    return history_index_buffer.proc_data(ans)
项目:Book_DeepLearning_Practice    作者:wac81    | 项目源码 | 文件源码
def download_fq_data_from_tushare(code):
    '''
    ??????????????????????????????
    :param code:
    :return:
    '''
    path = './data/'
    import tushare as ts
    import datetime
    sh_index_lastday = open(path+'sh.csv', 'rb').readlines()[1:][-1]  # ???????????????????????
    # if os.path.exists(path+'stock_data/' + str(code) + '.csv'):
    #     import tushare as ts
    #
    #     f = open(path+'stock_data/' + str(code) + '.csv', 'rb').readlines()[1:]
    #     # 3years line
    #     fp_data = ts.get_h_data(str(code), start=f[-1].split(',')[0], end=f[0].split(',')[0])
    #     if fp_data is not None:
    #         fp_data.to_csv(path+"stock_data/"+str(code)+'_fq.csv')
    #         # ts.get_h_data(str(code), start=raw_dates[0], end=raw_dates[-1]).to_csv(path+"stock_data/"+str(code)+'_fq.csv')
    #         return True
    #     else:
    #         return False
    # else:
    #     return False
    years = 3
    start = datetime.datetime.today().date() + datetime.timedelta(-365 * years)
    # 3years line
    # fp_data = ts.get_h_data(str(code), start=str(start))
    fp_data = ts.get_k_data(str(code), start=str(start))
    if fp_data is not None and len(fp_data) > 1 and fp_data['date'].tolist()[-1] == sh_index_lastday.split(',')[1]:
        fp_data.to_csv(path + "stock_data/" + str(code) + '_fq.csv')
        return True
    else:
        return False
项目:stock_cn    作者:QuantFisher    | 项目源码 | 文件源码
def get_stock_daily_trading_data(code, start_date=startDate, end_date=endDate):
    code = str(code).zfill(6)
    try:
        stock_data = ts.get_h_data(str(code), start=start_date, end=end_date, autype=None)
        filename = hisDataPath + code + ".csv"
        stock_data.to_csv(filename, index=True, encoding='gbk')
        print('Get stock:', code, 'historical data done!')
    except Exception:
        print('Failed to get stock:', code, 'historical data!')
        time.sleep(30)
        get_stock_daily_trading_data(code, startDate, endDate)
项目:Python-Quant    作者:saberxxy    | 项目源码 | 文件源码
def get_data(code, start=None, end=None, index=False):
    """
    ??????
    :param code:
    :param start:
    :param end:
    :param index:
    :return:
    """
    end = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    data = ts.get_h_data(code=code, start=start, end=end, index=index, autype='None')
    return data, code
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def is_break_high(stockID, days, fast_type=True):
    end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day-2)
    days = days * 7 / 5
    #?????????
    print stockID
    start_day = end_day - datetime.timedelta(days)

    start_day = start_day.strftime("%Y-%m-%d")
    end_day = end_day.strftime("%Y-%m-%d")
    if fast_type:
        df = ts.get_h_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
    else:
        df = ts.get_hist_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
    if df is None:
        print "None len==0"
        return False
    if df.empty:
        print "%s Trading halt" % info.ix[stockID]['name'].decode('utf-8')
        return False
    period_high = df['high'].min()
    #print period_high
    curr_day = df[:1]
    today_high = curr_day.iloc[0]['high']
    #??????? .values
    #????df??1? ????.values
    #print today_high
    if today_high >= period_high:
        stock_h = []
        day = curr_day.index.values[0]

        #print curr_day
        name = info.ix[stockID]['name'].decode('utf-8')
        if fast_type:
            turnover = 0
            p_change = 0
        else:
            turnover = curr_day.iloc[0]['turnover']
            p_change = curr_day.iloc[0]['p_change']

        print day
        print stockID
        print p_change
        print turnover
        #print day
        #date=curr_day['date']
        stock_h.append(day)
        stock_h.append(stockID)
        stock_h.append(name)
        stock_h.append(p_change)
        stock_h.append(turnover)

        #print name.decode('utf-8')
        #print date
        #all_high_stock.append(stock)
        sql_db_h.store_break(stock_h)
        return True
    else:
        return False
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def is_break_low(stockID, days, fast_type=True):
    end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day-2)
    days = days * 7 / 5
    #?????????
    print stockID
    start_day = end_day - datetime.timedelta(days)

    start_day = start_day.strftime("%Y-%m-%d")
    end_day = end_day.strftime("%Y-%m-%d")
    if fast_type:
        df = ts.get_h_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
    else:
        df = ts.get_hist_data(stockID, start=start_day, end=end_day, retry_count=10, pause=10)
    if df is None:
        print "None len==0"
        return False
    if df.empty:
        print "%s Trading halt" % info.ix[stockID]['name'].decode('utf-8')
        return False
    period_low = df['low'].max()
    #print period_high
    curr_day = df[:1]
    today_low = curr_day.iloc[0]['low']
    #??????? .values
    #????df??1? ????.values
    #print today_high
    if today_low <= period_low:
        stock_l= []
        day = curr_day.index.values[0]

        #print curr_day
        name = info.ix[stockID]['name'].decode('utf-8')
        if fast_type:
            turnover = 0
            p_change = 0
        else:
            turnover = curr_day.iloc[0]['turnover']
            p_change = curr_day.iloc[0]['p_change']

        print day
        print stockID
        print p_change
        print turnover
        #print day
        #date=curr_day['date']
        stock_l.append(day)
        stock_l.append(stockID)
        stock_l.append(name)
        stock_l.append(p_change)
        stock_l.append(turnover)

        #print name.decode('utf-8')
        #print date
        #all_high_stock.append(stock)
        sql_db_l.store_break(stock_l)
        return True
    else:
        return False