Example #1
0
def get_sina_tick_js_LastPrice(symbols):
    symbols_list=''
    if len(symbols) == 0:
        return ''
    if isinstance(symbols, list) or isinstance(symbols, set) or isinstance(symbols, tuple) or isinstance(symbols, pd.Series):
        for code in symbols:
            symbols_list += cct.code_to_symbol(code) + ','
    else:
        symbols_list = cct.code_to_symbol(symbols)
    # print symbol_str
    url="http://hq.sinajs.cn/list=%s"%(symbols_list)
    # print url
    data = cct.get_url_data(url)
    # vollist=re.findall('{data:(\d+)',code)
    # print data
    ulist=data.split(";")
    price_dict={}
    for var in range(0,len(ulist)-1):
        # print var
        if len(ulist)==2:
            code=symbols
        else:
            code=symbols[var]
        tempData = re.search('''(")(.+)(")''', ulist[var]).group(2)
        stockInfo = tempData.split(",")
        # stockName   = stockInfo[0]  #名称
        # stockStart  = stockInfo[1]  #开盘
        stockLastEnd= stockInfo[2]  #昨收盘
        # stockCur    = stockInfo[3]  #当前
        # stockMax    = stockInfo[4]  #最高
        # stockMin    = stockInfo[5]  #最低
        # price_dict[code]=stockLastEnd
        price_dict[code]=float(stockLastEnd)

        # stockUp     = round(float(stockCur) - float(stockLastEnd), 2)
        # stockRange  = round(float(stockUp) / float(stockLastEnd), 4) * 100
        # stockVolume = round(float(stockInfo[8]) / (100 * 10000), 2)
        # stockMoney  = round(float(stockInfo[9]) / (100000000), 2)
        # stockTime   = stockInfo[31]
        # dd={}
    return price_dict
Example #2
0
def get_tdx_Exp_day_to_df(code, type="f", start=None, end=None, dt=None, dl=None):
    # start=cct.day8_to_day10(start)
    # end=cct.day8_to_day10(end)
    # day_path = day_dir % 'sh' if code[:1] in ['5', '6', '9'] else day_dir % 'sz'
    code_u = cct.code_to_symbol(code)
    log.debug("code:%s code_u:%s" % (code, code_u))
    if type == "f":
        file_path = exp_path + "forwardp" + path_sep + code_u.upper() + ".txt"
    elif type == "b":
        file_path = exp_path + "backp" + path_sep + code_u.upper() + ".txt"
    else:
        return None
    log.debug("daypath:%s" % file_path)
    # p_day_dir = day_path.replace('/', path_sep).replace('\\', path_sep)
    # p_exp_dir = exp_dir.replace('/', path_sep).replace('\\', path_sep)
    # print p_day_dir,p_exp_dir
    if not os.path.exists(file_path):
        # ds = Series(
        #     {'code': code, 'date': cct.get_today(), 'open': 0, 'high': 0, 'low': 0, 'close': 0, 'amount': 0,
        #      'vol': 0})
        ds = pd.DataFrame()
        log.error("file_path:not exists")
        return ds
    # ofile = open(file_path, 'rb')
    if dt is None and dl is None:
        ofile = open(file_path, "rb")
        buf = ofile.readlines()
        ofile.close()
        num = len(buf)
        no = num - 1
        dt_list = []
        for i in xrange(no):
            a = buf[i].split(",")
            # 01/15/2016,27.57,28.15,26.30,26.97,714833.15,1946604544.000
            # da=a[0].split('/')
            tdate = a[0]
            # tdate = str(a[0])[:4] + '-' + str(a[0])[4:6] + '-' + str(a[0])[6:8]
            # tdate=dt.strftime('%Y-%m-%d')
            topen = float(a[1])
            thigh = float(a[2])
            tlow = float(a[3])
            tclose = float(a[4])
            # tvol = round(float(a[5]) / 10, 2)
            tvol = float(a[5])
            amount = round(float(a[6].replace("\r\n", "")), 1)  # int
            # tpre = int(a[7])  # back
            if int(amount) == 0:
                continue
            dt_list.append(
                {
                    "code": code,
                    "date": tdate,
                    "open": topen,
                    "high": thigh,
                    "low": tlow,
                    "close": tclose,
                    "amount": amount,
                    "vol": tvol,
                }
            )
            # if dt is not None and tdate < dt:
            #     break
        df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
        # df.sort_index(ascending=False, inplace=True)
        if start is not None and end is not None:
            df = df[(df.date >= start) & (df.date <= end)]
        elif end is not None:
            df = df[df.date <= end]
        elif start is not None:
            df = df[df.date >= start]
        df = df.set_index("date")
        return df
    elif int(dl) == 1:
        # fileSize = os.path.getsize(file_path)
        # if fileSize < 60 * newstockdayl:
        #     return Series()
        data = cct.read_last_lines(file_path, int(dl) + 3)
        data_l = data.split("\n")
        dt_list = Series()
        data_l.reverse()
        log.debug("day 1:%s" % data_l)
        for line in data_l:
            a = line.split(",")
            # 01/15/2016,27.57,28.15,26.30,26.97,714833.15,1946604544.000
            # da=a[0].split('/')
            log.debug("day 1 len(a):%s a:%s" % (len(a), a))
            if len(a) > 5:
                tdate = a[0]
                log.debug("day 1 tdate:%s" % tdate)
                # tdate = str(a[0])[:4] + '-' + str(a[0])[4:6] + '-' + str(a[0])[6:8]
                # tdate=dt.strftime('%Y-%m-%d')
                topen = float(a[1])
                thigh = float(a[2])
                tlow = float(a[3])
                tclose = float(a[4])
                # tvol = round(float(a[5]) / 10, 2)
                tvol = float(a[5])
                amount = round(float(a[6].replace("\r\n", "")), 1)  # int
                # tpre = int(a[7])  # back
                if int(amount) == 0:
                    continue
                dt_list = Series(
                    {
                        "code": code,
                        "date": tdate,
                        "open": topen,
                        "high": thigh,
                        "low": tlow,
                        "close": tclose,
                        "amount": amount,
                        "vol": tvol,
                    }
                )
                break
            else:
                continue
                # if dt is not None and tdate < dt:
                #     break
        # df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
        # df = df.set_index('date')
        return dt_list

    else:
        fileSize = os.path.getsize(file_path)
        # if fileSize < 60 * newstockdayl:
        # return Series()
        data = cct.read_last_lines(file_path, int(dl) + 2)
        dt_list = []
        data_l = data.split("\n")
        data_l.reverse()
        for line in data_l:
            a = line.split(",")
            # 01/15/2016,27.57,28.15,26.30,26.97,714833.15,1946604544.000
            # da=a[0].split('/')
            if len(a) > 5:
                tdate = a[0]
                # tdate = str(a[0])[:4] + '-' + str(a[0])[4:6] + '-' + str(a[0])[6:8]
                # tdate=dt.strftime('%Y-%m-%d')
                topen = float(a[1])
                thigh = float(a[2])
                tlow = float(a[3])
                tclose = float(a[4])
                tvol = round(float(a[5]) / 10, 2)
                amount = round(float(a[6].replace("\r\n", "")), 1)  # int
                # tpre = int(a[7])  # back
                if int(amount) == 0:
                    continue
                dt_list.append(
                    {
                        "code": code,
                        "date": tdate,
                        "open": topen,
                        "high": thigh,
                        "low": tlow,
                        "close": tclose,
                        "amount": amount,
                        "vol": tvol,
                    }
                )
            else:
                continue
                # if dt is not None and tdate < dt:
                #     break
        df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
        # df.sort_index(ascending=False, inplace=True)
        # if start is not None and end is not None:
        #     df = df[(df.date >= start) & (df.date <= end)]
        # elif end is not None:
        #     df = df[df.date <= end]
        # elif start is not None:
        #     df = df[df.date >= start]
        df = df.set_index("date")
        # print "time:",(time.time()-time_s)*1000
        return df
Example #3
0
def get_tdx_day_to_df_last(code, dayl=1, type=0, dt=None, ptype="low", dl=None):
    """
    :param code:999999
    :param dayl:Duration Days
    :param type:TDX type
    :param dt:  Datetime
    :param ptype:low or high
    :return:Series or df
    """
    # dayl=int(dayl)
    # type=int(type)
    # print "t:",dayl,"type",type
    if not type == 0:
        f = lambda x: str((1000000 - int(x))) if x.startswith("0") else x
        code = f(code)
    code_u = cct.code_to_symbol(code)
    day_path = day_dir % "sh" if code.startswith(("5", "6", "9")) else day_dir % "sz"
    p_day_dir = day_path.replace("/", path_sep).replace("\\", path_sep)
    # p_exp_dir=exp_dir.replace('/',path_sep).replace('\\',path_sep)
    # print p_day_dir,p_exp_dir
    file_path = p_day_dir + code_u + ".day"
    if not os.path.exists(file_path):
        ds = Series(
            {"code": code, "date": cct.get_today(), "open": 0, "high": 0, "low": 0, "close": 0, "amount": 0, "vol": 0}
        )
        return ds
    ofile = file(file_path, "rb")
    b = 0
    e = 32
    if dayl == 1 and dt == None:
        log.debug("%s" % (dayl == 1 and dt == None))
        fileSize = os.path.getsize(file_path)
        if fileSize < 32:
            print "why", code
        ofile.seek(-e, 2)
        buf = ofile.read()
        ofile.close()
        a = unpack("IIIIIfII", buf[b:e])
        tdate = str(a[0])[:4] + "-" + str(a[0])[4:6] + "-" + str(a[0])[6:8]
        topen = float(a[1] / 100.0)
        thigh = float(a[2] / 100.0)
        tlow = float(a[3] / 100.0)
        tclose = float(a[4] / 100.0)
        amount = float(a[5] / 10.0)
        tvol = int(a[6])  # int
        # tpre = int(a[7])  # back
        dt_list = Series(
            {
                "code": code,
                "date": tdate,
                "open": topen,
                "high": thigh,
                "low": tlow,
                "close": tclose,
                "amount": amount,
                "vol": tvol,
            }
        )
        return dt_list
    elif dayl == 1 and dt is not None and dl is not None:
        log.debug("dt:%s" % (dt))
        dt_list = []
        # if len(str(dt)) == 8:
        # dt = cct.day8_to_day10(dt)
        # else:
        # dt=get_duration_price_date(code, ptype=ptype, dt=dt)
        # print ("dt:%s"%dt)
        fileSize = os.path.getsize(file_path)
        if fileSize < 32:
            print "why", code
        b = fileSize
        ofile.seek(-fileSize, 2)
        no = int(fileSize / e)
        # if no < newstockdayl:
        # return Series()
        # print no,b,day_cout,fileSize
        buf = ofile.read()
        ofile.close()
        # print repr(buf)
        # df=pd.DataFrame()
        for i in xrange(no):
            a = unpack("IIIIIfII", buf[-e:b])
            tdate = str(a[0])[:4] + "-" + str(a[0])[4:6] + "-" + str(a[0])[6:8]
            topen = float(a[1] / 100.0)
            thigh = float(a[2] / 100.0)
            tlow = float(a[3] / 100.0)
            tclose = float(a[4] / 100.0)
            amount = float(a[5] / 10.0)
            tvol = int(a[6])  # int
            # tpre = int(a[7])  # back
            dt_list.append(
                {
                    "code": code,
                    "date": tdate,
                    "open": topen,
                    "high": thigh,
                    "low": tlow,
                    "close": tclose,
                    "amount": amount,
                    "vol": tvol,
                }
            )
            # print series
            # dSeries.append(series)
            # dSeries.append(Series({'code':code,'date':tdate,'open':topen,'high':thigh,'low':tlow,'close':tclose,'amount':amount,'vol':tvol,'pre':tpre}))
            b = b - 32
            e = e + 32
            # print tdate,dt
            if tdate < dt:
                # print "why"
                break
        df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
        # print "len:%s %s"%(len(df),fileSize)
        df = df.set_index("date")
        dt = get_duration_price_date(code, ptype=ptype, dt=dt, df=df, dl=dl)
        log.debug("last_dt:%s" % dt)
        dd = df[df.index == dt]
        if len(dd) > 0:
            dd = dd[:1]
            dt = dd.index.values[0]
            dd = dd.T[dt]
            dd["date"] = dt
        else:
            log.warning("no < dt:NULL")
            dd = Series()
            # dd = Series(
            # {'code': code, 'date': cct.get_today(), 'open': 0, 'high': 0, 'low': 0, 'close': 0, 'amount': 0,
            # 'vol': 0})
        return dd
    else:
        dt_list = []
        fileSize = os.path.getsize(file_path)
        # print fileSize
        day_cout = abs(e * int(dayl))
        # print day_cout
        if day_cout > fileSize:
            b = fileSize
            ofile.seek(-fileSize, 2)
            no = int(fileSize / e)
        else:
            no = int(dayl)
            b = day_cout
            ofile.seek(-day_cout, 2)
        # print no,b,day_cout,fileSize
        buf = ofile.read()
        ofile.close()
        # print repr(buf)
        # df=pd.DataFrame()
        for i in xrange(no):
            a = unpack("IIIIIfII", buf[-e:b])
            tdate = str(a[0])[:4] + "-" + str(a[0])[4:6] + "-" + str(a[0])[6:8]
            topen = float(a[1] / 100.0)
            thigh = float(a[2] / 100.0)
            tlow = float(a[3] / 100.0)
            tclose = float(a[4] / 100.0)
            amount = float(a[5] / 10.0)
            tvol = int(a[6])  # int
            # tpre = int(a[7])  # back
            dt_list.append(
                {
                    "code": code,
                    "date": tdate,
                    "open": topen,
                    "high": thigh,
                    "low": tlow,
                    "close": tclose,
                    "amount": amount,
                    "vol": tvol,
                }
            )
            # print series
            # dSeries.append(series)
            # dSeries.append(Series({'code':code,'date':tdate,'open':topen,'high':thigh,'low':tlow,'close':tclose,'amount':amount,'vol':tvol,'pre':tpre}))
            b = b - 32
            e = e + 32
        df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
        df = df.set_index("date")
        return df
Example #4
0
def get_tdx_day_to_df(code):
    """
        获取个股历史交易记录
    Parameters
    ------
      code:string
                  股票代码 e.g. 600848
      start:string
                  开始日期 format:YYYY-MM-DD 为空时取到API所提供的最早日期数据
      end:string
                  结束日期 format:YYYY-MM-DD 为空时取到最近一个交易日数据
      ktype:string
                  数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
      retry_count : int, 默认 3
                 如遇网络等问题重复执行的次数 
      pause : int, 默认 0
                重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
    return
    -------
      DataFrame
          属性:日期 ,开盘价, 最高价, 收盘价, 最低价, 成交量, 价格变动 ,涨跌幅,5日均价,10日均价,20日均价,5日均量,10日均量,20日均量,换手率
    """
    # time_s=time.time()
    # print code
    code_u = cct.code_to_symbol(code)
    day_path = day_dir % "sh" if code[:1] in ["5", "6", "9"] else day_dir % "sz"
    p_day_dir = day_path.replace("/", path_sep).replace("\\", path_sep)
    # p_exp_dir = exp_dir.replace('/', path_sep).replace('\\', path_sep)
    # print p_day_dir,p_exp_dir
    file_path = p_day_dir + code_u + ".day"
    if not os.path.exists(file_path):
        ds = Series(
            {"code": code, "date": cct.get_today(), "open": 0, "high": 0, "low": 0, "close": 0, "amount": 0, "vol": 0}
        )
        return ds

    ofile = open(file_path, "rb")
    buf = ofile.read()
    ofile.close()
    num = len(buf)
    no = int(num / 32)
    b = 0
    e = 32
    dt_list = []
    for i in xrange(no):
        a = unpack("IIIIIfII", buf[b:e])
        # dt=datetime.date(int(str(a[0])[:4]),int(str(a[0])[4:6]),int(str(a[0])[6:8]))
        tdate = str(a[0])[:4] + "-" + str(a[0])[4:6] + "-" + str(a[0])[6:8]
        # tdate=dt.strftime('%Y-%m-%d')
        topen = float(a[1] / 100.0)
        thigh = float(a[2] / 100.0)
        tlow = float(a[3] / 100.0)
        tclose = float(a[4] / 100.0)
        amount = float(a[5] / 10.0)
        tvol = int(a[6])  # int
        tpre = int(a[7])  # back
        dt_list.append(
            {
                "code": code,
                "date": tdate,
                "open": topen,
                "high": thigh,
                "low": tlow,
                "close": tclose,
                "amount": amount,
                "vol": tvol,
                "pre": tpre,
            }
        )
        b = b + 32
        e = e + 32
    df = pd.DataFrame(dt_list, columns=ct.TDX_Day_columns)
    df = df.set_index("date")
    # print "time:",(time.time()-time_s)*1000
    return df