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

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

项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
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']


#get_all_stock_id()
#check_type()
#news()
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def boll():
    #??tushare??????
    df=ts.get_k_data('300580',start='2017-01-12',end='2017-05-26')
    #?????
    closed=df['close'].values

    upper,middle,lower=talib.BBANDS(closed,matype=talib.MA_Type.SMA)
    print upper,middle,lower
    plt.plot(upper)
    plt.plot(middle)
    plt.plot(lower)
    plt.grid()
    plt.show()
    diff1=upper-middle
    diff2=middle-lower
    print diff1
    print diff2
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def get_stick_hisdata_w(self,begin_date,end_date,all='YES'):

        if all == 'YES':
            print('all stock ...........')
            self.cur.execute(
                "select code,outstanding from t_all_stickcode ;")
        else:
            print('part stock ........................')
            self.cur.execute("select code,outstanding from t_all_stickcode where outstanding <  50000 and (substr(code,1,1)='0' or substr(code,1,1)='6'); ;")
        #self.cur.execute("select code,outstanding from t_all_stickcode  a where not EXISTS (select * from t_stick_data_w where code = a.code);")
        sqlrs = self.cur.fetchall();
        # self.cur.execute("delete from t_stick_data_w")
        #self.sqlconn.commit()
        for code in sqlrs:
            print(code[0]+'_w' ,begin_date,end_date)
            try:
                rs = ts.get_k_data(code=code[0],start=begin_date,end=end_date,ktype='W')
            except Exception as e:
                print('ERR:',e)
            self.db.delete_date('t_stick_data_w',begin_date,code[0])
            self.db.insert_data('t_stick_data_w',rs.as_matrix())
            self.db.commit()
        return
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def get_stick_hisdata_d(self,begin_date,end_date):

        # cur.execute("select code,outstanding from t_all_stickcode where outstanding <  50000 and (substr(code,1,1)='0' or substr(code,1,1)='6'); ;")
        self.cur.execute("select code,outstanding from t_all_stickcode  ;")
        sqlrs = self.cur.fetchall();
        # self.cur.execute("delete from t_stick_data_d")
        self.sqlconn.commit()
        for code in sqlrs:
            print(code[0]+'_d' )
            try:
                rs = ts.get_k_data(code=code[0],start=begin_date,end=end_date,ktype='D')
            except Exception as e:
                print('ERR:',e)
                continue
            # rs.to_sql('t_stick_data_d',self.engine,if_exists='replace',index=False);
            self.db.delete_date('t_stick_data_d',end_date,code[0])
            self.db.insert_data('t_stick_data_d',rs.as_matrix())
        self.sqlconn.commit()
        return
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def get_little_stock(self):
        self.cur.execute("select code from t_all_stickcode where liquidassets <  70000  and liquidassets >  60000 and (substr(code,1,1)='0' or substr(code,1,1)='6') ;")
        rs = self.cur.fetchall();
        return rs

# enddate = datetime.date.today()
# begindate = datetime.date.today() - datetime.timedelta(days=336)
#
# ld = load()
# ld.get_all_stick_inf()
# ld.get_stick_hisdata_w(begindate.strftime('%Y-%m-%d'),enddate.strftime('%Y-%m-%d'),all='YES')

# ld.get_stick_hisdata_w()
#get_stick_hisdata_d()
#get_stick_hisdata_w()
#get_stick_hisdata_m()
#get_stick_hisdata_add_m()
# rs = ts.get_k_data(code='600848', start='2016-06-01', end='2017-01-01', ktype='w')
# print(rs)

# cf = configparser.ConfigParser()
# cf.read('stock.init')
# usr = cf.get('db','db_user')
# pwd = cf.get('db', 'db_pass')
# print(usr,pwd)
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
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)
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
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
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def caculation(self):
            df_t=ts.get_today_all()
            print self.df[u'??'].values
            for i in self.df[u'??'].values:
                name=self.base[self.base['code']==i]['name'].values[0]
                print name
                t=name.decode('utf-8')
                print
                print type(t)
                #print chardet.detect(t)
                self.df.ix[self.df[u'??']==i,u'????']=self.today
                #t=ts.get_k_data(i)

                pchange=df_t.ix[df_t['code']==i,'changepercent'].values[0]
                print pchange
                self.df.ix[self.df[u'??']==i,u'????']=pchange
                current=df_t[df_t['code']==i]['trade'].values[0]
                self.df.ix[self.df[u'??']==i,u'????']=current
                current_profit=(current-self.df[self.df[u'??']==i][u'????'].values[0])/self.df[self.df[u'??']==i][u'????'].values[0]
                self.df.ix[self.df[u'??']==i,u'????']=round(current_profit*100,2)
                print current_profit
            print self.df
            self.df.to_excel(self.name,encoding='utf-8')
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
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
        '''
        sz_index=ts.get_k_data('399001',index=True)
        sz=sz_index['close'].values
        print sz

        cy_index=ts.get_k_data('399006',index=True)
        s1=Series(sh)
        s2=Series(sz)
        print s1.corr(s2)
        '''
    #?????????????st
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def getTotal():
    path=os.path.join(os.getcwd(),'data')
    os.chdir(path)

    all=pd.read_csv('bases.csv',dtype={'code':np.str})
    #print all

    all_code=all['code'].values
    #print all_code

    lists=[]
    for i in all_code:
        df=ts.get_k_data(i,start='2017-07-17',end='2017-07-17')
        lists.append(df)

    all_df=pd.DataFrame(lists)
    print all_df
    all_df.to_csv('2017-all.csv',encoding='gbk')
    all_df.to_excel('2017-excel.xls')
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def getSingleStockByTime(symbol, from_date, till_date):
    start = from_date.split('-')
    start_y, start_m, start_d = start[0], start[1], start[2] # starting date

    end = till_date.split('-')
    end_y, end_m, end_d = end[0], end[1], end[2] # until now

    repeat_times = 1
    message = ""
    df = pd.DataFrame()

    for _ in range(repeat_times): 
        try:
            data = ts.get_k_data(symbol, ktype='W')
            data.sort_index(ascending=True, inplace=True)
            return data, ""
        except Exception as e:
            message = symbol + " fetch exception: " + str(e)
            continue   
    return df, message
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def getSingleStockByTime(symbol, from_date, till_date):
    start = from_date.split('-')
    start_y, start_m, start_d = start[0], start[1], start[2] # starting date

    end = till_date.split('-')
    end_y, end_m, end_d = end[0], end[1], end[2] # until now

    repeat_times = 1
    message = ""
    df = pd.DataFrame()

    for _ in range(repeat_times): 
        try:
            data = ts.get_k_data(symbol)#, from_date, till_date)
            data.sort_index(ascending=True, inplace=True)
            return data, ""
        except Exception as e:
            message = symbol + " fetch exception: " + str(e)
            continue   
    return df, message
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_OPEN_DAY(self, context, security, ref=0):
        if ref==0:
            dfreal = ts.get_realtime_quotes(security)
            if dfreal.empty == True:
                return np.nan
            return float(dfreal['open'][0])
        else:
            dataCount =  ref + 1
            df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
            if df_data.empty == True:
                print "security:%s in context:%s NO GET_OPEN_DAY!" %(str(security),str(context))
                return np.nan
            if len(df_data['open']) < ref:
                return np.nan
            return df_data['open'].values[-ref]

    # ???????????
项目: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')
项目:QUANTAXIS    作者:yutiansut    | 项目源码 | 文件源码
def query_k(code):
    data = json.loads(ts.get_k_data(code).to_json(orient='records'))

    return jsonify(data)
项目:QUANTAXIS    作者:yutiansut    | 项目源码 | 文件源码
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
项目:timeTrader    作者:orxg    | 项目源码 | 文件源码
def _get_backtest_calendar(self):
        '''
        ???????
        ????????tushare???

        Return
        -------
            list,????????[u'2016-01-04',...,u'2016-06-17']
        '''
        data = ts.get_k_data('000001',self.start_date,
                             self.end_date,
                         index = True)
        data = data['date'].tolist()
        return data
项目:timeTrader    作者:orxg    | 项目源码 | 文件源码
def update_tradedate_calendar():
    '''
    ??MySQL??????????
    '''
    start_date = '1990-01-01'
    end_date = dt.date.today().strftime('%Y-%m-%d')
    tradedates = ts.get_k_data('000001',start = start_date,
                               end = end_date,
                               index = True)[['date']]

    tradedates.date = pd.to_datetime(tradedates.date)
    now = dt.date.today()
    tradedates['last_updated_time'] = now

    con = pymysql.connect(
            db_host,
            db_user,
            db_pass,
            db_name,
            charset = 'utf8')

    with con:
        tradedates.to_sql('tradedates',con,flavor = 'mysql',
                          if_exists = 'replace')

    print('??????????tradedates')
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def new_api():
    data = ts.get_k_data('300333')
    print data


#????????
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
def get_index():
    df = ts.get_k_data(code='000001', index=True, start='2017-03-01')
    print df
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
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()
项目:base_function    作者:Rockyzsu    | 项目源码 | 文件源码
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()
项目:flats    作者:librae8226    | 项目源码 | 文件源码
def __get_k_data_of_last_trade_day(code):
    d = datetime.now()
    k = None
    while True:
    k = ts.get_k_data(code, ktype='M', start=d.strftime("%Y-%m-%d"), end=d.strftime("%Y-%m-%d"))
        if (len(k) > 0):
            break
        else:
            d = d + timedelta(days = -1)
    return k, d
项目:flats    作者:librae8226    | 项目源码 | 文件源码
def __get_pb_and_bvps(code, quarter):
    ''' get pb of spbcific quarter
    args: code, quarter(e.g. 2015q3)
    '''
    r = {}
    bvps = 0
    y = quarter.split('q')[0]
    q = quarter.split('q')[1]
    q_str = 'code==' + '\"' + code + '\"'

    r[quarter] = __pd_read_report(quarter)

    if (len(r[quarter].query(q_str)) > 0):
        bvps = r[quarter].query(q_str).bvps.values[0]
    else:
        log.warn('no entry in %s (bvps)', quarter)
        return False, False

    s, e = __quarter_to_date(quarter)
    k = ts.get_k_data(code, ktype='M', start=s, end=e)
    if (len(k) == 0):
        log.warn('no k data entry in %s', quarter)
        return False, False
    pps = k.loc[k.last_valid_index()].close
    log.debug('%s, price: %.2f', e, pps)
    log.debug('bvps: %.2f', bvps)
    pb = round(pps/bvps, 2)
    log.debug('pb: %.2f', pb)

    return pb, bvps
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def wangge():
    df = ts.get_k_data('002321',ktype='D')
    return df
#data_complete()
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def test2():
    rs = ts.get_k_data(code='600275', start='2016-12-01', end='2016-12-14', ktype='W')
    print(rs)
项目:Stock    作者:liuguoyaolgy    | 项目源码 | 文件源码
def get_stick_hisdata_m(self,begin_date,end_date):

        # cur.execute("select code,outstanding from t_all_stickcode where outstanding <  50000 and (substr(code,1,1)='0' or substr(code,1,1)='6'); ;")
        self.cur.execute("select code,outstanding from t_all_stickcode a where not EXISTS (select * from t_stick_data_d where code = a.code);")
        sqlrs = self.cur.fetchall();
        # self.cur.execute("delete from t_stick_data_m")
        self.sqlconn.commit()
        for code in sqlrs:
            print(code[0] )
            rs = ts.get_k_data(code=code[0],start=begin_date,end=end_date,ktype='M')
            self.db.delete_date('t_stick_data_m',end_date,code[0])
            self.db.insert_data('t_stick_data_m',rs.as_matrix())
            self.sqlconn.commit()
        return
项目: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 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

    #?????????
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def volume_calculate(self,codes):
        delta_day = 180 * 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
        result_m5_large=[]
        result_m5_small=[]
        for each_code in codes:
            # print each_code
            try:
                df = ts.get_k_data(each_code, start=start_day, end=end_day)
                print df
            except Exception,e:
                print "Failed to get"
                print e
                continue

            if len(df) < 20:
                # print "not long enough"
                continue
            print each_code
            all_mean = df['volume'].mean()
            m5_volume_m = df['volume'][-5:].mean()
            m10_volume_m = df['volume'][-10:].mean()
            last_vol=df['volume'][-1]  #???????????
            #??????????? 180????4?
            if  m5_volume_m > (4.0 * all_mean):
                print "m5 > m_all_avg "
                print each_code,
                temp = self.base[self.base['code'] == each_code]['name'].values[0]
                print temp
                result_m5_large.append(each_code)

            #?????
            if last_vol < (m5_volume_m/3.0):
                result_m5_small.append(each_code)
        return result_m5_large,result_m5_large
项目:stock    作者:Rockyzsu    | 项目源码 | 文件源码
def save_csv(self,code):
        df = ts.get_k_data(code,start='2016-01-01',end='2016-12-28')
        filename=code+".csv"
        #df.to_csv(filename)
        df.to_sql("newtable",self.db,flavor='sqlite')
项目: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)
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def getSingleStock(symbol):
    repeat_times = 1
    message = ""
    df = pd.DataFrame()

    for _ in range(repeat_times): 
        try:
            data = ts.get_k_data(symbol, ktype='W')
            data.sort_index(ascending=True, inplace=True)
            return data, ""
        except Exception as e:
            message = symbol + " fetch exception: " + str(e)
            continue   
    return df, message
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def getSingleStock(symbol):
    repeat_times = 1
    message = ""
    df = pd.DataFrame()

    for _ in range(repeat_times): 
        try:
            data = ts.get_k_data(symbol)
            data.sort_index(ascending=True, inplace=True)
            return data, ""
        except Exception as e:
            message = symbol + " fetch exception: " + str(e)
            continue   
    return df, message
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def getSingleStock(symbol):
    repeat_times = 1
    message = ""
    df = pd.DataFrame()

    for _ in range(repeat_times): 
        try:
            data = ts.get_k_data(symbol, ktype='M')
            data.sort_index(ascending=True, inplace=True)
            return data, ""
        except Exception as e:
            message = symbol + " fetch exception: " + str(e)
            continue   
    return df, message
项目:StockRecommendSystem    作者:doncat99    | 项目源码 | 文件源码
def get_single_stock_data_daily(root_path, symbol):
    '''
    All data is from quandl wiki dataset
    Feature set: [Open  High    Low  Close    Volume  Ex-Dividend  Split Ratio Adj. Open  Adj. High  Adj. Low
    Adj. Close  Adj. Volume]
    '''
    #df, lastUpdateTime = queryStock(root_path, "DB_STOCK", "SHEET_CHN", "_DAILY", symbol, "daily_update")
    try:
        df = ts.get_k_data(symbol)
        df.set_index('date', inplace=True)
        df.sort_index(ascending=True, inplace=True)
    except:
        print("stock delisted", symbol)
        return pd.DataFrame()

    if df.empty:
        print("stock delisted", symbol)
        return pd.DataFrame()

    out_path = root_path + "/Data/CSV/symbols/"

    if os.path.exists(out_path) == False:
        os.mkdir(out_path)

    out_file = out_path + symbol + ".csv"
    df.to_csv(out_file)

    return df
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def __initFlag__(self):
        #use sh for index flag
        flagindex = self.INDEX_LABELS[0]
        df_data = ts.get_k_data(flagindex, index=False, ktype='D').tail(1)
        if df_data.empty == True:
            print "security:%s NO __initFlag__!" %(str(flagindex))
            return
        if len(df_data['close'].values) < 1:
            print "security:%s noclose data __initFlag__!" %(str(flagindex))
        dateStr = df_data['date'].values[0]
        close = df_data['close'].values[0]
        print "init flagindex last time:%s,close:%s" %(str(dateStr),str(close)) 
        current_dt =  self.__getdatetime__(dateStr)
        self.__indexc__= close
        self.__context__= TsContext(current_dt)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_CLOSE_DATA_INTRADAY(self, context, security, data={}, freq=5, dataCount=1):
        periodtype = str(freq)
        df_data = ts.get_k_data(security, index=False, ktype=periodtype).tail(dataCount)
        if df_data.empty == True:
            print "security:%s in freq:%s NO GET_CLOSE_DATA_INTRADAY!" %(str(security),str(freq))
            return np.array([np.nan])
        return self.GET_CLOSE_DATA_INTRADAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_HIGH_DATA_INTRADAY(self, context, security, data={}, freq=5, dataCount=1):
        periodtype = str(freq)
        df_data = ts.get_k_data(security, index=False, ktype=periodtype).tail(dataCount)
        if df_data.empty == True:
            print "security:%s in freq:%s NO GET_HIGH_DATA_INTRADAY!" %(str(security),str(freq))
            return np.array([np.nan])
        return self.GET_HIGH_DATA_INTRADAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_LOW_DATA_INTRADAY(self, context, security, data={}, freq=5, dataCount=1):
        periodtype = str(freq)
        df_data = ts.get_k_data(security, index=False, ktype=periodtype).tail(dataCount)
        if df_data.empty == True:
            print "security:%s in freq:%s NO GET_LOW_DATA_INTRADAY!" %(str(security),str(freq))
            return np.array([np.nan])
        return self.GET_LOW_DATA_INTRADAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_VOL_DATA_INTRADAY(self, context, security, data={}, freq=5, dataCount=1):
        periodtype = str(freq)
        df_data = ts.get_k_data(security, index=False, ktype=periodtype).tail(dataCount)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_VOL_DATA_INTRADAY!" %(str(security),str(context))
            return np.array([np.nan])
        return df_data['volume'].values
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_LOW_DAY(self, context, security, ref=0):
        dataCount =  ref + 1
        df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_LOW_DAY!" %(str(security),str(context))
            return np.nan
        if len(df_data['low']) < ref:
            return np.nan
        return df_data['low'].values[-ref]
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_HIGH_DATA_DAY(self, context,security,isLastest=True,data={},dataCount=1):
        df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_HIGH_DATA_DAY!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_HIGH_DATA_DAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_LOW_DATA_DAY(self, context,security,isLastest=True,data={},dataCount=1):
        df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_LOW_DATA_DAY!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_LOW_DATA_DAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_HIGH_DATA_WEEK(self,context,security,isLastest=True,data={},dataCount=1,isSample=False):
        df_data = ts.get_k_data(security, index=False, ktype='W').tail(dataCount) if isSample else ts.get_k_data(security, index=False, ktype='D').tail(dataCount*5)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_HIGH_DATA_WEEK!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_HIGH_DATA_WEEK_DF(context, df_data, isSample)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_HIGH_DATA_MONTH(self, context,security,isLastest=True,data={},dataCount=1,isSample=True):
        df_data = ts.get_k_data(security, index=False, ktype='M').tail(dataCount) if isSample else ts.get_k_data(security, index=False, ktype='D').tail(dataCount*20)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_HIGH_DATA_MONTH!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_HIGH_DATA_MONTH_DF(context, df_data, isSample)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_LOW_DATA_MONTH(self, context,security,isLastest=True,data={},dataCount=1,isSample=True):
        df_data = ts.get_k_data(security, index=False, ktype='M').tail(dataCount) if isSample else ts.get_k_data(security, index=False, ktype='D').tail(dataCount*20)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_LOW_DATA_MONTH!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_LOW_DATA_MONTH_DF(context, df_data, isSample)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_CLOSE_DAY(self, context, security, ref=0 ,data={}):
        dataCount =  ref + 1
        df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
        if df_data.empty == True:
            print "security:%s NO GET_CLOSE_DAY!" %(str(security))
            return np.nan
        if len(df_data['close']) < ref + 1:
            return np.nan
        return df_data['close'].values[-ref]

    # ????????
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_CLOSE_DATA_DAY(self, context, security,isLastest=True,data={},dataCount=20):
        df_data = ts.get_k_data(security, index=False, ktype='D').tail(dataCount)
        if df_data.empty == True:
            print "security:%s NO GET_CLOSE_DATA_DAY!" %(str(security))
            return np.array([np.nan])
        return self.GET_CLOSE_DATA_DAY_DF(context, df_data)
项目:zStock    作者:superxhy    | 项目源码 | 文件源码
def GET_CLOSE_DATA_WEEK(self, context, security,isLastest=True,data={},dataCount=20,isSample=False):
        df_data = ts.get_k_data(security, index=False, ktype='W').tail(dataCount) if isSample else ts.get_k_data(security, index=False, ktype='D').tail(dataCount*5)
        if df_data.empty == True:
            print "security:%s in context:%s NO GET_CLOSE_DATA_WEEK!" %(str(security),str(context))
            return np.array([np.nan])
        return self.GET_CLOSE_DATA_WEEK_DF(context, df_data, isSample)