{"id":1389,"date":"2026-08-15T09:18:21","date_gmt":"2026-08-15T01:18:21","guid":{"rendered":"https:\/\/khsci.com\/khQuant\/forum-topic\/%e8%bf%99%e4%b8%aa%e6%89%93%e6%9d%bf%e7%ad%96%e7%95%a5%e5%a6%82%e4%bd%95%e5%ad%a6%e4%b9%a0%e5%91%a2\/"},"modified":"2026-08-15T09:18:21","modified_gmt":"2026-08-15T01:18:21","slug":"%e8%bf%99%e4%b8%aa%e6%89%93%e6%9d%bf%e7%ad%96%e7%95%a5%e5%a6%82%e4%bd%95%e5%ad%a6%e4%b9%a0%e5%91%a2","status":"publish","type":"forum_topic","link":"https:\/\/khsci.com\/khQuant\/forum-topic\/%e8%bf%99%e4%b8%aa%e6%89%93%e6%9d%bf%e7%ad%96%e7%95%a5%e5%a6%82%e4%bd%95%e5%ad%a6%e4%b9%a0%e5%91%a2\/","title":{"rendered":"\u8fd9\u4e2a\u6253\u677f\u7b56\u7565\u5982\u4f55\u5b66\u4e60\u5462"},"content":{"rendered":"<h1>\u6da8\u5e45\u76d1\u63a7\u7b56\u7565\uff08\u4e2d\u8bc1500\u7248\u672c\uff09<\/h1>\n<h1>\u5f53\u80a1\u7968\u5f53\u65e5\u6da8\u5e45\u8d85\u8fc79%\u65f6\u4e70\u5165\uff0c\u7b2c\u4e8c\u5929\u5f00\u76d8\u5356\u51fa<\/h1>\n<p>from xtquant import xtdata<br \/>\nfrom khQTTools import generate_signal<br \/>\nimport logging<br \/>\nimport os<\/p>\n<h1>\u914d\u7f6e\u65e5\u5fd7\u7cfb\u7edf<\/h1>\n<p>LOGS_DIR = os.path.join(os.path.dirname(os.path.dirname(<strong>file<\/strong>)), &#8216;logs&#8217;)<br \/>\nos.makedirs(LOGS_DIR, exist_ok=True)<\/p>\n<p>logging.basicConfig(<br \/>\nfilename=os.path.join(LOGS_DIR, &#8216;strategy.log&#8217;),<br \/>\nlevel=logging.INFO,<br \/>\nformat=&#8217;%(asctime)s &#8211; %(levelname)s &#8211; %(message)s&#8217;,<br \/>\nfilemode=&#8217;a&#8217;<br \/>\n)<\/p>\n<h1>\u5168\u5c40\u53d8\u91cf<\/h1>\n<p>g_today_bought = set()     # \u4eca\u65e5\u4e70\u5165\u7684\u80a1\u7968<br \/>\ng_yesterday_bought = set() # \u6628\u65e5\u4e70\u5165\u7684\u80a1\u7968\uff0c\u4eca\u65e5\u5f00\u76d8\u5356\u51fa<br \/>\ng_is_first_bar = True      # \u662f\u5426\u662f\u5f53\u5929\u7b2c\u4e00\u4e2abar<br \/>\ng_trigger_prices = {}      # \u5b58\u50a8\u89e6\u53d1\u4ef7\u683c\u7684\u5b57\u5178<br \/>\ng_max_positions = 5        # \u6700\u5927\u6301\u4ed3\u6570\u91cf<\/p>\n<p>def init(stock_list, context):<br \/>\n&quot;&quot;&quot;\u7b56\u7565\u521d\u59cb\u5316\u51fd\u6570\uff0c\u7cfb\u7edf\u542f\u52a8\u65f6\u4f1a\u8c03\u7528\u6b64\u51fd\u6570&quot;&quot;&quot;<br \/>\nglobal g_today_bought, g_yesterday_bought, g_is_first_bar, g_trigger_prices, g_max_positions<\/p>\n<pre><code># \u8bb0\u5f55\u65e5\u5fd7\nlogging.info('\u7b56\u7565\u5f00\u59cb\u8fd0\u884c\uff0c\u521d\u59cb\u5316\u51fd\u6570\u5168\u5c40\u53ea\u8fd0\u884c\u4e00\u6b21')\n\n# \u83b7\u53d6\u6846\u67b6\u5b9e\u4f8b\nframework = context.get(\"__framework__\")\nif framework and hasattr(framework, 'trader_callback') and hasattr(framework.trader_callback, 'gui'):\n    framework.trader_callback.gui.log_message(f'\u7b56\u7565\u5f00\u59cb\u8fd0\u884c\uff0c\u521d\u59cb\u5316\u51fd\u6570\u5168\u5c40\u53ea\u8fd0\u884c\u4e00\u6b21', \"INFO\")\n    framework.trader_callback.gui.log_message(f'\u8bbe\u7f6e\u76d1\u63a7\u80a1\u7968\u6570\u91cf: {len(stock_list)} \u53ea', \"INFO\")\n\n# \u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf\ng_today_bought = set()\ng_yesterday_bought = set()\ng_is_first_bar = True\ng_trigger_prices = {}\n\n# \u9884\u52a0\u8f7d\u5173\u952e\u5386\u53f2\u6570\u636e\ntry:\n    batch_size = 50# \u6bcf\u6b21\u5904\u740650\u53ea\u80a1\u7968\n    for i in range(0, len(stock_list), batch_size):\n        batch_stocks = stock_list[i:i+batch_size]\n\n        # \u6279\u91cf\u83b7\u53d6\u5386\u53f2\u6570\u636e\u4ee5\u63d0\u9ad8\u6548\u7387\n        xtdata.get_market_data(\n            field_list=['open', 'close'], \n            stock_list=batch_stocks, \n            period='1d',\n            count=5\n        )\nexcept Exception as e:\n    logging.error(f\"\u9884\u52a0\u8f7d\u5386\u53f2\u6570\u636e\u65f6\u51fa\u9519: {str(e)}\")\n\nlogging.info(\"\u521d\u59cb\u5316\u5b8c\u6210\")<\/code><\/pre>\n<p>def khPreMarket(data):<br \/>\n&quot;&quot;&quot;\u76d8\u524d\u56de\u8c03\u51fd\u6570\uff0c\u8ba1\u7b97\u5f53\u65e5\u89e6\u53d1\u4ef7\u683c&quot;&quot;&quot;<br \/>\nglobal g_today_bought, g_yesterday_bought, g_is_first_bar, g_trigger_prices<\/p>\n<pre><code># \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u8f6c\u6362\u4e3aAPI\u6240\u9700\u683c\u5f0f\ncurrent_time = data[\"__current_time__\"]\ndate = current_time[\"date\"]\ndate_yyyymmdd = date.replace('-', '')\n\n# \u83b7\u53d6\u6846\u67b6\u5b9e\u4f8b\nframework = data.get(\"__framework__\")\ngui = None\nif framework and hasattr(framework, 'trader_callback') and hasattr(framework.trader_callback, 'gui'):\n    gui = framework.trader_callback.gui\n    gui.log_message(f'=== \u4ea4\u6613\u65e5 {date} \u76d8\u524d\u8fd0\u884c ===', \"INFO\")\n\n# \u91cd\u7f6e\u6bcf\u65e5\u72b6\u6001\ng_today_bought = set()\ng_is_first_bar = True\ng_trigger_prices = {}\n\n# \u83b7\u53d6\u80a1\u7968\u5217\u8868\nstock_list = []\ntry:\n    # \u5c1d\u8bd5\u4ece\u914d\u7f6e\u83b7\u53d6\u80a1\u7968\u5217\u8868\n    if framework and hasattr(framework, 'config'):\n        stock_list_file = framework.config.config_dict.get(\"data\", {}).get(\"stock_list_file\", \"\")\n        if stock_list_file and os.path.exists(stock_list_file):\n            with open(stock_list_file, 'r', encoding='utf-8') as f:\n                stock_list = [line.strip() for line in f if line.strip()]\n\n    # \u5982\u679c\u65e0\u6cd5\u4ece\u914d\u7f6e\u83b7\u53d6\uff0c\u5c1d\u8bd5\u4ece\u6301\u4ed3\u4e2d\u83b7\u53d6\n    ifnot stock_list:\n        positions = data.get(\"__positions__\", {})\n        stock_list = list(positions.keys())\nexcept Exception as e:\n    logging.error(f\"\u83b7\u53d6\u80a1\u7968\u5217\u8868\u5931\u8d25: {str(e)}\")\n\n# \u8ba1\u7b97\u6240\u6709\u80a1\u7968\u7684\u89e6\u53d1\u4ef7\u683c\nif stock_list:\n    try:\n        # \u6279\u91cf\u83b7\u53d6\u524d\u4e00\u65e5\u6536\u76d8\u4ef7\u5e76\u8ba1\u7b97\u89e6\u53d1\u4ef7\n        hist_data = xtdata.get_market_data_ex(\n            field_list=['close'],\n            stock_list=stock_list,\n            period='1d',\n            start_time=str(int(date_yyyymmdd) - 1),\n            end_time=str(int(date_yyyymmdd) - 1),\n            dividend_type='none'\n        )\n\n        # \u5904\u7406\u6bcf\u53ea\u80a1\u7968\u7684\u6570\u636e\n        for stock in stock_list:\n            if stock in hist_data and len(hist_data[stock]['close']) &amp;gt; 0:\n                prev_close = hist_data[stock]['close'].iloc[0]\n                if prev_close &amp;gt; 0:\n                    g_trigger_prices[stock] = prev_close * 1.09\n    except Exception as e:\n        logging.error(f\"\u6279\u91cf\u8ba1\u7b97\u89e6\u53d1\u4ef7\u683c\u65f6\u51fa\u9519: {str(e)}\")\n\n# \u786e\u4fdd\u6628\u65e5\u4e70\u5165\u7684\u80a1\u7968\u5728\u4eca\u65e5\u5356\u51fa\npositions = data.get(\"__positions__\", {})\ng_yesterday_bought.update(positions.keys())\n\nreturn []<\/code><\/pre>\n<p>def khHandlebar(data):<br \/>\n&quot;&quot;&quot;\u7b56\u7565\u4e3b\u51fd\u6570\uff0c\u5904\u7406\u5b9e\u65f6\u884c\u60c5\u5e76\u751f\u6210\u4ea4\u6613\u4fe1\u53f7&quot;&quot;&quot;<br \/>\nglobal g_today_bought, g_yesterday_bought, g_is_first_bar, g_trigger_prices, g_max_positions<\/p>\n<pre><code>signals = []\n\n# \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u548c\u80a1\u7968\u4ee3\u7801\u5217\u8868\ncurrent_time = data[\"__current_time__\"]\ndate = current_time[\"date\"]\ndate_yyyymmdd = date.replace('-', '')\n\n# \u83b7\u53d6\u6709\u6548\u7684\u80a1\u7968\u4ee3\u7801\u5217\u8868\uff08\u8fc7\u6ee4\u6389\u7279\u6b8a\u952e\uff09\nstock_codes = [key for key in data.keys() ifnot key.startswith('__')]\n\n# \u83b7\u53d6\u8d26\u6237\u548c\u6301\u4ed3\u4fe1\u606f\naccount = data.get(\"__account__\", {})\npositions = data.get(\"__positions__\", {})\navailable_cash = account.get(\"cash\", 0)\ncurrent_positions_count = len(positions)\n\n# \u5982\u679c\u662f\u5f53\u5929\u7b2c\u4e00\u4e2abar\uff0c\u5148\u5904\u7406\u5356\u51fa\u6628\u65e5\u4e70\u5165\u7684\u80a1\u7968\nif g_is_first_bar:\n    for stock in g_yesterday_bought:\n        if stock in positions:\n            # \u83b7\u53d6\u5f53\u524d\u4ef7\u683c\n            current_price = positions[stock].get(\"close\", 0)\n            if current_price  0:\n                sell_signals = generate_signal(data, stock, current_price, 1.0, \"sell\", \"\u6da8\u505c\u540e\u7b2c\u4e8c\u5929\u5356\u51fa\")\n                signals.extend(sell_signals)\n\n    g_is_first_bar = False\n\n    # \u5982\u679c\u6709\u5356\u51fa\u4fe1\u53f7\uff0c\u5148\u8fd4\u56de\u6267\u884c\n    if signals:\n        return signals\n\n# \u5982\u679c\u6301\u4ed3\u5df2\u8fbe\u5230\u6700\u5927\u9650\u5236\uff0c\u4e0d\u6267\u884c\u4e70\u5165\u64cd\u4f5c\nif current_positions_count &amp;gt;= g_max_positions:\n    return signals\n\n# \u8ba1\u7b97\u5355\u80a1\u53ef\u7528\u8d44\u91d1\uff08\u5e73\u5747\u5206\u914d\uff09\nposition_value = available_cash \/ g_max_positions if g_max_positions &amp;gt; 0else0\n\n# \u904d\u5386\u6240\u6709\u80a1\u7968\u68c0\u67e5\u662f\u5426\u6ee1\u8db3\u4e70\u5165\u6761\u4ef6\nfor stock_code in stock_codes:\n    # \u8df3\u8fc7\u5df2\u4e70\u5165\u6216\u5df2\u6301\u6709\u7684\u80a1\u7968\n    if stock_code in g_today_bought or stock_code in positions:\n        continue\n\n    # \u83b7\u53d6\u5f53\u524d\u4ef7\u683c\n    current_price = data[stock_code].get(\"close\", 0)\n    if current_price  0:\n                open_price = hist_data[stock_code]['open'].iloc[0]\n                if open_price &amp;gt; 0:\n                    trigger_price = open_price * 1.09\n                    g_trigger_prices[stock_code] = trigger_price\n        except Exception as e:\n            logging.error(f\"\u8865\u5145\u8ba1\u7b97 {stock_code} \u89e6\u53d1\u4ef7\u683c\u65f6\u51fa\u9519: {str(e)}\")\n\n    # \u5982\u679c\u5f53\u524d\u4ef7\u683c\u8fbe\u5230\u6216\u8d85\u8fc7\u89e6\u53d1\u4ef7\uff0c\u6267\u884c\u4e70\u5165\n    if trigger_price and current_price &amp;gt;= trigger_price:\n        # \u8ba1\u7b97\u53ef\u4e70\u80a1\u6570\uff08\u6574\u6570\u624b\uff09\n        shares_to_buy = int(position_value \/ current_price \/ 100) * 100\n\n        # \u786e\u4fdd\u81f3\u5c11\u4e70\u4e00\u624b(100\u80a1)\n        if shares_to_buy &amp;gt;= 100:\n            # \u751f\u6210\u4e70\u5165\u4fe1\u53f7\n            buy_signals = generate_signal(data, stock_code, current_price, shares_to_buy, \"buy\", \"\u5f53\u65e5\u6da8\u5e45\u8d85\u8fc79%\u4e70\u5165\")\n            signals.extend(buy_signals)\n\n            # \u6dfb\u52a0\u5230\u4eca\u65e5\u4e70\u5165\u8bb0\u5f55\n            g_today_bought.add(stock_code)\n\n            # \u5982\u679c\u5df2\u8fbe\u5230\u6700\u5927\u6301\u4ed3\u6570\uff0c\u7acb\u5373\u8fd4\u56de\n            if len(g_today_bought) + current_positions_count &amp;gt;= g_max_positions:\n                break\n\nreturn signals<\/code><\/pre>\n<p>def khPostMarket(data):<br \/>\n&quot;&quot;&quot;\u76d8\u540e\u56de\u8c03\u51fd\u6570\uff0c\u8bb0\u5f55\u4eca\u65e5\u4e70\u5165\u7684\u80a1\u7968&quot;&quot;&quot;<br \/>\nglobal g_yesterday_bought, g_today_bought<\/p>\n<pre><code># \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\ncurrent_time = data[\"__current_time__\"]\ndate_time = current_time[\"datetime\"]\n\n# \u66f4\u65b0\u6628\u65e5\u4e70\u5165\u8bb0\u5f55\uff0c\u7528\u4e8e\u660e\u5929\u5f00\u76d8\u5356\u51fa\ng_yesterday_bought = g_today_bought.copy()\n\n# \u8bb0\u5f55\u65e5\u5fd7\nif g_today_bought:\n    logging.info(f\"\u4eca\u65e5\u4e70\u5165\u80a1\u7968: {list(g_today_bought)}\")\n    logging.info(f\"\u660e\u65e5\u5f00\u76d8\u5c06\u5356\u51fa\u80a1\u7968: {list(g_yesterday_bought)}\")\nelse:\n    logging.info(f\"\u4eca\u65e5\u672a\u4e70\u5165\u80a1\u7968\")\n\n# \u83b7\u53d6\u8d26\u6237\u4fe1\u606f\naccount = data.get(\"__account__\", {})\navailable_cash = account.get(\"cash\", 0)\ntotal_asset = account.get(\"total_asset\", 0)\n\nlogging.info(f\"=== \u4ea4\u6613\u65e5 {date_time} \u76d8\u540e\u8fd0\u884c ===\")\nlogging.info(f\"\u5f53\u524d\u8d44\u91d1: {available_cash:.2f}, \u603b\u8d44\u4ea7: {total_asset:.2f}\")\nlogging.info(f\"====================\")\n\nreturn []<\/code><\/pre>\n","protected":false},"author":1779,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"forum_category":"strategy","forum_views":68,"forum_upvotes":0,"forum_is_pinned":false,"forum_is_locked":false,"forum_is_solved":false,"forum_preview_image":"","forum_attachments":"[]","forum_poll":""},"class_list":["post-1389","forum_topic","type-forum_topic","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/forum-topics\/1389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/forum-topics"}],"about":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/types\/forum_topic"}],"author":[{"embeddable":true,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/users\/1779"}],"replies":[{"embeddable":true,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/comments?post=1389"}],"wp:attachment":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/media?parent=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}