Exemplo n.º 1
0
def history_fund(engine, session, code):
    tsl.log("get data for code : " + code + " start...")
    sdate = datetime.date(2013, 1, 1)
    edate = datetime.date.today()
    df = ts.get_nav_history(code, start=str(sdate), end=str(edate))
    df.to_csv('/home/data/f_' + code + '.csv')
    tsl.log("get data for code : " + code + " done")
Exemplo n.º 2
0
 def fund_value_history(self, fund_code, duration='1m'):
     """
     Query fund trading history data from Sina finance
     :param duration: string
         default is '1m', means query one month history.
         OR using one of following:
             '1m' - one month history,
             '3m' - three month history,
             '6m' - six month history,
             '1y' - one year history,
             '2y' - two year history,
             '3y' - three year history.
     :param fund_code: string
         specify the code of the fund you want to query
     :return:
         DataFrame:
             date - index, trading date,
             value - fund net / annual income
             total - accumulated net value / fund million return
             change - fund net growth rate
     """
     result = []
     kv = {'1m': -1, '3m': -3, '6m': -6, '1y': -12, '2y': -24, '3y': -36}
     duration_arrow = self.get_last_trading_info(fund_code)['date'].shift(
         months=kv.get(duration, -1))
     df = ts.get_nav_history(fund_code, duration_arrow).reset_index()
     df.date = df.date.astype(str)
     return df
Exemplo n.º 3
0
 def __init__(self, code, start, end):
     self.code = code
     self.start = start
     self.end = end
     self.result = ts.get_nav_history(code=self.code,
                                      start=self.start,
                                      end=self.end)
     self.info = ts.get_fund_info(code=self.code)
Exemplo n.º 4
0
def load_fund_hist(fund_code, start_date=None, end_date=None):
    #fund = Funds.query.filter_by(code=fund_code)
    #if not fund:
    #get fund detail from Sina
    #pass

    df = ts.get_nav_history(fund_code, start_date, end_date)
    return df.to_html()
Exemplo n.º 5
0
 def get_last_trading_date(self, fund_code):
     today = arrow.now().shift(months=-1)
     while True:
         latest_df = ts.get_nav_history(fund_code,
                                        today.format('YYYY-MM-DD'))
         if (latest_df is not None):
             return arrow.get(latest_df.index[0])
         else:
             today = today.shift(months=-1)
Exemplo n.º 6
0
    def test_000290(self):
        print(tushare.get_fund_info("000290"))
        history = tushare.get_nav_history("000290", start="2015-01-01")
        print(history)

        data_path = "S:/data/funds/000290"
        if not os.path.exists(data_path):
            os.makedirs(data_path)
        data_file = data_path + "/history.csv"
        if os.path.exists(data_file):
            os.remove(data_file)
        history.to_csv(data_file)
Exemplo n.º 7
0
def fund_nav_history_worker(engine, codes, sdate, edate):
    pid = os.getpid()
    tsl.log("pid %i start with %i codes..." % (pid, len(codes)))
    temp = []
    df = pd.DataFrame()
    for code in codes:
        try:
            newdf = ts.get_nav_history(code[0], code[1], sdate, edate)
            if newdf is not None:
                newdf['symbol'] = code[0]
                df = df.append(newdf)
        except BaseException, e:
            if 'timed out' in str(e) or 'urlopen error' in str(e):
                temp.append(code)
            else:
                print e
                tsl.log("pid %i error for %s" % (pid, code))
Exemplo n.º 8
0
def get_fund_nav(fund_id, start_date, end_date, pkl_file=None):
    """
    :param fund_id: list
    :param start_date: str, start date of query
    :param end_date: str, end date of query
    :param pkl_file:
    :return:  pd.DataFrame, index = date, col = fund_id, values = cumul NAV
    """
    ret = pd.DataFrame()
    for fund in fund_id:
        fund_nav = ts.get_nav_history(fund, start_date, end_date)
        # 取累计净值数据
        if fund_nav is not None:
            fund_nav = fund_nav['total']
            fund_nav.name = fund
            ret = pd.concat([ret, fund_nav], axis=1)
        else:
            continue

    if pkl_file is not None:
        pickle_dump_data(ret, pkl_file)

    return ret