admin
2023-06-12 3e14bc857e694074c4ad0133f83dd7734f2c9b71
掘金分时改为定时循环请求
2个文件已修改
117 ■■■■■ 已修改文件
juejin_core.py 94 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
win32_util.py 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
juejin_core.py
@@ -1,10 +1,12 @@
# 策略中必须有init方法
import json
import threading
import time
import gm.api as gmapi
import setting
import tool
def init(context):
@@ -12,33 +14,65 @@
    gpCodeManager = GPCodeManager()
    # 订阅股票代码
    subscript()
    t1 = threading.Thread(target=lambda: __run_schedule())
    # 后台运行
    t1.setDaemon(True)
    t1.start()
    # t1 = threading.Thread(target=lambda: __run_schedule())
    # # 后台运行
    # t1.setDaemon(True)
    # t1.start()
def __run_schedule():
    while True:
        st = __pipe.recv()
        if st:
            print("读取到内容", st)
            if st == "resub":
                subscript()
#
# def __run_schedule():
#     while True:
#         st = __pipe.recv()
#         if st:
#             print("读取到内容", st)
#             if st == "resub":
#                 subscript()
def subscript():
    # 清除收盘价
    GPCodeManager.pre_prices.clear()
    __get_latest_price()
    # 读取订阅代码
    codes = gpCodeManager.get_codes()
    gpcode_list = GPCodeManager.get_gp_list_with_prefix(codes)
    if gpcode_list:
        codes_str = ",".join(gpcode_list)
        print("订阅:", codes_str)
        gmapi.subscribe(symbols=codes_str, frequency='tick', count=2, wait_group=False, wait_group_timeout='10s',
                        unsubscribe_previous=True)
    # t1 = threading.Thread(target=lambda: __get_latest_price())
    # # 后台运行
    # t1.setDaemon(True)
    # t1.start()
    # # 读取订阅代码
    # codes = gpCodeManager.get_codes()
    # gpcode_list = GPCodeManager.get_gp_list_with_prefix(codes)
    # if gpcode_list:
    #     codes_str = ",".join(gpcode_list)
    #     print("订阅:", codes_str)
    #     gmapi.subscribe(symbols=codes_str, frequency='tick', count=2, wait_group=False, wait_group_timeout='10s',
    #                     unsubscribe_previous=True)
# 获取最新的现价
def __get_latest_price():
    _last_create_time_dict = {}
    # strategy_id, token = setting.get_juejin_params()
    # gmapi.set_token(token)
    gpCodeManager = GPCodeManager()
    while True:
        try:
            if tool.is_trade_time():
                codes = gpCodeManager.get_codes()
                gpcode_list = GPCodeManager.get_gp_list_with_prefix(codes)
                if gpcode_list:
                    codes_str = ",".join(gpcode_list)
                    datas = gmapi.current(symbols=codes_str, fields="symbol,price,cum_amount,cum_volume,created_at")
                    for data in datas:
                        if _last_create_time_dict.get(data['symbol']) == data['created_at']:
                            print("重复数据")
                            continue
                        _last_create_time_dict[data['symbol']] = data['created_at']
                        on_tick(None, data)
        except:
            pass
        time.sleep(1)
def parse_tick(tick):
@@ -47,15 +81,16 @@
    # 保存最新价
    symbol = symbol.split(".")[1]
    pre_price = GPCodeManager().get_pre_prices(symbol)
    average_price = round(tick["cum_amount"] / tick["cum_volume"], 2) if tick["cum_volume"]>0 else pre_price
    average_price = round(tick["cum_amount"] / tick["cum_volume"], 2) if tick["cum_volume"] > 0 else pre_price
    rate = round((price - pre_price) / pre_price, 4)
    average_rate= round((average_price - pre_price) / pre_price, 4)
    average_rate = round((average_price - pre_price) / pre_price, 4)
    data = {"code": symbol, "rate": rate, "created_at": tick["created_at"], "price": price,
            "average_price":average_price,"average_rate":average_rate}
            "average_price": average_price, "average_rate": average_rate}
    return data
def on_tick(context, tick):
    print(tick)
    data = parse_tick(tick)
    data = {"type": 0, "data": data}
    __pipe.send(data)
@@ -65,10 +100,13 @@
    global __pipe, token
    __pipe = pipe
    strategy_id, token = setting.get_juejin_params()
    gmapi.run(strategy_id=strategy_id,
              filename='juejin_core.py',
              mode=gmapi.MODE_LIVE,
              token=token)
    gmapi.set_token(token)
    # gmapi.run(strategy_id=strategy_id,
    #           filename='juejin_core.py',
    #           mode=gmapi.MODE_LIVE,
    #           token=token)
    subscript()
    # gmapi.run(strategy_id=strategy_id,
    #           filename='juejin_core.py',
@@ -227,6 +265,4 @@
if __name__ == "__main__":
    datas = GPCodeManager().get_current_price("002769")
    for data in datas:
        print(data["cum_amount"] / data["cum_volume"])
    __get_latest_price()
win32_util.py
@@ -18,16 +18,19 @@
# 根据标题模糊匹配
def search_window(title):
    hwnds = []
    hwnd = win32gui.GetDesktopWindow()
    temp = None
    while True:
        if temp and win32gui.IsWindowVisible(temp):
            str_ = getText(temp)
            if str_.find(title) > -1:
                hwnds.append(temp)
        temp = win32gui.FindWindowEx(hwnd, temp, None, None)
        if not temp:
            break
    try:
        hwnd = win32gui.GetDesktopWindow()
        temp = None
        while True:
            if temp and win32gui.IsWindowVisible(temp):
                str_ = getText(temp)
                if str_.find(title) > -1:
                    hwnds.append(temp)
            temp = win32gui.FindWindowEx(hwnd, temp, None, None)
            if not temp:
                break
    except:
        pass
    return hwnds