Ejemplo n.º 1
0
async def get_eastMoney_basic(stockList):
    stockBase = {}

    async with aiohttp.ClientSession() as session:
        tasks = [get(session, stock) for stock in stockList]
        doneSet, pendingSet = await asyncio.wait(tasks)

        print("成功个数", len(doneSet))
        print("失败个数", len(pendingSet))

        for doneInfo in doneSet:
            if doneInfo._result != None: # 因断连等原因导致返回为None时,这里过滤掉
                # print(doneInfo._result)
                stockBase.update(doneInfo._result)

        if pendingSet:
            print("失败了???")
            for pending in pendingSet:
                pending.cancel()

        file_path = txt_file_save_folder + "eastMoney.txt"
        with open(file_path, "a", encoding='utf-8') as f:
            f.write(str(Common().to_json(stockBase)))

        print(len(stockBase))
Ejemplo n.º 2
0
    def __init__(self, is_subscribe_record=False):
        if not SqliteDB.__first_init:
            # 先创建目录
            if not os.path.exists(db_save_folder):
                os.makedirs(db_save_folder)

            self.common = Common()
            self.logger = get_log()
            self.conn = sqlite3.connect(dbPath,
                                        check_same_thread=False,
                                        timeout=15)
            currentDayTimeStampInfo = self.common.getCurrentDayTimeStampInfo()
            self.todayBeginTimeStamp = currentDayTimeStampInfo[
                'todayBeginTimeStamp']
            self.todayEndTimeStamp = currentDayTimeStampInfo[
                'todayEndTimeStamp']
            self.is_subscribe_record = is_subscribe_record
            self.assemble_num = sql_assemble_num
            self.assemble_sql = ''
            self.assemble_float_num = 0
            self.transaction_num = sql_transaction_num
            self.transaction_float_num = 0
            # db_file = SETUP_DIR + "\\zmq_py3\\zmq_save_files\\db\\SIT_market_test.db"
            # if not os.path.exists(db_file):
            self.create_table()
            SqliteDB.__first_init = True
Ejemplo n.º 3
0
def QueryExchangeSort(exchange: str, count: int, sortFiled: str):
    frequence = None
    start_time_stamp = int(time.time() * 1000)
    # http = MarketHttpClient()
    # market_token = http.get_market_token(
    #     http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id))
    market_token = None
    loop = Common().getNewLoop()
    api = SubscribeApi(union_ws_url, loop)
    loop.run_until_complete(future=api.client.ws_connect())
    loop.run_until_complete(
        future=api.LoginReq(token=market_token,
                            start_time_stamp=start_time_stamp,
                            frequence=frequence))
    # asyncio.run_coroutine_threadsafe(api.hearbeat_job(), new_loop)
    rsp_sort_list = asyncio.get_event_loop().run_until_complete(
        future=api.QueryExchangeSortMsgReqApi(
            isSubTrade=None,
            exchange=exchange,
            sortFiled=sortFiled,
            count=count,
            start_time_stamp=start_time_stamp,
            recv_num=1))
    app_sort = rsp_sort_list[0]
    api.client.disconnect()
    return app_sort
Ejemplo n.º 4
0
 def __init__(self):
     super(TimerTask, self).__init__()
     self.logger = get_log("timerTask")
     self.max_instances = 20  # 最大并发数
     # self.scheduler = BlockingScheduler()
     self.scheduler = AsyncIOScheduler()
     self.common = Common()
Ejemplo n.º 5
0
 def __init__(self):
     self.common = Common()
     self.logger = get_log()
     self.http = MarketHttpClient()
     self.market_token = self.http.get_market_token(
         self.http.get_login_token(phone=login_phone,
                                   pwd=login_pwd,
                                   device_id=login_device_id))
     self.new_loop = self.common.getNewLoop()
     asyncio.set_event_loop(self.new_loop)
     self.api = SubscribeApi(union_ws_url, self.new_loop)
     asyncio.get_event_loop().run_until_complete(
         future=self.api.client.ws_connect())
     asyncio.get_event_loop().run_until_complete(future=self.api.LoginReq(
         token=self.market_token))
     asyncio.run_coroutine_threadsafe(self.api.hearbeat_job(),
                                      self.new_loop)
Ejemplo n.º 6
0
def StartChartData(exchange: str, code: str):
    # exchange = exchange
    # code = code

    frequence = None
    start_time_stamp = int(time.time() * 1000)
    # http = MarketHttpClient()
    # market_token = http.get_market_token(
    #     http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id))
    market_token = None
    loop = Common().getNewLoop()
    api = SubscribeApi(union_ws_url, loop)
    loop.run_until_complete(future=api.client.ws_connect())
    loop.run_until_complete(
        future=api.LoginReq(token=market_token,
                            start_time_stamp=start_time_stamp,
                            frequence=frequence))
    asyncio.run_coroutine_threadsafe(api.hearbeat_job(), loop)
    app_rsp_list = loop.run_until_complete(future=api.StartChartDataReqApi(
        exchange, code, start_time_stamp, recv_num=1))
    app_rsp = app_rsp_list[0]
    api.client.disconnect()

    # return app_rsp

    return """
    <html>
    <head>
    <title>查询合约</title>
    </head>
    <body>
    <pre id="out_pre"></pre>
    </body>
    <script type="text/javascript">
       var result = JSON.stringify({text}, null, 2);//将字符串转换成json对象
       document.getElementById('out_pre').innerText= result ;
    </script>
    </html>
    """.format(text=app_rsp)
Ejemplo n.º 7
0
 def __init__(self, ws_url, new_loop, extra_headers=None):
     self.new_loop = new_loop
     self.client = BaseWebSocketClient(url=ws_url,
                                       loop=self.new_loop,
                                       extra_headers=extra_headers)
     self.common = Common()
Ejemplo n.º 8
0
        for base_req in all_req:
            if base_req == None:
                continue
            if base_req == '_Heart_PONG':
                base_req = await self.client.recv()
            rev_data = QuoteMsgCarrier()
            rev_data.ParseFromString(base_req)
            if rev_data.type == QuoteMsgType.AUTH_CENTER_AUTH:
                single_data = AuthRsp()
                single_data.ParseFromString(rev_data.data)
                json_single_data = json_format.MessageToJson(single_data)
                json_single_data = json.loads(json_single_data)
                json_rsp_list.append(json_single_data)
        # print('AuthReqApi json_rsp_list:{}'.format(json_rsp_list))
        return json_rsp_list

    async def HearBeatApi(self):
        await self.client.send('_Heart_PING')


if __name__ == '__main__':
    common = Common()
    api = AuthApi(auth_ws_url,
                  asyncio.get_event_loop(),
                  extra_headers=auth_headers)
    start_time = time.time()
    asyncio.get_event_loop().run_until_complete(api.client.ws_connect())
    asyncio.get_event_loop().run_until_complete(
        api.AuthReqApi(token=str(market_token)))
    asyncio.get_event_loop().run_until_complete(api.client.stress_disconnect())
Ejemplo n.º 9
0
 def __init__(self):
     self.common = Common()
     self.logger = self.common.logger
Ejemplo n.º 10
0
 def __init__(self, ws_url, new_loop):
     self.new_loop = new_loop
     self.client = BaseWebSocketClient(url=ws_url, loop=self.new_loop)
     self.common = Common()
Ejemplo n.º 11
0
 def __init__(self):
     self.common = Common()
     self.sq = MarketSqliteDBClient()
     self.my_font = FontProperties(
         fname='/usr/share/fonts/cjkuni-uming/uming.ttc')
Ejemplo n.º 12
0
 def __init__(self):
     self.cookies = RequestsCookieJar()
     self.verify = False
     self.common = Common()
Ejemplo n.º 13
0
 def __init__(self, db_path):
     self.db = rocksdb.DB(db_path,
                          rocksdb.Options(create_if_missing=False),
                          read_only=True)
     self.logger = get_log()
     self.common = Common()
Ejemplo n.º 14
0
 def __init__(self, host=redis_host, port=redis_port):
     self.host = host
     self.port = port
     self.conn = self.__connectPika(self.host, self.port)
     self.logger = get_log()
     self.common = Common()
class Test_Comtrast_PRD():

    common = Common()
    logger = get_log()
    market_token = None

    @pytest.mark.parametrize("exchange, code", [
        ("SEHK", "00700"),
        ("SEHK", SEHK_indexCode1),
        (ASE_exchange, ASE_code1),
        (NASDAQ_exchange, NASDAQ_code1),
        (NYSE_exchange, NYSE_code1),
        (HK_exchange, HK_main1),
        (NYMEX_exchange, NYMEX_code1),
    ])
    def test_contrast_StartChartDataReq(self, SZ_api, HK_api, exchange, code):
        """订阅手机图表数据(手机专用)--订阅一个港股,frequence=4"""
        # exchange = SEHK_exchange
        # code = "00700"

        exchange = exchange
        code = code

        frequence = None
        start_time_stamp = int(time.time() * 1000)

        loop = asyncio.get_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(
            future=SZ_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop)
        self.logger.debug(u'订阅深圳--手机图表')
        sz_rsp_list = loop.run_until_complete(
            future=SZ_api.StartChartDataReqApi(
                exchange, code, start_time_stamp, recv_num=1))

        app_rsp = sz_rsp_list[0]
        assert self.common.searchDicKV(app_rsp, 'retCode') == 'SUCCESS'
        sz_basic_json_list = app_rsp['basicData']
        sz_before_snapshot_json_list = app_rsp['snapshot']
        sz_before_orderbook_json_list = []
        if self.common.searchDicKV(sz_before_snapshot_json_list,
                                   "dataType") != "EX_INDEX":
            sz_before_orderbook_json_list = app_rsp['orderbook']

        del sz_basic_json_list["commonInfo"]
        del sz_before_snapshot_json_list["commonInfo"]

        loop.run_until_complete(
            future=HK_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop)
        self.logger.debug(u'订阅香港--手机图表')
        hk_rsp_list = loop.run_until_complete(
            future=HK_api.StartChartDataReqApi(
                exchange, code, start_time_stamp, recv_num=1))

        app_rsp = hk_rsp_list[0]
        assert self.common.searchDicKV(app_rsp, 'retCode') == 'SUCCESS'
        hk_basic_json_list = app_rsp['basicData']
        hk_before_snapshot_json_list = app_rsp['snapshot']
        hk_before_orderbook_json_list = []
        if self.common.searchDicKV(hk_before_snapshot_json_list,
                                   "dataType") != "EX_INDEX":
            hk_before_orderbook_json_list = app_rsp['orderbook']

        del hk_basic_json_list["commonInfo"]
        del hk_before_snapshot_json_list["commonInfo"]

        assert sz_basic_json_list == hk_basic_json_list
        assert sz_before_snapshot_json_list == hk_before_snapshot_json_list
        if sz_before_orderbook_json_list or hk_before_orderbook_json_list:
            del sz_before_orderbook_json_list["commonInfo"]
            del hk_before_orderbook_json_list["commonInfo"]
            assert sz_before_orderbook_json_list == hk_before_orderbook_json_list

    @pytest.mark.parametrize("exchange, code", [
        ("SEHK", "00700"),
        ("SEHK", SEHK_indexCode1),
        (ASE_exchange, ASE_code1),
        (NASDAQ_exchange, NASDAQ_code1),
        (NYSE_exchange, NYSE_code1),
        (HK_exchange, HK_main1),
        (NYMEX_exchange, NYMEX_code1),
    ])
    def test_contrast_QueryKLineMinMsgReqApi(self, SZ_api, HK_api, exchange,
                                             code):
        """分时查询--查询并订阅港股的分时数据: isSubKLineMin = True"""
        frequence = 100
        isSubKLineMin = False
        exchange = exchange
        code = code

        query_type = QueryKLineMsgType.UNKNOWN_QUERY_KLINE  # app 订阅服务该字段无意义
        direct = QueryKLineDirectType.WITH_BACK  # app 订阅服务该字段无意义
        start = 0  # app 订阅服务该字段无意义
        end = 0  # app 订阅服务该字段无意义
        vol = 0  # app 订阅服务该字段无意义
        start_time_stamp = int(time.time() * 1000)

        loop = asyncio.get_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(
            future=SZ_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop)
        self.logger.debug(u'查询深圳分时数据')
        final_rsp = loop.run_until_complete(
            future=SZ_api.QueryKLineMinMsgReqApi(
                isSubKLineMin, exchange, code, query_type, direct, start, end,
                vol, start_time_stamp))

        sz_query_kline_min_rsp_list = final_rsp['query_kline_min_rsp_list'][0]

        assert self.common.searchDicKV(sz_query_kline_min_rsp_list,
                                       'retCode') == 'SUCCESS'

        loop.run_until_complete(
            future=HK_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop)
        self.logger.debug(u'查询深圳分时数据')
        final_rsp = loop.run_until_complete(
            future=HK_api.QueryKLineMinMsgReqApi(
                isSubKLineMin, exchange, code, query_type, direct, start, end,
                vol, start_time_stamp))

        hk_query_kline_min_rsp_list = final_rsp['query_kline_min_rsp_list'][0]

        assert self.common.searchDicKV(hk_query_kline_min_rsp_list,
                                       'retCode') == 'SUCCESS'

        assert sz_query_kline_min_rsp_list.get(
            "data") == hk_query_kline_min_rsp_list.get("data")

    @pytest.mark.parametrize("exchange, code", [
        ("SEHK", "00700"),
        ("SEHK", SEHK_indexCode1),
        (ASE_exchange, ASE_code1),
        (NASDAQ_exchange, NASDAQ_code1),
        (NYSE_exchange, NYSE_code1),
        (HK_exchange, HK_main1),
        (NYMEX_exchange, NYMEX_code1),
    ])
    def test_contrast_QueryFiveDaysKLineMinReqApi(self, SZ_api, HK_api,
                                                  exchange, code):
        """五日分时查询, 查询并订阅数据: isSubKLineMin = True"""
        frequence = 100
        isSubKLineMin = False
        exchange = exchange
        code = code
        start = None  # app 订阅服务该字段无意义
        start_time_stamp = int(time.time() * 1000)

        loop = asyncio.get_event_loop()
        asyncio.set_event_loop(loop)
        # 深圳
        loop.run_until_complete(
            future=SZ_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop)
        final_rsp = loop.run_until_complete(
            future=SZ_api.QueryFiveDaysKLineMinReqApi(
                isSubKLineMin, exchange, code, start, start_time_stamp))
        sz_query_5day_klinemin_rsp_list = final_rsp[
            'query_5day_klinemin_rsp_list'][0]
        assert self.common.searchDicKV(sz_query_5day_klinemin_rsp_list,
                                       'retCode') == 'SUCCESS'

        # 香港
        loop.run_until_complete(
            future=HK_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop)
        final_rsp = loop.run_until_complete(
            future=HK_api.QueryFiveDaysKLineMinReqApi(
                isSubKLineMin, exchange, code, start, start_time_stamp))
        hk_query_5day_klinemin_rsp_list = final_rsp[
            'query_5day_klinemin_rsp_list'][0]
        assert self.common.searchDicKV(hk_query_5day_klinemin_rsp_list,
                                       'retCode') == 'SUCCESS'

        assert sz_query_5day_klinemin_rsp_list.get(
            "hk_query_5day_klinemin_rsp_list"
        ) == hk_query_5day_klinemin_rsp_list.get(
            "hk_query_5day_klinemin_rsp_list")

    @pytest.mark.parametrize("exchange, code", [
        ("SEHK", "00700"),
        ("SEHK", SEHK_indexCode1),
        (ASE_exchange, ASE_code1),
        (NASDAQ_exchange, NASDAQ_code1),
        (NYSE_exchange, NYSE_code1),
        (HK_exchange, HK_main1),
        (NYMEX_exchange, NYMEX_code1),
    ])
    def test_contrast_QueryKLineMsgReqApi(self, SZ_api, HK_api, exchange,
                                          code):
        """K线查询港股: 按BY_DATE_TIME方式查询, 1分K, 前一小时的数据, 并订阅K线数据"""
        frequence = None
        isSubKLine = False
        exchange = SEHK_exchange
        code = SEHK_code1
        peroid_type = KLinePeriodType.MINUTE
        query_type = QueryKLineMsgType.BY_VOL
        direct = QueryKLineDirectType.WITH_FRONT
        start_time_stamp = int(time.time() * 1000)
        start = start_time_stamp
        end = None
        vol = 80

        loop = asyncio.get_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(
            future=SZ_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop)
        self.logger.debug(u'查询K线数据,并检查返回结果')
        final_rsp = loop.run_until_complete(future=SZ_api.QueryKLineMsgReqApi(
            isSubKLine, exchange, code, peroid_type, query_type, direct, start,
            end, vol, start_time_stamp))
        sz_query_kline_rsp_list = final_rsp['query_kline_rsp_list']
        assert self.common.searchDicKV(sz_query_kline_rsp_list[0],
                                       'retCode') == 'SUCCESS'

        loop.run_until_complete(
            future=HK_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop)
        self.logger.debug(u'查询K线数据,并检查返回结果')
        final_rsp = loop.run_until_complete(future=HK_api.QueryKLineMsgReqApi(
            isSubKLine, exchange, code, peroid_type, query_type, direct, start,
            end, vol, start_time_stamp))
        hk_query_kline_rsp_list = final_rsp['query_kline_rsp_list']
        assert self.common.searchDicKV(hk_query_kline_rsp_list[0],
                                       'retCode') == 'SUCCESS'

        assert sz_query_kline_rsp_list[0].get(
            "kData") == hk_query_kline_rsp_list[0].get("kData")

    @pytest.mark.parametrize("exchange, code", [
        ("SEHK", "00700"),
        (ASE_exchange, ASE_code1),
        (NASDAQ_exchange, NASDAQ_code1),
        (NYSE_exchange, NYSE_code1),
        (HK_exchange, HK_main1),
        (NYMEX_exchange, NYMEX_code1),
    ])
    def test_contrast_QueryTradeTickMsgReqApi(self, SZ_api, HK_api, exchange,
                                              code):
        """
        查询逐笔成交--查询5分钟的逐笔成交数据, 并订阅逐笔成交
        """
        frequence = 100
        isSubTrade = False
        exchange = exchange
        code = code
        type = QueryKLineMsgType.BY_VOL
        direct = QueryKLineDirectType.WITH_FRONT
        start_time_stamp = int(time.time() * 1000)
        start_time = start_time_stamp
        end_time = None
        vol = 100

        loop = asyncio.get_event_loop()
        asyncio.set_event_loop(loop)

        loop.run_until_complete(
            future=SZ_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop)
        self.logger.debug(u'逐笔成交查询,并检查返回结果')
        _start = time.time()
        final_rsp = loop.run_until_complete(
            future=SZ_api.QueryTradeTickMsgReqApi(
                isSubTrade, exchange, code, type, direct, start_time, end_time,
                vol, start_time_stamp))
        self.logger.debug("查询回包时间 : {}".format(time.time() - _start))
        sz_query_trade_tick_rsp_list = final_rsp['query_trade_tick_rsp_list']

        assert self.common.searchDicKV(sz_query_trade_tick_rsp_list[0],
                                       'retCode') == 'SUCCESS'

        loop.run_until_complete(
            future=HK_api.LoginReq(token=self.market_token,
                                   start_time_stamp=start_time_stamp,
                                   frequence=frequence))
        asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop)
        self.logger.debug(u'逐笔成交查询,并检查返回结果')
        final_rsp = loop.run_until_complete(
            future=HK_api.QueryTradeTickMsgReqApi(
                isSubTrade, exchange, code, type, direct, start_time, end_time,
                vol, start_time_stamp))
        hk_query_trade_tick_rsp_list = final_rsp['query_trade_tick_rsp_list']

        assert self.common.searchDicKV(hk_query_trade_tick_rsp_list[0],
                                       'retCode') == 'SUCCESS'

        assert sz_query_trade_tick_rsp_list[0].get(
            "tickData") == hk_query_trade_tick_rsp_list[0].get("tickData")