Пример #1
0
def sh_margins(start=None, end=None, retry_count=3, pause=0.001):
    """
    获取沪市融资融券数据列表
    Parameters
    --------
    start:string
                  开始日期 format:YYYY-MM-DD 为空时取去年今日
    end:string
                  结束日期 format:YYYY-MM-DD 为空时取当前日期
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
    opDate:信用交易日期
    rzye:本日融资余额(元)
    rzmre: 本日融资买入额(元)
    rqyl: 本日融券余量
    rqylje: 本日融券余量金额(元)
    rqmcl: 本日融券卖出量
    rzrqjyzl:本日融资融券余额(元)
    """
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    if du.diff_day(start, end) < 0:
        return None
    start, end = start.replace("-", ""), end.replace("-", "")
    data = pd.DataFrame()
    ct._write_head()
    df = _sh_hz(data, start=start, end=end, retry_count=retry_count, pause=pause)
    return df
Пример #2
0
def top_detail(code, date=None, retry_count= 3, pause= 0.001):
    """
    根据指定日期,指定股票的龙虎榜交易细节
    Parameters
    --------
	code: string
			指定股票代码
	date: string
			指定日期
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题

    Return
    --------
    code:代码
    name:名称
    bamount:累积买入额(万)
    bcount:买入次数
    samount:累积卖出额(万)
    scount:卖出次数
    net:净额(万)
    """
    if code is None or code == "":
        return None
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if(du.is_holiday(date)):
            return None
        time.sleep(pause)
        try:
            request = Request(rv.LHB_SINA_URL2%(ct.P_TYPE['http'], ct.DOMAINS['vsf'], code, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('details=')[1][2:-2]
            text = eval(text, type('Dummy', (dict,),
                                           dict(__getitem__ = lambda s, n:n))())
            text = json.dumps(text)
            text = json.loads(text)
            buy_df = pd.DataFrame(text['buy'], columns=rv.LHB_TMP_DETAIL_COLS)
            buy_df['type'] = 'buy'
            sell_df = pd.DataFrame(text['sell'], columns=rv.LHB_TMP_DETAIL_COLS)
            sell_df['type'] = 'sell'
            #LHB_DETAIL_COLS = ['code', 'type', 'insCode', 'insName', 'bamount', 'samount', 'net']
            df = sell_df.append(buy_df)
            df.columns = rv.LHB_DETAIL_COLS
            df['bamount'] = df['bamount'].astype(float)
            df['samount'] = df['samount'].astype(float)
            df['net'] = df['net'].astype(float)
            df['date'] = date
        except Exception as e:
            print e
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #3
0
def top_list(date = None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题

    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if not date:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if(du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            html = lxml.html.parse(StringIO(text))
            res = html.xpath("//table[@id=\"dt_1\"]")
            sarr = [etree.tostring(node).decode('utf-8') for node in res]
            sarr = ''.join(sarr)
            df = pd.read_html(sarr)[0]
            df.columns = [i for i in range(1,12)]
            df = df.apply(_f_rows, axis=1)
            df = df.fillna(method='ffill')
            df = df.drop([1, 4], axis=1)
            df.columns = rv.LHB_COLS
            df = df.drop_duplicates()
            df['code'] = df['code'].astype(int)
            df['code'] = df['code'].map(lambda x: str(x).zfill(6))
            df['date'] = date
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #4
0
def get_today_ticks(code=None, retry_count=3, pause=0.001, use_proxy=False):
    """
        获取当日分笔明细数据
    Parameters
    ------
        code:string
                  股票代码 e.g. 600848
        retry_count : int, 默认 3
                  如遇网络等问题重复执行的次数
        pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
        DataFrame 当日所有股票交易数据(DataFrame)
              属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
    """
    if code is None or len(code) != 6:
        return None
    symbol = ct._code_to_symbol(code)
    date = du.today()
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(ct.TODAY_TICKS_PAGE_URL %
                              (ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                               ct.PAGES['jv'], date, symbol))
            data_str = urlopen(request, timeout=10).read()
            data_str = data_str.decode('utf-8')
            data_str = data_str[1:-1]
            data_str = eval(
                data_str,
                type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
            data_str = json.dumps(data_str)
            data_str = json.loads(data_str)
            pages = len(data_str['detailPages'])
            data = pd.DataFrame()
            ct._write_head()
            for pNo in range(1, pages + 1):
                proxies = {}
                if use_proxy:
                    ip = get_proxy()
                    proxies['http'] = 'http://' + ip
                data = data.append(_today_ticks(symbol, date, pNo, retry_count,
                                                pause, proxies),
                                   ignore_index=True)
        except Exception as er:
            print(traceback.format_exc())
            print(str(er))
        else:
            return data
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #5
0
def day_boxoffice(date=None, retry_count=3, pause=0.001):
    """
    获取单日电影票房数据
    数据来源:EBOT艺恩票房智库
    Parameters
    ------
        date:日期,默认为上一日
        retry_count : int, 默认 3
                  如遇网络等问题重复执行的次数
        pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
        DataFrame 
              AvgPrice 平均票价
              AvpPeoPle 场均人次
              BoxOffice 单日票房(万)
              BoxOffice_Up 环比变化 (%)
              MovieDay 上映天数
              MovieName 影片名 
              SumBoxOffice 累计票房(万) 
              WomIndex 口碑指数 
    """
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            if date is None:
                date = 0
            else:
                date = int(du.diff_day(du.today(), date)) + 1

            request = Request(
                ct.BOXOFFICE_DAY %
                (ct.P_TYPE['http'], ct.DOMAINS['mbox'], date, _random()))
            lines = urlopen(request, timeout=10).read()
            if len(lines) < 15:  #no data
                return None
        except Exception as e:
            print(e)
        else:
            js = json.loads(lines.decode('utf-8') if ct.PY3 else lines)
            df = pd.DataFrame(js['data1'])
            df = df.drop([
                'MovieImg', 'BoxOffice1', 'MovieID', 'Director', 'IRank',
                'IRank_pro'
            ],
                         axis=1)
            return df
Пример #6
0
def CalculateRealTimeMA(stocknumber,n):
    '''
    计算实时均价,包含实时现价作为当天收盘价,使用前复权。开盘前不要用,建议只开盘时使用
    stocknumber 股票代码 类型int 
    n 几日均价 类型int
    '''
    nowtime = int(time.strftime('%H',time.localtime(time.time())))
    today = du.today()
    if du.is_holiday(today) or nowtime > 16 or nowtime < 9:
        return CalculateMA(stocknumber,n)
    else:
        price = ts.get_realtime_quotes(str(stocknumber))['price'].values
        behandma = CalculateMA(stocknumber,n-1)
        for i in price: 
            c = behandma*(n-1) + float(i)
        return float(c)/n
Пример #7
0
def get_nav_history(code,
                    start=None,
                    end=None,
                    retry_count=3,
                    pause=0.001,
                    timeout=10):
    '''
    获取历史净值数据
    Parameters
    ------
      code:string
                  基金代码 e.g. 000001
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
      timeout: int 默认 10s
                请求大量数据时的网络超时
    return
    -------
      DataFrame
          date 发布日期 (index)
          value 基金净值(股票/混合/QDII型基金) / 年华收益(货币/债券基金)
          total 累计净值(股票/混合/QDII型基金) / 万分收益(货币/债券基金)
          change 净值增长率(股票/混合/QDII型基金)
    '''
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end

    # 判断基金类型
    ismonetary = False  # 是否是债券型和货币型基金
    df_fund = get_fund_info(code)

    fund_type = df_fund.ix[0]['Type2Name']
    if fund_type is not None:
        if (fund_type.find(u'债券型') != -1) or (fund_type.find(u'货币型') != -1):
            ismonetary = True

    ct._write_head()
    nums = _get_nav_histroy_num(code, start, end, ismonetary)
    data = _parse_nav_history_data(code, start, end, nums, ismonetary,
                                   retry_count, pause, timeout)
    return data
Пример #8
0
def sz_margins(start=None, end=None, retry_count=3, pause=0.001):
    """
    获取深市融资融券数据列表
    Parameters
    --------
    start:string
                  开始日期 format:YYYY-MM-DD 默认为上一周的今天
    end:string
                  结束日期 format:YYYY-MM-DD 默认为今日
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
    opDate:信用交易日期(index)
    rzmre: 融资买入额(元)
    rzye:融资余额(元)
    rqmcl: 融券卖出量
    rqyl: 融券余量
    rqye: 融券余量(元)
    rzrqye:融资融券余额(元)
    """
    data = pd.DataFrame()
    if start is None and end is None:
        end = du.today()
        start = du.day_last_week()
    if start is None or end is None:
        ct._write_msg(rv.MAR_SZ_HZ_MSG2)
        return None
    try:
        date_range = pd.date_range(start=start, end=end, freq='B')
        if len(date_range) > 261:
            ct._write_msg(rv.MAR_SZ_HZ_MSG)
        else:
            ct._write_head()
            for date in date_range:
                data = data.append(_sz_hz(str(date.date()), retry_count,
                                          pause))
    except:
        ct._write_msg(ct.DATA_INPUT_ERROR_MSG)
    else:
        return data
Пример #9
0
def sz_margins(start=None, end=None, retry_count=3, pause=0.001):
    """
    获取深市融资融券数据列表
    Parameters
    --------
    start:string
                  开始日期 format:YYYY-MM-DD 默认为上一周的今天
    end:string
                  结束日期 format:YYYY-MM-DD 默认为今日
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
    opDate:信用交易日期(index)
    rzmre: 融资买入额(元)
    rzye:融资余额(元)
    rqmcl: 融券卖出量
    rqyl: 融券余量
    rqye: 融券余量(元)
    rzrqye:融资融券余额(元)
    """
    data = pd.DataFrame()
    if start is None and end is None:
        end = du.today()
        start = du.day_last_week()
    if start is None or end is None:
        ct._write_msg(rv.MAR_SZ_HZ_MSG2)
        return None
    try:
        date_range = pd.date_range(start=start, end=end, freq='B')
        if len(date_range)>261:
            ct._write_msg(rv.MAR_SZ_HZ_MSG)
        else:
            ct._write_head()
            for date in date_range:
                data = data.append(_sz_hz(str(date.date()), retry_count, pause) )
    except:
        ct._write_msg(ct.DATA_INPUT_ERROR_MSG)
    else:
        return data
Пример #10
0
def get_today_ticks(code=None, retry_count=3, pause=0.001):
    """
        获取当日分笔明细数据
    Parameters
    ------
        code:string
                  股票代码 e.g. 600848
        retry_count : int, 默认 3
                  如遇网络等问题重复执行的次数
        pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
        DataFrame 当日所有股票交易数据(DataFrame)
              属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
    """
    if code is None or len(code)!=6 :
        return None
    symbol = _code_to_symbol(code)
    date = du.today()
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(ct.TODAY_TICKS_PAGE_URL % (ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                                                       ct.PAGES['jv'], date,
                                                       symbol))
            data_str = urlopen(request, timeout=10).read()
            data_str = data_str.decode('GBK')
            data_str = data_str[1:-1]
            data_str = eval(data_str, type('Dummy', (dict,), 
                                           dict(__getitem__ = lambda s, n:n))())
            data_str = json.dumps(data_str)
            data_str = json.loads(data_str)
            pages = len(data_str['detailPages'])
            data = pd.DataFrame()
            ct._write_head()
            for pNo in range(1, pages+1):
                data = data.append(_today_ticks(symbol, date, pNo,
                                                retry_count, pause), ignore_index=True)
        except Exception as er:
            print(str(er))
        else:
            return data
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #11
0
def day_boxoffice(date=None, retry_count=3, pause=0.001):
    """
    获取单日电影票房数据
    数据来源:EBOT艺恩票房智库
    Parameters
    ------
        date:日期,默认为上一日
        retry_count : int, 默认 3
                  如遇网络等问题重复执行的次数
        pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
        DataFrame 
              AvgPrice      平均票价
              AvpPeoPle     场均人次
              BoxOffice     单日票房(万)
              BoxOffice_Up  环比变化 (%)
              IRank         排名
              MovieDay      上映天数
              MovieName     影片名 
              SumBoxOffice  累计票房(万) 
              WomIndex      口碑指数 
    """
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            if date is None:
                date = 0
            else:
                date = int(du.diff_day(du.today(), date)) + 1
                
            request = Request(ct.BOXOFFICE_DAY%(ct.P_TYPE['http'], ct.DOMAINS['mbox'],
                              ct.BOX, date, _random()))
            lines = urlopen(request, timeout = 10).read()
            if len(lines) < 15: #no data
                return None
        except Exception as e:
            print(e)
        else:
            js = json.loads(lines.decode('utf-8') if ct.PY3 else lines)
            df = pd.DataFrame(js['data1'])
            df = df.drop(['MovieImg', 'BoxOffice1', 'MovieID', 'Director', 'IRank_pro'], axis=1)
            return df
Пример #12
0
def get_today_ticks(code=None, retry_count=3, pause=0.001):
    """
        获取当日分笔明细数据
    Parameters
    ------
        code:string
                  股票代码 e.g. 600848
        retry_count : int, 默认 3
                  如遇网络等问题重复执行的次数
        pause : int, 默认 0
                 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
     return
     -------
        DataFrame 当日所有股票交易数据(DataFrame)
              属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
    """
    import tushare.util.dateu as du
    if code is None or len(code) != 6:
        return None
    symbol = _code_to_symbol(code)
    date = du.today()
    try:
        request = Request(ct.TODAY_TICKS_PAGE_URL %
                          (ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                           ct.PAGES['jv'], date, symbol))
        data_str = urlopen(request, timeout=10).read()
        data_str = data_str.decode('GBK')
        data_str = data_str[1:-1]
        data_str = eval(
            data_str,
            type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
        data_str = json.dumps(data_str)
        data_str = json.loads(data_str)
        pages = len(data_str['detailPages'])
        data = pd.DataFrame()
        ct._write_head()
        for pNo in range(1, pages):
            data = data.append(_today_ticks(symbol, date, pNo, retry_count,
                                            pause),
                               ignore_index=True)
    except Exception as er:
        print(str(er))
    return data
Пример #13
0
def get_nav_history(code, start=None, end=None, retry_count=3, pause=0.001, timeout=10):
    '''
    获取历史净值数据
    Parameters
    ------
      code:string
                  基金代码 e.g. 000001
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
      timeout: int 默认 10s
                请求大量数据时的网络超时
    return
    -------
      DataFrame
          date 发布日期 (index)
          value 基金净值(股票/混合/QDII型基金) / 年华收益(货币/债券基金)
          total 累计净值(股票/混合/QDII型基金) / 万分收益(货币/债券基金)
          change 净值增长率(股票/混合/QDII型基金)
    '''
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end

    # 判断基金类型
    ismonetary = False  # 是否是债券型和货币型基金
    df_fund = get_fund_info(code)

    fund_type = df_fund.ix[0]['Type2Name']
    if (fund_type.find(u'债券型') != -1) or (fund_type.find(u'货币型') != -1):
        ismonetary = True

    ct._write_head()
    nums = _get_nav_histroy_num(code, start, end, ismonetary)
    data = _parse_nav_history_data(
        code, start, end, nums, ismonetary, retry_count, pause, timeout)
    return data
Пример #14
0
def sh_margins(start=None, end=None, retry_count=3, pause=0.001):
    """
    获取沪市融资融券数据列表
    Parameters
    --------
    start:string
                  开始日期 format:YYYY-MM-DD 为空时取去年今日
    end:string
                  结束日期 format:YYYY-MM-DD 为空时取当前日期
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
    opDate:信用交易日期
    rzye:本日融资余额(元)
    rzmre: 本日融资买入额(元)
    rqyl: 本日融券余量
    rqylje: 本日融券余量金额(元)
    rqmcl: 本日融券卖出量
    rzrqjyzl:本日融资融券余额(元)
    """
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    if du.diff_day(start, end) < 0:
        return None
    start, end = start.replace('-', ''), end.replace('-', '')
    data = pd.DataFrame()
    ct._write_head()
    df = _sh_hz(data,
                start=start,
                end=end,
                retry_count=retry_count,
                pause=pause)
    return df
Пример #15
0
def lhb_daily_detail(code, date, type):
    """
    获取个股龙虎榜买5和卖5
    :param code: 股票代码
    :param date: 日期 yyyy-MM-dd
    :param type: SINA龙虎榜类型
    :return:
    """
    if (date is None):
        date = du.today()
    request = Request(LHB_SINA_URL % (code, date, type))
    text = urlopen(request, timeout=10).read()
    text = text.decode('GBK')
    text = text.splitlines()[1]
    ctxt = pyv8.JSContext()
    ctxt.enter()
    index = text.find('{') - 1
    text = text[index:-1]
    temp = ctxt.eval(text)
    data = []
    date_columns = ['SYMBOL', 'buyAmount', 'sellAmount', 'netAmount', 'comCode', 'comName']
    for a in temp.buy:
        tempdata = []
        for k in date_columns:
            tempdata.append(a[k])
        tempdata.append('b')
        data.append(tempdata)
    for a in temp.sell:
        tempdata = []
        for k in date_columns:
            tempdata.append(a[k])
        tempdata.append('s')
        data.append(tempdata)
    df = pd.DataFrame(data, columns=LHB_SINA_COLUMNS)
    df['date'] = date
    return df
Пример #16
0
def top_list(date=None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if (du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(
                rv.LHB_URL % (ct.P_TYPE['http'], ct.DOMAINS['em'], date, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('_1=')[1]
            text = eval(
                text,
                type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
            text = json.dumps(text)
            text = json.loads(text)
            df = pd.DataFrame(text['data'], columns=rv.LHB_TMP_COLS)
            df.columns = rv.LHB_COLS
            df['buy'] = df['buy'].astype(float)
            df['sell'] = df['sell'].astype(float)
            df['amount'] = df['amount'].astype(float)
            df['Turnover'] = df['Turnover'].astype(float)
            df['bratio'] = df['buy'] / df['Turnover']
            df['sratio'] = df['sell'] / df['Turnover']
            df['bratio'] = df['bratio'].map(ct.FORMAT)
            df['sratio'] = df['sratio'].map(ct.FORMAT)
            df['date'] = date
            for col in ['amount', 'buy', 'sell']:
                df[col] = df[col].astype(float)
                df[col] = df[col] / 10000
                df[col] = df[col].map(ct.FORMAT)
            df = df.drop('Turnover', axis=1)
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #17
0
def get_k_data(code=None, start='', end='',
                  ktype='D', autype='qfq', 
                  index=False,
                  retry_count=3,
                  pause=0.001):
    """
    获取k线数据
    ---------
    Parameters:
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取上市首日
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取最近一个交易日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      ktype:string
                  数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交额
          turnoverratio 换手率
          code 股票代码
    """
    symbol = ct.INDEX_SYMBOL[code] if index else _code_to_symbol(code)
    url = ''
    dataflag = ''
    autype = '' if autype is None else autype
    if (start is not None) & (start != ''):
        end = du.today() if end is None or end == '' else end
    if ktype.upper() in ct.K_LABELS:
        fq = autype if autype is not None else ''
        if code[:1] in ('1', '5') or index:
            fq = ''
        kline = '' if autype is None else 'fq'
        if (start is None or start == '') & (end is None or end == ''):
            urls = [ct.KLINE_TT_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
                                    kline, fq, symbol, 
                                    ct.TT_K_TYPE[ktype.upper()], start, end,
                                    fq, _random(17))]
        else:
            years = du.tt_dates(start, end)
            urls = []
            for year in years:
                startdate = str(year) + '-01-01'
                enddate = str(year+1) + '-12-31'
                url = ct.KLINE_TT_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
                                    kline, fq+str(year), symbol, 
                                    ct.TT_K_TYPE[ktype.upper()], startdate, enddate,
                                    fq, _random(17))
                urls.append(url)
        dataflag = '%s%s'%(fq, ct.TT_K_TYPE[ktype.upper()])
    elif ktype in ct.K_MIN_LABELS:
        urls = [ct.KLINE_TT_MIN_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
                                    symbol, ktype, ktype,
                                    _random(16))]
        dataflag = 'm%s'%ktype
    else:
        raise TypeError('ktype input error.')
    data = pd.DataFrame()
    for url in urls:
        data = data.append(_get_k_data(url, dataflag, 
                                       symbol, code,
                                       index, ktype,
                                       retry_count, pause), 
                           ignore_index=True)
    if ktype not in ct.K_MIN_LABELS:
        if ((start is not None) & (start != '')) & ((end is not None) & (end != '')):
            if data.empty==False:       
                data = data[(data.date >= start) & (data.date <= end)]
    return data
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #18
0
def tick(code, conn=None, date='', asset='E', market='', retry_count = 3):
    """
    tick数据
    Parameters:
    ------------
    code:证券代码,支持股票,ETF/LOF,期货/期权,港股
    conn:服务器连接 ,通过ts.api()或者ts.xpi()获得
    date:日期
    asset:证券品种,E:沪深交易所股票和基金, INDEX:沪深交易所指数, X:其他证券品种,大致如下:
                     支持的扩展行情包括(asset='X'):
                            郑州商品期权         OZ 大连商品期权         OD 上海商品期权         OS
                            上海个股期权         QQ 香港指数         FH 郑州商品         QZ 大连商品         QD 上海期货         QS
                            香港主板         KH 香港权证         KR 开放式基金         FU 货币型基金         FB
                            招商理财产品         LC 招商货币产品         LB 国际指数         FW 国内宏观指标         HG 中国概念股         CH
                            美股知名公司         MG B股转H股         HB 股份转让         SB 股指期货         CZ 香港创业板         KG 香港信托基金         KT
                             国债预发行         GY 主力期货合约         MA
                              中证指数         ZZ 港股通         GH
    market:市场代码,通过ts.get_markets()获取
                  
    Return
    ----------
    DataFrame
    date:日期
    time:时间
    price:成交价
    vol:成交量
    type:买卖方向,0-买入 1-卖出 2-集合竞价成交
            期货  0:开仓  1:多开   -1:空开
         期货多一列数据oi_change:增仓数据

    """
    code = code.strip().upper()
    date = int(date.replace('-', ''))
    today = int(str(du.today()).replace('-', ''))
    for _ in range(retry_count):
        try:
            if conn is None:
                print(ct.MSG_NOT_CONNECTED)
                return None
            api, xapi = conn
            data = pd.DataFrame()
            mkcode = _get_mkcode(code, asset=asset, xapi=xapi) if market == '' else market
            con = api if asset in['E', 'INDEX'] else xapi
            for i in range(200):
                if date == today:
                    ds = con.get_transaction_data(market=mkcode, code=code, start=i * 300, count=300)
                else:
                    ds = con.get_history_transaction_data(market=mkcode, code=code, date=date, start=i * 300, count=300)
                df =  api.to_df(ds)
                data = data.append(df) if i == 0 else df.append(data,  ignore_index=True)
                if len(ds) < 300:
                    break
            if asset in['E', 'INDEX']:
                data['date'] = date
                data['date'] = data['date'].map(lambda x: '%s-%s-%s '%(str(x)[0:4], str(x)[4:6], str(x)[6:8]))
                data['datetime'] = data['date'] + data['time']
                data = data[['datetime', 'price', 'vol', 'buyorsell']]
                data.columns = ['datetime', 'price', 'vol', 'type']
            else:
                if mkcode in [31, 71]:
                    if date == today:
                        data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction', 
                                        'second', 'nature_mark', 'nature_value'], axis=1)
                    else:
                        data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction'], axis=1)
                    data.loc[data.nature== 512, 'nature' ] = 2
                    data.loc[data.nature== 256, 'nature' ] = 1
                    data = data.sort_values('date')
                    data.columns = ['date', 'price', 'vol', 'type']
                elif mkcode in [28, 29, 30, 47, 60]:
                    if date == today:
                        data = data.drop(['hour', 'minute', 'nature', 'direction', 
                                            'second', 'nature_mark', 'nature_value'], axis=1)
                    else:
                        data = data.drop(['hour', 'minute', 'nature', 'direction'], axis=1)
                    data.columns = ['date', 'price', 'vol', 'oi_change', 'type']
                else:
                    data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction', 'nature'], axis=1)
            
        except Exception as e:
            print(e)
        else:
            return data
Пример #19
0
def top_list(date = None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today() 
    else:
        if(du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            html = lxml.html.parse(StringIO(text))
            res = html.xpath("//table[@id=\"dt_1\"]")
            if ct.PY3:
                sarr = [etree.tostring(node).decode('utf-8') for node in res]
            else:
                sarr = [etree.tostring(node) for node in res]
            sarr = ''.join(sarr)
            df = pd.read_html(sarr)[0]
            df.columns = [i for i in range(1,12)]
            df = df.apply(_f_rows, axis=1)
            df = df.fillna(method='ffill')
            df = df.drop([1, 4], axis=1)
            df.columns = rv.LHB_COLS
            df = df.drop_duplicates()
            df['code'] = df['code'].astype(int)
            df['code'] = df['code'].map(lambda x: str(x).zfill(6))
            df['date'] = date
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #20
0
def get_h_data(code, start=None, end=None, autype='qfq',
               retry_count=3, pause=0.001):
    '''
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volumn 成交量
          amount 成交金额
    '''
    
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    print ct.FQ_PRINTING%(qt[0], qt[1])
    data = _parse_fq_data(ct.HIST_FQ_URL%(ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                              code, qt[0], qt[1]), retry_count, pause)
    if len(qs)>1:
        for d in range(1, len(qs)):
            qt = qs[d]
            print ct.FQ_PRINTING%(qt[0], qt[1])
            url = ct.HIST_FQ_URL%(ct.P_TYPE['http'], ct.DOMAINS['vsf'],
                                  code, qt[0], qt[1])
            df = _parse_fq_data(url, retry_count, pause)
            data = data.append(df, ignore_index=True)
    data = data.drop_duplicates('date')
    if start is not None:
        data = data[data.date>=start]
    if end is not None:
        data = data[data.date<=end]
    if autype == 'hfq':
        data = data.drop('factor', axis=1)
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label].map(ct.FORMAT)
        data = data.set_index('date')
        data = data.sort_index(ascending=False)
        return data
    else:
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label] / data['factor']
        data = data.drop('factor', axis=1)
        if autype == 'qfq':
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates('date')
            df = df[df.date>=start]
            df = df[df.date<=end]
            df = pd.merge(data, df)
            df = df.sort('date', ascending=False)
            frow = df.head(1)
            rate = float(frow['close']) / float(frow['factor'])
            df['close_temp'] = df['close']
            df['close'] = rate * df['factor']
            for label in ['open', 'high', 'low']:
                df[label] = df[label] * (df['close'] / df['close_temp'])
                df[label] = df[label].map(ct.FORMAT)
            df = df.drop(['factor', 'close_temp'], axis=1)
            df['close'] = df['close'].map(ct.FORMAT)
            df = df.set_index('date')
            df = df.sort_index(ascending=False)
            return df
        else:
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index('date')
            data = data.sort_index(ascending=False)
            return data
Пример #21
0
# arguments can be passed through command line.
# usage: python parser.py "qqq spy xme xop" > output.txt
if len(sys.argv) > 1:
    ticker_list = sys.argv[1].split()

for t in ticker_list:
    parser = MyHTMLParser(t)
    filehandle = urllib.urlopen("http://finance.yahoo.com/q?s=%s" % t)
    html_string = filehandle.read()
    parser.feed(html_string)
    dowj_string = (parser.close())
    dowj_string = dowj_string[:-1]
    dowj = float(dowj_string) * 0.01
    print dowj    
    if (du.get_hour() > 15):
        dowj = 0
        
for code in codes:
    my_calc(code,times,dowj,0)

result = ""
for code in mycodes:
    du.get_hour()
    result = my_sell_notify(code,times,dowj,0) + result

date = du.today()
send_email("Stock Handle Policy " + date, result)


Пример #22
0
def top_list(date = None, retry_count=3, pause=0.001):
    """
    获取每日龙虎榜列表
    Parameters
    --------
    date:string
                明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
    retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
    pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    
    Return
    ------
    DataFrame
        code:代码
        name :名称
        pchange:涨跌幅     
        amount:龙虎榜成交额(万)
        buy:买入额(万)
        bratio:占总成交比例
        sell:卖出额(万)
        sratio :占总成交比例
        reason:上榜原因
        date  :日期
    """
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today() 
    else:
        if(du.is_holiday(date)):
            return None
    for _ in range(retry_count):
        time.sleep(pause)
        try:
            request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date, date))
            text = urlopen(request, timeout=10).read()
            text = text.decode('GBK')
            text = text.split('_1=')[1]
            text = eval(text, type('Dummy', (dict,), 
                                           dict(__getitem__ = lambda s, n:n))())
            text = json.dumps(text)
            text = json.loads(text)
            df = pd.DataFrame(text['data'], columns=rv.LHB_TMP_COLS)
            df.columns = rv.LHB_COLS
            df['buy'] = df['buy'].astype(float)
            df['sell'] = df['sell'].astype(float)
            df['amount'] = df['amount'].astype(float)
            df['Turnover'] = df['Turnover'].astype(float)
            df['bratio'] = df['buy'] / df['Turnover']
            df['sratio'] = df['sell'] /df['Turnover']
            df['bratio'] = df['bratio'].map(ct.FORMAT)
            df['sratio'] = df['sratio'].map(ct.FORMAT)
            df['date'] = date
            for col in ['amount', 'buy', 'sell']:
                df[col] = df[col].astype(float)
                df[col] = df[col] / 10000
                df[col] = df[col].map(ct.FORMAT)
            df = df.drop('Turnover', axis=1)
        except:
            pass
        else:
            return df
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #23
0
import os
import sys
from tushare.util import dateu as du

if "__main__" == __name__:


    if du.is_holiday(du.today()):
        pass
    else:
        ret = os.system('/home/work/anaconda/bin/python ./download_daily_data.py -p >>logs/daily.log 2>>logs/daily.err')
        if ret != 0:
            print >> sys.stderr, 'daily error with exit code %d'%ret
        else:
            ret = os.system('/home/work/anaconda/bin/python ./hfq2qfq.py -p >>logs/fq.log 2>>logs/fq.err')
            if ret != 0:
                print >> sys.stderr, 'fq error with exit code %d'%ret

Пример #24
0
def tick(code, conn=None, date='', asset='E', market='', retry_count=3):
    """
    tick数据
    Parameters:
    ------------
    code:证券代码,支持股票,ETF/LOF,期货/期权,港股
    conn:服务器连接 ,通过ts.api()或者ts.xpi()获得
    date:日期
    asset:证券品种,E:沪深交易所股票和基金, INDEX:沪深交易所指数, X:其他证券品种,大致如下:
                     支持的扩展行情包括(asset='X'):
                            郑州商品期权         OZ 大连商品期权         OD 上海商品期权         OS
                            上海个股期权         QQ 香港指数         FH 郑州商品         QZ 大连商品         QD 上海期货         QS
                            香港主板         KH 香港权证         KR 开放式基金         FU 货币型基金         FB
                            招商理财产品         LC 招商货币产品         LB 国际指数         FW 国内宏观指标         HG 中国概念股         CH
                            美股知名公司         MG B股转H股         HB 股份转让         SB 股指期货         CZ 香港创业板         KG 香港信托基金         KT
                             国债预发行         GY 主力期货合约         MA
                              中证指数         ZZ 港股通         GH
    market:市场代码,通过ts.get_markets()获取
                  
    Return
    ----------
    DataFrame
    date:日期
    time:时间
    price:成交价
    vol:成交量
    type:买卖方向,0-买入 1-卖出 2-集合竞价成交
            期货  0:开仓  1:多开   -1:空开
         期货多一列数据oi_change:增仓数据

    """
    code = code.strip().upper()
    date = int(date.replace('-', ''))
    today = int(str(du.today()).replace('-', ''))
    for _ in range(retry_count):
        try:
            if conn is None:
                print(ct.MSG_NOT_CONNECTED)
                return None
            api, xapi = conn
            data = pd.DataFrame()
            mkcode = _get_mkcode(code, asset=asset,
                                 xapi=xapi) if market == '' else market
            con = api if asset in ['E', 'INDEX'] else xapi
            for i in range(200):
                if date == today:
                    ds = con.get_transaction_data(market=mkcode,
                                                  code=code,
                                                  start=i * 300,
                                                  count=300)
                else:
                    ds = con.get_history_transaction_data(market=mkcode,
                                                          code=code,
                                                          date=date,
                                                          start=i * 300,
                                                          count=300)
                df = api.to_df(ds)
                data = data.append(df) if i == 0 else df.append(
                    data, ignore_index=True)
                if len(ds) < 300:
                    break
            if asset in ['E', 'INDEX']:
                data['date'] = date
                data['date'] = data['date'].map(lambda x: '%s-%s-%s ' % (str(
                    x)[0:4], str(x)[4:6], str(x)[6:8]))
                data['datetime'] = data['date'] + data['time']
                data = data[['datetime', 'price', 'vol', 'buyorsell']]
                data.columns = ['datetime', 'price', 'vol', 'type']
            else:
                if mkcode in [31, 71]:
                    if date == today:
                        data = data.drop([
                            'hour', 'minute', 'nature_name', 'zengcang',
                            'direction', 'second', 'nature_mark',
                            'nature_value'
                        ],
                                         axis=1)
                    else:
                        data = data.drop([
                            'hour', 'minute', 'nature_name', 'zengcang',
                            'direction'
                        ],
                                         axis=1)
                    data.loc[data.nature == 512, 'nature'] = 2
                    data.loc[data.nature == 256, 'nature'] = 1
                    data = data.sort_values('date')
                    data.columns = ['date', 'price', 'vol', 'type']
                elif mkcode in [28, 29, 30, 47, 60]:
                    if date == today:
                        data = data.drop([
                            'hour', 'minute', 'nature', 'direction', 'second',
                            'nature_mark', 'nature_value'
                        ],
                                         axis=1)
                    else:
                        data = data.drop(
                            ['hour', 'minute', 'nature', 'direction'], axis=1)
                    data.columns = [
                        'date', 'price', 'vol', 'oi_change', 'type'
                    ]
                else:
                    data = data.drop([
                        'hour', 'minute', 'nature_name', 'zengcang',
                        'direction', 'nature'
                    ],
                                     axis=1)

        except Exception as e:
            print(e)
        else:
            return data
Пример #25
0
def get_h_data(code,
               start=None,
               end=None,
               autype='qfq',
               retry_count=3,
               pause=0.001):
    '''
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volumn 成交量
          amount 成交金额
    '''

    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    ct._write_head()
    data = _parse_fq_data(
        ct.HIST_FQ_URL %
        (ct.P_TYPE['http'], ct.DOMAINS['vsf'], code, qt[0], qt[1]),
        retry_count, pause)
    if len(qs) > 1:
        for d in range(1, len(qs)):
            qt = qs[d]
            ct._write_console()
            url = ct.HIST_FQ_URL % (ct.P_TYPE['http'], ct.DOMAINS['vsf'], code,
                                    qt[0], qt[1])
            df = _parse_fq_data(url, retry_count, pause)
            data = data.append(df, ignore_index=True)
    data = data.drop_duplicates('date')
    if start is not None:
        data = data[data.date >= start]
    if end is not None:
        data = data[data.date <= end]
    if autype == 'hfq':
        data = data.drop('factor', axis=1)
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label].map(ct.FORMAT)
        data = data.set_index('date')
        data = data.sort_index(ascending=False)
        return data
    else:
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label] / data['factor']
        data = data.drop('factor', axis=1)
        if autype == 'qfq':
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates('date')
            df = df[df.date >= start]
            df = df[df.date <= end]
            df = pd.merge(data, df)
            df = df.sort('date', ascending=False)
            frow = df.head(1)
            rate = float(frow['close']) / float(frow['factor'])
            df['close_temp'] = df['close']
            df['close'] = rate * df['factor']
            for label in ['open', 'high', 'low']:
                df[label] = df[label] * (df['close'] / df['close_temp'])
                df[label] = df[label].map(ct.FORMAT)
            df = df.drop(['factor', 'close_temp'], axis=1)
            df['close'] = df['close'].map(ct.FORMAT)
            df = df.set_index('date')
            df = df.sort_index(ascending=False)
            df = df.astype(float)
            return df
        else:
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index('date')
            data = data.sort_index(ascending=False)
            data = data.astype(float)
            return data
Пример #26
0
def get_h_data(code, start=None, end=None, autype="qfq", index=False, retry_count=3, pause=0.001, drop_factor=True):
    """
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
      drop_factor : bool, 默认 True
                是否移除复权因子,在分析过程中可能复权因子意义不大,但是如需要先储存到数据库之后再分析的话,有该项目会更加灵活
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交金额
    """

    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    ct._write_head()
    data = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause)
    if len(qs) > 1:
        for d in range(1, len(qs)):
            qt = qs[d]
            ct._write_console()
            df = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause)
            data = data.append(df, ignore_index=True)
    if len(data) == 0 or len(data[(data.date >= start) & (data.date <= end)]) == 0:
        return None
    data = data.drop_duplicates("date")
    if index:
        data = data[(data.date >= start) & (data.date <= end)]
        data = data.set_index("date")
        data = data.sort_index(ascending=False)
        return data
    if autype == "hfq":
        if drop_factor:
            data = data.drop("factor", axis=1)
        data = data[(data.date >= start) & (data.date <= end)]
        for label in ["open", "high", "close", "low"]:
            data[label] = data[label].map(ct.FORMAT)
            data[label] = data[label].astype(float)
        data = data.set_index("date")
        data = data.sort_index(ascending=False)
        return data
    else:
        if autype == "qfq":
            if drop_factor:
                data = data.drop("factor", axis=1)
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates("date")
            df = df.sort("date", ascending=False)
            frow = df.head(1)
            rt = get_realtime_quotes(code)
            if rt is None:
                return None
            if (float(rt["high"]) == 0) & (float(rt["low"]) == 0):
                preClose = float(rt["pre_close"])
            else:
                if du.is_holiday(du.today()):
                    preClose = float(rt["price"])
                else:
                    if (du.get_hour() > 9) & (du.get_hour() < 18):
                        preClose = float(rt["pre_close"])
                    else:
                        preClose = float(rt["price"])

            rate = float(frow["factor"]) / preClose
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ["open", "high", "low", "close"]:
                data[label] = data[label] / rate
                data[label] = data[label].map(ct.FORMAT)
                data[label] = data[label].astype(float)
            data = data.set_index("date")
            data = data.sort_index(ascending=False)
            return data
        else:
            for label in ["open", "high", "close", "low"]:
                data[label] = data[label] / data["factor"]
            if drop_factor:
                data = data.drop("factor", axis=1)
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ["open", "high", "close", "low"]:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index("date")
            data = data.sort_index(ascending=False)
            data = data.astype(float)
            return data
Пример #27
0
def get_h_data(code, start=None, end=None, autype='qfq',
               index=False, retry_count=3, pause=0.001, drop_factor=True):
    '''
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
      drop_factor : bool, 默认 True
                是否移除复权因子,在分析过程中可能复权因子意义不大,但是如需要先储存到数据库之后再分析的话,有该项目会更加灵活
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交金额
    '''
    print("本接口即将停止更新,请尽快使用Pro版接口:https://waditu.com/document/2")
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    ct._write_head()
    data = _parse_fq_data(_get_index_url(index, code, qt), index,
                          retry_count, pause)
    if data is None:
        data = pd.DataFrame()
    if len(qs)>1:
        for d in range(1, len(qs)):
            qt = qs[d]
            ct._write_console()
            df = _parse_fq_data(_get_index_url(index, code, qt), index,
                                retry_count, pause)
            if df is None:  # 可能df为空,退出循环
                break
            else:
                data = data.append(df, ignore_index = True)
    if len(data) == 0 or len(data[(data.date >= start) & (data.date <= end)]) == 0:
        return pd.DataFrame()
    data = data.drop_duplicates('date')
    if index:
        data = data[(data.date >= start) & (data.date <= end)]
        data = data.set_index('date')
        data = data.sort_index(ascending = False)
        return data
    if autype == 'hfq':
        if drop_factor:
            data = data.drop('factor', axis=1)
        data = data[(data.date >= start) & (data.date <= end)]
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label].map(ct.FORMAT)
            data[label] = data[label].astype(float)
        data = data.set_index('date')
        data = data.sort_index(ascending = False)
        return data
    else:
        if autype == 'qfq':
            if drop_factor:
                data = data.drop('factor', axis = 1)
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates('date')
            df = df.sort_values('date', ascending = False)
            firstDate = data.head(1)['date']
            frow = df[df.date == firstDate[0]]
            rt = get_realtime_quotes(code)
            if rt is None:
                return pd.DataFrame()
            if ((float(rt['high']) == 0) & (float(rt['low']) == 0)):
                preClose = float(rt['pre_close'])
            else:
                if du.is_holiday(du.today()):
                    preClose = float(rt['price'])
                else:
                    if (du.get_hour() > 9) & (du.get_hour() < 18):
                        preClose = float(rt['pre_close'])
                    else:
                        preClose = float(rt['price'])
            
            rate = float(frow['factor']) / preClose
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ['open', 'high', 'low', 'close']:
                data[label] = data[label] / rate
                data[label] = data[label].map(ct.FORMAT)
                data[label] = data[label].astype(float)
            data = data.set_index('date')
            data = data.sort_index(ascending = False)
            return data
        else:
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label] / data['factor']
            if drop_factor:
                data = data.drop('factor', axis=1)
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index('date')
            data = data.sort_index(ascending = False)
            data = data.astype(float)
            return data
Пример #28
0
import pandas as pd
import tushare as ts
from sqlalchemy import create_engine
from tushare.util import dateu as du
import threading
import time
import TsExt as te
from util.util import last_tddate_delta


if __name__ == '__main__':


    engine = create_engine('mysql+pymysql://root:[email protected]/tushare?charset=utf8')

    td = du.today()
    if(du.is_holiday(td) | int(du.get_hour()) < 16):
        dt = du.last_tddate()
    else:
        dt = td
    print(dt)


    """数据表
    stk_lhb_broker_detail   龙虎榜股票营业部买入详细情况
    stk_k_line_data         股票K线数据
    """

    """
    获取股票基本信息
    """
Пример #29
0
def get_h_data(code, start=None, end=None, autype='qfq',
               index=False, retry_count=3, pause=0.001):
    '''
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交金额
    '''
    
    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    ct._write_head()
    data = _parse_fq_data(_get_index_url(index, code, qt), index,
                          retry_count, pause)
    if len(qs)>1:
        for d in range(1, len(qs)):
            qt = qs[d]
            ct._write_console()
            df = _parse_fq_data(_get_index_url(index, code, qt), index,
                                retry_count, pause)
            data = data.append(df, ignore_index=True)
    if len(data) == 0 or len(data[(data.date>=start)&(data.date<=end)]) == 0:
        return None
    data = data.drop_duplicates('date')
    if index:
        data = data[(data.date>=start) & (data.date<=end)]
        data = data.set_index('date')
        data = data.sort_index(ascending=False)
        return data
    if autype == 'hfq':
        data = data.drop('factor', axis=1)
        data = data[(data.date>=start) & (data.date<=end)]
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label].map(ct.FORMAT)
            data[label] = data[label].astype(float)
        data = data.set_index('date')
        data = data.sort_index(ascending = False)
        return data
    else:
        if autype == 'qfq':
            data = data.drop('factor', axis=1)
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates('date')
            df = df.sort('date', ascending=False)
            frow = df.head(1)
            rt = get_realtime_quotes(code)
            if rt is None:
                return None
            if ((float(rt['high']) == 0) & (float(rt['low']) == 0)):
                preClose = float(rt['pre_close'])
            else:
                if du.is_holiday(du.today()):
                    preClose = float(rt['price'])
                else:
                    if (du.get_hour() > 9) & (du.get_hour() < 18):
                        preClose = float(rt['pre_close'])
                    else:
                        preClose = float(rt['price'])
            
            rate = float(frow['factor']) / preClose
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ['open', 'high', 'low', 'close']:
                data[label] = data[label] / rate
                data[label] = data[label].map(ct.FORMAT)
                data[label] = data[label].astype(float)
            data = data.set_index('date')
            data = data.sort_index(ascending = False)
            return data
        else:
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label] / data['factor']
            data = data.drop('factor', axis=1)
            data = data[(data.date>=start) & (data.date<=end)]
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index('date')
            data = data.sort_index(ascending=False)
            data = data.astype(float)
            return data
Пример #30
0
# -*- coding:utf-8 -*-
"""
@author: wenhq
"""

import os
import tushare as ts
import tushare.util.dateu as dateu

from tushare.util.dateu import is_holiday

tradeyear = str(dateu.get_year())
tradeyear_path = os.path.join(os.getcwd(), 'DataYes', tradeyear)

# 按年创建每天行情数据文件
if not os.path.exists(tradeyear_path):
    os.makedirs(tradeyear_path)
# 保存文件
if not is_holiday(dateu.today()):
    df = ts.get_today_all()
    df.to_csv(os.path.join(tradeyear_path,
                           ''.join([dateu.today().replace('-', ''), '.csv'])),
              encoding='gb18030')
Пример #31
0
def get_k_data(code=None,
               start='',
               end='',
               ktype='D',
               autype='qfq',
               index=False,
               retry_count=3,
               pause=0.001):
    """
    获取k线数据
    ---------
    Parameters:
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取上市首日
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取最近一个交易日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      ktype:string
                  数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交额
          turnoverratio 换手率
          code 股票代码
    """
    symbol = ct.INDEX_SYMBOL[code] if index else _code_to_symbol(code)
    url = ''
    dataflag = ''
    autype = '' if autype is None else autype
    if (start is not None) & (start != ''):
        end = du.today() if end is None or end == '' else end
    if ktype.upper() in ct.K_LABELS:
        fq = autype if autype is not None else ''
        if code[:1] in ('1', '5') or index:
            fq = ''
        kline = '' if autype is None else 'fq'
        if (start is None or start == '') & (end is None or end == ''):
            urls = [
                ct.KLINE_TT_URL %
                (ct.P_TYPE['http'], ct.DOMAINS['tt'], kline, fq, symbol,
                 ct.TT_K_TYPE[ktype.upper()], start, end, fq, _random(17))
            ]
        else:
            years = du.tt_dates(start, end)
            urls = []
            for year in years:
                startdate = str(year) + '-01-01'
                enddate = str(year + 1) + '-12-31'
                url = ct.KLINE_TT_URL % (ct.P_TYPE['http'], ct.DOMAINS['tt'],
                                         kline, fq + str(year), symbol,
                                         ct.TT_K_TYPE[ktype.upper()],
                                         startdate, enddate, fq, _random(17))
                urls.append(url)
        dataflag = '%s%s' % (fq, ct.TT_K_TYPE[ktype.upper()])
    elif ktype in ct.K_MIN_LABELS:
        urls = [
            ct.KLINE_TT_MIN_URL % (ct.P_TYPE['http'], ct.DOMAINS['tt'], symbol,
                                   ktype, ktype, _random(16))
        ]
        dataflag = 'm%s' % ktype
    else:
        raise TypeError('ktype input error.')
    data = pd.DataFrame()
    for url in urls:
        data = data.append(_get_k_data(url, dataflag, symbol, code, index,
                                       ktype, retry_count, pause),
                           ignore_index=True)
    if ktype not in ct.K_MIN_LABELS:
        if ((start is not None) & (start != '')) & ((end is not None) &
                                                    (end != '')):
            if data.empty == False:
                data = data[(data.date >= start) & (data.date <= end)]
    return data
    raise IOError(ct.NETWORK_URL_ERROR_MSG)
Пример #32
0
Desc: 
File: ts_bar.py
Author: wangjinyu
Date: 2018/1/17 16:45
"""
import os
import sys
import tushare as ts
from code.pub.common import DATA_PATH, PATH_SEP
from tushare.fund import cons as ct
from tushare.util import dateu as du

reload(sys)
sys.setdefaultencoding('utf-8')

today = du.today()
profit_data_file = DATA_PATH + PATH_SEP + 'profit_data' + today + '.csv'
# 删除一周前同一天文件,如果天天运行,则只保留一周数据
del_file = DATA_PATH + PATH_SEP + 'profit_data' + du.day_last_week() + '.csv'
if os.path.exists(del_file):
    os.remove(del_file)

if not os.path.exists(profit_data_file):
    df = ts.profit_data(year=today[:4])
    if df.loc[0, 'code'] == '000nan':
        df = ts.profit_data(year=int(today[:4]) - 1)
    df.to_csv(profit_data_file,
              sep=',',
              header=True,
              index=False,
              encoding='GBK')
Пример #33
0
def get_h_data(code,
               start=None,
               end=None,
               autype='qfq',
               index=False,
               retry_count=3,
               pause=0.001):
    '''
    获取历史复权数据
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取当前日期
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取去年今日
      autype:string
                  复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          date 交易日期 (index)
          open 开盘价
          high  最高价
          close 收盘价
          low 最低价
          volume 成交量
          amount 成交金额
    '''

    start = du.today_last_year() if start is None else start
    end = du.today() if end is None else end
    qs = du.get_quarts(start, end)
    qt = qs[0]
    ct._write_head()
    data = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count,
                          pause)
    if len(qs) > 1:
        for d in range(1, len(qs)):
            qt = qs[d]
            ct._write_console()
            df = _parse_fq_data(_get_index_url(index, code, qt), index,
                                retry_count, pause)
            data = data.append(df, ignore_index=True)
    if len(data) == 0 or len(
            data[(data.date >= start) & (data.date <= end)]) == 0:
        return None
    data = data.drop_duplicates('date')
    if index:
        data = data[(data.date >= start) & (data.date <= end)]
        data = data.set_index('date')
        data = data.sort_index(ascending=False)
        return data
    if autype == 'hfq':
        data = data.drop('factor', axis=1)
        data = data[(data.date >= start) & (data.date <= end)]
        for label in ['open', 'high', 'close', 'low']:
            data[label] = data[label].map(ct.FORMAT)
            data[label] = data[label].astype(float)
        data = data.set_index('date')
        data = data.sort_index(ascending=False)
        return data
    else:
        if autype == 'qfq':
            data = data.drop('factor', axis=1)
            df = _parase_fq_factor(code, start, end)
            df = df.drop_duplicates('date')
            df = df.sort('date', ascending=False)
            frow = df.head(1)
            rt = get_realtime_quotes(code)
            if rt is None:
                return None
            if ((float(rt['high']) == 0) & (float(rt['low']) == 0)):
                preClose = float(rt['pre_close'])
            else:
                if du.is_holiday(du.today()):
                    preClose = float(rt['price'])
                else:
                    if (du.get_hour() > 9) & (du.get_hour() < 18):
                        preClose = float(rt['pre_close'])
                    else:
                        preClose = float(rt['price'])

            rate = float(frow['factor']) / preClose
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ['open', 'high', 'low', 'close']:
                data[label] = data[label] / rate
                data[label] = data[label].map(ct.FORMAT)
                data[label] = data[label].astype(float)
            data = data.set_index('date')
            data = data.sort_index(ascending=False)
            return data
        else:
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label] / data['factor']
            data = data.drop('factor', axis=1)
            data = data[(data.date >= start) & (data.date <= end)]
            for label in ['open', 'high', 'close', 'low']:
                data[label] = data[label].map(ct.FORMAT)
            data = data.set_index('date')
            data = data.sort_index(ascending=False)
            data = data.astype(float)
            return data
Пример #34
0
def lhb_details(retry_count=3, pause=0.001, code=None, date=None):
    """
	获取最近一个交易日龙虎榜上榜个股的交易详情. 包括买1-5和卖1-5的买入及卖出量,营业部名称.

	可选参数:
	证券代码(code): 指定代码用于只获取该证券的交易详情.省略的话会返回当日所有上榜证券的交易详情
	日期(date): 获取指定日期的交易详情.默认为最近一个交易日

	返回值:
	----------
	code:股票代码
	name:股票名称     
	date:交易日期     
	买1-5营业部名称及买入和卖出额及净额(万)    
	卖1-5营业部名称及买入和卖出额净额(万)    

	"""
    if date is None:
        if du.get_hour() < 18:
            date = du.last_tddate()
        else:
            date = du.today()
    else:
        if (du.is_holiday(date)):
            return None

    if code is None:
        print('请输入股票代码,比如: lhb_details(000001)')
        raise SystemExit(5)

    lhb_details_url = 'http://vip.stock.finance.sina.com.cn/q/api/jsonp.php/var%20details=/InvestConsultService.getLHBComBSData?symbol=%s&tradedate=%s&type=01'
    text = urlopen(lhb_details_url % (code, date), timeout=10).read()
    text = text.decode('GBK')
    text = text.split('details=')[1]
    text = eval(text,
                type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))())
    text = json.dumps(text)
    text = json.loads(text)
    ## text['buy'] = list of dicts
    ## text['sell'] = list of dicts

    #for i in range(len(text['buy'])):
    #	for key in text['buy'][i].keys():
    #		print(text['buy'][i][key],end=' ')
    #	print()
    #SYMBOL	sellAmount	buyAmount	comCode	type	netAmount	comName
    #000973 7.6802		2101.4900	80291363 01	2093.8098 浙商证券股份有限公司临安万马路证券营业部
    #000973 0.0000		1777.2700	80348499 01 1777.27 华泰证券股份有限公司浙江分公司
    #000973 317.3780	2069.5400	80127954 01 1752.162 安信证券股份有限公司南昌胜利路证券营业部
    #000973 0.9050		1389.3300	80564863 01 1388.425 华鑫证券有限责任公司郑州普惠路证券营业部
    #000973 39.0354		1239.5100	80034811 01 1200.4746 财通证券股份有限公司绍兴人民中路证券营业部

    print('%4s %6s %10s %10s %10s %s' %
          ('买入榜', '股票代码', '卖出额(万)', '买入额(万)', '买入净额(万)', '营业部名称'))
    for i in range(len(text['buy'])):
        print('%-4d %-8s %-10s %-10s %-12s %s' %(i+1,text['buy'][i]['SYMBOL'],text['buy'][i]['sellAmount'],\
         text['buy'][i]['buyAmount'],text['buy'][i]['netAmount'],text['buy'][i]['comName']))
    #1    000973   7.6802   2101.4900  2093.8098    浙商证券股份有限公司临安万马路证券营业部
    #2    000973   0.0000   1777.2700  1777.27      华泰证券股份有限公司浙江分公司
    #3    000973   317.3780 2069.5400  1752.162     安信证券股份有限公司南昌胜利路证券营业部
    #4    000973   0.9050   1389.3300  1388.425     华鑫证券有限责任公司郑州普惠路证券营业部
    #5    000973   39.0354  1239.5100  1200.4746    财通证券股份有限公司绍兴人民中路证券营业部

    print('%4s %6s %10s %10s %10s %s' %
          ('卖出榜', '股票代码', '卖出额(万)', '买入额(万)', '买入净额(万)', '营业部名称'))
    for i in range(len(text['sell'])):
        print('%-4d %-8s %-10s %-10s %-12s %s' %(i+1,text['sell'][i]['SYMBOL'],text['sell'][i]['sellAmount'],\
         text['sell'][i]['buyAmount'],text['sell'][i]['netAmount'],text['sell'][i]['comName']))