pu369com

pytdx多进程示例

# encoding=utf-8
import math
from pytdx.hq import TdxHq_API
import pathlib
import multiprocessing as mp
from multiprocessing import Pool


class myTdx:
    def __init__(self):
        self.HqHOSTS = pathlib.Path("HqHOSTS.txt").read_text().split()
        self.SYMBOLS = pathlib.Path("A.txt").read_text().split()
        self.SYMBOLS2 = self.getsymbols2()
        self.HSDICT = dict(zip(self.HqHOSTS, self.SYMBOLS2))
        self.manager = mp.Manager
        self.RESULT = self.manager().list()

    def getsymbols2(self):
        symbols = self.SYMBOLS
        total = len(symbols)
        page = len(self.HqHOSTS)
        perpage = math.ceil(total / page)
        return [symbols[i:i + perpage] for i in range(0, len(symbols), perpage)]

    def getdata(self, host, slist):
        # 数据获取接口一般返回list结构,
        api = TdxHq_API()
        liveapi = api.connect(host, 7709)
        if liveapi:
            r_list = []
            for symbol in slist:
                market_code = 1 if str(symbol[0]) == '6' else 0
                data = liveapi.get_transaction_data(market_code, symbol, 0, 30)
                rdic = {symbol: data}
                r_list.append(rdic)
            self.RESULT.extend(r_list)
        else:
            print(host, "服務器不可用...")
        liveapi.disconnect()

    def save(self):
        # print(self.RESULT)
        log = open('r.txt', 'a')
        print(self.RESULT, file=log)


if __name__ == '__main__':
    a = myTdx()
    pool = Pool(len(a.HqHOSTS))
    for host in a.HqHOSTS:
        alist = a.HSDICT.get(host)
        pool.apply_async(func=a.getdata, args=(host, alist))
    pool.close()
    pool.join()
    a.save()

  

# encoding=utf-8
from pytdx.hq import TdxHq_API
import pathlib
import re

if __name__ == '__main__':
    ips_text = pathlib.Path("HQHOSTA.txt").read_text(encoding='utf-8')  # 读取文件
    ips_list = re.compile(r'\d+\.\d+\.\d+\.\d+').findall(ips_text)  # 正则查找所有ip
    ips_listOK = []
    api = TdxHq_API()
    for ip in ips_list:
        if api.connect(ip, 7709):
            ips_listOK.append(ip)
    if len(ips_listOK) > 0:
        ips_listOld = pathlib.Path("HqHOSTS.txt").read_text(encoding='utf-8').split()
        ips_listOld.extend(ips_listOK)
        ips_new = list(set(ips_listOld))  # 去重
    pathlib.Path("HqHOSTS.txt").write_text("\n".join(ips_new))
    pathlib.Path('HQHOSTA.txt').unlink()
    print("完成")

  改成线程

# encoding=utf-8
import threading
from pytdx.hq import TdxHq_API
import pathlib


def get_data(host, symbol_list):
    # 数据获取接口一般返回list结构,
    api = TdxHq_API()
    live_api = api.connect(host, 7709)
    if live_api:
        for symbol in symbol_list:
            market_code = 1 if str(symbol[0]) == '6' else 0
            data = api.to_df(live_api.get_transaction_data(market_code, symbol, 0, 30))
            # data.to_csv(symbol + ".csv")
            # print(data)
    else:
        print(host, "服務器不可用...")
    live_api.disconnect()


class myTdx:
    def __init__(self):
        hosts = pathlib.Path("HqHOSTS.txt").read_text().split()
        symbols = pathlib.Path("A.txt").read_text().split()
        total = len(symbols)
        page = len(hosts)
        k, m = divmod(total, page)
        symbols_divided = [symbols[i * k + min(i, m):(i + 1) * k + min(i + 1, m)] for i in range(page)]
        self.hosts_symbols = dict(zip(hosts, symbols_divided))
        self.multi_thread_run()

    def multi_thread_run(self):
        t = []
        for host, symbol_list in self.hosts_symbols.items():
            t.append(
                threading.Thread(target=get_data, args=(host, symbol_list))
            )
        for thread in t:
            thread.start()
        for thread in t:
            thread.join()


if __name__ == '__main__':
    while True:
        myTdx()

  

单线程示例

import pathlib
import time

from pytdx.hq import TdxHq_API


def get_one(st):
    market_code = 1 if st[0] == '6' else 0
    with api.connect("192.168.1.12", 7709):
        datas = []
        for i in range(15):
            data = api.get_transaction_data(market_code, st, (15 - i) * 300, 300)
            if data:
                datas += data
        df = api.to_df(datas)
        if not df.empty:
            df = df[(df['vol'] > 10000) & (df['buyorsell'] == 0) & (df['time'] > '09:30') & (df['time'] < '14:57')]
        if not df.empty:
            return str(market_code) + st


time_start = time.time()
api = TdxHq_API(auto_retry=True,raise_exception=False)
symbols = pathlib.Path("A.txt").read_text().split()
wan = []
i = len(symbols)
for st in symbols:
    one = get_one(st)
    if one:
        wan.append(one)
    i = i - 1
    print(i)

f = open(r'D:\zq\T0002\blocknew\WS.blk', 'w')
f.write('\n'.join(wan))
f.close()
time_end = time.time()
print('耗时:', time_end - time_start)

  

posted on 2023-04-11 15:15  pu369com  阅读(481)  评论(0编辑  收藏  举报

导航