| | |
| | | # 策略中必须有init方法 |
| | | import json |
| | | import threading |
| | | import time |
| | | |
| | | import gm.api as gmapi |
| | | |
| | | import setting |
| | | import tool |
| | | |
| | | |
| | | def init(context): |
| | |
| | | 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() |
| | | |
| | | # 读取订阅代码 |
| | | # 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) |
| | | print("订阅:", codes_str) |
| | | gmapi.subscribe(symbols=codes_str, frequency='tick', count=2, wait_group=False, wait_group_timeout='10s', |
| | | unsubscribe_previous=True) |
| | | 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): |
| | |
| | | |
| | | |
| | | def on_tick(context, tick): |
| | | print(tick) |
| | | data = parse_tick(tick) |
| | | data = {"type": 0, "data": data} |
| | | __pipe.send(data) |
| | |
| | | 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', |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | datas = GPCodeManager().get_current_price("002769") |
| | | for data in datas: |
| | | print(data["cum_amount"] / data["cum_volume"]) |
| | | __get_latest_price() |