Beispiel #1
0
def get_realtime(symbol):
    try:
        if profile: t0 = time.time()
        if isinstance(symbol, list): symbol = ','.join(symbol)
        url = URLRealtime % symbol
        r = getURL(url)
        if profile: t1 = time.time()
        dd = []
        for s in r.split('\n'):
            if not s:
                break
            a = s.split('"')
            b = a[1].split(',')
            l = [re.match(r'^var +hq_str_(\w+)=$', a[0]).group(1), b[0]]
            l.extend([float(i) for i in b[1:len(keyRealtime)-2]])
            d = b[len(keyRealtime)-2]
            t = b[len(keyRealtime)-1]
            l.append(datetime.strptime(d+' '+t, '%Y-%m-%d %H:%M:%S'))
            dd.append(l)
        df = pd.DataFrame(dd, columns=keyRealtime)
        df.set_index('symbol', inplace=True)
        if profile: print('qDate.get_realtime(%s) (%s) %.3fs %.3fs'
            % (symbol, time.ctime(t0), time.time()-t0, t1-t0))
        return df
    except:
        printException()
        return pd.DataFrame()
Beispiel #2
0
def get_fq_(symbol, year, quarter):
    try:
        if profile>=2: t0 = time.time()
        code = re.sub(r'.*(\d{6}).*', r'\1', symbol)
        url = URLFqData % (code, year, quarter)
        s = getURL(url)
        if profile>=2: t1 = time.time()
        h = html.parse(StringIO(s))
        r = h.xpath('//table[@id=\"FundHoldSharesTable\"]')
        l = ','.join([etree.tostring(i) for i in r])
        df = pd.read_html(l, skiprows = [0, 1])[0]
        df.columns = keyFqData
        f = lambda x: datetime.strptime(x, '%Y-%m-%d').date()
        df['date'] = df['date'].apply(f)
        df.set_index('date', inplace=True)
        if profile>=2: print('qDate.get_fq_(%s, %s, %s) (%s) %.3fs %.3fs'
            % (symbol, year, quarter, time.ctime(t0), time.time()-t0, t1-t0))
        return df
    except:
        printException()
        return pd.DataFrame()
Beispiel #3
0
def get_today_all(ascend=0, num=8000, page=1):
    try:
        if profile: t0 = time.time()
        url = URLTodayAll % (ascend, num, page)
        s = getURL(url)
        if profile: t1 = time.time()
        s = re.sub(r'"(\d+\.\d+)"', r'\1', s)
        s = re.sub(r'\b([a-z]+):', r'"\1":', s)
        l = json.loads(s)
        for i in l:
            t = datetime.strptime(i['ticktime'], '%H:%M:%S').time()
            i['ticktime'] = t
        dd = [i.values() for i in l]
        dc = [keyTodayAll[i] for i in l[0].keys()]
        df = pd.DataFrame(dd, columns=dc)
        df.set_index('symbol', inplace=True)
        if profile: print('qDate.get_today_all(%s, %s, %s) (%s) %.3fs %.3fs'
            % (ascend, num, page, time.ctime(t0), time.time()-t0, t1-t0))
        return df
    except:
        printException()
        return pd.DataFrame()
Beispiel #4
0
def get_k_data(symbol, ktype='D'):
    try:
        if profile: t0 = time.time()
        ktype = str(ktype)
        kMinute = ktype in ('5', '15', '30', '60')
        if kMinute:
            url = URLKMinute % (symbol, ktype)
        elif ktype == 'M':
            url = URLKMonthly % symbol
        elif ktype == 'W':
            url = URLKWeekly % symbol
        else:
            url = URLKDaily % symbol
        s = getURL(url)
        if profile: t1 = time.time()
        s = re.sub(r'(?<=\]),', r';', s)
        if not kMinute:
            s = re.sub(r'(?<=\d),(\d{3})', r'\1', s)
        s = re.sub(r'(record|:(?=\[)|\[|\]|\{|\}|")', r'', s)
        l = []
        for i in s.split(';'):
            j = i.split(',')
            if kMinute:
                d = [datetime.strptime(j[0], '%Y-%m-%d %H:%M:%S')]
            else:
                d = [datetime.strptime(j[0], '%Y-%m-%d').date()]
            d.extend([float(k) for k in j[1:]])
            l.append(d)
        df = pd.DataFrame(l, columns=keyKData)
        df.set_index('date', inplace=True)
        if profile: print('qData.get_k_data(%s, %s) (%s) %.3fs %.3fs'
            % (symbol, ktype, time.ctime(t0), time.time()-t0, t1-t0))
        return df
    except:
        printException()
        return pd.DataFrame()
Beispiel #5
0
def get_hfq(symbol):
    try:
        if profile: t0 = time.time()
        url = URLHfq % symbol
        s = getURL(url)
        if profile: t1 = time.time()
        s = re.sub(r'(^.+:\{|\{|\}|\(|\)|")', '', s)
        l = []
        for i in s.split(','):
            j = i.split(':')
            t = datetime.strptime(j[0], '_%Y_%m_%d').date()
            try:
                d = float(j[1])
            except ValueError:
                d = 0
            l.append([t, d])
        df = pd.DataFrame(l, columns=keyHfq)
        df.set_index('date', inplace=True)
        if profile: print('qDate.get_hfq(%s) (%s) %.3fs %.3fs'
            % (symbol, time.ctime(t0), time.time()-t0, t1-t0))
        return df
    except:
        printException()
        return pd.DataFrame()