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

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

项目:klineyes    作者:tenstone    | 项目源码 | 文件源码
def _cache_monthly(self, date):
        '''
        ??date???????????????True
        :param code:
        :param ktype:
        :param date:
        :return:
        '''
        print 'caching...'
        start, end = self._get_date_range_of_month(date, 'str')
        df = ts.get_hist_data(code=self.code, ktype=self.ktype, start=start, end=end, retry_count=6)
        if df is not None:
            df.to_csv(self._get_cache_filename(date))

        waiting_seconds = 0
        while not self._in_cache(date=date):
            sleep(1)
            waiting_seconds += 1
            if waiting_seconds > 30:
                self._cache_monthly(date=date)
        return True
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def plot_test():
    df = ts.get_hist_data('600415', start='2015-04-01', end='2015-06-18')
    # ???????
    df.plot()
    # ??stock???????
    df.high.plot()
    # ????????????????
    with pd.plot_params.use('x_compat', True):
        df.open.plot(color='g')
        df.close.plot(color='y')
        df.high.plot(color='r')
        df.low.plot(color='b')
    # ??????????????
    with pd.plot_params.use('x_compat', True):
        df.high.plot(color='r', figsize=(10, 4), grid='on')
        df.low.plot(color='b', figsize=(10, 4), grid='on')
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def turnover_check(self):
        delta_day = 60 * 7 / 5
        end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
        start_day = end_day - datetime.timedelta(delta_day)

        start_day = start_day.strftime("%Y-%m-%d")
        end_day = end_day.strftime("%Y-%m-%d")
        print start_day
        print end_day
        for each_code in self.all_code:
            try:
                df = ts.get_hist_data(code=each_code, start=start_day, end=end_day)
            except:
                print "Failed to get data"
                continue
            mv5 = df['v_ma5'][-1]
            mv20 = df['v_ma20'][-1]
            mv_all = df['volume'].mean()
            print

    # ??csv??
项目:BetaStock    作者:qweraqq    | 项目源码 | 文件源码
def get_date_return(dt=None, max_day_try=10):
    """
    given a date, return the change value of date dt
    :param dt: type datetime
    :param max_day_try: type int, to skip stock breaks, default 10
    :return: None if invalid, return_next_day otherwise
    """
    if type(dt) is not datetime:
        return None
    assert max_day_try >= 1, 'at least one day'

    dt1 = dt
    dt2 = dt + timedelta(days=max_day_try)
    stock_data = ts.get_hist_data('sh', start=formatDateString(dt1),
                                  end=formatDateString(dt2), retry_count=10)
    if stock_data.empty:
        return None
    return stock_data.as_matrix(['p_change'])[-1]
    # since the return value is reversed ordered
项目:zipline_cn_databundle    作者:rainx    | 项目源码 | 文件源码
def tushare_bundle(environ,
                  asset_db_writer,
                  minute_bar_writer,
                  daily_bar_writer,
                  adjustment_writer,
                  calendar,
                  start_session,
                  end_session,
                  cache,
                  show_progress,
                  output_dir):

    metadata, histories, symbol_map = get_basic_info()
    # ????????
    asset_db_writer.write(metadata)
    # ????dailybar
    daily_bar_writer.write(get_hist_data(symbol_map, histories, start_session, end_session), show_progress=show_progress)
    # ??,????, ?squant ??
    splits, dividends = zipline_splits_and_dividends(symbol_map)
    adjustment_writer.write(
        splits=pd.concat(splits, ignore_index=True),
        dividends=pd.concat(dividends, ignore_index=True),
    )
项目:Stock    作者:CloudGao    | 项目源码 | 文件源码
def get_stock(stock_list,start,end,type):
    while len(stock_list)>0:
        stock_code=stock_list.pop()
        df = tushare.get_hist_data(code=stock_code,start=start,end=end,ktype=type)
        if(type == 'D'):
            stock_code = 'D_'+stock_code
        elif(type == 'W'):
            stock_code = 'W_'+stock_code
        elif(type == 'M'):
            stock_code = 'M_'+stock_code
        elif(type == '5'):
            stock_code = '5_'+stock_code
        elif(type == '15'):
            stock_code = '15_'+stock_code
        elif(type == '30'):
            stock_code = '30_'+stock_code
        elif(type == '60'):
            stock_code = '60_'+stock_code
        df.to_csv('E:/Project/Python/STOCK/DataSpider/DataInit/download/'+stock_code+'.csv')
        print("save "+stock_code)

#???????????????
#????????????????
项目:Stock    作者:CloudGao    | 项目源码 | 文件源码
def get_stock(stock_list,start,end,type):
    while len(stock_list)>0:
        stock_code=stock_list.pop()
        df = tushare.get_hist_data(code=stock_code,start=start,end=end,ktype=type)
        if(type == 'D'):
            stock_code = 'D_'+stock_code
        elif(type == 'W'):
            stock_code = 'W_'+stock_code
        elif(type == 'M'):
            stock_code = 'M_'+stock_code
        elif(type == '5'):
            stock_code = '5_'+stock_code
        elif(type == '15'):
            stock_code = '15_'+stock_code
        elif(type == '30'):
            stock_code = '30_'+stock_code
        elif(type == '60'):
            stock_code = '60_'+stock_code
        df.to_csv('E:/Project/Python/STOCK/DataSpider/DataUpdate/daily_update_d_download2/'+stock_code+'.csv')
        print("save "+stock_code)

#???????????????
#????????????????
项目:Book_DeepLearning_Practice    作者:wac81    | 项目源码 | 文件源码
def download_economy():
    import tushare as ts
    path = './data/'
    ts.get_money_supply().to_csv(path+'money_supply.csv')
    ts.get_gdp_quarter().to_csv(path+'gdp_quarter.csv')
    ts.get_gdp_year().to_csv(path + 'gdp_year.csv')
    ts.get_cpi().to_csv(path+'cpi.csv')
    # ts.get_hist_data('sz').to_csv(path + 'sz.csv')
    # ts.get_hist_data('sh').to_csv(path + 'sh.csv')

    # import time
    import datetime
    # now_year = time.localtime().tm_year
    # now_mon = time.localtime().tm_mon
    # now_day = time.localtime().tm_mday
    years = 3
    start = datetime.datetime.today().date() + datetime.timedelta(-365*years)
    end = datetime.datetime.today().date()
    ts.get_k_data('399001',  start=str(start), index=True).to_csv(path + 'sz.csv')  #??2? ,
    ts.get_k_data('000001',  start=str(start), index=True).to_csv(path + 'sh.csv')
    #??????
    ts.get_rrr().to_csv(path + 'rrr.csv')
项目:Investment-PythonModule    作者:tommyxu97    | 项目源码 | 文件源码
def index_model(self):
        """
        Build a asset protfolio using index model.
        """
        hs300_data = ts.get_hist_data('hs300', self.start, self.end)
        self.stockdata['hs300'] = hs300_data['close']
        for stock_single in self.stocklist:
            self.stockdata[stock_single] = ts.get_hist_data(stock_single, self.start, self.end)['close']
        returns = (self.stockdata/self.stockdata.shift(1))-1





        print('This function will be completed later!')

        # To be continued
项目:TuShare    作者:andyzsf    | 项目源码 | 文件源码
def csv():
    df = ts.get_hist_data('000875')
    df.to_csv('c:/day/000875.csv',columns=['open','high','low','close'])
项目:TuShare    作者:andyzsf    | 项目源码 | 文件源码
def xls():
    df = ts.get_hist_data('000875')
    #????
    df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)
项目:TuShare    作者:andyzsf    | 项目源码 | 文件源码
def hdf():
    df = ts.get_hist_data('000875')
#     df.to_hdf('c:/day/store.h5','table')

    store = HDFStore('c:/day/store.h5')
    store['000875'] = df
    store.close()
项目:TuShare    作者:andyzsf    | 项目源码 | 文件源码
def json():
    df = ts.get_hist_data('000875')
    df.to_json('c:/day/000875.json',orient='records')

    #??????
    print(df.to_json(orient='records'))
项目:TuShare    作者:andyzsf    | 项目源码 | 文件源码
def appends():
    filename = 'c:/day/bigfile.csv'
    for code in ['000875', '600848', '000981']:
        df = ts.get_hist_data(code)
        if os.path.exists(filename):
            df.to_csv(filename, mode='a', header=None)
        else:
            df.to_csv(filename)
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def check_type():
    df = ts.get_hist_data('300141', start=day30, end=day0)
    print df.dtypes
    print df.index
    t1 = df.iloc[0]
    print type(t1)

    t2 = df[:1]
    print type(t2)
    print t2.index.values[0]
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def empty_type():
    id = "300527"
    df = ts.get_hist_data(id)
    print type(df)
    if df is None:
        print "None"
    else:
        print "Not Empty"
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def exception_test():
    #???????


    stockid = '002316'
    df = ts.get_hist_data(stockid, start='20160601', end='20160701')
    if df.empty:
        print "empty"
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def plot_test2():
    fig = matplotlib.pyplot.gcf()
    df = ts.get_hist_data('600415', start='2015-04-01', end='2015-06-18')
    with pd.plot_params.use('x_compat', True):
        df.high.plot(color='r', figsize=(10, 4), grid='on')
        df.low.plot(color='b', figsize=(10, 4), grid='on')
        fig.savefig('graph.png')
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def get_volume():
    code = '600874'
    df = ts.get_hist_data(code=code, start='2017-01-01')
    vol = df['ma20']
    print vol
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def two_in_one_canvas():
    #fig,ax=plt.subplots(211)
    df=ts.get_hist_data('300333','2015-01-01','2016-12-30')
    closed=df.close
    vol=df.volume/10000
    print closed
    print vol
    #closed.plot()
    closed.plot()
    vol.plot()
    plt.show()
项目:quantdigger    作者:andyzsf    | 项目源码 | 文件源码
def load_data(self, pcontract, dt_start=None, dt_end=None):
        """ ??????????.

        Args:
            pcontract (PContract): ????

        Returns:
            DataFrame. 

        Raises:
            FileDoesNotExist
        """
        if pcontract.contract.exch_type == 'stock':
            import tushare as ts
            # ??tushare??
            print "load stock data with tushare..." 
            data = ts.get_hist_data(pcontract.contract.code)
            return process_tushare_data(data)
        else:
            # ????
            fname = ''.join([str(pcontract), ".csv"])
            try:
                data = pd.read_csv(fname, index_col=0, parse_dates=True)
                assert data.index.is_unique
            except Exception:
                #print u"**Warning: File \"%s\" doesn't exist!"%fname
                raise FileDoesNotExist(file=fname)
            else:
                return data
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def getHistory(self,id):
        data=ts.get_hist_data(id)
        print data
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
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
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def base_function(self, id):
        if id == None:
            print "Input stock id please "
            return

        stockInfo = ts.get_hist_data(id)
        # print type(stockInfo)
        # print stockInfo.head()
        # print stockInfo.dtypes
        df = ts.get_stock_basics()
        data = df.ix[id]['timeToMarket']
        print data
        all_data = ts.get_today_all()
        print all_data.ix[id]['name']
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def date_store(self):
        df = ts.get_hist_data("300333")
        # df.to_csv("300333.cvs")
        df.to_excel("300333.xlsx")
项目: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()
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def _break_line(self, codes, k_type):
        delta_day = 60 * 7 / 5
        end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
        start_day = end_day - datetime.timedelta(delta_day)

        start_day = start_day.strftime("%Y-%m-%d")
        end_day = end_day.strftime("%Y-%m-%d")
        print start_day
        print end_day
        all_break = []

        for i in codes:
            try:
                df = ts.get_hist_data(code=i, start=start_day, end=end_day)
                if len(df)==0:
                    continue
            except Exception,e:
                print e
                continue
            else:
                self.working_count=self.working_count+1
                current = df['close'][0]
                ma5 = df['ma5'][0]
                ma10 = df['ma10'][0]
                ma20 = df['ma20'][0]
                ma_dict = {'5': ma5, '10': ma10, '20': ma20}
                ma_x = ma_dict[k_type]
                #print ma_x
                if current < ma_x:
                    print u'??'
                    print i, " current: ", current
                    print self.base[self.base['code'] == i]['name'].values[0], " "
                    print "holding place: " , ma_x
                    print "Break MA", k_type, "\n"
                    all_break.append(i)
        return all_break

    # ????????????????
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def _break_line_thread(self,codes,k_type='5'):
        delta_day = 60 * 7 / 5
        end_day = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
        start_day = end_day - datetime.timedelta(delta_day)

        start_day = start_day.strftime("%Y-%m-%d")
        end_day = end_day.strftime("%Y-%m-%d")
        print start_day
        print end_day
        all_break = []
        for i in codes:
            try:
                df = ts.get_hist_data(code=i, start=start_day, end=end_day)
                if len(df)==0:
                    continue
            except Exception,e:
                print e
                continue
            else:
                self.working_count=self.working_count+1
                current = df['close'][0]
                ma5 = df['ma5'][0]
                ma10 = df['ma10'][0]
                ma20 = df['ma20'][0]
                ma_dict = {'5': ma5, '10': ma10, '20': ma20}
                ma_x = ma_dict[k_type]
                #print ma_x
                if current > ma_x:
                    print i, " current: ", current
                    print self.base[self.base['code'] == i]['name'].values[0], " "

                    print "Break MA", k_type, "\n"
                    all_break.append(i)
        q.put(all_break)
项目:Data_Analysis    作者:crown-prince    | 项目源码 | 文件源码
def stock():
     #?????????????????, ?????????
    stock_list = {"zsyh":"600036","jsyh":"601939","szzs":"000001","pfyh":"600000","msyh":"600061"}
    for stock, code in stock_list.items():
        globals()[stock] = tsh.get_hist_data(code,start="2015-01-01",end="2016-04-16")
    stock_list2 = stock_list.keys()
    #print(stock_list2)
    sl = [globals()[st]["close"] for st in stock_list2]
    df_close = pd.concat(sl,axis=1,join='inner')
    df_close.columns = stock_list2
    #print(df_close)
    df_close.sort_index(ascending=True,inplace=True) #ascending ??????????????????
    pc_ret = df_close.pct_change() #????????????????
    print(pc_ret)
    make_end_line()
    print(pc_ret.mean())
    make_end_line()
    #????????????
    plt.show(sns.jointplot("zsyh","jsyh",pc_ret,kind="hex")) #?? ????????1?????????? 0????? -1????????
    plt.show(sns.jointplot("zsyh","jsyh",pc_ret,kind="scatter"))
    plt.show(sns.jointplot("zsyh","szzs",pc_ret,kind="scatter"))
    plt.show(sns.pairplot(pc_ret[["jsyh","zsyh","pfyh","msyh"]].dropna())) #??????????
    print(pc_ret.std()) #????????????????????????????
    make_end_line()
    rets = pc_ret.dropna()
    print(rets.mean())
    make_end_line()
    area = np.pi *20 #????
    plt.scatter(rets.mean(),rets.std())    #???rets?????????xy? 
    plt.xlabel("Expected Return")#????xy????
    plt.ylabel("Risk")
    for label,x,y in zip(rets.columns,rets.mean(),rets.std()):
        plt.annotate(
            label,
            xy = (x,y),xytext = (50,50),
            textcoords = "offset points",ha = "right",va = "bottom",
            arrowprops = dict(arrowstyle = "-",connectionstyle = "arc3,rad=-0.3"))
    plt.show()
项目:mt4plus    作者:flameOnYou    | 项目源码 | 文件源码
def get(self):
        symbol = self.get_argument("symbol")#????
        period = self.get_argument("period")#????,??-??
        period_allow_list = ["5","15","30","60","1440","M","W"]
        if period not in period_allow_list:
            return 

        data = None
        if period =="1440":
            data = ts.get_hist_data(symbol)
        else:
            data = ts.get_k_data(symbol,ktype=period)
        print "=========",symbol,":",period
        resultlist = []
        lens = len(data)
        for unit in data.iterrows():
            obj  = {}
            dates = None
            if period =="1440":
                dates = unit[1].name
            else:
                dates = unit[1]['date']
            print "len",len(dates)
#             ????10??%Y-%m-%d??,16??%Y-%m-%d %H:%M ??
            dataformate = "%Y-%m-%d %H:%M"
            date_len = len(dates)
            if date_len == 10 :
                dataformate = "%Y-%m-%d"
            d=datetime.datetime.strptime(dates,dataformate)
            obj["date"]=int(time.mktime(d.timetuple()))
            obj["open"]=unit[1]['open']
            obj["close"]=unit[1]['close']
            obj["high"]=unit[1]['high']
            obj["low"]=unit[1]['low']
            obj["volume"]=unit[1]['volume']
            resultlist.append(obj)

        resultlist.sort(key=lambda obj:obj.get('date'), reverse=False) 

        s = json.dumps(resultlist)
        self.write(s)
项目:stock    作者:pythonstock    | 项目源码 | 文件源码
def get_hist_data_cache(code, date_start, date_end):
    cache_file = bash_stock_tmp + "%s^%s.gzip.pickle" % (date_end, code)
    # ?????????????????????
    if os.path.isfile(cache_file):
        print("######### read from cache #########", cache_file)
        return pd.read_pickle(cache_file, compression="gzip")
    else:
        stock = ts.get_hist_data(code, start=date_start, end=date_end)
        stock = stock.sort_index(0)  # ???????????
        stock.to_pickle(cache_file, compression="gzip")
        return stock
项目:glue.cal    作者:flameOnYou    | 项目源码 | 文件源码
def getDateFromMarket(id):
    price = {}
    # 002060
    data = ts.get_hist_data(id)
    for p in data.iterrows():
        # high
        high = float(p[1]['high'])
        # low
        low = float(p[1]['low'])
        price[p[0].encode("utf-8")] = (high + low) / 2
    return price
项目:glue.cal    作者:flameOnYou    | 项目源码 | 文件源码
def getDateFromMarket(id):
    price = {} 
    # 002060
    data = ts.get_hist_data(id)
    for p in data.iterrows():
        # high
        high = float(p[1]['high'])
        # low
        low = float(p[1]['low'])
        price[p[0].encode("utf-8")] = (high + low) / 2
    return price
项目:zipline_cn_databundle    作者:rainx    | 项目源码 | 文件源码
def get_basic_info(show_progress=True):
    # ?????
    if show_progress:
        click.echo("????????")
    ts_symbols = ts.get_stock_basics()
    if show_progress:
        click.echo("??????")

    symbols = []

    histories = {}

    # ??????
    i = 0
    total = len(ts_symbols)
    for index, row in ts_symbols.iterrows():
        i = i +1
        if i > 10:
            break

        srow = {}
        # ????????
        click.echo("??????%s(%s)??????? (%d/%d)" % (index, row['name'], i, total))
        histories[index] = ts.get_hist_data(index)
        srow['start_date'] = histories[index].index[-1]
        srow['end_date'] = histories[index].index[0]
        srow['symbol'] = index
        srow['asset_name'] = row['name']
        symbols.append(srow)

    df_symbols = pd.DataFrame(data=symbols).sort_values('symbol')
    symbol_map = pd.DataFrame.copy(df_symbols.symbol)

    # fix the symbol exchange info
    df = df_symbols.apply(func=convert_symbol_series, axis=1)


    return df, histories, symbol_map
项目:zipline_cn_databundle    作者:rainx    | 项目源码 | 文件源码
def get_hist_data(symbol_map, histories, start_session, end_session):
    for sid, index in symbol_map.iteritems():
        history = histories[index]

        """
        writer needs format with
        [index], open, close, high, low, volume

        so we do not need to change the format from tushare

        but we need resort it
        """
        yield sid, history.sort_index()
项目:python_data_tools    作者:king3366ster    | 项目源码 | 文件源码
def get_hist_data(self, code, start = None, end = None, ktype='D'):
        if end is None:
            end = dataTime.datetimeRelative(delta = 1)
        if start is None:
            start = dataTime.datetimeRelative(base = end, delta = -20)
        df = ts.get_hist_data(code, start, end, ktype)
        df = self.format_date_index(df)
        df = df.sort_values(['date'], ascending=[True])
        return df

# ????
    # ?????
        # code:string,???? e.g. 600848
        # start:string,???? format?YYYY-MM-DD ????????
        # end:string,???? format?YYYY-MM-DD ????????
        # autype:string,?????qfq-??? hfq-??? None-???????qfq
        # index:Boolean????????????False
        # retry_count : int, ??3,??????????????
        # pause : int, ?? 0,??????????????????????????????

    # ??????
        # date : ???? (index)
        # open : ???
        # high : ???
        # close : ???
        # low : ???
        # volume : ???
        # amount : ????
项目:quant    作者:yutiansut    | 项目源码 | 文件源码
def QA_fetch_get_stock_day(name, startDate=None, endDate=None):
    if (len(name) != 6):
        name = str(name)[0:6]
    data = QATs.get_hist_data(str(name), startDate, endDate).sort_index()

    data_json = json.loads(data.to_json(orient='records'))

    for j in range(0, len(data_json), 1):
        data_json[j]['date_stamp'] = QA_util_date_stamp(
            list(data.index)[j])
        data_json[j]['date'] = list(data.index)[j]
        data_json[j]['code'] = str(name)

    return data_json
项目:Book_DeepLearning_Practice    作者:wac81    | 项目源码 | 文件源码
def get_open_close_hist_price(code, start_date=None, end_date=None):
    '''
    :param code: ????
    :param date: ??????
    :return: ?????open close ???
    '''
    import tushare as ts
    if start_date != None and end_date != None:

        if type(code) is not str or type(start_date) is not str:
            code = str(code)
            start_date = str(start_date)
            end_date = str(end_date)



        df = ts.get_hist_data(code, start_date,end_date)
        df = df.head(1)
        dtime = df.set_index('time')
        price = dtime['price']
        if price.shape == (0,):
            print code, "can't get ", start_date, "am_pm data!"
            return float('nan'), float('nan')
        else:
            return price[-1], price[len(df.time)/4]

    else:
        df = ts.get_hist_data(code)
        df = df.head(1)
        dtime = df.set_index('time')
        price = dtime['price']
        if price.shape == (0,):
            print code, "can't get ", start_date, "am_pm data!"
            return float('nan'), float('nan')
        else:
            return price[-1], price[len(df.time)/4]
项目:Book_DeepLearning_Practice    作者:wac81    | 项目源码 | 文件源码
def download_from_tushare(code):
    '''
    #??????,????1:?????
    month :????
    m2 :???????????M2?(??)
    m2_yoy:???????????M2?????(%)
    m1:??(????M1)(??)
    m1_yoy:??(????M1)????(%)
    m0:?????(M0)(??)
    m0_yoy:?????(M0)????(%)
    cd:????(??)
    cd_yoy:????????(%)
    qm:???(??)
    qm_yoy:???????(%)
    ftd:????(??)
    ftd_yoy:????????(%)
    sd:????(??)
    sd_yoy:????????(%)
    rests:????(??)
    rests_yoy:????????(%)
    '''
    import tushare as ts
    path = './data/'

    # 1 day line
    hist_data = ts.get_hist_data(str(code))
    if hist_data is not None:
        hist_data.to_csv(path+"stock_data/"+str(code)+'.csv')
        return True
    else:
        return False
    # 30 day lines
    # ts.get_hist_data(str(code), ktype='M').to_csv(path+"stock_data/"+str(code)+'_month.csv')
项目:Investment-PythonModule    作者:tommyxu97    | 项目源码 | 文件源码
def data_to_csv(self, path_input):
        """
        Output the data of your asset portfolio with the format '.csv' to the place you want.
        """
        try:
            for i in range(len(self.stocklist)):
                self.stockdata[self.stocklist[i]]
        except:
            hs300_data = ts.get_hist_data('hs300', self.start, self.end)
            self.stockdata['hs300'] = hs300_data['close']
            for stock_single in self.stocklist:
                self.stockdata[stock_single] = ts.get_hist_data(stock_single, self.start, self.end)['close']             
        data = self.stockdata
        data.to_csv(path_input+'data.csv')
        print("The file has been generated at ", path_input, ".")
项目:Investment-PythonModule    作者:tommyxu97    | 项目源码 | 文件源码
def profile(self, path_input=None):
        """
        Calculate the profile made by your portfolio. It will give out the number of money you made, instead of pecentage.
        """
        profile = []
        profileratio = []
        sum_of_profile = 0
        for stock_single in self.stocklist:
            print("Now processing:", stock_single)
            self.stockdata[stock_single] = ts.get_hist_data(stock_single, self.start, self.end)['close']
            profileratio.append((self.stockdata[stock_single].loc[self.end]-self.stockdata[stock_single].loc[self.start])/self.stockdata[stock_single].loc[self.start])
        for i in range(0, len(self.stocklist)):
            profile.append(self.moneyallocation[i] * float(profileratio[i]))
            if math.isnan(profile[i]) != True:
                sum_of_profile = profile[i] + sum_of_profile
            else:
                pass
        for i in range(0, len(profile)):
            print(self.stocklist[i], profile[i],"\n")
        print("Totel profile is", sum_of_profile)
        # Update: 2017-08-12
        try:
            if path_input != None:
                _profile_stock_list = self.stocklist.append('Totel profile')
                _profile_single_stock = profile.append(sum_of_profile)
                _profile_data = {'List':_profile_stock_list, 'Profile':_profile_stock_list}
                _profile = pd.DataFrame(_profile_data)
                _profile.to_csv(path_input+'profile.csv')
            elif self.defaultpath:
                _profile_stock_list = self.stocklist.append('Totel profile')
                _profile_single_stock = profile.append(sum_of_profile)
                _profile_data = {'List':_profile_stock_list, 'Profile':_profile_stock_list}
                _profile = pd.DataFrame(_profile_data)
                _profile.to_csv(self.defaultpath+'profile.csv')
            else:
                pass
        except IOError:
            print('Path not exists!')
项目:BlackCoffee    作者:IMYin    | 项目源码 | 文件源码
def trade_data(code):
    data = ts.get_hist_data(code, end=today_str_Ymd, retry_count=10)
    data[u'date'] = data.index
    data[u'code'] = code

    data = data.sort_index(ascending=True)
    return data
项目:DataAnalysis    作者:IMYin    | 项目源码 | 文件源码
def Get_TA(df_Code,Dist):
    operate_array1=[]
    operate_array2=[]
    operate_array3=[]

    count = 0
    for code in df_Code.index:
# index,0 - 6 date??? open???? high???? close???? low???? volume???? price_change????? p_change????
# 7-12 ma5?5??? ma10?10??? ma20:20??? v_ma5:5???v_ma10:10??? v_ma20:20???
        df = ts.get_hist_data(code,start='2014-11-20')
        dflen = df.shape[0]
        count = count + 1       
        if dflen>35:
            try:
                (df,operate1) = Get_MACD(df) 
                (df,operate2) = Get_KDJ(df)
                (df,operate3) = Get_RSI(df)
            except Exception, e:
                 Write_Blog(e,Dist)
                 pass
        operate_array1.append(operate1)  #round(df.iat[(dflen-1),16],2)
        operate_array2.append(operate2)
        operate_array3.append(operate3)
        if count == 0:
            Write_Blog(str(count),Dist)
    df_Code['MACD']=pd.Series(operate_array1,index=df_Code.index)
    df_Code['KDJ']=pd.Series(operate_array2,index=df_Code.index)
    df_Code['RSI']=pd.Series(operate_array3,index=df_Code.index)
    return df_Code

#??MACD??????
项目:Python-Quant    作者:saberxxy    | 项目源码 | 文件源码
def getSzzs():
    try:
        data = ts.get_hist_data(code='000001', start='2016-01-01', end='2016-12-31')
        # print (data)
        number = '000001'
        name = '????'
        date = list(data.index)  # ??
        open = list(data.open)  # ???
        high = list(data.high)  # ???
        close = list(data.close)  # ???
        low = list(data.low)  # ???
        change = list(data.p_change)  # ???
        i = '000001_????_org'

        cur1 = conn.cursor()
        cur1.execute("DROP TABLE IF EXISTS %s;" % (i))  # ??????????
        cur1.execute("""create table %s
                        (b_id int comment '??',
                        b_index varchar(100) comment '????',
                        b_name varchar(100) comment '????',
                        b_date date comment '??',
                        b_open float comment '???',
                        b_high float comment '???',
                        b_close float comment '???',
                        b_low float comment '???',
                        b_change float comment '???',
                        PRIMARY key(b_id));""" % (i))
        for j in range(0, len(date)):
            # print (j + 1, number, name, date[j], open[j], high[j], close[j], low[j], change[j])
            cur1.execute("INSERT INTO %s(b_id, b_index, b_name, b_date, b_open, b_high, b_close, b_low, b_change) "
                         "VALUES('%d', '%s', '%s', '%s', '%f', '%f', '%f', '%f', '%f');"
                         % (i, j + 1, number, name, date[j], open[j], high[j], close[j], low[j], change[j]))
        cur1.execute("commit;")
    except Exception:
        pass
项目: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
项目:Data_Analysis    作者:crown-prince    | 项目源码 | 文件源码
def main():
    #?????????????????, ?????????
    stock_list = {"zsyh":"600036","jsyh":"601939","szzs":"000001","pfyh":"600000","msyh":"600061"}

    for stock, code in stock_list.items():
        globals()[stock] = tsh.get_hist_data(code,start="2015-01-01",end="2016-04-16")
        #code:?????start:?????end:????
    #print(zsyh) #???????????
    make_end_line()
    print(zsyh.head())
    make_end_line()
    print(zsyh.columns)
    make_end_line()
    """
    ????

    date???
    open????
    high????
    close????
    low????
    volume????
    price_change?????
    p_change????
    ma5?5???
    ma10?10???
    ma20: 20???
    v_ma5: 5???
    v_ma10: 10???
    v_ma20: 20???
    turnover:???[???????]
    """
    print(zsyh.describe())
    make_end_line()
    print(zsyh.info())
    make_end_line()
    plt.show(zsyh["close"].plot(figsize=(12,8))) #???????????
    #pd.set_option("display.float_format", lambda x: "%10.3f" % x) 
    plt.show(zsyh["volume"].plot(figsize=(12,8)))
    zsyh[["close","ma5","ma10","ma20"]].plot(subplots = True)
    plt.show()
    plt.show(zsyh[["close","ma5","ma10","ma20"]].plot(figsize=(12,8),linewidth=2))
    plt.show(zsyh["p_change"].plot())
    plt.show(zsyh["p_change"].plot(figsize=(10,4),legend=True,linestyle="--",marker="o"))
    #???????????
    plt.show(zsyh["p_change"].hist(bins=20))
    plt.show(zsyh["p_change"].plot.kde()) #?????
                                          #?????(kernel density estimation)?????????????????
    plt.show(sns.kdeplot(zsyh["p_change"].dropna()))
    plt.show(sns.distplot(zsyh["p_change"].dropna())) #??????????????????????