示例#1
0
文件: qData.py 项目: yx9527/yxQuant
def get_fq_data(symbol, dateStart=(), dateEnd=()):
    try:
        if profile: t0 = time.time()
        dateStart = dateStart and date(*dateStart) or date(1980,1,1)
        dateEnd = dateEnd and date(*dateEnd) or datetime.now().date()
        s = dateStart.year * 12 + dateStart.month - 1
        e = dateEnd.year * 12 + dateEnd.month - 1
        df = pd.DataFrame()
        for i in range(e, s-3, -3):
            j = get_fq_(symbol=symbol, year=i/12, quarter=(i%12)/3+1)
            df = df.append(j)
        df = df[df.index >= dateStart]
        df = df[df.index <= dateEnd]
        df['open' ] = df['open' ] / df['factor']
        df['high' ] = df['high' ] / df['factor']
        df['close'] = df['close'] / df['factor']
        df['low'  ] = df['low'  ] / df['factor']
        f = lambda x: float('%.2f' % x) #四舍五入到2位小数
        df['open' ] = df['open' ].apply(f)
        df['high' ] = df['high' ].apply(f)
        df['close'] = df['close'].apply(f)
        df['low'  ] = df['low'  ].apply(f)
        if profile: print('qDate.get_fq_data(%s, %s, %s) (%s) %.3fs'
            % (symbol, dateStart, dateEnd, time.ctime(t0), time.time()-t0))
        return df
    except:
        printException()
        return pd.DataFrame()        
示例#2
0
文件: qData.py 项目: yx9527/yxQuant
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()
示例#3
0
文件: qWind.py 项目: yx9527/yxQuant
 def fetch_stock(self, stock_list):
     stock_send = [i[0:2]=='sh' and i[2:8]+'.SH' or
                  (i[0:2]=='sz' and i[2:8]+'.SZ' or i) for i in stock_list]
     try:
         w = WindPy.w.wsq(','.join(stock_send), ','.join(self.stock_fields))
     except:
         printException()
         raise Exception('WindPy.w.wsq(%s, %s)' % ','.join(stock_send), ','.join(self.stock_fields))
     self.parse_stock(w)
示例#4
0
文件: qWind.py 项目: yx9527/yxQuant
 def windInit(self, retry=10):
     for i in range(retry):
         print('WindPy.w.start() %d' % i)
         time.sleep(1)
         WindPy.w.stop()
         try:
             WindPy.w.start()
         except:
             printException()
             break
         if WindPy.w.isconnected():
             print('Wind is connected')
             return
         print('Wind isn\'t connected')
     raise Exception('WindPy.w.start()')
示例#5
0
文件: qData.py 项目: yx9527/yxQuant
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()
示例#6
0
文件: qData.py 项目: yx9527/yxQuant
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()
示例#7
0
文件: qData.py 项目: yx9527/yxQuant
def get_hist_data(symbol, fuQuan=True, dateStart=(), dateEnd=()):
    try:
        if profile: t0 = time.time()
        df = get_fq_data(symbol, dateStart, dateEnd)
        rt = get_realtime(symbol)
        df['name'] = rt['name'][0]
        if fuQuan:
            df['qfq_factor'] = df['factor'    ] / df['factor'][0]
            df['qfq_open'  ] = df['open'      ] * df['qfq_factor']
            df['qfq_high'  ] = df['high'      ] * df['qfq_factor']
            df['qfq_close' ] = df['close'     ] * df['qfq_factor']
            df['qfq_low'   ] = df['low'       ] * df['qfq_factor']
            f = lambda x: float('%.2f' % x) #四舍五入到2位小数
            df['qfq_open'  ] = df['qfq_open'  ].apply(f)
            df['qfq_high'  ] = df['qfq_high'  ].apply(f)
            df['qfq_close' ] = df['qfq_close' ].apply(f)
            df['qfq_low'   ] = df['qfq_low'   ].apply(f)
        if profile: print('qData.get_hist_data(%s, %s) (%s) %.3fs'
            % (symbol, fuQuan, time.ctime(t0), time.time()-t0))
        return df
    except:
        printException()
        return pd.DataFrame()
示例#8
0
文件: qData.py 项目: yx9527/yxQuant
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()
示例#9
0
文件: qData.py 项目: yx9527/yxQuant
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()