예제 #1
0
def query_all_stock(day=None):
    """查询给定日期的所有证券信息,

    @param day: 默认当前日期
    """
    data = rs.ResultData()

    if day is None or day == "":
        day = time.strftime("%Y-%m-%d", time.localtime())

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s" % ("query_all_stock", user_id, "1",
                                cons.BAOSTOCK_PER_PAGE_COUNT, day)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYALLSTOCK_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYALLSTOCK_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    try:
        crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    except TypeError:
        crc32str = zlib.crc32(bytes(head_body))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.day = body_arr[7]
        data.setFields(body_arr[8])

    return data
예제 #2
0
def query_pmi_data(start_date="", end_date=""):
    """采购经理人指数
    @param sart_date: 起始日期,包含次此日期,可为空
    @param end_date: 结束日期,包含次此日期,可为空
    """
    data = rs.ResultData()

    if start_date is None or start_date == "":
        start_date = ""
    else:
        if strUtil.is_valid_date(start_date):
            pass
        else:
            print("起始日期格式不正确,请修改。")
            data.error_code = cons.BSERR_DATE_ERR
            data.error_msg = "起始日期格式不正确,请修改。"
            return data
    if end_date is None or end_date == "":
        end_date = ""
    else:
        if strUtil.is_valid_date(end_date):
            pass
        else:
            print("结束日期格式不正确,请修改。")
            data.error_code = cons.BSERR_DATE_ERR
            data.error_msg = "结束日期格式不正确,请修改。"
            return data
    if start_date != "" and end_date != "":
        start_date_time = datetime.datetime.strptime(
            start_date, '%Y-%m-%d')
        end_date_time = datetime.datetime.strptime(end_date, '%Y-%m-%d')
        if end_date_time < start_date_time:
            print("起始日期大于结束日期,请修改。")
            data.error_code = cons.BSERR_START_BIGTHAN_END
            data.error_msg = "起始日期大于结束日期,请修改。"
            return data

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "query_pmi_data," + str(user_id) + ",1," + \
        str(cons.BAOSTOCK_PER_PAGE_COUNT) + \
        "," + str(start_date) + "," + str(end_date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYPMIDATA_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYPMIDATA_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT + str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.start_date = body_arr[7]
        data.end_date = body_arr[8]
        data.setFields(body_arr[9])

    return data
예제 #3
0
def query_trade_dates(start_date=None, end_date=None):
    """查询出给定范围的交易日信息

    @param start_date: 起始日期,默认2015-01-01
    @param end_date: 终止日期,默认当前日期
    @return: calendar_date 日期;is_trading_day,是否交易日,0:非交易日;1:交易日
    """
    data = rs.ResultData()

    if start_date is None or start_date == "":
        start_date = cons.DEFAULT_START_DATE
    if end_date is None or end_date == "":
        end_date = time.strftime("%Y-%m-%d", time.localtime())

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s,%s" % (
        "query_trade_dates", user_id, "1",
        cons.BAOSTOCK_PER_PAGE_COUNT, start_date, end_date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYTRADEDATES_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYTRADEDATES_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT + str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.start_date = body_arr[7]
        data.end_date = body_arr[8]
        data.setFields(body_arr[9])

    return data
예제 #4
0
def query_stock_basic(code="", code_name=""):
    """A股证券基本资料
    @param code: 证券代码,可为空
    @param code_name: 证券名称,可为空,支持模糊查询
    """
    data = rs.ResultData()

    if code is None or code == "":
        code = ""
    if code != "" and code is not None:
        if len(code) != cons.STOCK_CODE_LENGTH:
            print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。")
            data.error_msg = "股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
            data.error_code = cons.BSERR_PARAM_ERR
            return data
        code = code.lower()
        if (code.endswith("sh") or code.endswith("sz")):
            code = code[7:9].lower() + "." + code[0:6]

    if code_name is None or code_name == "":
        code_name = ""

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "query_stock_basic," + str(user_id) + ",1," + \
        str(cons.BAOSTOCK_PER_PAGE_COUNT) + \
        "," + str(code) + "," + str(code_name)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYSTOCKBASIC_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYSTOCKBASIC_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT + str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.code_name = body_arr[8]
        data.setFields(body_arr[9])

    return data
예제 #5
0
def query_stocks_in_risk(date=""):
    """获取风险警示板分类
    @param date:查询日期,默认为空。不为空时,格式 XXXX-XX-XX。
    """
    data = rs.ResultData()

    if date is None or date == "":
        date = ""
    else:
        if strUtil.is_valid_date(date):
            pass
        else:
            print("日期格式不正确,请修改。")
            data.error_code = cons.BSERR_DATE_ERR
            data.error_msg = "日期格式不正确,请修改。"
            return data
    user_id = getattr(conx, "user_id")
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "query_stocks_in_risk," + str(user_id) + ",1," + \
            str(cons.BAOSTOCK_PER_PAGE_COUNT) + \
            "," + str(date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYSTOCKINRISK_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYSTOCKINRISK_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.date = body_arr[7]
        data.setFields(body_arr[8])

    return data
예제 #6
0
def query_stock_area(code="", date=""):
    """获取地域分类
    @param code:股票代码,默认为空。
    @param date:查询日期,默认为空。不为空时,格式 XXXX-XX-XX。
    """
    data = rs.ResultData()

    if code is None or code == "":
        code = ""
    if code != "" and code is not None:
        if len(code) != cons.STOCK_CODE_LENGTH:
            print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) +
                  "位,请检查。格式示例:sh.600000。")
            data.error_msg = "股票代码应为" + str(
                cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
            data.error_code = cons.BSERR_PARAM_ERR
            return data
        code = code.lower()
        if (code.endswith("sh") or code.endswith("sz")):
            code = code[7:9].lower() + "." + code[0:6]

    if date is None or date == "":
        date = ""
    else:
        if strUtil.is_valid_date(date):
            pass
        else:
            print("日期格式不正确,请修改。")
            data.error_code = cons.BSERR_DATE_ERR
            data.error_msg = "日期格式不正确,请修改。"
            return data
    user_id = getattr(conx, "user_id")
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "query_stock_area," + str(user_id) + ",1," + \
        str(cons.BAOSTOCK_PER_PAGE_COUNT) + \
        "," + str(code) + "," + str(date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYSTOCKAREA_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYSTOCKAREA_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.date = body_arr[8]
        data.setFields(body_arr[9])

    return data
예제 #7
0
def query_cash_flow_data(code, year=None, quarter=None):
    """季频现金流量
    @param code: 证券代码,不可为空
    @param year: 统计年份,为空时默认当前年
    @param quarter: 统计季度,为空时默认当前季度
    """
    data = rs.ResultData()
    if code is None or code == "":
        print("股票代码不能为空,请检查。")
        data.error_msg = "股票代码不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if len(code) != cons.STOCK_CODE_LENGTH:
        print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。")
        data.error_msg = "股票代码应为" + str(
            cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    code = code.lower()
    if (code.endswith("sh") or code.endswith("sz")):
        code = code[7:9].lower() + "." + code[0:6]

    if year is None or year == "":
        year = time.strftime("%Y", time.localtime())  # 当前年份
    if quarter is None or quarter == "":
        quarter = (int(time.strftime("%m", time.localtime())) + 2) // 3  # 当前季度
    elif quarter not in [1, 2, 3, 4] and quarter not in ["1", "2", "3", "4"]:
        print("季度填写错误,请检查。填写范围:1,2,3,4")
        data.error_msg = "季度填写错误,请检查。填写范围:1,2,3,4"
        data.error_code = cons.BSERR_PARAM_ERR
    year = str(year)
    if not year.isdigit():
        print("年份输入有误,请修改。")
        data.error_msg = "年份输入有误,请修改。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s,%s,%s" % ("query_cash_flow_data", user_id, "1",
                                      cons.BAOSTOCK_PER_PAGE_COUNT, code, year,
                                      quarter)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYCASHFLOWDATA_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYCASHFLOWDATA_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.year = body_arr[8]
        data.quarter = body_arr[9]
        data.setFields(body_arr[10])
    return data
예제 #8
0
def query_dividend_data(code, year=None, yearType="report"):
    """估值指标(季频),股息分红
    @param code: 证券代码,不可为空
    @param year: 年份,为空时默认当前年份
    @param yearType: 年份类别,默认为"report":预案公告年份,可选项"operate":除权除息年份
    """

    data = rs.ResultData()

    if code is None or code == "":
        print("股票代码不能为空,请检查。")
        data.error_msg = "股票代码不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if len(code) != cons.STOCK_CODE_LENGTH:
        print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。")
        data.error_msg = "股票代码应为" + str(
            cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    code = code.lower()
    if (code.endswith("sh") or code.endswith("sz")):
        code = code[7:9].lower() + "." + code[0:6]

    if year is None or year == "":
        year = time.strftime("%Y", time.localtime())
    if yearType is None or yearType == "":
        print("年份类别输入有误,请修改。")
        data.error_msg = "年份类别输入有误,请修改。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    year = str(year)
    if not year.isdigit():
        print("年份输入有误,请修改。")
        data.error_msg = "年份输入有误,请修改。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s,%s,%s" % ("query_dividend_data", user_id, "1",
                                      cons.BAOSTOCK_PER_PAGE_COUNT, code, year,
                                      yearType)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYDIVIDENDDATA_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYDIVIDENDDATA_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.year = body_arr[8]
        data.yearType = body_arr[9]
        data.setFields(body_arr[10])

    return data
예제 #9
0
def query_adjust_factor(code, start_date=None, end_date=None):
    """复权因子信息
    @param code: 证券代码,不可为空
    @param start_date: 起始除权除息日期,为空时默认2015-01-01)
    @param end_date: 终止除权除息日期,为空时默认当前时间
    """

    data = rs.ResultData()

    if code is None or code == "":
        print("股票代码不能为空,请检查。")
        data.error_msg = "股票代码不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if len(code) != cons.STOCK_CODE_LENGTH:
        print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000")
        data.error_msg = "股票代码应为" + str(
            cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    code = code.lower()
    if (code.endswith("sh") or code.endswith("sz")):
        code = code[7:9].lower() + "." + code[0:6]

    if start_date is None or start_date == "":
        start_date = cons.DEFAULT_START_DATE
    if end_date is None or end_date == "":
        end_date = time.strftime("%Y-%m-%d", time.localtime())

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s,%s,%s" % ("query_adjust_factor", user_id, "1",
                                      cons.BAOSTOCK_PER_PAGE_COUNT, code,
                                      start_date, end_date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_ADJUSTFACTOR_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_ADJUSTFACTOR_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.start_date = body_arr[8]
        data.end_date = body_arr[9]
        data.setFields(body_arr[10])

    return data
예제 #10
0
def logout(user_id='anonymous'):
    """登出系统,默认用户ID:anonymous
    :param user_id:用户ID
    :return:ResultData()
    """

    now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

    if hasattr(conx, "user_id"):
        user_id = getattr(conx, "user_id")
        if user_id is None or user_id == "":
            print("you don't login, logout failed!")
            return

    # 组织体信息
    msg_body = "logout" + cons.MESSAGE_SPLIT + \
        user_id + cons.MESSAGE_SPLIT + now_time

    # 组织头信息
    msg_header = msgheader.to_message_header(cons.MESSAGE_TYPE_LOGOUT_REQUEST,
                                             len(msg_body))

    head_body = msg_header + msg_body

    try:
        crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    except TypeError:
        crc32str = zlib.crc32(bytes(head_body))

    # 发送并接收消息
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    data = rs.ResultData()

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]

    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)

    data.msg_type = header_arr[1]
    data.msg_body_length = header_arr[2]

    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        print("logout success!")
        data.method = body_arr[2]
        data.user_id = body_arr[3]
    else:
        print("logout failed!")

    if hasattr(conx, "defallt_socket"):
        if getattr(conx, "default_socket") is not None:
            getattr(conx, "default_socket").close()

    return data
예제 #11
0
def login(user_id='anonymous', password='******', options=0):
    """登录系统
    :param user_id:用户ID
    :param password:密码
    :param options:可选项,00.5.00版本暂未使用
    :return: ResultData()
    """

    data = rs.ResultData()
    if user_id is None or user_id == "":
        print("用户ID不能为空。")
        data.error_msg = "用户ID不能为空。"
        data.error_code = cons.BSERR_USERNAME_EMPTY
        return data

    setattr(conx, "user_id", user_id)

    if password is None or password == "":
        print("密码不能为空。")
        data.error_msg = "密码不能为空。"
        data.error_code = cons.BSERR_PASSWORD_EMPTY
        return data

    # 组织体信息
    msg_body = "login" + cons.MESSAGE_SPLIT + user_id + cons.MESSAGE_SPLIT + \
        password + cons.MESSAGE_SPLIT + str(options)

    # 组织头信息
    msg_header = msgheader.to_message_header(cons.MESSAGE_TYPE_LOGIN_REQUEST,
                                             len(msg_body))
    head_body = msg_header + msg_body

    try:
        crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    except TypeError:
        crc32str = zlib.crc32(bytes(head_body))

    # 发送并接收消息
    mySocketUtil = sock.SocketUtil()
    # 创建连接
    mySocketUtil.connect()

    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]

    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)

    data.msg_type = header_arr[1]
    data.msg_body_length = header_arr[2]

    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        print("login success!")
        data.method = body_arr[2]
        data.user_id = body_arr[3]
    else:
        print("login failed!")

    return data
예제 #12
0
def __query_history_k_data_page(cur_page_num, per_page_count, code, fields,
                                start_date, end_date, frequency, adjustflag):
    """获取历史K线,私有方法"""
    data = rs.ResultData()
    if code is None or code == "":
        print("股票代码不能为空,请检查。")
        data.error_msg = "股票代码不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if len(code) != cons.STOCK_CODE_LENGTH:
        print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。")
        data.error_msg = "股票代码应为" + str(
            cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    code = code.lower()
    if (code.endswith("sh") or code.endswith("sz")):
        code = code[7:9].lower() + "." + code[0:6]
    if fields is None or fields == "":
        data.error_msg = "指示简称不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        print("指示简称不能为空,请检查。")
        return data

    if start_date is None or start_date == "":
        start_date = cons.DEFAULT_START_DATE
    if end_date is None or end_date == "":
        end_date = time.strftime("%Y-%m-%d", time.localtime())

    if start_date != "" and end_date != "":
        if strUtil.is_valid_date(start_date) and strUtil.is_valid_date(
                end_date):
            start_date_time = datetime.datetime.strptime(
                start_date, '%Y-%m-%d')
            end_date_time = datetime.datetime.strptime(end_date, '%Y-%m-%d')
            if end_date_time < start_date_time:
                print("起始日期大于终止日期,请修改。")
                data.error_code = cons.BSERR_START_BIGTHAN_END
                data.error_msg = "起始日期大于终止日期,请修改。"
                return data
        else:
            print("日期格式不正确,请修改。")
            return

    if frequency is None or frequency == "":
        print("数据类型(frequency)不可为空,请检查。")
        data.error_msg = "数据类型(frequency)不可为空,请检查"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if adjustflag is None or adjustflag == "":
        print("复权类型(adjustflag)不可为空,请检查。")
        data.error_msg = "复权类型(adjustflag)不可为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    msg_body = "query_history_k_data" + cons.MESSAGE_SPLIT + user_id + cons.MESSAGE_SPLIT \
               + str(cur_page_num) + cons.MESSAGE_SPLIT + str(per_page_count) + cons.MESSAGE_SPLIT + code \
               + cons.MESSAGE_SPLIT + fields + cons.MESSAGE_SPLIT + start_date \
               + cons.MESSAGE_SPLIT + end_date + cons.MESSAGE_SPLIT + frequency \
               + cons.MESSAGE_SPLIT + adjustflag

    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_GETKDATA_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_GETKDATA_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))

    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]

    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)

    # data.version = header_arr[0]
    # data.msg_type = header_arr[1]
    data.msg_body_length = header_arr[2]

    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.setFields(body_arr[8])
        data.start_date = body_arr[9]
        data.end_date = body_arr[10]
        data.frequency = body_arr[11]
        data.adjustflag = body_arr[12]

    return data
예제 #13
0
def query_performance_express_report(code, start_date=None, end_date=None):
    """公司业绩快报。
    @param code: 证券代码,不可为空
    @param start_date: 开始日期,默认2015-01-01;发布日期或更新日期在这个范围内。
    @param end_date: 结束日期,默认系统当前日期;发布日期或更新日期在这个范围内。
    """

    data = rs.ResultData()
    if code is None or code == "":
        print("股票代码不能为空,请检查。")
        data.error_msg = "股票代码不能为空,请检查。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    if len(code) != cons.STOCK_CODE_LENGTH:
        print("股票代码应为" + str(cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。")
        data.error_msg = "股票代码应为" + str(
            cons.STOCK_CODE_LENGTH) + "位,请检查。格式示例:sh.600000。"
        data.error_code = cons.BSERR_PARAM_ERR
        return data
    code = code.lower()
    if (code.endswith("sh") or code.endswith("sz")):
        code = code[7:9].lower() + "." + code[0:6]

    if start_date is None or start_date == "":
        start_date = cons.DEFAULT_START_DATE
    if end_date is None or end_date == "":
        end_date = time.strftime("%Y-%m-%d", time.localtime())

    if start_date != "" and start_date is not None and end_date != "" and end_date is not None:
        if strUtil.is_valid_date(start_date) and strUtil.is_valid_date(
                end_date):
            start_date_time = datetime.datetime.strptime(
                start_date, '%Y-%m-%d')
            end_date_time = datetime.datetime.strptime(end_date, '%Y-%m-%d')
            if end_date_time < start_date_time:
                print("起始日期大于终止日期,请修改。")
                data.error_code = cons.BSERR_START_BIGTHAN_END
                data.error_msg = "起始日期大于终止日期,请修改。"
                return data
        else:
            print("日期格式不正确,请修改。")
            return

    user_id = ""
    try:
        user_id = getattr(conx, "user_id")
    except Exception:
        print("you don't login.")
        data.error_code = cons.BSERR_NO_LOGIN
        data.error_msg = "you don't login."
        return data

    param = "%s,%s,%s,%s,%s,%s,%s" % (
        "query_performance_express_report", user_id, "1",
        cons.BAOSTOCK_PER_PAGE_COUNT, code, start_date, end_date)

    msg_body = strUtil.organize_msg_body(param)
    msg_header = msgheader.to_message_header(
        cons.MESSAGE_TYPE_QUERYPERFORMANCEEXPRESSREPORT_REQUEST, len(msg_body))

    data.msg_type = cons.MESSAGE_TYPE_QUERYPERFORMANCEEXPRESSREPORT_REQUEST
    data.msg_body = msg_body

    head_body = msg_header + msg_body
    crc32str = zlib.crc32(bytes(head_body, encoding='utf-8'))
    receive_data = sock.send_msg(head_body + cons.MESSAGE_SPLIT +
                                 str(crc32str))

    if receive_data is None or receive_data.strip() == "":
        data.error_code = cons.BSERR_RECVSOCK_FAIL
        data.error_msg = "网络接收错误。"
        return data

    msg_header = receive_data[0:cons.MESSAGE_HEADER_LENGTH]
    msg_body = receive_data[cons.MESSAGE_HEADER_LENGTH:-1]
    header_arr = msg_header.split(cons.MESSAGE_SPLIT)
    body_arr = msg_body.split(cons.MESSAGE_SPLIT)
    data.msg_body_length = header_arr[2]
    data.error_code = body_arr[0]
    data.error_msg = body_arr[1]

    if cons.BSERR_SUCCESS == data.error_code:
        data.method = body_arr[2]
        data.user_id = body_arr[3]
        data.cur_page_num = body_arr[4]
        data.per_page_count = body_arr[5]
        data.setData(body_arr[6])
        data.code = body_arr[7]
        data.start_date = body_arr[8]
        data.end_date = body_arr[9]
        data.setFields(body_arr[10])

    return data