{"id":1820,"date":"2026-07-11T17:40:16","date_gmt":"2026-07-11T17:40:16","guid":{"rendered":"https:\/\/www.patternsmart.com\/cn\/?p=1820"},"modified":"2026-07-11T17:40:30","modified_gmt":"2026-07-11T17:40:30","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-python-%e7%bc%96%e5%86%99%e5%8a%a8%e6%80%81%e6%bb%91%e7%82%b9%e6%a8%a1%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.patternsmart.com\/cn\/%e5%a6%82%e4%bd%95%e7%94%a8-python-%e7%bc%96%e5%86%99%e5%8a%a8%e6%80%81%e6%bb%91%e7%82%b9%e6%a8%a1%e5%9e%8b%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u7528 Python \u7f16\u5199\u52a8\u6001\u6ed1\u70b9\u6a21\u578b\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python \u5b9e\u73b0\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u81ea\u5b9a\u4e49\u7684 Pandas \u56de\u6d4b\u7cfb\u7edf\u4e2d\uff0c\u5bf9\u57fa\u4e8e\u6210\u4ea4\u91cf\u7684\u52a8\u6001\u6ed1\u70b9\u8fdb\u884c\u5efa\u6a21\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"559\" src=\"https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3.png\" alt=\"Python \u7f16\u5199\u52a8\u6001\u6ed1\u70b9\u6a21\u578b\" class=\"wp-image-1821\" srcset=\"https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3.png 1024w, https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3-300x164.png 300w, https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3-768x419.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u5176\u5bf9\u6bcf\u7b14\u4ea4\u6613\u5e94\u7528\u4e00\u6210\u4e0d\u53d8\u4e14\u4e0d\u5207\u5b9e\u9645\u7684\u56fa\u5b9a\u60e9\u7f5a\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u4e86\u4e00\u79cd<strong>\u975e\u7ebf\u6027\u5e02\u573a\u51b2\u51fb\u6a21\u578b<\/strong>\uff08\u901a\u5e38\u88ab\u79f0\u4e3a\u201c\u5e73\u65b9\u6839\u5b9a\u5f8b\u201d\u53d8\u4f53\uff09\u3002\u8be5\u6a21\u578b\u4f1a\u6839\u636e\u4e24\u4e2a\u6838\u5fc3\u53d8\u91cf\u52a8\u6001\u8c03\u6574\u6267\u884c\u6469\u64e6\uff1a<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>\u76f8\u5bf9\u4e8e\u5e02\u573a\u6d41\u52a8\u6027\u7684\u8ba2\u5355\u89c4\u6a21\uff1a<\/strong> \u8ba2\u5355\u89c4\u6a21\u8d8a\u5927\uff0c\u5403\u6389\u7684\u8ba2\u5355\u7c3f\u6df1\u5ea6\u5c31\u8d8a\u6df1\uff0c\u906d\u53d7\u7684\u6ed1\u70b9\u60e9\u7f5a\u4e5f\u5c31\u8d8a\u4e25\u91cd\u3002<\/li>\n\n\n\n<li><strong>\u5386\u53f2\u6ce2\u52a8\u7387\uff1a<\/strong> \u5728\u5e02\u573a\u5267\u70c8\u6ce2\u52a8\u671f\u95f4\uff0c\u7531\u4e8e\u4e70\u5356\u4ef7\u5dee\u81ea\u7136\u62c9\u5927\uff0c\u6ed1\u70b9\u4e5f\u4f1a\u968f\u4e4b\u589e\u52a0\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport pandas as pd\n\n\ndef apply_dynamic_slippage(\n    df: pd.DataFrame,\n    order_size_col: str,\n    market_vol_col: str,\n    market_return_col: str,\n    participation_rate: float = 0.10,\n    impact_exponent: float = 0.5,\n) -&gt; pd.DataFrame:\n    \"\"\"\u6a21\u62df\u57fa\u4e8e\u6210\u4ea4\u91cf\u548c\u6ce2\u52a8\u7387\u9a71\u52a8\u7684\u975e\u7ebf\u6027\u5e02\u573a\u51b2\u51fb\u6a21\u578b\uff0c\n\n    \u4ee5\u4fbf\u5728\u56de\u6d4b\u6267\u884c\u4e2d\u8fdb\u884c\u7cbe\u51c6\u7684\u60e9\u7f5a\u3002\n\n    \u53c2\u6570:\n    -----------\n    df : pd.DataFrame\n        \u56de\u6d4b\u65f6\u95f4\u5e8f\u5217\u7684 DataFrame\u3002\n    order_size_col : str\n        \u4ee3\u8868\u7b56\u7565\u5728\u8be5 K \u7ebf\uff08Bar\uff09\u4e0a\u8ba1\u5212\u4ea4\u6613\u7684\u80a1\u7968\/\u5355\u4f4d\u6570\u91cf\u7684\u5217\u540d\u3002\uff08\u6b63\u6570\u4ee3\u8868\u4e70\u5165\uff0c\u8d1f\u6570\u4ee3\u8868\u5356\u51fa\uff09\u3002\n    market_vol_col : str\n        \u8be5 K \u7ebf\u4e0a\u7684\u5386\u53f2\u5e02\u573a\u603b\u6210\u4ea4\u91cf\u7684\u5217\u540d\u3002\n    market_return_col : str\n        \u4ee3\u8868\u8d44\u4ea7 volatility \u7684\u5217\u540d\uff08\u4f8b\u5982\uff1a\u5bf9\u6570\u6536\u76ca\u7387\u7684\u6eda\u52a8\u6807\u51c6\u5dee\uff0c\u6216\u6eda\u52a8 ATR \u767e\u5206\u6bd4\uff09\u3002\n    participation_rate : float, \u9ed8\u8ba4 0.10\n        \u7f29\u653e\u56e0\u5b50\uff0c\u51b3\u5b9a\u8ba2\u5355\u6210\u4ea4\u91cf\u5bf9\u8ba2\u5355\u7c3f\u4ef7\u5dee\u7684\u51b2\u51fb\u6fc0\u8fdb\u7a0b\u5ea6\u3002\n    impact_exponent : float, \u9ed8\u8ba4 0.5\n        \u5e73\u65b9\u6839\u5e42\u6b21\u56e0\u5b50\u3002\u5fae\u89c2\u7ed3\u6784\u7ecf\u9a8c\u6570\u636e\u8868\u660e\uff0c\u5e02\u573a\u51b2\u51fb\u5927\u81f4\u4e0e\u8ba2\u5355\u89c4\u6a21\u7684\u5e73\u65b9\u6839\uff080.5\uff09\u6210\u6b63\u6bd4\u3002\n\n    \u8fd4\u56de:\n    --------\n    pd.DataFrame\n        \u589e\u52a0\u4e86 'slippage_pct'\uff08\u6ed1\u70b9\u767e\u5206\u6bd4\uff09\u548c 'execution_price'\uff08\u6267\u884c\u4ef7\u683c\uff09\u5217\u7684 DataFrame\u3002\n    \"\"\"\n    # \u521b\u5efa\u6df1\u62f7\u8d1d\u4ee5\u9632\u6b62\u4fee\u6539\u539f\u59cb DataFrame\n    backtest_df = df.copy()\n\n    # \u8ba1\u7b97\u6210\u4ea4\u91cf\u53c2\u4e0e\u7387: (\u4f60\u7684\u8ba2\u5355\u89c4\u6a21 \/ \u8be5 K \u7ebf\u5e02\u573a\u603b\u6210\u4ea4\u91cf)\n    # \u4f7f\u7528\u7edd\u5bf9\u503c\uff0c\u56e0\u4e3a\u4e70\u5355\u548c\u5356\u5355\u90fd\u4f1a\u6d88\u8017\u6d41\u52a8\u6027\n    backtest_df&#91;\"volume_participation\"] = backtest_df&#91;order_size_col].abs() \/ (\n        backtest_df&#91;market_vol_col] + 1e-8\n    )\n\n    # \u975e\u7ebf\u6027\u5e02\u573a\u51b2\u51fb\u516c\u5f0f:\n    # \u6ed1\u70b9 % = \u53c2\u4e0e\u7387 * (\u6210\u4ea4\u91cf\u53c2\u4e0e\u7387)^\u51b2\u51fb\u6307\u6570 * \u5e02\u573a\u6ce2\u52a8\u7387\n    backtest_df&#91;\"slippage_pct\"] = (\n        participation_rate\n        * (backtest_df&#91;\"volume_participation\"] ** impact_exponent)\n        * backtest_df&#91;market_return_col]\n    )\n\n    # \u7528 0 \u586b\u5145 NaN \u503c\uff08\u9002\u7528\u4e8e\u7b56\u7565\u672a\u53d1\u51fa\u4efb\u4f55\u8ba2\u5355\u7684 K \u7ebf\uff09\n    backtest_df&#91;\"slippage_pct\"] = backtest_df&#91;\"slippage_pct\"].fillna(0.0)\n\n    # \u5e94\u7528\u6267\u884c\u65b9\u5411\u7684\u7ea6\u675f\u6761\u4ef6:\n    # \u4e70\u5355\uff08\u6b63\u8ba2\u5355\uff09\u7684\u6210\u4ea4\u4ef7\u683c\u8981\u9ad8\u4e8e\uff08HIGHER\uff09\u6253\u5370\u51fa\u7684\u5e02\u573a\u4ef7\u3002\n    # \u5356\u5355\uff08\u8d1f\u8ba2\u5355\uff09\u7684\u6210\u4ea4\u4ef7\u683c\u8981\u4f4e\u4e8e\uff08LOWER\uff09\u6253\u5370\u51fa\u7684\u5e02\u573a\u4ef7\u3002\n    backtest_df&#91;\"execution_price\"] = np.where(\n        backtest_df&#91;order_size_col] &gt; 0,\n        backtest_df&#91;\"close\"] * (1 + backtest_df&#91;\"slippage_pct\"]),  # \u4e70\u5165\u60e9\u7f5a\n        np.where(\n            backtest_df&#91;order_size_col] &lt; 0,\n            backtest_df&#91;\"close\"] * (1 - backtest_df&#91;\"slippage_pct\"]),  # \u5356\u51fa\u60e9\u7f5a\n            backtest_df&#91;\"close\"],  # \u672a\u53d1\u51fa\u4ea4\u6613\u4fe1\u53f7\n        ),\n    )\n\n    return backtest_df\n\n\n# ==========================================\n# \u4f7f\u7528\u793a\u4f8b\u53ca\u6837\u672c\u6570\u636e\u6a21\u62df\n# ==========================================\nif __name__ == \"__main__\":\n    # \u751f\u6210\u6a21\u62df\u7684 15 \u5206\u949f K \u7ebf\u5e02\u573a\u6570\u636e\n    np.random.seed(42)\n    dates = pd.date_range(start=\"2026-07-10 09:30\", periods=5, freq=\"15min\")\n\n    data = {\n        \"close\": &#91;150.00, 150.50, 152.00, 151.20, 151.80],\n        \"market_volume\": &#91;\n            50000,\n            12000,\n            85000,\n            22000,\n            45000,\n        ],  # \u6ce8\u610f\uff1a\u7b2c 2 \u6839 K \u7ebf\u6d41\u52a8\u6027\u6781\u4f4e\n        \"rolling_volatility\": &#91;\n            0.012,\n            0.015,\n            0.025,\n            0.018,\n            0.011,\n        ],  # \u7b2c 3 \u6839 K \u7ebf\u6ce2\u52a8\u7387\u6781\u9ad8\n        \"strategy_order\": &#91;\n            2500,\n            2500,\n            -5000,\n            0,\n            100,\n        ],  # \u6b63\u6570 = \u4e70\u5165\uff0c\u8d1f\u6570 = \u5356\u51fa\n    }\n\n    raw_backtest = pd.DataFrame(data, index=dates)\n\n    # \u901a\u8fc7\u9632\u5fa1\u6027\u6ed1\u70b9\u5f15\u64ce\u8fdb\u884c\u5904\u7406\n    realistic_backtest = apply_dynamic_slippage(\n        df=raw_backtest,\n        order_size_col=\"strategy_order\",\n        market_vol_col=\"market_volume\",\n        market_return_col=\"rolling_volatility\",\n    )\n\n    # \u6253\u5370\u4e13\u6ce8\u4e8e\u6267\u884c\u504f\u5dee\u7684\u7ed3\u679c\n    display_cols = &#91;\n        \"close\",\n        \"strategy_order\",\n        \"volume_participation\",\n        \"slippage_pct\",\n        \"execution_price\",\n    ]\n    print(realistic_backtest&#91;display_cols].to_string())\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801\u8f93\u51fa\u903b\u8f91\u8be6\u89e3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b2c 1 \u6839 K \u7ebf\uff08\u6807\u51c6\u4e70\u5165\uff09\uff1a<\/strong> \u5728 50,000 \u7684\u5e02\u573a\u603b\u6210\u4ea4\u91cf\u4e2d\u4e70\u5165 2,500 \u80a1\uff0c\u4ea7\u751f\u4e86 <strong>5%<\/strong> \u7684\u6210\u4ea4\u91cf\u53c2\u4e0e\u7387\u3002\u6700\u7ec8\u7684\u6210\u4ea4\u4ef7\u683c\u76f8\u6bd4\u4e8e 150.00 \u7f8e\u5143\u7684\u57fa\u51c6\u4ef7\u683c\u8f7b\u5fae\u4e0a\u6ed1\u81f3 150.40 \u7f8e\u5143\u3002<\/li>\n\n\n\n<li><strong>\u7b2c 2 \u6839 K \u7ebf\uff08\u4f4e\u6d41\u52a8\u6027\u9677\u9631\uff09\uff1a<\/strong> \u5728\u6d41\u52a8\u6027\u67af\u7aed\u3001\u5e02\u573a\u603b\u6210\u4ea4\u91cf\u4ec5\u6709 12,000 \u80a1\u7684\u60c5\u51b5\u4e0b\uff0c\u4e70\u5165\u5b8c\u5168\u76f8\u540c\u7684 2,500 \u80a1\uff0c\u5bfc\u81f4\u6210\u4ea4\u91cf\u53c2\u4e0e\u7387\u98d9\u5347\u81f3\u63a5\u8fd1 <strong>21%<\/strong>\u3002\u5c3d\u7ba1\u5f53\u65f6\u5e02\u573a\u4ef7\u683c\u4ec5\u4e0a\u6da8\u4e86 0.50 \u7f8e\u5143\uff0c\u4f46\u7531\u4e8e\u7f3a\u4e4f\u53ef\u7528\u6d41\u52a8\u6027\uff0c\u4f60\u6fc0\u8fdb\u7684\u8ba2\u5355\u5c5e\u6027\u5bfc\u81f4\u6267\u884c\u4ef7\u683c\u88ab\u8fdb\u4e00\u6b65\u5927\u5e45\u63a8\u9ad8\u3002<\/li>\n\n\n\n<li><strong>\u7b2c 3 \u6839 K \u7ebf\uff08\u9ad8\u6ce2\u52a8\u7387\u4e0b\u7684\u6d41\u52a8\u6027\u6a2a\u626b\uff09\uff1a<\/strong> \u5728\u9ad8\u6ce2\u52a8\u7387\uff080.025\uff09\u7684\u7a97\u53e3\u671f\u5927\u4e3e\u5356\u51fa 5,000 \u80a1\u3002\u8fd9\u52a0\u5267\u4e86\u5fae\u89c2\u7ed3\u6784\u4e0a\u7684\u4e1a\u7ee9\u635f\u8017\uff0c\u8feb\u4f7f\u6700\u7ec8\u7684\u6210\u4ea4\u4ef7\u88ab\u4e25\u91cd\u60e9\u7f5a\uff0c\u8fdc\u4f4e\u4e8e\u5386\u53f2\u57fa\u51c6\u6536\u76d8\u4ef7 152.00 \u7f8e\u5143\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em><em><a href=\"https:\/\/www.patternsmart.com\/cn\/custom\/\">\u5982\u679c\u60a8\u6b63\u5728\u5bfb\u627e\u4e13\u4e1a\u7684\u5b9a\u5236\u4ea4\u6613\u8f6f\u4ef6\u5f00\u53d1\u670d\u52a1\uff0c\u6b22\u8fce\u8054\u7cfb\u6211\u4eec\u3002<\/a><\/em><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5f3a\u5927\u7684 Python \u5b9e\u73b0\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u81ea\u5b9a\u4e49\u7684 Pandas \u56de\u6d4b\u7cfb\u7edf\u4e2d\uff0c\u5bf9\u57fa\u4e8e\u6210\u4ea4\u91cf\u7684\u52a8\u6001\u6ed1\u70b9\u8fdb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1821,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[233],"tags":[],"class_list":["post-1820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-233"],"jetpack_featured_media_url":"https:\/\/www.patternsmart.com\/cn\/wp-content\/uploads\/2026\/07\/\u56fe\u7247-3.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/posts\/1820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/comments?post=1820"}],"version-history":[{"count":2,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/posts\/1820\/revisions"}],"predecessor-version":[{"id":1823,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/posts\/1820\/revisions\/1823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/media\/1821"}],"wp:attachment":[{"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/media?parent=1820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/categories?post=1820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.patternsmart.com\/cn\/wp-json\/wp\/v2\/tags?post=1820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}