{"id":566,"date":"2025-07-29T23:48:04","date_gmt":"2025-07-29T15:48:04","guid":{"rendered":"https:\/\/khsci.com\/khQuant\/?page_id=566"},"modified":"2026-11-20T12:09:04","modified_gmt":"2026-11-20T04:09:04","slug":"chapter13","status":"publish","type":"page","link":"https:\/\/khsci.com\/khQuant\/chapter13\/","title":{"rendered":"3.2 \u7b56\u7565\u5de5\u5177\u7bb1"},"content":{"rendered":"\n<style>\n.khq-web-manual{max-width:980px;margin:0 auto;padding:8px 0 36px;line-height:1.82;font-size:16px;color:#243042;word-break:break-word;}\n.khq-web-manual h2{font-size:1.45rem;line-height:1.4;margin:38px 0 14px;padding-top:8px;color:#12345f;border-top:1px solid #e7edf5;}\n.khq-web-manual h3{font-size:1.16rem;line-height:1.5;margin:24px 0 10px;color:#163b68;}\n.khq-web-manual blockquote{margin:18px 0;padding:14px 18px;border-left:4px solid #1e88e5;background:#f4f9ff;color:#36506f;border-radius:0 10px 10px 0;}\n.khq-web-manual code{background:#f3f6fa;border:1px solid #e6edf5;border-radius:5px;padding:.12em .36em;font-size:.92em;}\n.khq-web-manual pre{background:#0f172a;color:#e5edf7;border-radius:12px;padding:16px;overflow:auto;line-height:1.65;border:1px solid #24324a;}\n.khq-web-manual pre code{background:transparent;border:0;color:inherit;padding:0;font-size:.93em;white-space:pre;}\n.khq-tn .khq-tn-table{width:100%;overflow-x:auto;margin:16px 0 22px;}\n.khq-web-manual.khq-tn table{display:table!important;width:100%!important;max-width:100%;border-collapse:collapse!important;margin:0!important;font-size:.96em;border:1px solid #e2e8f0!important;table-layout:auto;}\n.khq-web-manual.khq-tn th,.khq-web-manual.khq-tn td{border:1px solid #e2e8f0!important;padding:10px 12px;vertical-align:top;}\n.khq-web-manual th{background:#f7fafc;color:#173b63;font-weight:600;}\n.khq-web-manual img{max-width:100%;height:auto;border-radius:10px;box-shadow:0 8px 28px rgba(15,35,60,.09);}\n.khq-web-manual a{color:#1167b1;text-decoration:none;border-bottom:1px solid rgba(17,103,177,.25);}\n.khq-web-manual a:hover{border-bottom-color:rgba(17,103,177,.8);}\n.khq-tn .khq-tn-ver{display:flex;flex-wrap:wrap;align-items:center;gap:8px;margin:0 0 16px;font-size:14px;color:#5b6b7c;}\n.khq-tn .ver{display:inline-block;padding:2px 10px;border-radius:999px;font-weight:600;font-size:13px;line-height:1.6;}\n.khq-tn .ver-v2{background:#e8f3ff;color:#0b5cad;border:1px solid #b8d8fb;}\n.khq-tn .ver-v3{background:#fff1e6;color:#b1520a;border:1px solid #f8cfa8;}\n.khq-tn .ver-note{color:#6b7a8c;}\n.khq-tn .khq-tn-video{margin:6px 0 26px;border-radius:14px;overflow:hidden;background:#0f172a;box-shadow:0 10px 30px rgba(15,35,60,.12);}\n.khq-tn .khq-tn-video video{display:block;width:100%;aspect-ratio:16\/9;background:#000;}\n.khq-tn .khq-tn-video .cap{padding:8px 14px;color:#cbd6e4;font-size:13px;}\n.khq-tn .khq-tn-video.empty{background:linear-gradient(135deg,#eef4fb,#f7f9fc);border:1px dashed #b9cbe0;box-shadow:none;}\n.khq-tn .khq-tn-video.empty .ph{aspect-ratio:16\/9;max-height:300px;width:100%;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:6px;color:#4d6480;text-align:center;padding:16px;box-sizing:border-box;}\n.khq-tn .khq-tn-video.empty .play{width:54px;height:54px;border-radius:50%;background:#dbe7f5;color:#7b93b0;display:flex;align-items:center;justify-content:center;font-size:22px;padding-left:4px;box-sizing:border-box;}\n.khq-tn .khq-tn-video.empty span:last-child{font-size:13px;color:#7890ab;}\n.khq-tn .khq-tn-callout{margin:18px 0;padding:12px 16px 4px;border-radius:12px;border:1px solid;}\n.khq-tn .khq-tn-callout .lbl{font-weight:700;font-size:13px;margin-bottom:4px;letter-spacing:.04em;}\n.khq-tn .khq-tn-callout p{margin:0 0 10px;}\n.khq-tn .khq-tn-callout.v3{background:#fff8f1;border-color:#f6d3b0;}\n.khq-tn .khq-tn-callout.v3 .lbl{color:#b1520a;}\n.khq-tn .khq-tn-callout.tip{background:#f3f9ff;border-color:#cfe3f7;}\n.khq-tn .khq-tn-callout.tip .lbl{color:#0b5cad;}\n.khq-tn .khq-tn-callout.warn{background:#fff6f5;border-color:#f3c7c2;}\n.khq-tn .khq-tn-callout.warn .lbl{color:#b3261e;}\n.khq-tn .khq-tn-fig{margin:18px 0 22px;text-align:center;}\n.khq-tn .khq-tn-fig figcaption{margin-top:6px;color:#66758a;font-size:.9em;}\n.khq-tn .khq-tn-fig.pending .ph{border:1px dashed #c7d3e0;border-radius:10px;background:#f7f9fb;color:#8594a6;font-size:14px;padding:34px 16px;}\n.khq-tn p.q{margin:18px 0 4px;}\n.khq-tn .khq-tn-nav{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:40px;padding-top:18px;border-top:1px solid #e7edf5;}\n.khq-tn .khq-tn-nav a{display:block;padding:12px 14px;border:1px solid #e2e8f0;border-radius:12px;border-bottom:1px solid #e2e8f0;color:#163b68;font-weight:600;}\n.khq-tn .khq-tn-nav a:hover{border-color:#9cc3ea;background:#f7fbff;}\n.khq-tn .khq-tn-nav a span{display:block;font-size:12px;font-weight:400;color:#7a8a9c;margin-bottom:2px;}\n.khq-tn .khq-tn-nav a.next{text-align:right;}\n@media (max-width:768px){.khq-web-manual{font-size:15px;padding:0 2px 24px}.khq-web-manual h2{font-size:1.27rem}.khq-web-manual h3{font-size:1.08rem}.khq-web-manual pre{padding:13px;border-radius:10px}.khq-tn .khq-tn-nav{grid-template-columns:1fr}.khq-tn .khq-tn-nav a.next{text-align:left}}\n.khq-tn .khq-tn-figrow{display:grid;grid-template-columns:1fr 1fr;gap:16px;align-items:start;margin:8px 0 20px;}\n.khq-tn .khq-tn-figrow .khq-tn-fig{margin:0;}\n@media (max-width:640px){.khq-tn .khq-tn-figrow{grid-template-columns:1fr;}}\n.khq-tn .khq-tn-fig a{border:0;display:inline-block;cursor:zoom-in;}\n.khq-tn .khq-tn-fig img{max-width:100%;height:auto;border:1px solid #e2e8f0;}\n.khq-tn .khq-tn-v3{position:relative;margin:22px 0;padding:15px 18px 15px 24px;border-radius:14px;background:#fffaf5;border:1px solid #fbd9c2;overflow:hidden;}\n.khq-tn .khq-tn-v3::before{content:\"\";position:absolute;left:0;top:0;bottom:0;width:5px;background:linear-gradient(180deg,#ff8a00,#e52e71);}\n.khq-tn .khq-tn-v3.promo{background:linear-gradient(135deg,#fff6ec 0%,#ffeef4 100%);border-color:#f7c6b0;box-shadow:0 8px 26px rgba(229,46,113,.10);}\n.khq-tn .v3-head{display:flex;align-items:center;flex-wrap:wrap;gap:10px;margin-bottom:6px;}\n.khq-tn .v3-badge{display:inline-block;background:linear-gradient(90deg,#ff8a00,#e52e71);color:#fff;border-radius:999px;padding:1px 11px;font-size:12.5px;font-weight:700;letter-spacing:.5px;line-height:1.8;white-space:nowrap;}\n.khq-tn .v3-title{font-weight:700;color:#8c2d10;font-size:1.04em;}\n.khq-tn .khq-tn-v3.promo .v3-title{font-size:1.12em;background:linear-gradient(90deg,#d95f00,#c41f5f);-webkit-background-clip:text;background-clip:text;color:transparent;}\n.khq-tn .v3-body{color:#4a3a33;}\n.khq-tn .v3-body p{margin:6px 0;}\n.khq-tn .v3-body ul{margin:6px 0 6px 1.2em;padding:0;}\n.khq-tn .v3-body .khq-tn-fig{margin:12px 0 4px;}\n.khq-tn .v3-body .khq-tn-fig img{border-color:#f3d2c2;}\n.khq-tn .v3-body .khq-tn-fig img{max-height:480px;width:auto;object-fit:contain;}\n.khq-tn .v3-body .khq-tn-figrow{margin:10px 0 0;}\n.khq-tn .v3-cta{display:flex;flex-wrap:wrap;gap:10px;margin-top:12px;}\n.khq-tn .v3-btn{display:inline-block;border-radius:999px;padding:7px 18px;font-weight:700;font-size:14.5px;line-height:1.5;text-decoration:none!important;border-bottom:0!important;}\n.khq-tn .v3-btn.primary{background:linear-gradient(90deg,#ff8a00,#e52e71);color:#fff!important;box-shadow:0 6px 16px rgba(229,46,113,.25);}\n.khq-tn .v3-btn.primary:hover{filter:brightness(1.06);}\n.khq-tn .v3-btn.ghost{background:#fff;color:#c2410c!important;border:1px solid #f5b58f!important;}\n.khq-tn .khq-tn-v3grid{margin:22px 0 26px;padding:20px;border-radius:18px;background:linear-gradient(135deg,#1d1433 0%,#3b1732 100%);color:#f4e9ef;}\n.khq-tn .v3-grid-head{display:flex;flex-wrap:wrap;align-items:center;gap:10px;margin-bottom:14px;}\n.khq-tn .v3-grid-head b{font-size:1.2em;color:#fff;}\n.khq-tn .v3-grid-head .v3-sub{flex-basis:100%;color:#d9c3cf;font-size:.95em;}\n.khq-tn .v3-tiles{display:grid;grid-template-columns:repeat(3,1fr);gap:12px;}\n.khq-tn .v3-tile{background:rgba(255,255,255,.06);border:1px solid rgba(255,160,120,.25);border-radius:12px;padding:14px;}\n.khq-tn .v3-ico{font-size:24px;line-height:1.2;}\n.khq-tn .v3-tt{font-weight:700;color:#ffb36b;margin:6px 0 4px;}\n.khq-tn .v3-td{font-size:.93em;color:#efe2ea;line-height:1.65;}\n.khq-tn .v3-v2{margin-top:8px;font-size:.82em;color:#a8909c;}\n.khq-tn .khq-tn-v3grid .v3-note{color:#c9aebb;font-size:.85em;margin:12px 0 0;}\n.khq-tn .khq-tn-v3grid .v3-btn.ghost{background:transparent;color:#ffcfae!important;border-color:rgba(255,190,150,.6)!important;}\n.khq-tn .khq-tn-v3end{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:16px;margin:34px 0 8px;padding:18px 22px;border-radius:16px;background:linear-gradient(120deg,#1d1433 0%,#4a1636 100%);color:#f4e9ef;}\n.khq-tn .v3-end-l{flex:1 1 380px;}\n.khq-tn .v3-end-t{display:inline-block;margin-left:8px;font-weight:700;font-size:1.1em;color:#fff;}\n.khq-tn .khq-tn-v3end ul{list-style:none;margin:10px 0 0;padding:0;}\n.khq-tn .khq-tn-v3end li{position:relative;padding-left:22px;margin:4px 0;color:#efe2ea;font-size:.95em;}\n.khq-tn .khq-tn-v3end li::before{content:\"\u2713\";position:absolute;left:0;color:#ffb36b;font-weight:700;}\n.khq-tn .khq-tn-v3end .v3-note{color:#c9aebb;font-size:.82em;margin:8px 0 0;}\n.khq-tn .khq-tn-v3end .v3-cta{flex-direction:column;margin:0;}\n.khq-tn .khq-tn-v3end .v3-btn{text-align:center;}\n.khq-tn .khq-tn-v3end .v3-btn.ghost{background:transparent;color:#ffcfae!important;border-color:rgba(255,190,150,.6)!important;}\n.khq-tn .v3-cases{display:grid;grid-template-columns:1fr 1fr;gap:12px;margin:10px 0 8px;}\n.khq-tn .v3-case{display:block;background:#fff;border:1px solid #f5c7ab;border-radius:12px;padding:12px 14px;text-decoration:none!important;border-bottom:1px solid #f5c7ab!important;transition:box-shadow .15s,transform .15s;}\n.khq-tn .v3-case:hover{box-shadow:0 8px 20px rgba(229,46,113,.14);transform:translateY(-1px);}\n.khq-tn .v3-case .c-t{display:flex;align-items:center;gap:8px;font-weight:700;color:#b33a12;font-size:1.02em;}\n.khq-tn .v3-case .c-tag{font-size:11.5px;font-weight:700;color:#fff;background:linear-gradient(90deg,#ff8a00,#e52e71);border-radius:999px;padding:0 8px;line-height:1.7;}\n.khq-tn .v3-case .c-d{color:#5a4a42;font-size:.92em;line-height:1.65;margin-top:4px;}\n.khq-tn .v3-case .c-go{color:#c2410c;font-size:.9em;font-weight:700;margin-top:6px;}\n.khq-tn .v3-cases > p,.khq-tn .v3-cases > br{display:none;}\n.khq-tn .v3-warn{margin:12px 0 4px;padding:10px 14px;border-radius:10px;background:#fff4e5;border:1px dashed #f0a35e;color:#8a4b12;font-size:.93em;line-height:1.7;}\n.khq-tn .v3-warn b{color:#b45309;}\n@media (max-width:640px){.khq-tn .v3-cases{grid-template-columns:1fr;}}\n@media (max-width:900px){.khq-tn .v3-tiles{grid-template-columns:repeat(2,1fr);}}\n@media (max-width:600px){.khq-tn .v3-tiles{grid-template-columns:1fr;}.khq-tn .khq-tn-v3end .v3-cta{flex-direction:row;}}\n<\/style>\n<style>.khq-tn-ver .plat{display:inline-block;margin-left:6px;padding:1px 8px;border-radius:10px;font-size:.82em;background:#eef3f9;color:#2b4a6f;border:1px solid #d7e2ef}.khq-tn-ver .plat.no{background:#f6f6f6;color:#999;text-decoration:line-through;border-color:#e5e5e5}.khq-web-manual pre{white-space:pre-wrap}<\/style>\n<div class=\"khq-web-manual khq-tn\">\n<div class=\"khq-tn-ver\">\u9002\u7528\u7248\u672c <span class=\"ver ver-v2\">V2<\/span><span class=\"ver ver-v3\">V3<\/span><\/div>\n\n<div class=\"khq-tn-callout tip\"><div class=\"lbl\">\u5148\u8bfb\u8fd9\u91cc\uff1a\u7c97\u8bfb\u5373\u53ef\uff0c\u7528\u65f6\u518d\u67e5<\/div>\n\n<p>\u5199\u7b56\u7565\u65f6\uff0c\u5927\u90e8\u5206\u4ee3\u7801\u90fd\u5728\u91cd\u590d\u51e0\u4ef6\u4e8b\uff1a\u53d6\u5f53\u524d\u65f6\u95f4\u548c\u4ef7\u683c\u3001\u67e5\u6301\u4ed3\u3001\u62ff\u4e00\u6bb5\u5386\u53f2 K \u7ebf\u7b97\u6307\u6807\u3001\u7b97\u80fd\u4e70\u591a\u5c11\u80a1\u3001\u62fc\u4ea4\u6613\u4fe1\u53f7\u3002khQuant \u628a\u8fd9\u4e9b\u5c01\u88c5\u6210\u4e86\u4e00\u7ec4\u73b0\u6210\u51fd\u6570\uff0c\u7b56\u7565\u6587\u4ef6\u5f00\u5934\u5199\u4e00\u884c <code>from khQuantImport import *<\/code> \u5c31\u80fd\u5168\u90e8\u76f4\u63a5\u8c03\u7528\uff0c\u4e0d\u7528\u5173\u5fc3\u5b83\u4eec\u6765\u81ea\u54ea\u4e2a\u5185\u90e8\u6a21\u5757\u3002<\/p>\n<p>\u8fd9\u4e00\u7bc7\u6309\u7528\u9014\u628a\u8fd9\u4e9b\u51fd\u6570\u5206\u6210\u51e0\u7ec4\uff0c\u6bcf\u4e2a\u51fd\u6570\u7ed9\u51fa\u7b7e\u540d\u3001\u7528\u9014\u3001\u5173\u952e\u53c2\u6570\u548c\u4e00\u4e2a\u77ed\u4f8b\u5b50\u3002\u5e38\u7528\u7684\u653e\u5728\u524d\u9762\u8bb2\u900f\uff0c\u4e0d\u5e38\u7528\u7684\u653e\u5728\u6587\u672b\u901f\u67e5\u8868\u3002V2 \u548c V3 \u7684\u7b56\u7565\u63a5\u53e3\u5b8c\u5168\u76f8\u540c\uff0c\u672c\u7bc7\u7684\u51fd\u6570\u4e24\u7248\u90fd\u80fd\u7528\u3002\u7b56\u7565\u6587\u4ef6\u6574\u4f53\u600e\u4e48\u7ec4\u7ec7\u3001\u51e0\u4e2a\u56de\u8c03\u5206\u522b\u5728\u4ec0\u4e48\u65f6\u5019\u88ab\u8c03\u7528\uff0c\u8bf7\u5148\u770b <a href=\"https:\/\/khsci.com\/khQuant\/chapter12\/\">3.1 \u6846\u67b6\u4ecb\u7ecd<\/a>\u3002<\/p>\n\n<p>\u8fd9\u4e9b\u51fd\u6570\u7684\u7b7e\u540d\u3001\u53c2\u6570\u548c\u7528\u6cd5\u7684\u5b8c\u6574\u5b9a\u4e49\u90fd\u5df2\u7ecf\u5199\u8fdb khQuant Skill\uff08\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/khquant-skill\/\">3.3<\/a>\uff09\u3002\u7528 AI \u5199\u7b56\u7565\u65f6\uff0cAI \u4f1a\u81ea\u5df1\u53bb\u67e5\u8fd9\u4e9b\u89c4\u5219\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u6df1\u5165\u638c\u63e1\u6846\u67b6\u7ec6\u8282\u3002\u8fd9\u4e00\u7ae0\u7c97\u7565\u8bfb\u4e00\u904d\u3001\u77e5\u9053\u6709\u54ea\u4e9b\u4e1c\u897f\u5c31\u591f\u4e86\uff0c\u4ee5\u540e\u9700\u8981\u65f6\u518d\u628a\u5b83\u5f53\u624b\u518c\u67e5\u9605\u3002\u60f3\u76f4\u63a5\u8ba9 AI \u5e2e\u4f60\u5199\u7b56\u7565\uff0c\u770b <a href=\"https:\/\/khsci.com\/khQuant\/ai-strategy\/\">3.4<\/a>\uff1bV3 \u7528\u6237\u7684\u4e0a\u624b\u8def\u7ebf\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/cli\/\">2.2<\/a>\u3002<\/p><\/div>\n\n<p class=\"khq-tn-lead\" style=\"font-size:17px;line-height:1.75;color:#163b68;background:#f3f7fc;border-left:4px solid #2b6cb0;padding:12px 18px;border-radius:8px;margin:18px 0 26px;\"><strong>\u672c\u7bc7\u91cd\u70b9\uff1a<\/strong>\u672c\u7bc7\u662f\u7b56\u7565\u91cc\u53ef\u7528\u51fd\u6570\u7684\u5b8c\u6574\u6e05\u5355\uff1a\u53d6\u6570\u3001\u5386\u53f2\u6570\u636e\u3001\u6307\u6807\u3001\u4e0b\u5355\u4e0e\u6302\u5355\u3001\u65e5\u5185\u518d\u89e6\u53d1\u548c\u81ea\u5b9a\u4e49\u6307\u6807\u3002<\/p>\n\n<h2>1 \u5148\u770b\u5168\u8c8c\uff1a\u6309\u7528\u9014\u627e\u51fd\u6570<\/h2>\n<p>\u4e0b\u8868\u662f\u672c\u7bc7\u6d89\u53ca\u7684\u51fd\u6570\u5206\u7ec4\u3002\u8868\u91cc\u6ca1\u6709\u7684\u51b7\u95e8\u51fd\u6570\uff0c\u5728\u7b2c 11 \u8282\u901f\u67e5\u8868\u91cc\uff1b\u7b2c 10 \u8282\u662f\u4e00\u4e2a\u628a\u5e38\u7528\u51fd\u6570\u4e32\u8d77\u6765\u7684\u6a21\u677f\u3002<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u7528\u9014<\/th><th>\u51fd\u6570<\/th><th>\u672c\u7bc7\u4f4d\u7f6e<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>\u53d6\u65f6\u95f4\u4e0e\u884c\u60c5<\/td><td><code>khGet<\/code>\u3001<code>khPrice<\/code>\u3001<code>khHas<\/code>\u3001<code>is_trade_day<\/code>\u3001<code>get_trade_days_count<\/code><\/td><td>\u7b2c 2 \u8282<\/td><\/tr>\n<tr><td>\u5386\u53f2\u6570\u636e\u4e0e\u6307\u6807<\/td><td><code>khHistory<\/code>\u3001<code>khMA<\/code>\u3001<code>khKline<\/code>\u3001MyTT \u6307\u6807\u3001<code>khTdxFormula<\/code>\uff08\u8c03\u7528\u901a\u8fbe\u4fe1\u516c\u5f0f\uff09\u3001\u7f20\u8bba\u5de5\u5177<\/td><td>\u7b2c 3 \u8282<\/td><\/tr>\n<tr><td>\u8d26\u6237\u4e0e\u6301\u4ed3<\/td><td><code>khGet(data, 'positions')<\/code>\u3001<code>PositionParser<\/code><\/td><td>\u7b2c 4 \u8282<\/td><\/tr>\n<tr><td>\u4e0b\u5355\u4e0e\u4ed3\u4f4d<\/td><td><code>generate_signal<\/code>\u3001<code>calculate_max_buy_volume<\/code>\u3001<code>round_price<\/code>\u3001\u8ba2\u5355\u7c7b\u578b\u4e0e\u6709\u6548\u671f<\/td><td>\u7b2c 5 \u8282<\/td><\/tr>\n<tr><td>\u6302\u5355\u7ba1\u7406<\/td><td><code>khGetPendingOrders<\/code>\u3001<code>khGetPendingSummary<\/code>\u3001<code>khCancelOrder<\/code>\u3001<code>khCancelOrdersByCode<\/code>\u3001<code>khCancelAllOrders<\/code><\/td><td>\u7b2c 6 \u8282<\/td><\/tr>\n<tr><td>\u65e5\u5185\u591a\u6b21\u89e6\u53d1<\/td><td><code>khRequestNextDailyTrigger<\/code><\/td><td>\u7b2c 7 \u8282<\/td><\/tr>\n<tr><td>\u81ea\u5b9a\u4e49\u6307\u6807<\/td><td><code>khDuckWrite<\/code>\u3001<code>khDuckDB<\/code>\u3001<code>khAddExtraFields<\/code>\u3001<code>khIndex<\/code><\/td><td>\u7b2c 8 \u8282<\/td><\/tr>\n<tr><td>\u65e5\u5fd7<\/td><td><code>logging<\/code><\/td><td>\u7b2c 9 \u8282<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u6240\u6709\u4f8b\u5b50\u90fd\u9ed8\u8ba4\u5199\u5728\u7b56\u7565\u6587\u4ef6\u91cc\uff0c\u5f00\u5934\u5df2\u7ecf\u6709\uff1a<\/p>\n<pre><code>from khQuantImport import *<\/code><\/pre>\n<p>\u8fd9\u4e00\u884c\u9664\u4e86\u5bfc\u5165 khQuant \u81ea\u5df1\u7684\u51fd\u6570\uff0c\u8fd8\u5bfc\u5165\u4e86 <code>os<\/code>\u3001<code>sys<\/code>\u3001<code>json<\/code>\u3001<code>logging<\/code>\u3001<code>datetime<\/code>\uff08\u6ce8\u610f\u5bfc\u5165\u7684\u662f <code>datetime<\/code> \u7c7b\uff0c\u4e0d\u662f\u6a21\u5757\uff0c\u5199 <code>datetime.now()<\/code>\uff0c\u4e0d\u8981\u5199 <code>datetime.datetime.now()<\/code>\uff09\u3001<code>dt<\/code>\u3001<code>timedelta<\/code>\u3001<code>np<\/code>\u3001<code>pd<\/code>\uff0c\u4ee5\u53ca\u7c7b\u578b\u6807\u6ce8\u7528\u7684 <code>Dict<\/code>\u3001<code>List<\/code>\u3001<code>Optional<\/code> \u7b49\uff0c\u4e0d\u9700\u8981\u518d\u5355\u72ec import\u3002<\/p>\n\n<h2>2 \u53d6\u65f6\u95f4\u4e0e\u884c\u60c5<\/h2>\n\n<h3>2.1 khGet\uff1a\u53d6\u65f6\u95f4\u3001\u8d44\u91d1\u3001\u80a1\u7968\u6c60\u3001\u6301\u4ed3<\/h3>\n<pre><code>khGet(data: Dict, key: str) -&gt; Any<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u4ece\u56de\u8c03\u6536\u5230\u7684 <code>data<\/code> \u91cc\u6309\u300c\u522b\u540d\u300d\u53d6\u503c\uff0c\u7701\u6389\u624b\u5199\u591a\u5c42\u5b57\u5178\u8bbf\u95ee\u3002<code>key<\/code> \u652f\u6301\u4e0b\u9762\u8fd9\u4e9b\u522b\u540d\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>key<\/th><th>\u8fd4\u56de<\/th><th>\u793a\u4f8b\u503c<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>'date'<\/code> \/ <code>'date_str'<\/code><\/td><td>\u65e5\u671f\u5b57\u7b26\u4e32<\/td><td><code>\"2024-07-15\"<\/code><\/td><\/tr>\n<tr><td><code>'date_num'<\/code><\/td><td>\u65e0\u5206\u9694\u7b26\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/td><td><code>\"20240715\"<\/code><\/td><\/tr>\n<tr><td><code>'time'<\/code> \/ <code>'time_str'<\/code><\/td><td>\u65f6\u95f4\u5b57\u7b26\u4e32<\/td><td><code>\"09:31:00\"<\/code><\/td><\/tr>\n<tr><td><code>'datetime'<\/code> \/ <code>'datetime_str'<\/code><\/td><td>\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32<\/td><td><code>\"2024-07-15 09:31:00\"<\/code><\/td><\/tr>\n<tr><td><code>'datetime_obj'<\/code><\/td><td>Python <code>datetime<\/code> \u5bf9\u8c61<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'timestamp'<\/code><\/td><td>\u5f53\u524d\u65f6\u95f4\u6233<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'cash'<\/code><\/td><td>\u53ef\u7528\u8d44\u91d1\uff08float\uff09<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'market_value'<\/code><\/td><td>\u6301\u4ed3\u603b\u5e02\u503c\uff08float\uff09<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'total_asset'<\/code><\/td><td>\u603b\u8d44\u4ea7\uff08float\uff09<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'stocks'<\/code><\/td><td>\u80a1\u7968\u6c60\u4ee3\u7801\u5217\u8868\uff08list\uff09<\/td><td><code>&#91;\"000001.SZ\", ...]<\/code><\/td><\/tr>\n<tr><td><code>'first_stock'<\/code><\/td><td>\u80a1\u7968\u6c60\u7b2c\u4e00\u53ea\u4ee3\u7801\uff1b\u80a1\u7968\u6c60\u4e3a\u7a7a\u65f6\u4e3a <code>None<\/code><\/td><td><code>\"000001.SZ\"<\/code><\/td><\/tr>\n<tr><td><code>'positions'<\/code><\/td><td>\u6301\u4ed3\u5b57\u5178\u7684\u526f\u672c\uff08\u7ed3\u6784\u89c1\u7b2c 4 \u8282\uff09<\/td><td>\u2014<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u8fd4\u56de\u503c\uff1a\u6309 key \u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u3002\u53d6\u4e0d\u5230\u65f6\u8fd4\u56de\u5b89\u5168\u9ed8\u8ba4\u503c\uff1a\u65f6\u95f4\u7c7b\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff0c\u8d44\u91d1\u7c7b\u8fd4\u56de <code>0<\/code>\u3002\u4e0d\u5728\u4e0a\u8868\u91cc\u7684 key\uff0c\u4f1a\u539f\u6837\u5f53\u4f5c <code>data<\/code> \u7684\u952e\u53bb\u53d6\uff0c\u53d6\u4e0d\u5230\u8fd4\u56de <code>None<\/code>\u3002<\/p>\n<pre><code>def khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    today = khGet(data, 'date_num')        # \"20240715\"\n    now = khGet(data, 'datetime_str')      # \"2024-07-15 09:31:00\"\n    cash = khGet(data, 'cash')\n    stocks = khGet(data, 'stocks')\n    logging.info(f\"{now} \u53ef\u7528\u8d44\u91d1 {cash:.2f}\uff0c\u80a1\u7968\u6c60 {len(stocks)} \u53ea\")\n    return &#91;]<\/code><\/pre>\n<div class=\"khq-tn-callout tip\"><div class=\"lbl\">\u63d0\u793a<\/div><p>\u8fd9\u91cc\u7684\u300c\u5f53\u524d\u65f6\u95f4\u300d\u662f\u56de\u6d4b\u65f6\u95f4\uff0c\u4e0d\u662f\u7535\u8111\u65f6\u95f4\u3002<code>date_num<\/code> \u548c <code>datetime_str<\/code> \u6700\u5e38\u7528\u7684\u5730\u65b9\uff0c\u662f\u4f5c\u4e3a <code>khHistory<\/code> \u7684 <code>current_time<\/code> \u53c2\u6570\u4f20\u8fdb\u53bb\uff0c\u89c1\u7b2c 3 \u8282\u3002<\/p><\/div>\n\n<h3>2.2 khPrice\uff1a\u53d6\u5f53\u524d\u884c\u60c5\u5b57\u6bb5<\/h3>\n<pre><code>khPrice(data: Dict, stock_code: str, field: str = 'close') -&gt; float<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u53d6\u67d0\u53ea\u80a1\u7968\u5728\u5f53\u524d\u8fd9\u6839 K \u7ebf\uff08\u6216\u5f53\u524d tick\uff09\u4e0a\u7684\u67d0\u4e2a\u884c\u60c5\u5b57\u6bb5\u3002<\/p>\n<ul>\n<li><code>field<\/code>\uff1a\u9ed8\u8ba4 <code>'close'<\/code>\uff0c\u4e5f\u53ef\u4ee5\u662f <code>'open'<\/code>\u3001<code>'high'<\/code>\u3001<code>'low'<\/code>\u3001<code>'volume'<\/code>\u3001<code>'amount'<\/code> \u7b49\u6570\u636e\u91cc\u6709\u7684\u5b57\u6bb5\u3002<\/li>\n<li>tick \u6570\u636e\u6ca1\u6709\u6709\u6548\u7684 close\uff0c\u4f20 <code>'close'<\/code> \u65f6\u4f1a\u81ea\u52a8\u6539\u53d6 <code>lastPrice<\/code>\uff08\u6700\u65b0\u4ef7\uff09\uff0c\u65e5\u7ebf\u3001\u5206\u949f\u7ebf\u548c tick \u7b56\u7565\u53ef\u4ee5\u7528\u540c\u4e00\u79cd\u5199\u6cd5\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1afloat\u3002\u6570\u636e\u4e0d\u5b58\u5728\u3001\u4e3a None\u3001\u4e3a NaN \u65f6\u90fd\u8fd4\u56de <code>0.0<\/code>\uff0c\u4e0d\u4f1a\u629b\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<pre><code>price = khPrice(data, '000001.SZ')            # \u5f53\u524d\u4ef7\uff08K \u7ebf\u6536\u76d8\u4ef7 \/ tick \u6700\u65b0\u4ef7\uff09\nopen_price = khPrice(data, '000001.SZ', 'open')\nif price &lt;= 0:\n    return &#91;]    # \u5f53\u524d\u6ca1\u6709\u6709\u6548\u4ef7\u683c\uff08\u505c\u724c\u3001\u6570\u636e\u7f3a\u5931\uff09\uff0c\u8df3\u8fc7<\/code><\/pre>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p>\u8fd4\u56de <code>0.0<\/code> \u8868\u793a\u300c\u6ca1\u53d6\u5230\u300d\uff0c\u4e0d\u662f\u4ef7\u683c\u771f\u7684\u4e3a 0\u3002\u62ff\u4ef7\u683c\u53bb\u4e0b\u5355\u6216\u7b97\u80a1\u6570\u4e4b\u524d\uff0c\u4e00\u5b9a\u5148\u5224\u65ad <code>price &lt;= 0<\/code> \u5c31\u8df3\u8fc7\u3002<\/p><\/div>\n\n<h3>2.3 khHas\uff1a\u662f\u5426\u6301\u6709\u67d0\u53ea\u80a1\u7968<\/h3>\n<pre><code>khHas(data: Dict, stock_code: str) -&gt; bool<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u5224\u65ad\u5f53\u524d\u662f\u5426\u6301\u6709\u8be5\u80a1\u7968\u3002\u6301\u4ed3\u91cc\u6709\u8fd9\u53ea\u80a1\u7968\u4e14\u6570\u91cf\u5927\u4e8e 0 \u65f6\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219 <code>False<\/code>\u3002<\/p>\n<pre><code>stock = '600519.SH'\nif not khHas(data, stock):\n    pass   # \u672a\u6301\u4ed3\uff1a\u5199\u4e70\u5165\u903b\u8f91\nelse:\n    pass   # \u5df2\u6301\u4ed3\uff1a\u5199\u5356\u51fa\u903b\u8f91<\/code><\/pre>\n<p>\u6ce8\u610f\u5b83\u53ea\u770b\u603b\u6301\u4ed3\uff0c\u4e0d\u770b\u80fd\u4e0d\u80fd\u5356\u3002T+1 \u4e0b\u5f53\u5929\u4e70\u5165\u7684\u80a1\u7968 <code>khHas<\/code> \u5df2\u7ecf\u662f <code>True<\/code>\uff0c\u4f46\u5f53\u5929\u5356\u4e0d\u51fa\u53bb\u3002\u8981\u5224\u65ad\u53ef\u5356\u6570\u91cf\uff0c\u770b\u7b2c 4 \u8282\u3002<\/p>\n\n<h3>2.4 \u4ea4\u6613\u65e5\u5de5\u5177<\/h3>\n<p><strong>is_trade_day<\/strong>\uff1a\u5224\u65ad\u67d0\u5929\u662f\u4e0d\u662f A \u80a1\u4ea4\u6613\u65e5\u3002<\/p>\n<pre><code>is_trade_day(date_str: str = None) -&gt; bool<\/code><\/pre>\n<p><code>date_str<\/code> \u652f\u6301 <code>\"YYYY-MM-DD\"<\/code> \u548c <code>\"YYYYMMDD\"<\/code>\uff0c\u4e0d\u4f20\u65f6\u5224\u65ad\u7535\u8111\u4e0a\u7684\u4eca\u5929\u3002\u67e5\u8be2\u4f18\u5148\u8bfb\u672c\u5730\u4ea4\u6613\u65e5\u5386\u7f13\u5b58\uff0c\u7f13\u5b58\u6ca1\u8986\u76d6\u8be5\u5e74\u4efd\u65f6\u4f1a\u8054\u7f51\u8865\u9f50\uff1b\u90fd\u53d6\u4e0d\u5230\u65f6\u9000\u5316\u4e3a\u300c\u975e\u5468\u672b\u5373\u4ea4\u6613\u65e5\u300d\u3002<\/p>\n<pre><code>is_trade_day(\"20241001\")    # False\uff0c\u56fd\u5e86\u4f11\u5e02\nis_trade_day(khGet(data, 'date_num'))<\/code><\/pre>\n<p><strong>get_trade_days_count<\/strong>\uff1a\u6570\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\uff08\u542b\u9996\u5c3e\uff09\u6709\u591a\u5c11\u4e2a\u4ea4\u6613\u65e5\u3002<\/p>\n<pre><code>get_trade_days_count(start_date: str, end_date: str) -&gt; int<\/code><\/pre>\n<p>\u4e24\u4e2a\u65e5\u671f<strong>\u53ea\u63a5\u53d7<\/strong> <code>\"YYYY-MM-DD\"<\/code> \u683c\u5f0f\uff0c\u4f20 <code>\"20240101\"<\/code> \u4f1a\u8bb0\u4e00\u6761\u9519\u8bef\u65e5\u5fd7\u5e76\u8fd4\u56de 0\u3002\u5f00\u59cb\u65e5\u671f\u665a\u4e8e\u7ed3\u675f\u65e5\u671f\u4e5f\u8fd4\u56de 0\u3002<\/p>\n<pre><code>days = get_trade_days_count(\"2024-01-01\", \"2024-03-31\")<\/code><\/pre>\n<p>\u9700\u8981\u62ff\u5230\u5177\u4f53\u65e5\u671f\u5217\u8868\u65f6\uff0c\u7528\u901f\u67e5\u8868\u91cc\u7684 <code>get_trade_days_set(start_date, end_date)<\/code>\uff0c\u5b83\u8fd4\u56de <code>\"YYYYMMDD\"<\/code> \u5b57\u7b26\u4e32\u7ec4\u6210\u7684\u96c6\u5408\uff0c\u4e24\u79cd\u65e5\u671f\u683c\u5f0f\u90fd\u63a5\u53d7\u3002<\/p>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p><code>is_trade_time()<\/code> \u5224\u65ad\u7684\u662f<strong>\u7535\u8111\u5f53\u524d\u65f6\u949f<\/strong>\u662f\u5426\u5728 09:30\u201311:30\u300113:00\u201315:00 \u4e4b\u5185\uff0c\u548c\u56de\u6d4b\u65f6\u95f4\u65e0\u5173\u3002\u56de\u6d4b\u91cc\u7528\u5b83\u505a\u65f6\u6bb5\u8fc7\u6ee4\uff0c\u7ed3\u679c\u53d6\u51b3\u4e8e\u4f60\u51e0\u70b9\u70b9\u7684\u300c\u5f00\u59cb\u56de\u6d4b\u300d\uff0c\u51e0\u4e4e\u4e00\u5b9a\u662f\u9519\u7684\u3002\u56de\u6d4b\u91cc\u8981\u6309\u65f6\u6bb5\u8fc7\u6ee4\uff0c\u8bf7\u7528 <code>khGet(data, 'time_str')<\/code> \u81ea\u5df1\u6bd4\u8f83\uff0c\u4f8b\u5982 <code>if khGet(data, 'time_str') &gt;= \"14:50:00\":<\/code>\u3002<\/p><\/div>\n\n<h2>3 \u5386\u53f2\u6570\u636e\u4e0e\u6307\u6807<\/h2>\n\n<h3>3.1 khHistory\uff1a\u53d6\u5386\u53f2 K \u7ebf\uff08\u6700\u5e38\u7528\uff09<\/h3>\n<pre><code>khHistory(symbol_list, fields, bar_count, fre_step,\n          current_time=None, skip_paused=False, fq=None, force_download=False)<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u53d6\u4e00\u53ea\u6216\u591a\u53ea\u80a1\u7968\u5728\u67d0\u4e2a\u65f6\u95f4\u70b9<strong>\u4e4b\u524d<\/strong>\u7684\u82e5\u5e72\u6839 K \u7ebf\uff0c\u662f\u7b97\u6307\u6807\u7684\u57fa\u7840\u3002<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u53c2\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>symbol_list<\/code><\/td><td>\u5355\u4e2a\u4ee3\u7801\u5b57\u7b26\u4e32\u6216\u4ee3\u7801\u5217\u8868\uff0c\u5fc5\u987b\u5e26\u4ea4\u6613\u6240\u540e\u7f00\uff0c\u5982 <code>'000001.SZ'<\/code>\u3002<\/td><\/tr>\n<tr><td><code>fields<\/code><\/td><td>\u5b57\u6bb5\u5217\u8868\uff0c\u5982 <code>&#91;'open', 'high', 'low', 'close', 'volume']<\/code>\u3002\u4e0d\u80fd\u4e3a\u7a7a\u3002<\/td><\/tr>\n<tr><td><code>bar_count<\/code><\/td><td>\u8981\u51e0\u6839 K \u7ebf\uff0c\u5fc5\u987b\u5927\u4e8e 0\u3002<\/td><\/tr>\n<tr><td><code>fre_step<\/code><\/td><td>\u5468\u671f\uff1a<code>'1d'<\/code>\u3001<code>'1m'<\/code>\u3001<code>'5m'<\/code>\u3001<code>'tick'<\/code>\u3002<\/td><\/tr>\n<tr><td><code>current_time<\/code><\/td><td>\u622a\u6b62\u65f6\u95f4\u70b9\uff0c<strong>\u4e0d\u542b<\/strong>\u8be5\u65f6\u95f4\u70b9\u3002\u683c\u5f0f\u89c1\u4e0b\u6587\u3002\u4e0d\u4f20\u65f6\uff0c\u56de\u6d4b\u4e2d\u81ea\u52a8\u7528\u5f53\u524d\u8fd9\u6839 K \u7ebf\u7684\u65f6\u95f4\u3002<\/td><\/tr>\n<tr><td><code>skip_paused<\/code><\/td><td>\u662f\u5426\u8df3\u8fc7\u505c\u724c\u6570\u636e\uff0c\u9ed8\u8ba4 <code>False<\/code>\u3002<\/td><\/tr>\n<tr><td><code>fq<\/code><\/td><td>\u590d\u6743\u65b9\u5f0f\uff1a<code>'pre'<\/code> \u524d\u590d\u6743\u3001<code>'post'<\/code> \u540e\u590d\u6743\u3001<code>'none'<\/code> \u4e0d\u590d\u6743\u3002\u9ed8\u8ba4 <code>None<\/code>\uff1a\u56de\u6d4b\u4e2d\u8ddf\u968f\u754c\u9762\uff08\u914d\u7f6e\uff09\u91cc\u9009\u7684\u590d\u6743\u65b9\u5f0f\uff0c\u5355\u72ec\u5728\u56de\u6d4b\u5916\u8c03\u7528\u65f6\u6309\u524d\u590d\u6743\u3002<\/td><\/tr>\n<tr><td><code>force_download<\/code><\/td><td>\u662f\u5426\u5148\u5f3a\u5236\u4e0b\u8f7d\u6700\u65b0\u6570\u636e\uff0c\u9ed8\u8ba4 <code>False<\/code>\u3002\u56de\u6d4b\u91cc\u4fdd\u6301\u9ed8\u8ba4\u5373\u53ef\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u8fd4\u56de\u503c\uff1a\u5b57\u5178 <code>{\u80a1\u7968\u4ee3\u7801: DataFrame}<\/code>\u3002DataFrame \u6709\u4e00\u5217 <code>time<\/code> \u52a0\u4e0a\u4f60\u8981\u7684\u5b57\u6bb5\uff0c\u6309\u65f6\u95f4\u5347\u5e8f\u6392\u5217\uff0c\u6700\u540e\u4e00\u884c\u662f\u79bb\u622a\u6b62\u65f6\u95f4\u6700\u8fd1\u7684\u90a3\u6839\u3002\u53c2\u6570\u5199\u9519\uff08\u80a1\u7968\u5217\u8868\u6216\u5b57\u6bb5\u4e3a\u7a7a\u3001<code>bar_count<\/code> \u4e0d\u5927\u4e8e 0\u3001\u65f6\u95f4\u683c\u5f0f\u4e0d\u5bf9\uff09\u4f1a\u629b\u51fa <code>ValueError<\/code>\uff1b\u53d6\u6570\u8fc7\u7a0b\u4e2d\u51fa\u9519\u8fd4\u56de\u7a7a\u5b57\u5178 <code>{}<\/code>\uff1b\u6570\u636e\u4e0d\u8db3\u65f6\u8fd4\u56de\u7684\u884c\u6570\u4f1a\u5c11\u4e8e <code>bar_count<\/code>\uff0c\u5e76\u5728\u65e5\u5fd7\u91cc\u63d0\u793a\u3002<\/p>\n\n<p><strong>current_time \u7684\u683c\u5f0f\u548c\u300c\u4e0d\u542b\u300d\u89c4\u5219<\/strong><\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u683c\u5f0f<\/th><th>\u9002\u7528<\/th><th>\u8fc7\u6ee4\u89c4\u5219<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>\"YYYYMMDD\"<\/code>\u3001<code>\"YYYY-MM-DD\"<\/code><\/td><td>\u65e5\u7ebf<\/td><td>\u53ea\u8981\u65e5\u671f<strong>\u65e9\u4e8e<\/strong>\u8be5\u65e5\u7684 K \u7ebf\u3002\u4f20 <code>\"20240115\"<\/code>\uff0c\u6700\u540e\u4e00\u6839\u662f 15 \u65e5\u524d\u4e00\u4e2a\u4ea4\u6613\u65e5\u3002<\/td><\/tr>\n<tr><td><code>\"YYYYMMDD HHMMSS\"<\/code>\u3001<code>\"YYYY-MM-DD HH:MM:SS\"<\/code><\/td><td>\u5206\u949f\u7ebf\u3001tick<\/td><td>\u53ea\u8981\u65f6\u95f4<strong>\u65e9\u4e8e<\/strong>\u8be5\u65f6\u523b\u7684\u6570\u636e\u3002\u4f20 <code>\"2024-01-15 10:30:00\"<\/code>\uff0c\u4e0d\u542b 10:30 \u8fd9\u6839\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u65e5\u7ebf\u5468\u671f\u4e0b\u5373\u4f7f\u4f20\u4e86\u5e26\u65f6\u5206\u79d2\u7684\u65f6\u95f4\uff0c\u4e5f\u53ea\u6309\u65e5\u671f\u6bd4\u8f83\uff0c\u6240\u4ee5\u65e5\u7ebf\u7b56\u7565\u76f4\u63a5\u4f20 <code>khGet(data, 'datetime_str')<\/code> \u4e5f\u80fd\u5f97\u5230\u300c\u622a\u81f3\u6628\u5929\u300d\u7684\u6570\u636e\u3002\u65e5\u671f\u548c\u65f6\u5206\u79d2\u4e4b\u95f4\u8981\u6709\u7a7a\u683c\uff0c<code>\"20240115103000\"<\/code> \u8fd9\u79cd\u8fde\u5199\u7684\u683c\u5f0f\u89e3\u6790\u4e0d\u4e86\uff0c\u4f1a\u62a5\u9519\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u300c\u4e0d\u542b\u300d\uff1a\u7b56\u7565\u5728\u8fd9\u6839 K \u7ebf\u4e0a\u505a\u51b3\u5b9a\u65f6\uff0c\u8fd9\u6839 K \u7ebf\u672c\u8eab\u7684\u6570\u636e\u901a\u8fc7 <code>khPrice<\/code> \u5df2\u7ecf\u80fd\u770b\u5230\uff1b\u5386\u53f2\u6570\u636e\u53ea\u7ed9\u5b83\u4e4b\u524d\u7684\u90e8\u5206\uff0c\u7b97\u5747\u7ebf\u3001\u7b97\u6307\u6807\u65f6\u5c31\u4e0d\u4f1a\u628a\u300c\u5f53\u524d\u300d\u548c\u300c\u8fc7\u53bb\u300d\u6df7\u5728\u4e00\u8d77\uff0c\u4e5f\u4e0d\u4f1a\u7528\u5230\u672a\u6765\u6570\u636e\u3002<\/p>\n\n<p><strong>\u4f8b 1\uff1a\u65e5\u7ebf\u7b56\u7565\u53d6\u6700\u8fd1 20 \u4e2a\u4ea4\u6613\u65e5\u6536\u76d8\u4ef7<\/strong><\/p>\n<pre><code>def khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    stock = '000001.SZ'\n    hist = khHistory(stock, &#91;'close'], 20, '1d',\n                     current_time=khGet(data, 'date_num'))\n    if stock not in hist or len(hist&#91;stock]) &lt; 20:\n        return &#91;]                      # \u6570\u636e\u4e0d\u591f\uff0c\u5148\u4e0d\u505a\u5224\u65ad\n    closes = hist&#91;stock]&#91;'close'].values\n    ma20 = closes.mean()\n    return &#91;]<\/code><\/pre>\n<p><strong>\u4f8b 2\uff1a\u5206\u949f\u7b56\u7565\u53d6\u6700\u8fd1 60 \u6839 1 \u5206\u949f\u7ebf<\/strong><\/p>\n<pre><code>now = khGet(data, 'datetime_str')     # \"2024-01-15 14:00:00\"\nhist = khHistory(&#91;'000001.SZ', '600000.SH'], &#91;'close', 'volume'], 60, '1m',\n                 current_time=now)\nfor code, df in hist.items():\n    if df.empty:\n        continue\n    last_close = df&#91;'close'].iloc&#91;-1]  # 13:59 \u90a3\u6839\u7684\u6536\u76d8\u4ef7<\/code><\/pre>\n<p><strong>\u4f8b 3\uff1a\u5728\u56de\u6d4b\u5916\u67e5\u67d0\u4e2a\u5386\u53f2\u65f6\u70b9\uff08\u590d\u76d8\u3001\u8c03\u8bd5\uff09<\/strong><\/p>\n<pre><code>hist = khHistory('000001.SZ', &#91;'close', 'volume'], 10, '1d',\n                 current_time='2023-01-21')<\/code><\/pre>\n<div class=\"khq-tn-callout tip\"><div class=\"lbl\">\u63d0\u793a<\/div><p>\u4e09\u6761\u4e60\u60ef\uff1a<code>fre_step<\/code> \u548c\u56de\u6d4b\u914d\u7f6e\u91cc\u7684\u89e6\u53d1\u5468\u671f\u3001K \u7ebf\u5468\u671f\u4fdd\u6301\u4e00\u81f4\uff08\u65e5\u7ebf\u7b56\u7565\u7528 <code>'1d'<\/code>\uff09\uff1b<code>current_time<\/code> \u663e\u5f0f\u4f20\u5165\uff0c\u4ee3\u7801\u4e00\u773c\u80fd\u770b\u51fa\u622a\u6b62\u70b9\uff1b\u7528\u7ed3\u679c\u524d\u68c0\u67e5 <code>code in hist<\/code> \u548c\u884c\u6570\u3002\u4e0d\u4f20 <code>fq<\/code> \u65f6\uff0c\u6307\u6807\u548c\u754c\u9762\u884c\u60c5\u7528\u7684\u662f\u540c\u4e00\u79cd\u590d\u6743\uff0c\u8fd9\u4e5f\u662f\u63a8\u8350\u5199\u6cd5\uff1b\u663e\u5f0f\u4f20\u4e86\u4e0e\u754c\u9762\u4e0d\u4e00\u81f4\u7684 <code>fq<\/code>\uff0c\u65e5\u5fd7\u91cc\u4f1a\u63d0\u793a\u4e00\u6b21\u3002<\/p><\/div>\n\n<h3>3.2 khMA\uff1a\u4e00\u884c\u7b97\u5747\u7ebf<\/h3>\n<pre><code>khMA(stock_code: str, period: int, field: str = 'close', fre_step: str = '1d',\n     end_time: str = None, fq: str = None, data: Dict = None) -&gt; float<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u76f4\u63a5\u7b97\u67d0\u53ea\u80a1\u7968\u7684 N \u5468\u671f\u7b80\u5355\u5747\u7ebf\uff0c\u5185\u90e8\u5c31\u662f <code>khHistory<\/code> \u53d6 <code>period<\/code> \u6839\u518d\u6c42\u5e73\u5747\u3002<\/p>\n<ul>\n<li><code>end_time<\/code>\uff1a\u622a\u6b62\u65f6\u95f4\uff0c\u89c4\u5219\u540c <code>khHistory<\/code> \u7684 <code>current_time<\/code>\uff08\u4e0d\u542b\u8be5\u65f6\u95f4\u70b9\uff09\u3002\u56de\u6d4b\u4e2d\u4e0d\u4f20\u65f6\u81ea\u52a8\u7528\u5f53\u524d K \u7ebf\u65f6\u95f4\uff0c\u6240\u4ee5\u65e5\u7ebf\u4e0a <code>khMA<\/code> \u7528\u7684\u662f<strong>\u622a\u81f3\u524d\u4e00\u5929<\/strong>\u7684\u6570\u636e\uff0c\u4e0d\u542b\u5f53\u5929\u3002<\/li>\n<li><code>fq<\/code>\uff1a\u540c <code>khHistory<\/code>\uff0c\u9ed8\u8ba4\u8ddf\u968f\u56de\u6d4b\u7684\u590d\u6743\u65b9\u5f0f\u3002<\/li>\n<li><code>data<\/code>\uff1a\u53ef\u9009\uff0c\u4f20\u5165\u540e\u6309\u6846\u67b6\u8bbe\u7f6e\u7684\u4ef7\u683c\u7cbe\u5ea6\u53d6\u6574\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u6309\u4ef7\u683c\u7cbe\u5ea6\u56db\u820d\u4e94\u5165\u540e\u7684 float\uff08\u80a1\u7968 2 \u4f4d\u5c0f\u6570\uff0cETF \u7b49\u573a\u5185\u57fa\u91d1 3 \u4f4d\uff09\u3002<\/li>\n<li>\u6570\u636e\u4e0d\u8db3 <code>period<\/code> \u6839\u65f6\u4f1a<strong>\u629b\u51fa ValueError<\/strong>\uff0c\u4e0d\u662f\u8fd4\u56de 0\u3002<\/li>\n<\/ul>\n<pre><code>try:\n    ma5 = khMA('000001.SZ', 5, data=data)\n    ma20 = khMA('000001.SZ', 20, data=data)\nexcept ValueError as e:\n    logging.warning(f\"\u5747\u7ebf\u6570\u636e\u4e0d\u8db3: {e}\")\n    return &#91;]<\/code><\/pre>\n<p>\u53ea\u8981\u4e00\u4e24\u6761\u5747\u7ebf\u65f6 <code>khMA<\/code> \u6700\u7701\u4e8b\u3002\u8981\u540c\u65f6\u7b97\u591a\u6761\u5747\u7ebf\u6216\u522b\u7684\u6307\u6807\uff0c\u4e00\u6b21 <code>khHistory<\/code> \u53d6\u591f\u6570\u636e\u518d\u4ea4\u7ed9 MyTT \u8ba1\u7b97\u4f1a\u66f4\u5feb\uff0c\u907f\u514d\u91cd\u590d\u53d6\u6570\u3002<\/p>\n\n<h3>3.3 khKline\uff1a\u4efb\u610f\u5468\u671f K \u7ebf<\/h3>\n<pre><code>khKline(symbol_list, period: str, bar_count: int, fields: List&#91;str] = None,\n        end_time: str = None, fq: str = None, force_download: bool = False)\n        -&gt; Dict&#91;str, pd.DataFrame]<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u628a\u65e5\u7ebf\u6216\u5206\u949f\u7ebf\u62fc\u6210\u4efb\u610f\u5468\u671f\uff0c\u4f8b\u5982 <code>'15m'<\/code>\u3001<code>'30m'<\/code>\u3001<code>'2h'<\/code>\u3001<code>'3d'<\/code>\uff0c\u5e38\u7528\u4e8e\u7f20\u8bba\u7b49\u591a\u7ea7\u522b\u5206\u6790\u3002<\/p>\n<ul>\n<li><code>period<\/code>\uff1a\u6570\u5b57\u52a0\u5355\u4f4d\uff0c\u5355\u4f4d\u662f <code>m<\/code>\uff08\u5206\u949f\uff09\u3001<code>h<\/code>\uff08\u5c0f\u65f6\uff09\u3001<code>d<\/code>\uff08\u5929\uff09\u3002<\/li>\n<li><code>fields<\/code>\uff1a\u9ed8\u8ba4 <code>&#91;'open', 'high', 'low', 'close', 'volume']<\/code>\u3002<\/li>\n<li><code>end_time<\/code>\uff1a\u652f\u6301 <code>'2024-12-01 14:30:00'<\/code>\u3001<code>'2024-12-01 14:30'<\/code>\u3001<code>'20241201 1430'<\/code>\u3001<code>'20241201'<\/code> \u7b49\uff0c\u79d2\u6570\u4f1a\u88ab\u5ffd\u7565\u3002\u56de\u6d4b\u4e2d\u4e0d\u4f20\u65f6\u7528\u5f53\u524d K \u7ebf\u65f6\u95f4\u3002<\/li>\n<li>\u591a\u65e5\u5468\u671f\uff082d\u30013d\u2026\uff09\u6bcf\u5e74\u90fd\u4ee5\u7b2c\u4e00\u4e2a\u4ea4\u6613\u65e5\u4e3a\u8d77\u70b9\u5206\u7ec4\uff1b\u5e74\u521d\u6839\u6570\u4e0d\u591f\u65f6\uff0c\u4f1a\u63a5\u7740\u5f80\u524d\u4e00\u5e74\u53d6\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a<code>{\u80a1\u7968\u4ee3\u7801: DataFrame}<\/code>\uff0c\u51fa\u9519\u65f6\u8fd4\u56de\u7a7a\u5b57\u5178\u3002<\/li>\n<\/ul>\n<pre><code>k15 = khKline('000001.SZ', '15m', 50)\ndf = k15.get('000001.SZ')\nif df is not None and len(df) &gt;= 3:\n    logging.info(f\"\u6700\u65b0\u4e00\u6839 15 \u5206\u949f\u7ebf\u6536\u76d8 {df&#91;'close'].iloc&#91;-1]}\")<\/code><\/pre>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p><code>khKline<\/code> \u4e0e <code>khHistory<\/code> \u7684\u622a\u6b62\u89c4\u5219<strong>\u76f8\u53cd<\/strong>\uff1a\u5b83\u4fdd\u7559\u65f6\u95f4\u5c0f\u4e8e\u7b49\u4e8e <code>end_time<\/code> \u7684\u6570\u636e\uff0c<strong>\u5305\u542b\u5f53\u524d\u8fd9\u6839<\/strong>\u3002\u8fd8\u6ca1\u8d70\u5b8c\u7684\u5468\u671f\uff0c\u6700\u540e\u4e00\u6839\u662f\u5230\u5f53\u524d\u65f6\u523b\u4e3a\u6b62\u7684\u300c\u534a\u622a\u300dK \u7ebf\u3002\u65e5\u7ebf\u56de\u6d4b\u91cc\u7528 <code>'1d'<\/code> \u6216\u591a\u65e5\u5468\u671f\uff0c\u6700\u540e\u4e00\u6839\u5c31\u5305\u542b\u5f53\u5929\u7684\u6536\u76d8\u4ef7\u3002\u7528\u5b83\u5224\u65ad\u5f62\u6001\u65f6\uff0c\u8981\u60f3\u6e05\u695a\u6700\u540e\u4e00\u6839\u662f\u5426\u5df2\u7ecf\u6536\u7ebf\uff0c\u5fc5\u8981\u65f6\u7528\u5012\u6570\u7b2c\u4e8c\u6839\u3002\u53e6\u5916\uff0c\u591a\u65e5\u5468\u671f\u6bcf\u5e74\u6309\u5e74\u521d\u5bf9\u9f50\uff0c\u5e74\u521d\u6839\u6570\u4e0d\u591f\u65f6\u4f1a\u5411\u524d\u4e00\u5e74\u63a5\u7740\u53d6\uff083.4.1.9 \u53ca\u66f4\u65e9\u53ea\u5728\u5f53\u5e74\u8303\u56f4\u5185\u53d6\u6570\uff0c\u5e74\u521d\u62ff\u5230\u7684\u6839\u6570\u4f1a\u5c11\u4e8e <code>bar_count<\/code>\uff09\u3002<\/p><\/div>\n\n<h3>3.4 MyTT \u5e38\u7528\u6307\u6807<\/h3>\n<p>khQuant \u5185\u7f6e MyTT \u6307\u6807\u5e93\uff0c\u51fd\u6570\u540d\u548c\u5e38\u89c1\u884c\u60c5\u8f6f\u4ef6\u7684\u516c\u5f0f\u5199\u6cd5\u4e00\u81f4\uff0c\u8f93\u5165\u662f numpy \u6570\u7ec4\uff08\u6216 pandas Series \u7684 <code>.values<\/code>\uff09\uff0c\u8f93\u51fa\u662f\u540c\u6837\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u53d6 <code>&#91;-1]<\/code> \u5c31\u662f\u6700\u65b0\u503c\u3002\u7528\u6cd5\u56fa\u5b9a\u4e3a\u4e24\u6b65\uff1a\u5148\u7528 <code>khHistory<\/code> \u53d6\u6570\u636e\uff0c\u518d\u4ea4\u7ed9 MyTT \u8ba1\u7b97\u3002<\/p>\n<pre><code>stock = '000001.SZ'\nhist = khHistory(stock, &#91;'close', 'high', 'low'], 100, '1d',\n                 current_time=khGet(data, 'date_num'))\nif stock not in hist or len(hist&#91;stock]) &lt; 60:\n    return &#91;]\nC = hist&#91;stock]&#91;'close'].values\nH = hist&#91;stock]&#91;'high'].values\nL = hist&#91;stock]&#91;'low'].values\n\nDIF, DEA, MACD_BAR = MACD(C)            # \u9ed8\u8ba4 12, 26, 9\nif DIF&#91;-1] &gt; DEA&#91;-1] and DIF&#91;-2] &lt;= DEA&#91;-2]:\n    logging.info(f\"{stock} MACD \u91d1\u53c9\")\nK, D, J = KDJ(C, H, L)\nUPPER, MID, LOWER = BOLL(C)\nrsi14 = RSI(C, 14)&#91;-1]                  # \u60f3\u8981 14 \u65e5 RSI \u8981\u663e\u5f0f\u5199 14<\/code><\/pre>\n<p>\u5e38\u7528\u6307\u6807\u7684\u7b7e\u540d\u548c\u9ed8\u8ba4\u53c2\u6570\uff08\u4ee5\u6e90\u7801 MyTT.py \u4e3a\u51c6\uff09\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u51fd\u6570<\/th><th>\u9ed8\u8ba4\u53c2\u6570<\/th><th>\u8fd4\u56de<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>MA(S, N)<\/code> \/ <code>EMA(S, N)<\/code><\/td><td>\u65e0\u9ed8\u8ba4\uff0cN \u5fc5\u586b<\/td><td>\u5747\u7ebf\u5e8f\u5217<\/td><\/tr>\n<tr><td><code>SMA(S, N, M=1)<\/code><\/td><td>M=1<\/td><td>\u4e2d\u56fd\u5f0f SMA<\/td><\/tr>\n<tr><td><code>MACD(CLOSE, SHORT=12, LONG=26, M=9)<\/code><\/td><td>12, 26, 9<\/td><td><code>(DIF, DEA, MACD)<\/code>\uff0cMACD \u67f1 = (DIF\u2212DEA)\u00d72<\/td><\/tr>\n<tr><td><code>KDJ(CLOSE, HIGH, LOW, N=9, M1=3, M2=3)<\/code><\/td><td>9, 3, 3<\/td><td><code>(K, D, J)<\/code><\/td><\/tr>\n<tr><td><code>RSI(CLOSE, N=24)<\/code><\/td><td><strong>N=24<\/strong><\/td><td>RSI \u5e8f\u5217<\/td><\/tr>\n<tr><td><code>BOLL(CLOSE, N=20, P=2)<\/code><\/td><td>20, 2<\/td><td><code>(UPPER, MID, LOWER)<\/code><\/td><\/tr>\n<tr><td><code>WR(CLOSE, HIGH, LOW, N=10, N1=6)<\/code><\/td><td>10, 6<\/td><td><code>(WR, WR1)<\/code><\/td><\/tr>\n<tr><td><code>BIAS(CLOSE, L1=6, L2=12, L3=24)<\/code><\/td><td>6, 12, 24<\/td><td><code>(BIAS1, BIAS2, BIAS3)<\/code><\/td><\/tr>\n<tr><td><code>CCI(CLOSE, HIGH, LOW, N=14)<\/code><\/td><td>14<\/td><td>CCI \u5e8f\u5217<\/td><\/tr>\n<tr><td><code>ATR(CLOSE, HIGH, LOW, N=20)<\/code><\/td><td>20<\/td><td>\u771f\u5b9e\u6ce2\u5e45 TR \u7684 N \u65e5\u5747\u503c<\/td><\/tr>\n<tr><td><code>TRIX(CLOSE, M1=12, M2=20)<\/code><\/td><td>12, 20<\/td><td><code>(TRIX, TRMA)<\/code><\/td><\/tr>\n<tr><td><code>DMI(CLOSE, HIGH, LOW, M1=14, M2=6)<\/code><\/td><td>14, 6<\/td><td>\u591a\u4e2a\u5e8f\u5217\uff08PDI\u3001MDI\u3001ADX\u3001ADXR\uff09<\/td><\/tr>\n<tr><td><code>BBI(CLOSE, M1=3, M2=6, M3=12, M4=20)<\/code><\/td><td>3, 6, 12, 20<\/td><td>\u591a\u7a7a\u5747\u7ebf<\/td><\/tr>\n<tr><td><code>OBV(CLOSE, VOL)<\/code><\/td><td>\u2014<\/td><td>\u80fd\u91cf\u6f6e<\/td><\/tr>\n<tr><td><code>SAR(HIGH, LOW, N=10, S=2, M=20)<\/code><\/td><td>10, 2, 20<\/td><td>\u629b\u7269\u7ebf\u8f6c\u5411<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u5199\u6761\u4ef6\u65f6\u5e38\u7528\u7684\u57fa\u7840\u51fd\u6570\uff1a<code>REF(S, N=1)<\/code> \u53d6 N \u5468\u671f\u524d\u7684\u503c\uff1b<code>HHV(S, N)<\/code> \/ <code>LLV(S, N)<\/code> \u53d6 N \u5468\u671f\u6700\u9ad8 \/ \u6700\u4f4e\uff1b<code>CROSS(S1, S2)<\/code> \u5224\u65ad\u4e0a\u7a7f\uff1b<code>COUNT(S, N)<\/code>\u3001<code>EVERY(S, N)<\/code>\u3001<code>EXIST(S, N)<\/code> \u7edf\u8ba1 N \u5468\u671f\u5185\u6761\u4ef6\u6210\u7acb\u7684\u6b21\u6570\u3001\u662f\u5426\u5168\u90e8\u6210\u7acb\u3001\u662f\u5426\u6709\u4e00\u6b21\u6210\u7acb\uff1b<code>BARSLAST(S)<\/code> \u4e0a\u6b21\u6761\u4ef6\u6210\u7acb\u5230\u73b0\u5728\u7684\u5468\u671f\u6570\u3002\u5176\u4f59 PSY\u3001MTM\u3001ROC\u3001KTN\u3001VR\u3001EMV\u3001MFI\u3001CR\u3001BRAR\u3001ASI\u3001DPO \u7b49\u6307\u6807\u4e5f\u90fd\u80fd\u76f4\u63a5\u8c03\u7528\uff0c\u540d\u5b57\u548c\u53c2\u6570\u89c1 MyTT.py\u3002<\/p>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p>MyTT \u7684 <code>MA<\/code>\u3001<code>RSI<\/code> \u548c khQuant \u7684 <code>khMA<\/code> \u662f\u4e24\u56de\u4e8b\uff1a\u524d\u8005\u5bf9\u4f60\u4f20\u5165\u7684\u6570\u7ec4\u8ba1\u7b97\uff0c\u8fd4\u56de\u6570\u7ec4\uff1b\u540e\u8005\u81ea\u5df1\u53bb\u53d6\u5386\u53f2\u6570\u636e\uff0c\u8fd4\u56de\u4e00\u4e2a\u6570\u3002\u53e6\u5916\uff0c\u53d6\u6570\u7684\u6839\u6570\u8981\u660e\u663e\u591a\u4e8e\u6307\u6807\u5468\u671f\uff0c\u50cf MACD \u8fd9\u7c7b\u57fa\u4e8e EMA \u7684\u6307\u6807\uff0c\u6570\u636e\u592a\u77ed\u65f6\u524d\u9762\u7684\u503c\u8fd8\u6ca1\u6536\u655b\uff0c\u5efa\u8bae\u53d6 100 \u6839\u4ee5\u4e0a\u3002<\/p><\/div>\n\n<h3 id=\"tdxformula\">3.5 khTdxFormula\uff1a\u76f4\u63a5\u8c03\u7528\u901a\u8fbe\u4fe1\u516c\u5f0f\uff08V3.4.1.16 \u8d77\uff09<\/h3>\n<p>\u7528\u9014\uff1a\u5728\u7b56\u7565\u91cc\u76f4\u63a5\u8c03\u7528\u901a\u8fbe\u4fe1\u300c\u516c\u5f0f\u7ba1\u7406\u5668\u300d\u91cc\u7684\u6280\u672f\u6307\u6807\u516c\u5f0f\uff0c\u7cfb\u7edf\u516c\u5f0f\uff08MACD\u3001KDJ\u3001RSI\u3001BOLL\u2026\u2026\uff09\u548c\u4f60\u81ea\u5df1\u5199\u7684\u6307\u6807\u516c\u5f0f\u90fd\u53ef\u4ee5\u3002\u9002\u5408\u5df2\u7ecf\u5728\u901a\u8fbe\u4fe1\u91cc\u5199\u597d\u3001\u9a8c\u8bc1\u8fc7\u7684\u516c\u5f0f\uff0c\u4e0d\u60f3\u518d\u7528 Python \u91cd\u5199\u4e00\u904d\u7684\u60c5\u51b5\u3002<\/p>\n<pre><code>khTdxFormula(data, stock_code, formula, args='', *, line=None, period=None,\n             dividend_type=None, include_current=False, client_path=None)<\/code><\/pre>\n<ul>\n<li><code>data<\/code>\uff1a\u7b56\u7565\u56de\u8c03\u6536\u5230\u7684 <code>data<\/code>\uff0c\u7528\u5b83\u786e\u5b9a\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u987a\u5e26\u628a\u540c\u4e00\u80a1\u7968\u6c60\u4e00\u8d77\u9884\u53d6\u3002<\/li>\n<li><code>formula<\/code>\uff1a\u516c\u5f0f\u540d\u79f0\uff0c\u548c\u901a\u8fbe\u4fe1\u516c\u5f0f\u7ba1\u7406\u5668\u91cc\u4e00\u81f4\uff0c\u5982 <code>'MACD'<\/code>\u3001<code>'KDJ'<\/code>\uff0c\u81ea\u5b9a\u4e49\u516c\u5f0f\u5199\u5b83\u7684\u540d\u5b57\u3002<\/li>\n<li><code>args<\/code>\uff1a\u53c2\u6570\uff0c\u6309\u516c\u5f0f\u91cc\u53c2\u6570\u7684\u987a\u5e8f\u7528\u82f1\u6587\u9017\u53f7\u5206\u9694\uff0c\u5982 <code>'12,26,9'<\/code>\uff1b\u7559\u7a7a\u7528\u516c\u5f0f\u9ed8\u8ba4\u53c2\u6570\u3002<\/li>\n<li>\u8fd4\u56de <code>{\u8f93\u51fa\u7ebf\u540d: \u6570\u503c}<\/code>\uff1b\u4f20 <code>line='DIF'<\/code> \u65f6\u53ea\u8fd4\u56de\u8fd9\u4e00\u6761\u7ebf\u7684\u6570\u503c\u3002\u4e0a\u5e02\u524d\u3001\u9884\u70ed\u671f\u4e4b\u524d\u6ca1\u6709\u503c\u65f6\u4e3a <code>None<\/code>\u3002<\/li>\n<li><code>period<\/code>\uff08<code>'1d'<\/code>\/<code>'1m'<\/code>\/<code>'5m'<\/code>\uff09\u3001<code>dividend_type<\/code>\uff08<code>'none'<\/code>\/<code>'front'<\/code>\/<code>'back'<\/code>\uff09\u9ed8\u8ba4\u8ddf\u968f\u56de\u6d4b\u8bbe\u7f6e\uff1b\u4e0d\u652f\u6301 tick \u548c\u7b49\u6bd4\u590d\u6743\u3002<\/li>\n<\/ul>\n<pre><code>v = khTdxFormula(data, '000001.SZ', 'MACD', '12,26,9')\n# v == {'DIF': 0.1861, 'DEA': 0.1273, 'MACD': 0.1176}\nj = khTdxFormula(data, '000001.SZ', 'KDJ', '9,3,3', line='J')\nif v['DIF'] is not None and v['DIF'] &gt; v['DEA'] and j &lt; 80:\n    ...<\/code><\/pre>\n<p><strong>\u53d6\u6570\u548c\u901f\u5ea6<\/strong>\uff1a\u6bcf\u53ea\u80a1\u7968\u3001\u6bcf\u4e2a\u516c\u5f0f\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u8f6f\u4ef6\u901a\u8fc7\u72ec\u7acb\u7684\u53d6\u6570\u8fdb\u7a0b\u5411\u901a\u8fbe\u4fe1\u4e00\u6b21\u53d6\u56de\u6574\u6bb5\u6307\u6807\uff08\u65e5\u7ebf\u4ece\u4e0a\u5e02\u8d77\u7b97\uff0c\u5206\u949f\u7ebf\u53d6\u5ba2\u6237\u7aef\u5df2\u6709\u7684\u5168\u90e8\u5206\u949f\u6570\u636e\uff09\uff0c\u5e76\u628a <code>data<\/code> \u91cc\u540c\u4e00\u80a1\u7968\u6c60\u7684\u5176\u4ed6\u80a1\u7968\u4e00\u8d77\u53d6\u56de\uff1b\u4e4b\u540e\u6bcf\u6839 K \u7ebf\u53ea\u662f\u67e5\u8868\uff0c\u7ea6 2 \u6beb\u79d2\u4e00\u6b21\uff0c\u4e0d\u62d6\u6162\u56de\u6d4b\u3002\u6570\u503c\u4fdd\u7559 4 \u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>\u4e0d\u7528\u672a\u6765\u6570\u636e<\/strong>\uff1a\u548c <code>khMA<\/code>\u3001<code>khHistory<\/code> \u4e00\u6837\uff0c\u9ed8\u8ba4\u53ea\u8fd4\u56de\u5f53\u524d K \u7ebf\u4e4b\u524d\u5df2\u7ecf\u5b8c\u6210\u7684\u503c\uff1a\u65e5\u7ebf\u7b56\u7565\u62ff\u5230\u7684\u662f\u524d\u4e00\u4ea4\u6613\u65e5\u7684\u6307\u6807\uff0c\u5206\u949f\u7ebf\u62ff\u5230\u7684\u662f\u4e0a\u4e00\u6839\u7684\u6307\u6807\u3002\u786e\u5b9e\u9700\u8981\u5f53\u524d\u8fd9\u6839\uff08\u6bd4\u5982\u5728\u6536\u76d8\u540e\u505a\u51b3\u7b56\uff09\u65f6\u4f20 <code>include_current=True<\/code>\u3002<\/p>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u4f7f\u7528\u6761\u4ef6<\/div><p>\u53ea\u80fd\u5728 Windows \u4e0a\u7528\uff1b\u56de\u6d4b\u548c\u8fd0\u884c\u671f\u95f4\u901a\u8fbe\u4fe1\u5ba2\u6237\u7aef\u8981\u5f00\u7740\u5e76\u7528\u8d26\u53f7\u767b\u5f55\uff1b\u300c\u8f6f\u4ef6\u8bbe\u7f6e \u2192 \u6570\u636e\u8bbe\u7f6e\u300d\u91cc\u8981\u586b\u597d\u901a\u8fbe\u4fe1\u5ba2\u6237\u7aef\u76ee\u5f55\uff08\u6216\u8c03\u7528\u65f6\u4f20 <code>client_path<\/code>\uff09\u3002\u6307\u6807\u662f\u7528\u901a\u8fbe\u4fe1\u5ba2\u6237\u7aef\u91cc\u7684 K \u7ebf\u548c\u590d\u6743\u7b97\u7684\uff1a\u5206\u949f\u6307\u6807\u53ea\u80fd\u7b97\u5230\u4f60\u5728\u901a\u8fbe\u4fe1\u300c\u76d8\u540e\u6570\u636e\u4e0b\u8f7d\u300d\u91cc\u4e0b\u8fc7\u7684\u65e5\u671f\uff081 \u5206\u949f\u7ea6 100 \u5929\u30015 \u5206\u949f\u7ea6 700 \u5929\uff09\uff1b\u5982\u679c\u5ba2\u6237\u7aef\u7684\u6570\u636e\u548c\u672c\u5730\u56de\u6d4b\u5e93\u4e0d\u4e00\u81f4\uff08\u6bd4\u5982\u5ba2\u6237\u7aef\u5206\u949f\u7ebf\u6ca1\u66f4\u65b0\uff09\uff0c\u6307\u6807\u548c\u6210\u4ea4\u4ef7\u5c31\u4f1a\u5bf9\u4e0d\u4e0a\uff0c\u5148\u5728\u5ba2\u6237\u7aef\u8865\u4e0b\u8f7d\u3002macOS\u3001Linux \u8bf7\u7528 MyTT\u3002<\/p><\/div>\n<p>\u5b9e\u6d4b\uff1a2025 \u5e74\u5168\u5e74 4 \u53ea\u80a1\u7968\u7684\u65e5\u7ebf MACD\u30012026 \u5e74 10\uff5e11 \u6708 2 \u53ea\u7684 5 \u5206\u949f MACD\uff0c\u4e0e MyTT \u6309\u540c\u4e00\u4efd\u672c\u5730\u524d\u590d\u6743\u6570\u636e\u7b97\u51fa\u7684\u7ed3\u679c\u9010\u6839\u4e00\u81f4\uff0c\u5dee\u5f02\u53ea\u6765\u81ea MyTT \u7ed3\u679c\u4fdd\u7559 3 \u4f4d\u5c0f\u6570\u3002\u5b8c\u6574\u7684\u793a\u4f8b\u7b56\u7565\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/examples\/#tdx\">3.5 V3 \u5185\u7f6e\u7b56\u7565\u4e00\u89c8 \u00b7 \u901a\u8fbe\u4fe1\u516c\u5f0f<\/a>\u3002<\/p>\n\n<h3>3.6 \u7f20\u8bba\u5de5\u5177<\/h3>\n<p>\u516d\u4e2a\u51fd\u6570\u90fd\u4ee5\u4e00\u6bb5 K \u7ebf DataFrame \u4e3a\u8f93\u5165\uff0cDataFrame \u81f3\u5c11\u8981\u6709 <code>high<\/code>\u3001<code>low<\/code> \u5217\uff08\u5224\u65ad\u8d8b\u52bf\u8fd8\u8981 <code>close<\/code>\uff09\u3002\u6570\u636e\u901a\u5e38\u6765\u81ea <code>khKline<\/code> \u6216 <code>khHistory<\/code>\u3002<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u51fd\u6570<\/th><th>\u7528\u9014\u4e0e\u8fd4\u56de<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>is_bottom_fractal(kline_data, index=-2)<\/code><\/td><td>\u4ee5 <code>index<\/code> \u5904\u4e3a\u4e2d\u95f4 K \u7ebf\uff0c\u5224\u65ad\u662f\u5426\u4e3a\u5e95\u5206\u578b\uff1a\u4e2d\u95f4\u4e00\u6839\u7684\u9ad8\u70b9\u3001\u4f4e\u70b9\u90fd\u4f4e\u4e8e\u5de6\u53f3\u4e24\u6839\u3002\u8fd4\u56de bool\u3002<\/td><\/tr>\n<tr><td><code>is_top_fractal(kline_data, index=-2)<\/code><\/td><td>\u540c\u4e0a\uff0c\u5224\u65ad\u9876\u5206\u578b\uff1a\u4e2d\u95f4\u4e00\u6839\u7684\u9ad8\u70b9\u3001\u4f4e\u70b9\u90fd\u9ad8\u4e8e\u5de6\u53f3\u4e24\u6839\u3002<\/td><\/tr>\n<tr><td><code>find_all_fractals(kline_data)<\/code><\/td><td>\u626b\u63cf\u6574\u6bb5\u6570\u636e\uff0c\u8fd4\u56de <code>(\u9876\u5206\u578b\u4f4d\u7f6e\u5217\u8868, \u5e95\u5206\u578b\u4f4d\u7f6e\u5217\u8868)<\/code>\u3002<\/td><\/tr>\n<tr><td><code>get_trend_direction(kline_data, period=20)<\/code><\/td><td>\u7efc\u5408\u5747\u7ebf\u65b9\u5411\u3001\u9ad8\u4f4e\u70b9\u548c\u6536\u76d8\u4f4d\u7f6e\u5224\u65ad\u8d8b\u52bf\uff0c\u8fd4\u56de <code>'up'<\/code>\u3001<code>'down'<\/code> \u6216 <code>'sideways'<\/code>\u3002<\/td><\/tr>\n<tr><td><code>get_fractal_price(kline_data, index, fractal_type)<\/code><\/td><td><code>fractal_type<\/code> \u4e3a <code>'top'<\/code> \u8fd4\u56de\u8be5\u6839\u6700\u9ad8\u4ef7\uff0c<code>'bottom'<\/code> \u8fd4\u56de\u6700\u4f4e\u4ef7\u3002<\/td><\/tr>\n<tr><td><code>check_fractal_break(kline_data, fractal_index, fractal_type, current_index=-1)<\/code><\/td><td>\u540e\u7eed K \u7ebf\u662f\u5426\u7a81\u7834\u8be5\u5206\u578b\u7684\u6781\u503c\uff1a\u9876\u5206\u578b\u88ab\u5411\u4e0a\u7a81\u7834\u3001\u5e95\u5206\u578b\u88ab\u5411\u4e0b\u8dcc\u7834\u65f6\u8fd4\u56de <code>True<\/code>\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p>\u5206\u578b\u9700\u8981\u4e2d\u95f4 K \u7ebf\u5de6\u53f3\u5404\u6709\u4e00\u6839\u3002<code>index<\/code> \u9ed8\u8ba4\u662f <code>-2<\/code>\uff0c\u4e5f\u5c31\u662f\u7528\u6700\u65b0\u7684\u4e09\u6839 K \u7ebf\u5224\u65ad\uff08\u4e2d\u95f4\u90a3\u6839\u662f\u5012\u6570\u7b2c\u4e8c\u6839\uff09\uff0c\u9ed8\u8ba4\u5373\u53ef\uff1b\u4e5f\u53ef\u4ee5\u4f20 <code>index<\/code> \u5224\u65ad\u66f4\u65e9\u7684\u4f4d\u7f6e\u30023.4.1.9 \u53ca\u66f4\u65e9\u7684\u7248\u672c\u9ed8\u8ba4\u662f <code>-1<\/code>\uff0c\u7528\u9ed8\u8ba4\u503c\u8c03\u7528\u6c38\u8fdc\u8fd4\u56de False\uff0c\u65e7\u7248\u672c\u8bf7\u663e\u5f0f\u4f20 <code>index=-2<\/code>\u3002<\/p><\/div>\n<pre><code>stock = '000001.SZ'\ndf = khKline(stock, '15m', 50).get(stock)\nif df is not None and len(df) &gt;= 20:\n    if is_bottom_fractal(df, index=-2) and get_trend_direction(df) != 'down':\n        stop_price = get_fractal_price(df, -2, 'bottom')\n        logging.info(f\"{stock} 15 \u5206\u949f\u5e95\u5206\u578b\u786e\u7acb\uff0c\u5206\u578b\u4f4e\u70b9 {stop_price}\")<\/code><\/pre>\n<p>\u6ce8\u610f\u4e0a\u4e00\u5c0f\u8282\u8bf4\u8fc7\u7684\uff1a<code>khKline<\/code> \u7684\u6700\u540e\u4e00\u6839\u53ef\u80fd\u8fd8\u6ca1\u6536\u7ebf\uff0c\u7528 <code>index=-2<\/code> \u5224\u65ad\u65f6\uff0c\u53f3\u8fb9\u90a3\u6839\u5c31\u662f\u8fd9\u6839\u672a\u6536\u7ebf\u7684 K \u7ebf\u3002\u5982\u679c\u8981\u6c42\u5206\u578b\u5b8c\u5168\u786e\u8ba4\uff0c\u53ef\u4ee5\u518d\u5f80\u524d\u9519\u4e00\u6839\uff0c\u6216\u53ea\u5728\u5468\u671f\u6536\u7ebf\u7684\u65f6\u523b\u5224\u65ad\u3002<\/p>\n\n<h2>4 \u8d26\u6237\u4e0e\u6301\u4ed3<\/h2>\n\n<h3>4.1 \u8d26\u6237\u8d44\u91d1<\/h3>\n<p>\u8d44\u91d1\u7c7b\u6570\u636e\u76f4\u63a5\u7528 <code>khGet<\/code> \u53d6\uff1a<code>khGet(data, 'cash')<\/code> \u53ef\u7528\u8d44\u91d1\u3001<code>khGet(data, 'market_value')<\/code> \u6301\u4ed3\u5e02\u503c\u3001<code>khGet(data, 'total_asset')<\/code> \u603b\u8d44\u4ea7\u3002\u5728\u540c\u4e00\u4e2a\u56de\u8c03\u91cc\u8fd4\u56de\u7684\u4fe1\u53f7\u8fd8\u6ca1\u6267\u884c\uff0c\u8d44\u91d1\u4e0d\u4f1a\u5728\u4f60\u751f\u6210\u4fe1\u53f7\u65f6\u7acb\u523b\u6263\u51cf\uff0c\u591a\u53ea\u80a1\u7968\u540c\u65f6\u4e70\u5165\u65f6\u8981\u81ea\u5df1\u5206\u914d\u6bd4\u4f8b\uff08\u89c1\u7b2c 5 \u8282\uff09\u3002<\/p>\n\n<h3>4.2 \u6301\u4ed3\u5b57\u5178<\/h3>\n<p><code>khGet(data, 'positions')<\/code> \u8fd4\u56de <code>{\u80a1\u7968\u4ee3\u7801: \u6301\u4ed3\u4fe1\u606f}<\/code>\uff0c\u6bcf\u53ea\u80a1\u7968\u5e38\u7528\u7684\u5b57\u6bb5\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u5b57\u6bb5<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>volume<\/code><\/td><td>\u603b\u6301\u4ed3\u6570\u91cf\uff08\u80a1\uff09<\/td><\/tr>\n<tr><td><code>can_use_volume<\/code><\/td><td>\u5f53\u524d\u53ef\u5356\u6570\u91cf\uff08\u80a1\uff09\u3002T+1 \u4e0b\u5f53\u5929\u4e70\u5165\u7684\u90e8\u5206\u4e0d\u8ba1\u5165\u3002<\/td><\/tr>\n<tr><td><code>avg_price<\/code><\/td><td>\u6301\u4ed3\u5747\u4ef7<\/td><\/tr>\n<tr><td><code>current_price<\/code><\/td><td>\u6700\u65b0\u4ef7<\/td><\/tr>\n<tr><td><code>market_value<\/code><\/td><td>\u6301\u4ed3\u5e02\u503c<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<pre><code>for code, pos in khGet(data, 'positions').items():\n    cost = pos.get('avg_price', 0)\n    price = khPrice(data, code)\n    if cost &gt; 0 and price &gt; 0 and price \/ cost - 1 &lt; -0.08:\n        logging.info(f\"{code} \u6d6e\u4e8f\u8d85\u8fc7 8%\uff0c\u53ef\u5356 {pos.get('can_use_volume', 0)} \u80a1\")<\/code><\/pre>\n\n<h3>4.3 PositionParser\uff1a\u6301\u4ed3\u89e3\u6790\u5668<\/h3>\n<p>\u4e0d\u60f3\u624b\u5199\u5b57\u5178\u8bbf\u95ee\u65f6\uff0c\u53ef\u4ee5\u7528\u89e3\u6790\u5668\uff1a<\/p>\n<pre><code>pp = PositionParser(data)\npp.has('000001.SZ')         # \u662f\u5426\u6301\u6709\uff0c\u7b49\u540c khHas\npp.get_volume('000001.SZ')  # \u603b\u6301\u4ed3\uff0c\u672a\u6301\u6709\u8fd4\u56de 0\npp.get_cost('000001.SZ')    # \u6301\u4ed3\u5747\u4ef7\uff0c\u672a\u6301\u6709\u8fd4\u56de 0\npp.get_all()                # \u5168\u90e8\u6301\u4ed3\u7684\u526f\u672c<\/code><\/pre>\n<p>\u8981\u53d6\u53ef\u5356\u6570\u91cf\uff0c\u8bfb\u6301\u4ed3\u91cc\u7684 <code>can_use_volume<\/code>\uff1a<code>khGet(data, 'positions')&#91;code].get('can_use_volume', 0)<\/code>\u3002<\/p>\n\n<h2>5 \u4e0b\u5355\u4e0e\u4ed3\u4f4d<\/h2>\n\n<h3>5.1 \u4fe1\u53f7\u5b57\u5178<\/h3>\n<p><code>khHandlebar<\/code> \u8fd4\u56de\u4e00\u4e2a\u4fe1\u53f7\u5217\u8868\uff0c\u6bcf\u4e2a\u4fe1\u53f7\u662f\u4e00\u4e2a\u5b57\u5178\u3002\u5fc5\u586b\u56db\u4e2a\u5b57\u6bb5\uff0c\u5176\u4f59\u53ef\u9009\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u5b57\u6bb5<\/th><th>\u5fc5\u586b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>code<\/code><\/td><td>\u662f<\/td><td>\u80a1\u7968\u4ee3\u7801\uff0c\u5e26\u4ea4\u6613\u6240\u540e\u7f00\u3002<\/td><\/tr>\n<tr><td><code>action<\/code><\/td><td>\u662f<\/td><td><code>'buy'<\/code> \u6216 <code>'sell'<\/code>\u3002<\/td><\/tr>\n<tr><td><code>price<\/code><\/td><td>\u662f<\/td><td>\u59d4\u6258\u4ef7\u3002\u5e02\u4ef7\u5355\u4e5f\u8981\u586b\uff0c\u4e00\u822c\u586b\u5f53\u524d\u4ef7\u3002<\/td><\/tr>\n<tr><td><code>volume<\/code><\/td><td>\u662f<\/td><td>\u59d4\u6258\u6570\u91cf\uff0c<strong>\u5355\u4f4d\u662f\u80a1<\/strong>\uff0c\u4e0d\u662f\u624b\u3002A \u80a1\u4e70\u5165\u6309 100 \u80a1\u6574\u6570\u500d\u586b\u3002\u6570\u91cf\u5c0f\u4e8e\u7b49\u4e8e 0 \u7684\u4fe1\u53f7\u4f1a\u88ab\u5ffd\u7565\u3002<\/td><\/tr>\n<tr><td><code>reason<\/code><\/td><td>\u5426<\/td><td>\u4ea4\u6613\u539f\u56e0\uff0c\u5199\u8fdb\u4ea4\u6613\u8bb0\u5f55\u3002<\/td><\/tr>\n<tr><td><code>order_type<\/code><\/td><td>\u5426<\/td><td><code>'market'<\/code>\u3001<code>'limit'<\/code>\u3001<code>'stop'<\/code>\u3001<code>'stop_limit'<\/code>\uff0c\u89c1\u7b2c 5.5 \u8282\u3002<\/td><\/tr>\n<tr><td><code>stop_price<\/code><\/td><td>\u5426<\/td><td>\u6b62\u635f\u89e6\u53d1\u4ef7\uff0c<code>stop<\/code> \/ <code>stop_limit<\/code> \u5fc5\u987b\u586b\u3002<\/td><\/tr>\n<tr><td><code>time_in_force<\/code><\/td><td>\u5426<\/td><td>\u6709\u6548\u671f\uff0c\u4ee3\u7801\u9ed8\u8ba4 <code>'day'<\/code>\uff08\u5f53\u65e5\u6709\u6548\uff09\u3002<\/td><\/tr>\n<tr><td><code>expire_date<\/code><\/td><td>\u5426<\/td><td>\u914d\u5408 <code>time_in_force='gtd'<\/code>\uff0c\u683c\u5f0f <code>\"YYYY-MM-DD\"<\/code>\u3002<\/td><\/tr>\n<tr><td><code>max_pending_bars<\/code><\/td><td>\u5426<\/td><td>\u6302\u5355\u6700\u591a\u7b49\u5f85\u51e0\u6839 K \u7ebf\uff0c\u8d85\u8fc7\u5373\u8fc7\u671f\uff1b0 \u6216\u4e0d\u586b\u8868\u793a\u4e0d\u9650\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<pre><code>signals = &#91;{\n    \"code\": \"000001.SZ\",\n    \"action\": \"buy\",\n    \"price\": khPrice(data, \"000001.SZ\"),\n    \"volume\": 1000,           # 1000 \u80a1\uff0c\u5373 10 \u624b\n    \"reason\": \"\u624b\u5199\u4fe1\u53f7\u793a\u4f8b\",\n}]\nreturn signals<\/code><\/pre>\n\n<h3>5.2 generate_signal\uff1a\u6309\u6bd4\u4f8b\u751f\u6210\u4fe1\u53f7\uff08\u63a8\u8350\uff09<\/h3>\n<pre><code>generate_signal(data: Dict, stock_code: str, price: float, ratio: float,\n                action: str, reason: str = \"\") -&gt; List&#91;Dict]<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u53ea\u7ed9\u65b9\u5411\u548c\u6bd4\u4f8b\uff0c\u7531\u5b83\u7b97\u597d\u80a1\u6570\u3001\u62fc\u597d\u4fe1\u53f7\u3002<\/p>\n<ul>\n<li><code>ratio<\/code>\uff0c\u4e70\u5165\u65f6\uff1a\u5c0f\u4e8e\u7b49\u4e8e 1 \u8868\u793a\u7528<strong>\u53ef\u7528\u8d44\u91d1<\/strong>\u7684\u6bd4\u4f8b\uff080.5 \u5373\u4e00\u534a\u8d44\u91d1\uff09\uff1b\u5927\u4e8e 1 \u8868\u793a\u4e70\u5165\u7684<strong>\u80a1\u6570<\/strong>\uff0c\u5fc5\u987b\u662f 100 \u7684\u6574\u6570\u500d\uff0c\u5426\u5219\u8fd4\u56de\u7a7a\u5217\u8868\uff1b\u80a1\u6570\u8d85\u8fc7\u8d44\u91d1\u80fd\u4e70\u7684\u4e0a\u9650\u65f6\uff0c\u81ea\u52a8\u964d\u5230\u4e0a\u9650\u3002<\/li>\n<li><code>ratio<\/code>\uff0c\u5356\u51fa\u65f6\uff1a<strong>\u53ef\u5356\u6570\u91cf<\/strong>\uff08<code>can_use_volume<\/code>\uff09\u7684\u6bd4\u4f8b\uff0c1.0 \u5373\u5168\u90e8\u53ef\u5356\u90e8\u5206\u3002\u7ed3\u679c\u5411\u4e0b\u53d6\u6574\u5230 100 \u80a1\u3002<\/li>\n<li><code>price<\/code>\uff1a\u6309\u4ef7\u683c\u7cbe\u5ea6\u56db\u820d\u4e94\u5165\u540e\u5199\u5165\u4fe1\u53f7\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u53ea\u542b\u4e00\u4e2a\u4fe1\u53f7\u7684\u5217\u8868\uff1b\u8d44\u91d1\u4e0d\u8db3\u3001\u6ca1\u6709\u6301\u4ed3\u3001\u7b97\u51fa\u6765\u80a1\u6570\u4e3a 0 \u65f6\u8fd4\u56de\u7a7a\u5217\u8868\uff0c\u5e76\u5728\u65e5\u5fd7\u91cc\u8bf4\u660e\u539f\u56e0\u3002<\/li>\n<\/ul>\n<pre><code>def khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    signals = &#91;]\n    stock = '000001.SZ'\n    price = khPrice(data, stock)\n    if price &lt;= 0:\n        return signals\n    if not khHas(data, stock):\n        signals.extend(generate_signal(data, stock, price, 0.5, 'buy', \"\u7528\u4e00\u534a\u8d44\u91d1\u4e70\u5165\"))\n    else:\n        signals.extend(generate_signal(data, stock, price, 1.0, 'sell', \"\u5168\u90e8\u5356\u51fa\"))\n    return signals<\/code><\/pre>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p><code>generate_signal<\/code> \u8fd4\u56de\u7684\u662f<strong>\u5217\u8868<\/strong>\uff0c\u8981\u7528 <code>signals.extend(...)<\/code> \u5408\u5e76\uff0c\u4e0d\u8981\u7528 <code>append<\/code>\uff0c\u5426\u5219\u4fe1\u53f7\u5217\u8868\u91cc\u4f1a\u5d4c\u4e00\u5c42\u5217\u8868\u3002<\/p><\/div>\n<p>\u540c\u65f6\u4e70\u591a\u53ea\u80a1\u7968\u65f6\uff0c<code>ratio<\/code> \u6bcf\u6b21\u90fd\u662f\u6309\u300c\u5f53\u524d\u53ef\u7528\u8d44\u91d1\u300d\u7b97\u7684\uff0c\u800c\u540c\u4e00\u8f6e\u91cc\u524d\u4e00\u4e2a\u4fe1\u53f7\u8fd8\u6ca1\u6210\u4ea4\u3001\u4e0d\u4f1a\u6263\u8d44\u91d1\u3002\u6240\u4ee5\u60f3\u4e09\u53ea\u80a1\u7968\u5404\u7528\u4e09\u5206\u4e4b\u4e00\u8d44\u91d1\uff0c\u6bcf\u53ea\u90fd\u4f20 <code>1\/3<\/code>\uff0c\u4e0d\u8981\u4f9d\u6b21\u4f20 1\/3\u30011\/2\u30011\u3002<\/p>\n\n<h3>5.3 calculate_max_buy_volume\uff1a\u6700\u591a\u80fd\u4e70\u591a\u5c11\u80a1<\/h3>\n<pre><code>calculate_max_buy_volume(data: Dict, stock_code: str, price: float,\n                         cash_ratio: float = 1.0) -&gt; int<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u6309\u53ef\u7528\u8d44\u91d1\u4e58\u4ee5 <code>cash_ratio<\/code>\uff0c\u5728\u6263\u9664\u4f63\u91d1\u3001\u5370\u82b1\u7a0e\u3001\u8fc7\u6237\u8d39\u548c\u6ed1\u70b9\u4e4b\u540e\uff0c\u7b97\u51fa\u6700\u591a\u80fd\u4e70\u7684\u80a1\u6570\uff0c\u7ed3\u679c\u662f 100 \u7684\u6574\u6570\u500d\u3002\u4e0d\u8db3 100 \u80a1\u6216\u4ef7\u683c\u65e0\u6548\u65f6\u8fd4\u56de 0\u3002<code>generate_signal<\/code> \u4e70\u5165\u65f6\u5185\u90e8\u5c31\u662f\u8c03\u7528\u5b83\u3002<\/p>\n<pre><code>price = khPrice(data, '000001.SZ')\nvol = calculate_max_buy_volume(data, '000001.SZ', price, cash_ratio=0.3)\nif vol &gt; 0:\n    signals.append({\"code\": \"000001.SZ\", \"action\": \"buy\",\n                    \"price\": price, \"volume\": vol, \"reason\": \"30% \u8d44\u91d1\"})<\/code><\/pre>\n<p>\u624b\u5199\u4fe1\u53f7\u65f6\u7528 <code>append<\/code> \u52a0\u5355\u4e2a\u5b57\u5178\u662f\u5bf9\u7684\uff1b\u53ea\u6709 <code>generate_signal<\/code> \u8fd9\u79cd\u8fd4\u56de\u5217\u8868\u7684\u51fd\u6570\u624d\u7528 <code>extend<\/code>\u3002<\/p>\n\n<h3>5.4 \u4ef7\u683c\u7cbe\u5ea6<\/h3>\n<p>\u80a1\u7968\u62a5\u4ef7\u4e24\u4f4d\u5c0f\u6570\uff0cETF\u3001LOF \u7b49\u573a\u5185\u57fa\u91d1\u4e09\u4f4d\u5c0f\u6570\u3002\u81ea\u5df1\u7b97\u51fa\u6765\u7684\u4ef7\u683c\uff08\u6bd4\u5982\u300c\u73b0\u4ef7\u4e0b\u6d6e 1% \u6302\u5355\u300d\uff09\u8981\u5148\u53d6\u6574\u518d\u586b\u8fdb\u4fe1\u53f7\uff1a<\/p>\n<pre><code>round_price(price: float, decimals: int = None, data: Dict = None, stock_code: str = None) -&gt; float\nget_price_decimals(data: Dict = None, stock_code: str = None) -&gt; int<\/code><\/pre>\n<p><code>decimals<\/code> \u4e0d\u4f20\u65f6\u6309 <code>stock_code<\/code> \u5224\u65ad\uff1a\u573a\u5185\u57fa\u91d1 3 \u4f4d\u3001\u666e\u901a A \u80a1\u4ee3\u7801 2 \u4f4d\uff1b\u4e5f\u4e0d\u4f20 <code>stock_code<\/code> \u65f6\uff0c\u7528\u6846\u67b6\u6309\u80a1\u7968\u6c60\u8bbe\u7f6e\u7684\u7cbe\u5ea6\uff08\u7eaf\u80a1\u7968\u6c60 2 \u4f4d\uff0c\u542b\u57fa\u91d1 3 \u4f4d\uff09\u3002<\/p>\n<pre><code>code = '510300.SH'\nlimit_price = round_price(khPrice(data, code) * 0.99, data=data, stock_code=code)<\/code><\/pre>\n<p>\u6846\u67b6\u5728\u5904\u7406\u4fe1\u53f7\u65f6\u4e5f\u4f1a\u6309\u7cbe\u5ea6\u518d\u53d6\u6574\u4e00\u6b21\uff0c\u6240\u4ee5\u5c31\u7b97\u5fd8\u4e86\u53d6\u6574\u4e5f\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u63d0\u524d\u53d6\u6574\u80fd\u8ba9\u65e5\u5fd7\u91cc\u770b\u5230\u7684\u4ef7\u683c\u548c\u5b9e\u9645\u59d4\u6258\u4ef7\u4e00\u81f4\u3002<\/p>\n\n<h3>5.5 \u8ba2\u5355\u7c7b\u578b\u4e0e\u6709\u6548\u671f<\/h3>\n<p>\u64ae\u5408\u5f15\u64ce\u5728 V2 \u548c V3 \u4e2d\u90fd\u59cb\u7ec8\u5f00\u542f\uff0c\u4fe1\u53f7\u4f1a\u6309 <code>order_type<\/code> \u64ae\u5408\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>order_type<\/th><th>\u64ae\u5408\u65b9\u5f0f<\/th><th>\u672a\u6210\u4ea4\u65f6<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>'market'<\/code>\uff08<code>khHandlebar<\/code> \u4fe1\u53f7\u4e0d\u586b\u65f6\u7684\u9ed8\u8ba4\u503c\uff09<\/td><td>\u5f53\u6839\u76f4\u63a5\u6210\u4ea4\uff0c\u6210\u4ea4\u4ef7\u4e3a\u4fe1\u53f7\u4ef7\uff08\u52a0\u6ed1\u70b9\uff09\uff1b\u53ea\u6709\u4e70\u5165\u4ef7\u9ad8\u4e8e\u5f53\u6839\u6700\u9ad8\u4ef7\u3001\u6216\u5356\u51fa\u4ef7\u4f4e\u4e8e\u5f53\u6839\u6700\u4f4e\u4ef7\u65f6\uff0c\u624d\u6539\u6309\u5f53\u524d\u4ef7\u6210\u4ea4\uff08K \u7ebf\u53d6\u5f00\u76d8\u4ef7\uff0ctick \u53d6\u6700\u65b0\u4ef7\uff09\u3002\u4e70\u5165\u4ef7\u4f4e\u4e8e\u6700\u4f4e\u4ef7\u4e0d\u4fee\u6b63\u3002<\/td><td>\u2014<\/td><\/tr>\n<tr><td><code>'limit'<\/code><\/td><td>\u4e70\u5165\uff1a\u5f53\u6839\u6700\u4f4e\u4ef7 \u2264 \u9650\u4ef7\u5373\u6210\u4ea4\uff0c\u5f00\u76d8\u4ef7\u5df2\u4f4e\u4e8e\u9650\u4ef7\u65f6\u6309\u5f00\u76d8\u4ef7\u6210\u4ea4\uff0c\u5426\u5219\u6309\u9650\u4ef7\u3002\u5356\u51fa\u53cd\u4e4b\u3002\u59d4\u6258\u4ef7\u8d85\u51fa\u5f53\u65e5\u6da8\u8dcc\u505c\u8303\u56f4\u76f4\u63a5\u62d2\u5355\u3002<\/td><td>\u8fdb\u5165\u6302\u5355\u961f\u5217\uff0c\u540e\u7eed K \u7ebf\u7ee7\u7eed\u64ae\u5408<\/td><\/tr>\n<tr><td><code>'stop'<\/code><\/td><td>\u4e70\u5165\u6b62\u635f\uff1a\u5f53\u6839\u6700\u9ad8\u4ef7 \u2265 <code>stop_price<\/code> \u65f6\u89e6\u53d1\uff1b\u5356\u51fa\u6b62\u635f\uff1a\u6700\u4f4e\u4ef7 \u2264 <code>stop_price<\/code> \u65f6\u89e6\u53d1\u3002\u89e6\u53d1\u540e\u6309\u6b62\u635f\u4ef7\u6210\u4ea4\uff1b\u8df3\u7a7a\u8d8a\u8fc7\u6b62\u635f\u4ef7\u65f6\u6309\u5f00\u76d8\u4ef7\uff0c\u518d\u9650\u5236\u5728\u5f53\u6839\u6700\u9ad8\u3001\u6700\u4f4e\u4ef7\u4e4b\u95f4\u3002<\/td><td>\u672a\u89e6\u53d1\u5219\u6302\u5355\u7b49\u5f85<\/td><\/tr>\n<tr><td><code>'stop_limit'<\/code><\/td><td>\u89e6\u53d1\u6761\u4ef6\u540c <code>stop<\/code>\uff0c\u89e6\u53d1\u540e\u8f6c\u4e3a\u4ee5 <code>price<\/code> \u4e3a\u9650\u4ef7\u7684\u9650\u4ef7\u5355\u3002<\/td><td>\u672a\u89e6\u53d1\u6216\u9650\u4ef7\u4e0d\u6ee1\u8db3\u65f6\u6302\u5355<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u6240\u6709\u6210\u4ea4\u90fd\u8ba1\u5165\u6ed1\u70b9\u548c\u4ea4\u6613\u8d39\u7528\uff1b\u9650\u4ef7\u5355\u52a0\u6ed1\u70b9\u540e\u7684\u6210\u4ea4\u4ef7\u4e0d\u4f1a\u8d85\u8fc7\u9650\u4ef7\u3002\u4e00\u5b57\u6da8\u505c\u4e70\u4e0d\u8fdb\u3001\u4e00\u5b57\u8dcc\u505c\u5356\u4e0d\u51fa\u3002\u5982\u679c\u5728<a href=\"https:\/\/khsci.com\/khQuant\/chapter11\/\">\u8f6f\u4ef6\u8bbe\u7f6e<\/a>\u91cc\u5f00\u542f\u4e86\u6210\u4ea4\u91cf\u9650\u5236\uff0c\u5355\u6839\u6210\u4ea4\u91cf\u4e0d\u591f\u65f6\u4f1a\u90e8\u5206\u6210\u4ea4\uff0c\u5269\u4f59\u6570\u91cf\u7ee7\u7eed\u6302\u5355\u3002<\/p>\n<p>\u6709\u6548\u671f\u7531 <code>time_in_force<\/code> \u51b3\u5b9a\uff1a<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u5199\u6cd5<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>'day'<\/code>\uff08\u9ed8\u8ba4\uff09<\/td><td>\u5f53\u65e5\u6709\u6548\uff0c\u5f53\u5929\u6536\u76d8\u540e\u4ecd\u672a\u6210\u4ea4\u7684\u6302\u5355\u81ea\u52a8\u8fc7\u671f\u3002<\/td><\/tr>\n<tr><td><code>'gtd'<\/code> + <code>expire_date<\/code><\/td><td>\u6709\u6548\u5230 <code>expire_date<\/code> \u5f53\u5929\u6536\u76d8\uff0c\u4e4b\u540e\u8fc7\u671f\u3002<code>expire_date<\/code> \u5199 <code>\"YYYY-MM-DD\"<\/code>\u3002<\/td><\/tr>\n<tr><td><code>max_pending_bars=N<\/code><\/td><td>\u4e0e\u4e0a\u9762\u4e24\u79cd\u53ef\u540c\u65f6\u4f7f\u7528\uff1a\u6302\u5355\u8d85\u8fc7 N \u6839 K \u7ebf\u4ecd\u672a\u6210\u4ea4\u5c31\u8fc7\u671f\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<pre><code># \u73b0\u4ef7\u4e0b\u65b9 2% \u6302\u4e00\u5f20\u9650\u4ef7\u4e70\u5355\uff0c\u6700\u591a\u7b49\u5230 3 \u5929\u540e\ncode = '000001.SZ'\nprice = khPrice(data, code)\nlimit_price = round_price(price * 0.98, data=data, stock_code=code)\nexpire = (khGet(data, 'datetime_obj') + timedelta(days=3)).strftime('%Y-%m-%d')\nsignals.append({\n    \"code\": code, \"action\": \"buy\", \"price\": limit_price, \"volume\": 500,\n    \"order_type\": \"limit\", \"time_in_force\": \"gtd\", \"expire_date\": expire,\n    \"reason\": \"\u56de\u8c03 2% \u6302\u5355\",\n})\n\n# \u6301\u4ed3\u8bbe\u4e00\u4e2a\u6b62\u635f\uff1a\u8dcc\u5230\u6210\u672c\u4ef7\u7684 92% \u65f6\u5356\u51fa\npos = khGet(data, 'positions').get(code)\nif pos and pos.get('can_use_volume', 0) &gt; 0:\n    stop = round_price(pos&#91;'avg_price'] * 0.92, data=data, stock_code=code)\n    signals.append({\n        \"code\": code, \"action\": \"sell\", \"price\": stop, \"stop_price\": stop,\n        \"volume\": pos&#91;'can_use_volume'], \"order_type\": \"stop\",\n        \"reason\": \"\u6b62\u635f\",\n    })<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u6b62\u635f\u5355\u9ed8\u8ba4\u5f53\u65e5\u6709\u6548\uff0c\u6bcf\u5929\u90fd\u8981\u91cd\u65b0\u4e0b\uff1b\u4e5f\u53ef\u4ee5\u7528 <code>gtd<\/code> \u628a\u6709\u6548\u671f\u62c9\u957f\uff0c\u4f46\u8981\u6ce8\u610f\u4e0d\u8981\u6bcf\u5929\u91cd\u590d\u4e0b\u5355\u3002\u4e0b\u4e00\u8282\u8bb2\u600e\u4e48\u5148\u67e5\u6302\u5355\u518d\u51b3\u5b9a\u662f\u5426\u4e0b\u5355\u3002<\/p>\n<div class=\"khq-tn-callout tip\"><div class=\"lbl\">\u63d0\u793a<\/div><p>\u76d8\u524d\u56de\u8c03 <code>khPreMarket<\/code> \u8fd4\u56de\u7684\u4fe1\u53f7\uff0c\u6ca1\u5199 <code>order_type<\/code> \u65f6\u9ed8\u8ba4\u8f6c\u4e3a\u9650\u4ef7\u5355\u3001\u5f53\u65e5\u6709\u6548\uff1b\u76d8\u540e\u56de\u8c03 <code>khPostMarket<\/code> \u8fd4\u56de\u7684\u4fe1\u53f7\u9ed8\u8ba4\u8f6c\u4e3a\u9650\u4ef7\u5355\u3001\u6709\u6548\u5230\u4e0b\u4e00\u4e2a\u4ea4\u6613\u65e5\u6536\u76d8\u3002\u76d8\u524d\u76d8\u540e\u56de\u8c03\u9700\u8981\u5728\u914d\u7f6e\u91cc\u5f00\u542f\uff0c\u8be6\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/chapter7\/\">1.8 \u89e6\u53d1\u65b9\u5f0f\u4e0e\u76d8\u524d\u76d8\u540e<\/a>\u3002<\/p><\/div>\n\n<h2>6 \u6302\u5355\u7ba1\u7406\uff1a\u67e5\u8be2\u4e0e\u64a4\u5355<\/h2>\n<p>\u9650\u4ef7\u5355\u3001\u6b62\u635f\u5355\u6ca1\u6210\u4ea4\u65f6\u4f1a\u7559\u5728\u6302\u5355\u961f\u5217\u91cc\u3002\u4e0b\u9762\u516d\u4e2a\u51fd\u6570\u7528\u6765\u67e5\u770b\u548c\u64a4\u9500\u6302\u5355\uff0c\u4e24\u7248\u90fd\u6709\uff0c\u53c2\u6570\u91cc\u7684 <code>data<\/code> \u5c31\u662f\u56de\u8c03\u6536\u5230\u7684 <code>data<\/code>\u3002<\/p>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u51fd\u6570<\/th><th>\u8fd4\u56de<\/th><th>\u7528\u9014<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>khGetPendingOrders(data, code=None)<\/code><\/td><td><code>List&#91;Dict]<\/code><\/td><td>\u5f53\u524d\u672a\u6210\u4ea4\u6302\u5355\u5217\u8868\uff1b\u4f20 <code>code<\/code> \u53ea\u8fd4\u56de\u8be5\u80a1\u7968\u7684\u6302\u5355\u3002<\/td><\/tr>\n<tr><td><code>khGetPendingSummary(data)<\/code><\/td><td><code>Dict<\/code><\/td><td>\u6c47\u603b\uff1a<code>total_count<\/code>\u3001<code>buy_count<\/code>\u3001<code>sell_count<\/code>\u3001<code>total_buy_volume<\/code>\u3001<code>total_sell_volume<\/code>\uff08\u6570\u91cf\u6309\u5269\u4f59\u672a\u6210\u4ea4\u80a1\u6570\u7b97\uff09\u3002<\/td><\/tr>\n<tr><td><code>khCancelOrder(data, order_id)<\/code><\/td><td><code>bool<\/code><\/td><td>\u64a4\u9500\u4e00\u7b14\u6302\u5355\uff0c\u6210\u529f\u8fd4\u56de <code>True<\/code>\uff1b\u5355\u53f7\u4e0d\u5b58\u5728\u6216\u5df2\u4e0d\u5728\u6302\u5355\u72b6\u6001\u8fd4\u56de <code>False<\/code>\u3002<\/td><\/tr>\n<tr><td><code>khCancelOrdersByCode(data, code)<\/code><\/td><td><code>int<\/code><\/td><td>\u64a4\u9500\u67d0\u53ea\u80a1\u7968\u7684\u5168\u90e8\u6302\u5355\uff0c\u8fd4\u56de\u64a4\u9500\u7b14\u6570\u3002<\/td><\/tr>\n<tr><td><code>khCancelAllOrders(data)<\/code><\/td><td><code>int<\/code><\/td><td>\u64a4\u9500\u5168\u90e8\u6302\u5355\uff0c\u8fd4\u56de\u64a4\u9500\u7b14\u6570\u3002<\/td><\/tr>\n<tr><td><code>khIsMatchEngineEnabled(data)<\/code><\/td><td><code>bool<\/code><\/td><td>\u64ae\u5408\u5f15\u64ce\u662f\u5426\u542f\u7528\u3002\u73b0\u5728\u64ae\u5408\u5f15\u64ce\u59cb\u7ec8\u5f00\u542f\uff0c\u56de\u8c03\u91cc\u8c03\u7528\u603b\u662f\u8fd4\u56de <code>True<\/code>\uff0c\u4fdd\u7559\u5b83\u53ea\u662f\u4e3a\u4e86\u517c\u5bb9\u65e7\u4ee3\u7801\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n<p><code>khGetPendingOrders<\/code> \u8fd4\u56de\u7684\u6bcf\u7b14\u6302\u5355\uff0c\u5e38\u7528\u5b57\u6bb5\u6709\uff1a<code>order_id<\/code>\uff08\u5355\u53f7\uff0c\u64a4\u5355\u65f6\u7528\uff09\u3001<code>code<\/code>\u3001<code>action<\/code>\u3001<code>price<\/code>\uff08\u59d4\u6258\u4ef7\uff09\u3001<code>volume<\/code>\uff08\u59d4\u6258\u6570\u91cf\uff09\u3001<code>remaining_volume<\/code>\uff08\u5269\u4f59\u672a\u6210\u4ea4\u6570\u91cf\uff09\u3001<code>order_type<\/code>\u3001<code>time_in_force<\/code>\u3001<code>expire_date<\/code>\u3001<code>create_date<\/code>\uff08\u4e0b\u5355\u65e5\u671f\uff09\u3002\u8fd4\u56de\u7684\u662f\u526f\u672c\uff0c\u6539\u5b83\u4e0d\u4f1a\u5f71\u54cd\u771f\u5b9e\u6302\u5355\u3002<\/p>\n<pre><code>def khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    signals = &#91;]\n    code = '000001.SZ'\n    price = khPrice(data, code)\n    if price &lt;= 0:\n        return signals\n\n    pending = khGetPendingOrders(data, code)\n    # \u5df2\u6709\u4e70\u5355\u6302\u7740\uff0c\u4f46\u4ef7\u683c\u5df2\u7ecf\u6bd4\u6302\u5355\u4ef7\u9ad8\u51fa 3%\uff1a\u64a4\u6389\u65e7\u5355\uff0c\u6309\u65b0\u4ef7\u91cd\u6302\n    for order in pending:\n        if order&#91;'action'] == 'buy' and price &gt; order&#91;'price'] * 1.03:\n            if khCancelOrder(data, order&#91;'order_id']):\n                logging.info(f\"\u64a4\u5355 {order&#91;'order_id']}\uff0c\u539f\u4ef7 {order&#91;'price']}\")\n                pending = &#91;]\n\n    if not pending and not khHas(data, code):\n        signals.append({\n            \"code\": code, \"action\": \"buy\", \"volume\": 500,\n            \"price\": round_price(price * 0.99, data=data, stock_code=code),\n            \"order_type\": \"limit\", \"reason\": \"\u4f4e 1% \u6302\u5355\",\n        })\n\n    summary = khGetPendingSummary(data)\n    if summary.get('total_count', 0) &gt; 0:\n        logging.info(f\"\u5f53\u524d\u6302\u5355 {summary&#91;'total_count']} \u7b14\")\n    return signals<\/code><\/pre>\n<div class=\"khq-tn-callout tip\"><div class=\"lbl\">\u63d0\u793a<\/div><p>\u64a4\u5355\u662f\u5728\u51fd\u6570\u8c03\u7528\u65f6\u7acb\u523b\u751f\u6548\u7684\uff0c\u4e0d\u9700\u8981\u8fd4\u56de\u4ec0\u4e48\u4fe1\u53f7\u3002\u6302\u5355\u662f\u5426\u6210\u4ea4\uff0c\u662f\u5728\u4e4b\u540e\u7684 K \u7ebf\u4e0a\u64ae\u5408\u7684\uff0c\u6240\u4ee5\u540c\u4e00\u6839 K \u7ebf\u91cc\u521a\u8fd4\u56de\u7684\u9650\u4ef7\u5355\uff0c\u8981\u5230\u4e0b\u4e00\u6b21\u56de\u8c03\u624d\u80fd\u5728 <code>khGetPendingOrders<\/code> \u91cc\u770b\u5230\uff08\u82e5\u5f53\u6839\u5df2\u6210\u4ea4\u5219\u770b\u4e0d\u5230\uff09\u3002<\/p><\/div>\n\n<h2>7 \u65e5\u5185\u591a\u6b21\u89e6\u53d1\uff1akhRequestNextDailyTrigger<\/h2>\n<pre><code>khRequestNextDailyTrigger() -&gt; bool<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u65e5\u7ebf\u7b56\u7565\u9ed8\u8ba4\u6bcf\u5929\u53ea\u8c03\u7528\u4e00\u6b21 <code>khHandlebar<\/code>\u3002\u5728 <code>khHandlebar<\/code> \u91cc\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u7533\u8bf7\u5f53\u5929<strong>\u5904\u7406\u5b8c\u672c\u8f6e\u4fe1\u53f7\u540e<\/strong>\u518d\u8c03\u7528\u4e00\u6b21\u3002\u5178\u578b\u7528\u6cd5\u662f\u5148\u5356\u540e\u4e70\uff1a\u7b2c\u4e00\u8f6e\u5356\u51fa\uff0c\u5356\u51fa\u6210\u4ea4\u540e\u8d44\u91d1\u56de\u5230\u8d26\u6237\uff0c\u7b2c\u4e8c\u8f6e\u518d\u7528\u8fd9\u7b14\u8d44\u91d1\u4e70\u5165\u3002<\/p>\n<p>\u524d\u63d0\uff1a\u89e6\u53d1\u65b9\u5f0f\u662f\u65e5\u7ebf\uff08<code>1d<\/code>\uff09\uff0c\u5e76\u5728\u754c\u9762\u7684\u300c\u65e5\u5185\u89e6\u53d1\u4e0a\u9650\u300d\uff08\u914d\u7f6e\u91cc\u7684 <code>daily_trigger_cap<\/code>\uff09\u8bbe\u4e3a\u5927\u4e8e 1 \u7684\u6570\u3002\u4e0a\u9650\u662f\u6bcf\u5929\u6700\u591a\u8c03\u7528 <code>khHandlebar<\/code> \u7684\u603b\u6b21\u6570\uff0c\u5305\u542b\u7b2c\u4e00\u6b21\u3002<\/p>\n<p>\u8fd4\u56de <code>True<\/code> \u8868\u793a\u7533\u8bf7\u5df2\u767b\u8bb0\u3002\u4e0b\u9762\u51e0\u79cd\u60c5\u51b5\u8fd4\u56de <code>False<\/code>\uff1a<\/p>\n<ul>\n<li>\u89e6\u53d1\u65b9\u5f0f\u4e0d\u662f\u65e5\u7ebf\uff08\u5206\u949f\u3001tick\u3001\u81ea\u5b9a\u4e49\u65f6\u95f4\u70b9\u90fd\u4e0d\u652f\u6301\uff09\uff1b<\/li>\n<li>\u5f53\u5929\u5df2\u8c03\u7528\u6b21\u6570\u8fbe\u5230\u300c\u65e5\u5185\u89e6\u53d1\u4e0a\u9650\u300d\uff1b<\/li>\n<li>\u4e0d\u5728\u7b56\u7565\u56de\u8c03\u6267\u884c\u8fc7\u7a0b\u4e2d\u8c03\u7528\uff08\u6bd4\u5982\u5728\u6a21\u5757\u9876\u5c42\u8c03\u7528\uff09\uff1b<\/li>\n<li>\u6846\u67b6\u5185\u90e8\u51fa\u9519\u3002<\/li>\n<\/ul>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p>\u53ea\u6709\u5728 <code>khHandlebar<\/code> \u91cc\u7533\u8bf7\u624d\u4f1a\u751f\u6548\u3002\u5728 <code>init<\/code>\u3001<code>khPreMarket<\/code>\u3001<code>khPostMarket<\/code> \u91cc\u8c03\u7528\uff0c\u5373\u4f7f\u8fd4\u56de <code>True<\/code>\uff0c\u4e5f\u4f1a\u5728\u4e0b\u4e00\u6b21\u8fdb\u5165 <code>khHandlebar<\/code> \u524d\u88ab\u6e05\u6389\uff0c\u4e0d\u4f1a\u591a\u89e6\u53d1\u3002<\/p><\/div>\n<p>\u518d\u6b21\u89e6\u53d1\u65f6\uff0c\u884c\u60c5\u6570\u636e\u548c\u4e0a\u4e00\u8f6e\u5b8c\u5168\u4e00\u6837\uff08\u540c\u4e00\u5929\u3001\u540c\u4e00\u6839\u65e5\u7ebf\uff09\uff0c\u4f46 <code>__account__<\/code>\u3001<code>__positions__<\/code> \u5df2\u7ecf\u53cd\u6620\u4e86\u4e0a\u4e00\u8f6e\u7684\u6210\u4ea4\u7ed3\u679c\u3002\u7b56\u7565\u8981\u81ea\u5df1\u7528\u5168\u5c40\u53d8\u91cf\u8bb0\u4f4f\u300c\u4eca\u5929\u662f\u7b2c\u51e0\u8f6e\u300d\uff1a<\/p>\n<pre><code>STATE = {\"date\": None, \"round\": 0}\n\ndef init(stock_codes, init_data):\n    STATE&#91;\"date\"], STATE&#91;\"round\"] = None, 0\n\ndef khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    signals = &#91;]\n    today = khGet(data, 'date')\n    if STATE&#91;\"date\"] != today:\n        STATE&#91;\"date\"], STATE&#91;\"round\"] = today, 1\n    else:\n        STATE&#91;\"round\"] += 1\n\n    if STATE&#91;\"round\"] == 1:\n        # \u7b2c 1 \u8f6e\uff1a\u53ea\u505a\u5356\u51fa\n        for code in khGet(data, 'positions'):\n            price = khPrice(data, code)\n            if price &gt; 0 and should_sell(code, data):   # should_sell \u4e3a\u4f60\u81ea\u5df1\u7684\u5356\u51fa\u6761\u4ef6\n                signals.extend(generate_signal(data, code, price, 1.0, 'sell', \"\u7b2c 1 \u8f6e\u5356\u51fa\"))\n        if khRequestNextDailyTrigger():\n            logging.info(f\"{today} \u5df2\u7533\u8bf7\u7b2c 2 \u8f6e\")\n    elif STATE&#91;\"round\"] == 2:\n        # \u7b2c 2 \u8f6e\uff1a\u7528\u91ca\u653e\u51fa\u6765\u7684\u8d44\u91d1\u4e70\u5165\n        logging.info(f\"{today} \u7b2c 2 \u8f6e\uff0c\u53ef\u7528\u8d44\u91d1 {khGet(data, 'cash'):.2f}\")\n        # \u2026\u2026\u4e70\u5165\u903b\u8f91\u2026\u2026\n    return signals<\/code><\/pre>\n<p>\u5b8c\u6574\u53ef\u8fd0\u884c\u7684\u6848\u4f8b\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/examples\/#multi\">3.5 V3 \u5185\u7f6e\u7b56\u7565\u4e00\u89c8 \u00b7 \u65e5\u5185\u591a\u6b21\u89e6\u53d1<\/a>\u3002<\/p>\n\n<h2>8 \u81ea\u5b9a\u4e49\u6307\u6807\uff1akhDuckWrite\u3001khDuckDB\u3001khAddExtraFields\u3001khIndex<\/h2>\n<p>\u56de\u6d4b\u51e0\u767e\u53ea\u80a1\u7968\u3001\u597d\u51e0\u5e74\u65f6\uff0c\u5728 <code>khHandlebar<\/code> \u91cc\u6bcf\u6839 K \u7ebf\u90fd\u53d6\u4e00\u904d\u5386\u53f2\u6570\u636e\u518d\u7b97\u6307\u6807\uff0c\u4f1a\u5f88\u6162\u3002\u66f4\u5feb\u7684\u505a\u6cd5\u662f\u628a\u6307\u6807\u63d0\u524d\u7b97\u597d\u3001\u5199\u8fdb\u672c\u5730\u884c\u60c5\u5e93\uff0c\u56de\u6d4b\u65f6\u6846\u67b6\u548c\u884c\u60c5\u4e00\u8d77\u4e00\u6b21\u6027\u52a0\u8f7d\u8fdb\u5185\u5b58\uff0c\u7b56\u7565\u91cc\u76f4\u63a5\u8bfb\u3002\u5206\u4e09\u6b65\uff1a<\/p>\n<ol>\n<li><strong>\u9884\u5148\u5199\u5e93<\/strong>\uff1a\u5728\u4e00\u4e2a\u72ec\u7acb\u811a\u672c\u91cc\uff08\u4e0d\u662f\u7b56\u7565\u6587\u4ef6\uff09\uff0c\u7528 <code>khDuckDB<\/code> \u8bfb\u884c\u60c5\u3001\u7b97\u6307\u6807\uff0c\u518d\u7528 <code>khDuckWrite<\/code> \u5199\u56de\u5e93\u91cc\u3002<\/li>\n<li><strong>\u6ce8\u518c\u5b57\u6bb5<\/strong>\uff1a\u7b56\u7565\u7684 <code>init<\/code> \u91cc\u7528 <code>khAddExtraFields<\/code> \u544a\u8bc9\u6846\u67b6\u8981\u591a\u52a0\u8f7d\u54ea\u51e0\u5217\u3002<\/li>\n<li><strong>\u8bfb\u53d6<\/strong>\uff1a<code>khHandlebar<\/code> \u91cc\u7528 <code>khIndex<\/code> \u8bfb\u5f53\u524d K \u7ebf\u4e0a\u7684\u6307\u6807\u503c\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u56db\u4e2a\u51fd\u6570 V2 \u548c V3 \u90fd\u6709\u3002<\/p>\n\n<h3>8.1 khDuckDB\uff1a\u76f4\u63a5\u8bfb\u672c\u5730\u884c\u60c5\u5e93<\/h3>\n<pre><code>khDuckDB(stock_list, period, fields=None, start_time=None, end_time=None,\n         dividend_type='none', duckdb_path=None, return_format='dict')<\/code><\/pre>\n<ul>\n<li><code>period<\/code>\uff1a<code>'1d'<\/code>\u3001<code>'1m'<\/code>\u3001<code>'5m'<\/code>\u3001<code>'tick'<\/code>\u3002<\/li>\n<li><code>fields<\/code>\uff1a\u8981\u7684\u5217\uff0c<code>None<\/code> \u8fd4\u56de\u5168\u90e8\u5217\uff08\u5305\u62ec\u4f60\u5199\u8fdb\u53bb\u7684\u81ea\u5b9a\u4e49\u5217\uff09\u3002<\/li>\n<li><code>start_time<\/code> \/ <code>end_time<\/code>\uff1a<code>'YYYYMMDD'<\/code> \u6216 <code>'YYYYMMDDHHmmss'<\/code>\u3002\u53ea\u5199\u65e5\u671f\u7684 <code>end_time<\/code> \u4f1a\u53d6\u5230\u5f53\u5929 23:59:59\uff0c<strong>\u5305\u542b\u5f53\u5929<\/strong>\u3002<\/li>\n<li><code>dividend_type<\/code>\uff1a<code>'none'<\/code>\uff08\u9ed8\u8ba4\uff09\u3001<code>'front'<\/code>\u3001<code>'back'<\/code>\u3001<code>'front_ratio'<\/code>\u3001<code>'back_ratio'<\/code>\u3002\u6ce8\u610f\u9ed8\u8ba4\u662f\u4e0d\u590d\u6743\uff0c\u548c <code>khHistory<\/code> \u9ed8\u8ba4\u8ddf\u968f\u754c\u9762\u4e0d\u540c\u3002<\/li>\n<li><code>duckdb_path<\/code>\uff1a\u6570\u636e\u76ee\u5f55\uff0c\u4e0d\u4f20\u65f6\u7528\u5f53\u524d\u56de\u6d4b\u7684\u6570\u636e\u76ee\u5f55\uff0c\u518d\u9000\u5230\u8f6f\u4ef6\u8bbe\u7f6e\u91cc\u7684\u6570\u636e\u76ee\u5f55\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a<code>{\u80a1\u7968\u4ee3\u7801: DataFrame}<\/code>\uff0c\u8bfb\u4e0d\u5230\u7684\u80a1\u7968\u5bf9\u5e94\u7a7a DataFrame\u3002<\/li>\n<\/ul>\n<pre><code>res = khDuckDB(&#91;'000001.SZ'], '1d', fields=&#91;'time', 'close'],\n               start_time='20230101', end_time='20231231')\ndf = res&#91;'000001.SZ']<\/code><\/pre>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p><code>khDuckDB<\/code> \u4e0d\u8ba4\u56de\u6d4b\u65f6\u949f\uff0c\u4f60\u7ed9\u4ec0\u4e48\u65f6\u95f4\u8303\u56f4\u5c31\u8fd4\u56de\u4ec0\u4e48\uff0c<code>end_time<\/code> \u8fd8\u5305\u542b\u5f53\u5929\u3002\u5728\u7b56\u7565\u91cc\u7528\u5b83\u53d6\u6570\uff0c\u5f88\u5bb9\u6613\u8bfb\u5230\u5f53\u524d K \u7ebf\u4e4b\u540e\u7684\u6570\u636e\u3002\u7b56\u7565\u91cc\u53d6\u5386\u53f2 K \u7ebf\u8bf7\u7528 <code>khHistory<\/code>\uff1b<code>khDuckDB<\/code> \u4e3b\u8981\u7528\u5728\u5199\u5e93\u811a\u672c\u91cc\u3002<\/p><\/div>\n\n<h3>8.2 khDuckWrite\uff1a\u628a\u6307\u6807\u5199\u8fdb\u5e93<\/h3>\n<pre><code>khDuckWrite(stock_list, period, data, fields=None, time_col=\"time\",\n            duckdb_path=None, field_types=None, insert_missing=False,\n            update_time=True, allow_builtin_fields=False)<\/code><\/pre>\n<ul>\n<li><code>data<\/code>\uff1a<code>{\u80a1\u7968\u4ee3\u7801: DataFrame}<\/code>\uff0c\u6216\u5355\u53ea\u80a1\u7968\u65f6\u76f4\u63a5\u4f20 DataFrame\u3002DataFrame \u5fc5\u987b\u6709\u65f6\u95f4\u5217\uff08\u9ed8\u8ba4\u53eb <code>time<\/code>\uff09\u3002<\/li>\n<li><code>fields<\/code>\uff1a\u8981\u5199\u5165\u7684\u5217\u540d\uff0c<code>None<\/code> \u65f6\u5199\u5165\u9664\u65f6\u95f4\u5217\u5916\u7684\u5168\u90e8\u5217\u3002\u5e93\u91cc\u6ca1\u6709\u7684\u5217\u4f1a\u81ea\u52a8\u65b0\u589e\u3002<\/li>\n<li>\u65e5\u7ebf\u6309\u4ea4\u6613\u65e5\u5339\u914d\uff0c\u65f6\u95f4\u5217\u5199\u7eaf\u65e5\u671f\u5373\u53ef\u3002<\/li>\n<li><code>insert_missing<\/code>\uff1a\u9ed8\u8ba4 <code>False<\/code>\uff0c\u53ea\u66f4\u65b0\u5e93\u91cc\u5df2\u6709\u65f6\u95f4\u7684\u884c\uff1b\u65f6\u95f4\u5bf9\u4e0d\u4e0a\u7684\u884c\u4e0d\u4f1a\u5199\u5165\uff0c\u5e76\u5728\u65e5\u5fd7\u91cc\u63d0\u793a\u591a\u5c11\u884c\u6ca1\u5bf9\u4e0a\u3002<\/li>\n<li><code>allow_builtin_fields<\/code>\uff1a\u9ed8\u8ba4 <code>False<\/code>\uff0c\u4e0d\u5141\u8bb8\u6539\u5199 <code>open<\/code>\u3001<code>close<\/code>\u3001<code>volume<\/code> \u7b49\u5185\u7f6e\u884c\u60c5\u5217\u548c\u590d\u6743\u5217\u3002\u81ea\u5b9a\u4e49\u6307\u6807\u8bf7\u6362\u4e2a\u540d\u5b57\u3002<\/li>\n<li>\u5217\u540d\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c<code>dif<\/code> \u548c <code>DIF<\/code> \u662f\u540c\u4e00\u5217\u3002<\/li>\n<li>\u8fd4\u56de\u503c\uff1a<code>{\u80a1\u7968\u4ee3\u7801: {'updated': \u66f4\u65b0\u884c\u6570, 'inserted': \u63d2\u5165\u884c\u6570, 'rows': \u8f93\u5165\u884c\u6570, 'fields': \u5199\u5165\u7684\u5217}}<\/code>\u3002<\/li>\n<\/ul>\n<pre><code># \u72ec\u7acb\u811a\u672c\uff1a\u7ed9\u80a1\u7968\u6c60\u7b97 MACD \u5e76\u5199\u5e93\uff08\u8fd0\u884c\u524d\u5173\u6389\u5360\u7528\u6570\u636e\u76ee\u5f55\u7684\u56de\u6d4b\u548c\u4e0b\u8f7d\u4efb\u52a1\uff09\nfrom khQTTools import khDuckDB, khDuckWrite\nimport pandas as pd\n\nfor code in &#91;'000001.SZ', '600000.SH']:\n    df = khDuckDB(&#91;code], '1d', fields=&#91;'time', 'close'])&#91;code]\n    if df.empty:\n        continue\n    fast = df&#91;'close'].ewm(span=12, adjust=False).mean()\n    slow = df&#91;'close'].ewm(span=26, adjust=False).mean()\n    df&#91;'DIF'] = fast - slow\n    df&#91;'DEA'] = df&#91;'DIF'].ewm(span=9, adjust=False).mean()\n    df&#91;'MACD'] = (df&#91;'DIF'] - df&#91;'DEA']) * 2\n    print(khDuckWrite(&#91;code], '1d', df&#91;&#91;'time', 'DIF', 'DEA', 'MACD']],\n                      fields=&#91;'DIF', 'DEA', 'MACD']))<\/code><\/pre>\n\n<h3>8.3 khAddExtraFields\uff1a\u5728 init \u91cc\u6ce8\u518c\u5b57\u6bb5<\/h3>\n<pre><code>khAddExtraFields(context: Dict, fields: List&#91;str]) -&gt; None<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u544a\u8bc9\u6846\u67b6\u56de\u6d4b\u52a0\u8f7d\u884c\u60c5\u65f6\uff0c\u628a\u8fd9\u4e9b\u5217\u4e5f\u4e00\u8d77\u8bfb\u8fdb\u6765\u3002<code>context<\/code> \u4f20 <code>init<\/code> \u6536\u5230\u7684 <code>data<\/code>\u3002\u53ea\u80fd\u5728 <code>init<\/code> \u91cc\u8c03\u7528\uff0c\u5230\u4e86 <code>khHandlebar<\/code> \u518d\u8c03\u7528\u5c31\u665a\u4e86\u3002<\/p>\n<p>\u5b83<strong>\u4e0d\u4f1a\u8ba1\u7b97\u6307\u6807<\/strong>\uff0c\u53ea\u8d1f\u8d23\u4ece\u5e93\u91cc\u8bfb\u51fa\u5df2\u6709\u7684\u5217\u3002\u5217\u540d\u8981\u548c\u5199\u5e93\u65f6\u5b8c\u5168\u4e00\u81f4\u3002\u5e93\u91cc\u6ca1\u6709\u8fd9\u4e00\u5217\u7684\u80a1\u7968\uff0c\u6574\u53ea\u80a1\u7968\u7684\u884c\u60c5\u90fd\u8bfb\u4e0d\u51fa\u6765\uff08\u65e5\u5fd7\u91cc\u62a5\u4e00\u6761\u9519\u8bef\u300c\u67e5\u8be2\u5931\u8d25 \u2026 Referenced column &#8220;DIF&#8221; not found\u300d\uff09\uff1a\u8fd9\u53ea\u80a1\u7968\u5728\u56de\u6d4b\u91cc\u7b49\u4e8e\u6ca1\u6709\u6570\u636e\uff0c<code>khIndex<\/code> \u8bfb\u5230 0\uff0c\u4e5f\u53d6\u4e0d\u5230\u4ef7\u683c\u3001\u4e0d\u4f1a\u4ea4\u6613\uff1b\u5176\u4ed6\u80a1\u7968\u7167\u5e38\u56de\u6d4b\u3002\u6240\u6709\u80a1\u7968\u90fd\u7f3a\u8fd9\u4e00\u5217\u65f6\uff0c\u56de\u6d4b\u62a5\u300c\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u6709\u6548\u7684\u65f6\u95f4\u70b9\u300d\u5e76\u505c\u6b62\u3002<\/p>\n\n<h3>8.4 khIndex\uff1a\u8bfb\u5f53\u524d K \u7ebf\u4e0a\u7684\u6307\u6807\u503c<\/h3>\n<pre><code>khIndex(data: Dict, stock_code: str, field: str) -&gt; float<\/code><\/pre>\n<p>\u7528\u9014\uff1a\u8bfb\u5f53\u524d K \u7ebf\u4e0a\u67d0\u4e2a\u81ea\u5b9a\u4e49\u6307\u6807\u7684\u503c\u3002\u5e95\u5c42\u548c <code>khPrice<\/code> \u76f8\u540c\uff0c\u53ea\u662f\u8bed\u4e49\u4e0a\u533a\u5206\u300c\u884c\u60c5\u300d\u548c\u300c\u6307\u6807\u300d\u3002<code>field<\/code> \u6ca1\u6709\u9ed8\u8ba4\u503c\uff0c\u5fc5\u987b\u5199\u3002\u53d6\u4e0d\u5230\u65f6\u8fd4\u56de <code>0.0<\/code>\u3002<\/p>\n<pre><code>PREV = {}\n\ndef init(stock_codes, init_data):\n    khAddExtraFields(init_data, &#91;\"DIF\", \"DEA\", \"MACD\"])\n\ndef khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    signals = &#91;]\n    for code in khGet(data, 'stocks'):\n        price = khPrice(data, code)\n        if price &lt;= 0:\n            continue\n        dif, dea = khIndex(data, code, \"DIF\"), khIndex(data, code, \"DEA\")\n        last = PREV.get(code)\n        PREV&#91;code] = (dif, dea)\n        if last is None:\n            continue\n        if last&#91;0] &lt; last&#91;1] and dif &gt; dea and not khHas(data, code):\n            signals.extend(generate_signal(data, code, price, 0.1, 'buy', \"DIF \u4e0a\u7a7f DEA\"))\n    return signals<\/code><\/pre>\n<div class=\"khq-tn-callout warn\"><div class=\"lbl\">\u6ce8\u610f<\/div><p>\u5199\u8fdb\u54ea\u4e00\u5929\u7684\u503c\uff0c<code>khIndex<\/code> \u5c31\u5728\u54ea\u4e00\u5929\u8bfb\u5230\u3002\u4e0a\u9762\u7684 MACD \u7528\u4e86\u5f53\u5929\u6536\u76d8\u4ef7\uff0c\u6240\u4ee5\u5f53\u5929\u8bfb\u5230\u7684\u6307\u6807\u5df2\u7ecf\u5305\u542b\u5f53\u5929\u6536\u76d8\u3002\u8fd9\u548c\u7528 <code>khPrice<\/code> \u53d6\u5f53\u5929\u6536\u76d8\u4ef7\u7684\u65f6\u70b9\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u5982\u679c\u4f60\u7684\u6307\u6807\u7528\u5230\u4e86\u5f53\u5929\u4e4b\u540e\u7684\u6570\u636e\uff08\u6bd4\u5982\u300c\u672a\u6765 5 \u65e5\u6536\u76ca\u300d\uff09\uff0c\u5199\u8fdb\u53bb\u518d\u8bfb\u5c31\u6210\u4e86\u672a\u6765\u51fd\u6570\u3002<\/p><\/div>\n<p>\u5199\u5e93\u811a\u672c\u3001\u6ce8\u518c\u548c\u8bfb\u53d6\u7684\u5b8c\u6574\u6848\u4f8b\uff0c\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/examples\/#indicators\">3.5 V3 \u5185\u7f6e\u7b56\u7565\u4e00\u89c8 \u00b7 \u81ea\u5b9a\u4e49\u6307\u6807<\/a>\u3002<\/p>\n\n<h2>9 \u65e5\u5fd7<\/h2>\n<p>\u7b56\u7565\u91cc\u76f4\u63a5\u7528 Python \u6807\u51c6\u5e93 <code>logging<\/code>\uff08\u5df2\u968f <code>khQuantImport<\/code> \u5bfc\u5165\uff09\uff0c\u8f93\u51fa\u4f1a\u663e\u793a\u5728\u754c\u9762\u7684\u65e5\u5fd7\u533a\uff0c\u4e5f\u4f1a\u5199\u8fdb\u672c\u6b21\u56de\u6d4b\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<pre><code>logging.info(\"\u666e\u901a\u4fe1\u606f\")\nlogging.warning(\"\u8b66\u544a\uff1a\u6570\u636e\u4e0d\u8db3\uff0c\u8df3\u8fc7\")\nlogging.error(\"\u51fa\u9519\u4e86\")\nlogging.info(f\"&#91;TRADE] {code} \u6ee1\u8db3\u4e70\u5165\u6761\u4ef6\uff0c\u4ef7\u683c {price}\")   # \u5e26 &#91;TRADE] \u7684\u6d88\u606f\u5728\u754c\u9762\u91cc\u9ad8\u4eae<\/code><\/pre>\n<p>\u6d88\u606f\u91cc\u5e26 <code>&#91;TRADE]<\/code> \u6807\u7b7e\u65f6\uff0c\u754c\u9762\u4f1a\u628a\u5b83\u5f53\u4f5c\u4ea4\u6613\u7c7b\u65e5\u5fd7\u9ad8\u4eae\u663e\u793a\uff08\u6807\u7b7e\u672c\u8eab\u4e0d\u663e\u793a\uff09\uff0c\u9002\u5408\u6807\u51fa\u5173\u952e\u51b3\u7b56\u70b9\u3002\u65e5\u5fd7\u5728\u754c\u9762\u4e0a\u600e\u4e48\u7b5b\u9009\u3001\u5728\u54ea\u91cc\u627e\u65e5\u5fd7\u6587\u4ef6\uff0c\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/chapter8\/\">1.9 \u8282<\/a>\u3002<\/p>\n<p>\u4e24\u70b9\u5efa\u8bae\uff1a\u53ea\u5728\u5173\u952e\u51b3\u7b56\u5904\u5199 info\uff0c\u5faa\u73af\u91cc\u6bcf\u6839 K \u7ebf\u90fd\u6253\u65e5\u5fd7\u4f1a\u62d6\u6162\u56de\u6d4b\uff1b\u7528 <code>try \/ except<\/code> \u5305\u4f4f\u5355\u53ea\u80a1\u7968\u7684\u5904\u7406\uff0c\u5728 <code>except<\/code> \u91cc <code>logging.error<\/code>\uff0c\u4e00\u53ea\u80a1\u7968\u51fa\u9519\u4e0d\u81f3\u4e8e\u8ba9\u6574\u4e2a\u56de\u6d4b\u505c\u4e0b\u3002<\/p>\n\n<h2>10 \u628a\u5de5\u5177\u4e32\u8d77\u6765\uff1a\u4e00\u4e2a\u591a\u80a1\u7968\u6a21\u677f<\/h2>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u6a21\u677f\u628a\u524d\u9762\u51e0\u8282\u6700\u5e38\u7528\u7684\u51fd\u6570\u4e32\u5728\u4e00\u8d77\uff1a\u904d\u5386\u80a1\u7968\u6c60\uff0c\u4e00\u6b21 <code>khHistory<\/code> \u53d6\u591f\u6570\u636e\uff0c\u7528 MyTT \u7b97\u4e24\u6761\u5747\u7ebf\uff0c\u5148\u5904\u7406\u5356\u51fa\u3001\u518d\u5904\u7406\u4e70\u5165\uff0c\u4e70\u5165\u8d44\u91d1\u6309\u80a1\u7968\u6570\u5e73\u5747\u5206\u914d\uff0c\u8fd8\u6302\u7740\u7684\u5355\u5148\u64a4\u6389\u3002\u5b83\u4e0d\u662f\u4e00\u4e2a\u63a8\u8350\u7684\u4ea4\u6613\u7b56\u7565\uff0c\u53ea\u662f\u6f14\u793a\u5404\u4e2a\u5de5\u5177\u5728\u4e00\u4e2a <code>khHandlebar<\/code> \u91cc\u600e\u4e48\u914d\u5408\u3002<\/p>\n<pre><code>from khQuantImport import *\n\nSHORT, LONG = 5, 20          # \u6f14\u793a\u53c2\u6570\n\ndef init(stock_codes, init_data):\n    logging.info(f\"\u80a1\u7968\u6c60 {len(stock_codes)} \u53ea\uff0c\u5747\u7ebf {SHORT}\/{LONG}\")\n\ndef khHandlebar(data: Dict) -&gt; List&#91;Dict]:\n    signals = &#91;]\n    stocks = khGet(data, 'stocks')\n    if not stocks:\n        return signals\n\n    # 1. \u4e00\u6b21\u53d6\u591f\u6240\u6709\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\uff08\u4e0d\u542b\u5f53\u5929\uff09\n    hist = khHistory(stocks, &#91;'close'], LONG + 5, '1d',\n                     current_time=khGet(data, 'date_num'))\n\n    # 2. \u8fd8\u6302\u7740\u6ca1\u6210\u4ea4\u7684\u5355\uff08\u5982 gtd \u5355\u3001\u76d8\u540e\u5355\uff09\u5148\u64a4\u6389\uff0c\u907f\u514d\u91cd\u590d\u4e0b\u5355\n    if khGetPendingSummary(data).get('total_count', 0) &gt; 0:\n        n = khCancelAllOrders(data)\n        logging.info(f\"\u64a4\u9500\u672a\u6210\u4ea4\u6302\u5355 {n} \u7b14\")\n\n    buy_list = &#91;]\n    for code in stocks:\n        try:\n            price = khPrice(data, code)\n            df = hist.get(code)\n            if price &lt;= 0 or df is None or len(df) &lt; LONG + 1:\n                continue\n            closes = df&#91;'close'].values\n            ma_s, ma_l = MA(closes, SHORT), MA(closes, LONG)\n            golden = ma_s&#91;-2] &lt;= ma_l&#91;-2] and ma_s&#91;-1] &gt; ma_l&#91;-1]\n            dead = ma_s&#91;-2] &gt;= ma_l&#91;-2] and ma_s&#91;-1] &lt; ma_l&#91;-1]\n\n            # 3. \u5148\u5356\uff1a\u6b7b\u53c9\u4e14\u6709\u6301\u4ed3\n            if dead and khHas(data, code):\n                signals.extend(generate_signal(data, code, price, 1.0, 'sell',\n                                               f\"MA{SHORT} \u4e0b\u7a7f MA{LONG}\"))\n            # \u8bb0\u4e0b\u8981\u4e70\u7684\uff0c\u7a0d\u540e\u7edf\u4e00\u5206\u914d\u8d44\u91d1\n            elif golden and not khHas(data, code):\n                buy_list.append((code, price))\n        except Exception as e:\n            logging.error(f\"{code} \u5904\u7406\u51fa\u9519: {e}\")\n\n    # 4. \u518d\u4e70\uff1a\u6bcf\u53ea\u80a1\u7968\u7528\u540c\u6837\u6bd4\u4f8b\u7684\u5f53\u524d\u53ef\u7528\u8d44\u91d1\n    if buy_list:\n        ratio = min(1.0 \/ len(buy_list), 0.3)     # \u5355\u53ea\u6700\u591a 30%\n        for code, price in buy_list:\n            sig = generate_signal(data, code, price, ratio, 'buy',\n                                  f\"MA{SHORT} \u4e0a\u7a7f MA{LONG}\")\n            if sig:\n                logging.info(f\"&#91;TRADE] \u4e70\u5165 {code} {sig&#91;0]&#91;'volume']} \u80a1\")\n            signals.extend(sig)\n    return signals<\/code><\/pre>\n<p>\u51e0\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u5199\u6cd5\uff1a\u5386\u53f2\u6570\u636e\u5728\u5faa\u73af\u5916\u4e00\u6b21\u6027\u53d6\uff0c\u907f\u514d\u6bcf\u53ea\u80a1\u7968\u5404\u53d6\u4e00\u6b21\uff1b\u5747\u7ebf\u7528 <code>&#91;-1]<\/code> \u548c <code>&#91;-2]<\/code> \u5224\u65ad\u4ea4\u53c9\uff0c\u4e8c\u8005\u90fd\u6765\u81ea\u622a\u81f3\u6628\u5929\u7684\u6570\u636e\uff1b\u5356\u51fa\u4fe1\u53f7\u548c\u4e70\u5165\u4fe1\u53f7\u5728\u540c\u4e00\u8f6e\u8fd4\u56de\u65f6\uff0c\u5356\u51fa\u7684\u8d44\u91d1\u5728\u8fd9\u4e00\u8f6e\u8fd8\u6ca1\u56de\u5230\u8d26\u6237\uff0c\u6240\u4ee5\u4e70\u5165\u6bd4\u4f8b\u53ea\u6309\u5f53\u524d\u53ef\u7528\u8d44\u91d1\u7b97\u3002\u5982\u679c\u60f3\u8ba9\u5356\u51fa\u7684\u94b1\u5f53\u5929\u5c31\u80fd\u7528\u6765\u4e70\u5165\uff0c\u5c31\u8981\u7528\u7b2c 7 \u8282\u7684\u65e5\u5185\u591a\u6b21\u89e6\u53d1\uff0c\u628a\u4e70\u5165\u653e\u5230\u7b2c\u4e8c\u8f6e\u3002<\/p>\n<p>\u60f3\u628a\u8fd9\u6837\u7684\u601d\u8def\u4ea4\u7ed9 AI \u53bb\u5199\u3001\u53bb\u56de\u6d4b\uff0c\u53ef\u4ee5\u770b <a href=\"https:\/\/khsci.com\/khQuant\/ai-strategy\/\">3.4 \u4f7f\u7528 AI \u7f16\u5199\u548c\u56de\u6d4b\u9a8c\u8bc1\u7b56\u7565<\/a>\uff1b\u60f3\u770b\u66f4\u591a\u73b0\u6210\u5199\u6cd5\uff0c\u53ef\u4ee5\u770b <a href=\"https:\/\/khsci.com\/khQuant\/examples\/\">3.5 V3 \u5185\u7f6e\u7b56\u7565\u4e00\u89c8<\/a>\u3002<\/p>\n\n<h2>11 \u51b7\u95e8\u51fd\u6570\u901f\u67e5\u8868<\/h2>\n<div class=\"khq-tn-table\"><table>\n<thead><tr><th>\u51fd\u6570 \/ \u7c7b<\/th><th>\u7b7e\u540d\u8981\u70b9<\/th><th>\u7528\u9014<\/th><\/tr><\/thead>\n<tbody>\n<tr><td><code>get_trade_days_set<\/code><\/td><td><code>(start_date, end_date) -&gt; set<\/code><\/td><td>\u533a\u95f4\u5185\u4ea4\u6613\u65e5\u96c6\u5408\uff0c\u5143\u7d20\u4e3a <code>\"YYYYMMDD\"<\/code>\u3002<\/td><\/tr>\n<tr><td><code>is_trade_time<\/code><\/td><td><code>() -&gt; bool<\/code><\/td><td>\u7535\u8111\u5f53\u524d\u65f6\u949f\u662f\u5426\u5728\u4ea4\u6613\u65f6\u6bb5\u3002\u56de\u6d4b\u4e2d\u4e0d\u8981\u7528\uff0c\u89c1\u7b2c 2 \u8282\u3002<\/td><\/tr>\n<tr><td><code>is_etf<\/code><\/td><td><code>(stock_code) -&gt; bool<\/code><\/td><td>\u662f\u5426 ETF\uff08\u4e0d\u542b LOF\uff09\u3002<\/td><\/tr>\n<tr><td><code>is_t0_etf<\/code><\/td><td><code>(stock_code) -&gt; bool<\/code><\/td><td>\u662f\u5426\u5728 T+0 ETF \u6e05\u5355\u91cc\u3002\u80a1\u7968\u6c60\u5168\u662f T+0 \u54c1\u79cd\u65f6\u6846\u67b6\u81ea\u52a8\u6309 T+0 \u56de\u6d4b\u3002<\/td><\/tr>\n<tr><td><code>format_price<\/code><\/td><td><code>(price, decimals=None, data=None) -&gt; str<\/code><\/td><td>\u6309\u7cbe\u5ea6\u628a\u4ef7\u683c\u683c\u5f0f\u5316\u6210\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u65e5\u5fd7\u3002<\/td><\/tr>\n<tr><td><code>get_stock_name<\/code><\/td><td><code>(stock_code) -&gt; str<\/code><\/td><td>\u67e5\u80a1\u7968\u540d\u79f0\uff0c\u67e5\u4e0d\u5230\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff0c\u9996\u6b21\u8c03\u7528\u4f1a\u52a0\u8f7d\u540d\u79f0\u7f13\u5b58\u3002<\/td><\/tr>\n<tr><td><code>get_stock_display<\/code><\/td><td><code>(stock_code) -&gt; str<\/code><\/td><td>\u8fd4\u56de <code>\"000001.SZ (\u5e73\u5b89\u94f6\u884c)\"<\/code> \u8fd9\u79cd\u663e\u793a\u7528\u5b57\u7b26\u4e32\u3002<\/td><\/tr>\n<tr><td><code>get_stock_names<\/code><\/td><td><code>(stock_codes, stock_list_file) -&gt; dict<\/code><\/td><td>\u4ece\u6307\u5b9a CSV \u6587\u4ef6\u67e5\u4e00\u6279\u540d\u79f0\u3002\u9700\u8981\u81ea\u5df1\u7ed9\u6587\u4ef6\u8def\u5f84\uff0c\u4e00\u822c\u7528 <code>get_stock_name<\/code> \u66f4\u65b9\u4fbf\u3002<\/td><\/tr>\n<tr><td><code>normalize_stock_code<\/code><\/td><td><code>(stock_code) -&gt; str<\/code><\/td><td><code>\"sh.600000\"<\/code> \u8f6c\u6210 <code>\"600000.SH\"<\/code>\uff1b\u4e0d\u5e26\u540e\u7f00\u7684\u7eaf\u6570\u5b57\u4ee3\u7801\u539f\u6837\u8fd4\u56de\u3002<\/td><\/tr>\n<tr><td><code>denormalize_stock_code<\/code><\/td><td><code>(stock_code, to_format='miniQMT') -&gt; str<\/code><\/td><td>\u53cd\u5411\u8f6c\u6362\uff0c<code>\"600000.SH\"<\/code> \u8f6c\u6210 <code>\"sh.600000\"<\/code>\u3002<\/td><\/tr>\n<tr><td><code>clear_khHistory_cache<\/code><\/td><td><code>() -&gt; None<\/code><\/td><td>\u6e05\u7a7a <code>khHistory<\/code> \u7f13\u5b58\u3002\u8fde\u7eed\u8dd1\u591a\u6b21\u56de\u6d4b\u3001\u6000\u7591\u6570\u636e\u88ab\u7f13\u5b58\u5f71\u54cd\u65f6\uff0c\u5728 <code>init<\/code> \u91cc\u8c03\u7528\u3002<\/td><\/tr>\n<tr><td><code>clear_khDuckDB_cache<\/code><\/td><td><code>() -&gt; None<\/code><\/td><td>\u6e05\u7a7a <code>khDuckDB<\/code> \u7f13\u5b58\u3002<\/td><\/tr>\n<tr><td><code>TimeInfo(data)<\/code><\/td><td>\u5c5e\u6027 <code>.date_str<\/code>\u3001<code>.date_num<\/code>\u3001<code>.time_str<\/code>\u3001<code>.datetime_str<\/code>\u3001<code>.datetime_num<\/code>\u3001<code>.datetime_obj<\/code>\u3001<code>.timestamp<\/code><\/td><td>\u65f6\u95f4\u89e3\u6790\u5668\u3002<code>.datetime_num<\/code> \u662f <code>\"20240603093000\"<\/code> \u8fd9\u79cd\u8fde\u5199\u683c\u5f0f\uff0c<code>khGet<\/code> \u6ca1\u6709\u5bf9\u5e94\u522b\u540d\u3002<\/td><\/tr>\n<tr><td><code>StockDataParser(data)<\/code><\/td><td><code>.get_price(code, field)<\/code>\u3001<code>.get_close<\/code>\u3001<code>.get_open<\/code>\u3001<code>.get_high<\/code>\u3001<code>.get_low<\/code>\u3001<code>.get_volume<\/code><\/td><td>\u884c\u60c5\u89e3\u6790\u5668\uff0c\u6548\u679c\u540c <code>khPrice<\/code>\u3002<\/td><\/tr>\n<tr><td><code>StockPoolParser(data)<\/code><\/td><td><code>.get_all()<\/code>\u3001<code>.size()<\/code>\u3001<code>.contains(code)<\/code>\u3001<code>.first()<\/code><\/td><td>\u80a1\u7968\u6c60\u89e3\u6790\u5668\u3002<\/td><\/tr>\n<tr><td><code>StrategyContext(data)<\/code> \/ <code>parse_context(data)<\/code><\/td><td><code>.time<\/code>\u3001<code>.stocks<\/code>\u3001<code>.positions<\/code>\u3001<code>.pool<\/code>\uff1b<code>.buy_signal(code, ratio=1.0, reason=\"\")<\/code>\u3001<code>.sell_signal(...)<\/code><\/td><td>\u628a\u4e0a\u9762\u56db\u4e2a\u89e3\u6790\u5668\u6253\u5305\u5728\u4e00\u8d77\u3002<code>buy_signal<\/code> \/ <code>sell_signal<\/code> \u7528\u5f53\u524d\u4ef7\u8c03\u7528 <code>generate_signal<\/code>\uff0c\u8fd4\u56de<strong>\u5355\u4e2a\u5b57\u5178<\/strong>\uff0c\u5931\u8d25\u65f6\u8fd4\u56de\u7a7a\u5b57\u5178 <code>{}<\/code>\uff0c\u52a0\u5165\u4fe1\u53f7\u5217\u8868\u524d\u8981\u5224\u65ad\u975e\u7a7a\u3002\u5b83\u4eec\u7684 <code>volume<\/code> \u53c2\u6570\u76ee\u524d\u4e0d\u8d77\u4f5c\u7528\uff0c\u6309\u80a1\u6570\u4e70\u8bf7\u7528 <code>ratio<\/code> \u4f20\u80a1\u6570\u3002<\/td><\/tr>\n<tr><td><code>get_default_risk_params<\/code><\/td><td><code>() -&gt; dict<\/code><\/td><td>\u8fd4\u56de\u4e00\u7ec4\u793a\u4f8b\u98ce\u63a7\u53c2\u6570\uff08\u6700\u5927\u4ed3\u4f4d 1.0\u3001\u5355\u53ea 0.3\u3001\u6b62\u635f 0.1\u3001\u6b62\u76c8 0.2\uff09\uff0c\u6846\u67b6\u4e0d\u4f1a\u81ea\u52a8\u6267\u884c\uff0c\u53ea\u662f\u7ed9\u4f60\u81ea\u5df1\u5199\u98ce\u63a7\u65f6\u53c2\u8003\u3002<\/td><\/tr>\n<tr><td><code>KhQuTools<\/code><\/td><td>\u7c7b<\/td><td>\u65e7\u7248\u5de5\u5177\u7c7b\uff0c\u4fdd\u7559\u517c\u5bb9\uff0c\u65b0\u4ee3\u7801\u76f4\u63a5\u7528\u6a21\u5757\u7ea7\u51fd\u6570\u3002<\/td><\/tr>\n<\/tbody>\n<\/table><\/div>\n\n<h2>\u5e38\u89c1\u95ee\u9898<\/h2>\n<p class=\"q\"><strong>\u95ee\uff1akhPrice \u4e00\u76f4\u8fd4\u56de 0 \u662f\u600e\u4e48\u56de\u4e8b\uff1f<\/strong><br>\n\u7b54\uff1a0 \u8868\u793a\u6ca1\u53d6\u5230\u6570\u636e\u3002\u5e38\u89c1\u539f\u56e0\uff1a\u4ee3\u7801\u6ca1\u5199\u4ea4\u6613\u6240\u540e\u7f00\uff08\u8981\u5199 <code>000001.SZ<\/code>\uff0c\u4e0d\u662f <code>000001<\/code>\uff09\uff1b\u8fd9\u53ea\u80a1\u7968\u4e0d\u5728\u80a1\u7968\u6c60\u91cc\uff0c<code>data<\/code> \u91cc\u6ca1\u6709\u5b83\u7684\u884c\u60c5\uff1b\u5f53\u5929\u505c\u724c\u6216\u6570\u636e\u7f3a\u5931\uff1b\u5b57\u6bb5\u540d\u5199\u9519\u3002\u5148 <code>logging.info(khGet(data, 'stocks'))<\/code> \u786e\u8ba4\u80a1\u7968\u6c60\uff0c\u518d\u68c0\u67e5\u6570\u636e\u662f\u5426\u4e0b\u8f7d\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1akhHistory \u8fd4\u56de\u7a7a\u5b57\u5178\u6216\u8005\u884c\u6570\u4e0d\u591f\uff1f<\/strong><br>\n\u7b54\uff1a\u5148\u770b\u65e5\u5fd7\u91cc\u7684\u9519\u8bef\u6216\u300c\u5386\u53f2\u6570\u636e\u4e0d\u8db3\u300d\u63d0\u793a\u3002\u6700\u5e38\u89c1\u7684\u662f\u672c\u5730\u6ca1\u6709\u8fd9\u6bb5\u65f6\u95f4\u7684\u6570\u636e\uff08\u56de\u6d4b\u5f00\u59cb\u65e5\u671f\u5f80\u524d\u8fd8\u9700\u8981 <code>bar_count<\/code> \u6839\uff0c\u6570\u636e\u8981\u591a\u4e0b\u8f7d\u4e00\u6bb5\uff09\uff0c\u5176\u6b21\u662f <code>fre_step<\/code> \u548c\u4e0b\u8f7d\u7684\u6570\u636e\u5468\u671f\u4e0d\u4e00\u81f4\u3002\u6570\u636e\u4e0b\u8f7d\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/chapter10\/\">1.4 \u6570\u636e\u7ba1\u7406\u6a21\u5757<\/a>\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1a\u540c\u4e00\u6839\u65e5\u7ebf\uff0ckhHistory \u7b97\u51fa\u6765\u7684\u5747\u7ebf\u548c khKline \u7b97\u51fa\u6765\u7684\u4e0d\u4e00\u6837\uff1f<\/strong><br>\n\u7b54\uff1a\u4e24\u8005\u622a\u6b62\u89c4\u5219\u76f8\u53cd\u3002<code>khHistory<\/code> \u4e0d\u542b\u5f53\u5929\uff0c<code>khKline<\/code> \u542b\u5f53\u5929\u3002\u7528 <code>khKline<\/code> \u65f6\u53bb\u6389\u6700\u540e\u4e00\u6839\u518d\u7b97\uff0c\u5c31\u548c <code>khHistory<\/code> \u5bf9\u5f97\u4e0a\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1agenerate_signal \u5356\u51fa\u65f6\u4e3a\u4ec0\u4e48\u5c11\u5356\u4e86\u51e0\u5341\u80a1\uff1f<\/strong><br>\n\u7b54\uff1a\u5356\u51fa\u6570\u91cf\u6309\u300c\u53ef\u5356\u6570\u91cf \u00d7 ratio\u300d\u5411\u4e0b\u53d6\u6574\u5230 100 \u80a1\u3002\u5982\u679c\u6301\u4ed3\u91cc\u6709\u4e0d\u8db3 100 \u80a1\u7684\u96f6\u80a1\uff08\u6bd4\u5982\u9001\u80a1\u540e\uff09\uff0c\u5168\u90e8\u5356\u51fa\u65f6\u8fd9\u90e8\u5206\u4f1a\u88ab\u820d\u6389\u3002\u8981\u6e05\u6389\u96f6\u80a1\uff0c\u53ef\u4ee5\u624b\u5199\u4fe1\u53f7\uff0c\u628a <code>volume<\/code> \u586b\u6210 <code>can_use_volume<\/code>\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1a\u4e3a\u4ec0\u4e48\u4eca\u5929\u4e70\u7684\u80a1\u7968\uff0cgenerate_signal \u5356\u51fa\u8fd4\u56de\u7a7a\u5217\u8868\uff1f<\/strong><br>\n\u7b54\uff1aA \u80a1\u662f T+1\uff0c\u5f53\u5929\u4e70\u5165\u7684\u80a1\u7968\u4e0d\u8ba1\u5165\u53ef\u5356\u6570\u91cf\uff0c<code>generate_signal<\/code> \u5356\u51fa\u770b\u7684\u662f\u53ef\u5356\u6570\u91cf\u3002\u53ea\u6709\u80a1\u7968\u6c60\u5168\u90e8\u662f T+0 \u54c1\u79cd\u65f6\uff0c\u6846\u67b6\u624d\u6309 T+0 \u56de\u6d4b\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1a\u9650\u4ef7\u5355\u4e00\u76f4\u6ca1\u6210\u4ea4\uff0c\u4e5f\u6ca1\u770b\u5230\u62a5\u9519\uff1f<\/strong><br>\n\u7b54\uff1a\u6ca1\u6ee1\u8db3\u6210\u4ea4\u6761\u4ef6\u7684\u9650\u4ef7\u5355\u4f1a\u8fdb\u6302\u5355\u961f\u5217\uff0c\u9ed8\u8ba4\u5f53\u5929\u6536\u76d8\u540e\u8fc7\u671f\uff0c\u4e0d\u7b97\u9519\u8bef\u3002\u7528 <code>khGetPendingOrders(data)<\/code> \u770b\u5b83\u8fd8\u5728\u4e0d\u5728\uff0c\u65e5\u5fd7\u91cc\u4e5f\u4f1a\u6709\u300c\u5df2\u52a0\u5165\u6302\u5355\u961f\u5217\u300d\u300c\u6302\u5355\u8fc7\u671f\u300d\u7684\u8bb0\u5f55\u3002\u53e6\u5916\uff0c\u59d4\u6258\u4ef7\u8d85\u51fa\u5f53\u65e5\u6da8\u8dcc\u505c\u8303\u56f4\u4f1a\u76f4\u63a5\u62d2\u5355\uff0c\u65e5\u5fd7\u91cc\u6709\u62d2\u5355\u539f\u56e0\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1akhRequestNextDailyTrigger \u4e00\u76f4\u8fd4\u56de False\uff1f<\/strong><br>\n\u7b54\uff1a\u4f9d\u6b21\u68c0\u67e5\uff1a\u89e6\u53d1\u65b9\u5f0f\u662f\u4e0d\u662f\u65e5\u7ebf\uff1b\u300c\u65e5\u5185\u89e6\u53d1\u4e0a\u9650\u300d\u662f\u4e0d\u662f\u5927\u4e8e 1\uff1b\u5f53\u5929\u662f\u4e0d\u662f\u5df2\u7ecf\u7528\u6ee1\u6b21\u6570\uff1b\u662f\u4e0d\u662f\u5728 <code>khHandlebar<\/code> \u91cc\u8c03\u7528\u7684\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1akhIndex \u8bfb\u51fa\u6765\u5168\u662f 0\uff1f<\/strong><br>\n\u7b54\uff1a\u591a\u534a\u662f\u5e93\u91cc\u6ca1\u6709\u8fd9\u4e00\u5217\uff08\u8fd9\u65f6\u8fd9\u53ea\u80a1\u7968\u6574\u53ea\u6ca1\u6709\u884c\u60c5\uff0c\u65e5\u5fd7\u91cc\u6709\u300cReferenced column \u2026 not found\u300d\uff09\uff0c\u6216\u8005\u5217\u540d\u548c <code>khAddExtraFields<\/code> \u91cc\u5199\u7684\u4e0d\u4e00\u81f4\uff0c\u6216\u8005 <code>khAddExtraFields<\/code> \u6ca1\u5199\u5728 <code>init<\/code> \u91cc\u3002\u5148\u7528 <code>khDuckDB<\/code> \u67e5\u4e00\u4e0b\u8fd9\u4e00\u5217\u5728\u4e0d\u5728\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1aV2 \u80fd\u7528 khIndex\u3001khAddExtraFields\u3001\u6302\u5355\u7ba1\u7406\u548c\u65e5\u5185\u591a\u6b21\u89e6\u53d1\u5417\uff1f<\/strong><br>\n\u7b54\uff1a\u80fd\u3002V2 \u548c V3 \u7684\u7b56\u7565\u63a5\u53e3\u5b8c\u5168\u76f8\u540c\uff0c\u8fd9\u4e9b\u7528\u6cd5\u4e5f\u76f8\u540c\u3002V3 \u591a\u51fa\u6765\u7684\u662f\u547d\u4ee4\u884c\u3001Web \u5de5\u4f5c\u53f0\u3001\u66f4\u591a\u6570\u636e\u6e90\u8fd9\u4e9b\u8f6f\u4ef6\u529f\u80fd\uff0c\u4e0d\u5728\u7b56\u7565\u63a5\u53e3\u4e0a\u3002<\/p>\n<p class=\"q\"><strong>\u95ee\uff1a\u80fd\u4e0d\u80fd\u8ba9 AI \u5e2e\u6211\u67e5\u8fd9\u4e9b\u51fd\u6570\u600e\u4e48\u7528\uff1f<\/strong><br>\n\u7b54\uff1a\u53ef\u4ee5\u3002khQuant \u7684 Skill \u91cc\u5e26\u6709\u5b8c\u6574\u7684\u63a5\u53e3\u8bf4\u660e\uff0cAI \u5199\u7b56\u7565\u65f6\u4f1a\u6309\u5b83\u6765\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u89c1 <a href=\"https:\/\/khsci.com\/khQuant\/khquant-skill\/\">3.3 khQuant \u7684 Skill \u4ecb\u7ecd<\/a> \u548c <a href=\"https:\/\/khsci.com\/khQuant\/ai-strategy\/\">3.4 \u4f7f\u7528 AI \u7f16\u5199\u548c\u56de\u6d4b\u9a8c\u8bc1\u7b56\u7565<\/a>\u3002V3 \u7528\u6237\u4e5f\u53ef\u4ee5\u5728<a href=\"https:\/\/khsci.com\/khQuant\/cli\/\">\u547d\u4ee4\u884c\u6a21\u5f0f<\/a>\u91cc\u8ba9 AI \u76f4\u63a5\u5199\u7b56\u7565\u3001\u8dd1\u56de\u6d4b\u3002<\/p>\n\n<p>\u672c\u7bc7\u7684\u4ee3\u7801\u7247\u6bb5\u53ea\u4e3a\u6f14\u793a\u51fd\u6570\u5199\u6cd5\uff0c\u5176\u4e2d\u7684\u6807\u7684\u3001\u53c2\u6570\u548c\u6761\u4ef6\u90fd\u4e0d\u6784\u6210\u6295\u8d44\u5efa\u8bae\uff1b\u636e\u6b64\u8dd1\u51fa\u7684\u56de\u6d4b\u7ed3\u679c\u4e0d\u4ee3\u8868\u672a\u6765\u6536\u76ca\u3002<\/p>\n\n<p style=\"color:#888;font-size:.9em;\">\u672c\u7bc7\u57fa\u4e8e V3.4.1.10 \u548c\u5f00\u6e90 V2 \u7f16\u5199\u3002<\/p>\n\n<nav class=\"khq-tn-nav\"><a class=\"prev\" href=\"https:\/\/khsci.com\/khQuant\/chapter12\/\"><span>\u4e0a\u4e00\u7bc7<\/span>3.1 \u6846\u67b6\u4ecb\u7ecd<\/a><a class=\"next\" href=\"https:\/\/khsci.com\/khQuant\/khquant-skill\/\"><span>\u4e0b\u4e00\u7bc7<\/span>3.3 khQuant \u7684 Skill \u4ecb\u7ecd<\/a><\/nav>\n<\/div>\n\n\n","protected":false},"excerpt":{"rendered":"<p>\u9002\u7528\u7248\u672c V2V3 \u5148\u8bfb\u8fd9\u91cc\uff1a\u7c97\u8bfb\u5373\u53ef\uff0c\u7528\u65f6\u518d\u67e5 \u5199\u7b56\u7565\u65f6\uff0c\u5927\u90e8\u5206\u4ee3\u7801\u90fd\u5728\u91cd\u590d\u51e0\u4ef6\u4e8b\uff1a\u53d6\u5f53\u524d\u65f6\u95f4\u548c\u4ef7\u683c\u3001\u67e5\u6301\u4ed3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"template\/caidan.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-566","page","type-page","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/pages\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":7,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/pages\/566\/revisions"}],"predecessor-version":[{"id":1012,"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/pages\/566\/revisions\/1012"}],"wp:attachment":[{"href":"https:\/\/khsci.com\/khQuant\/wp-json\/wp\/v2\/media?parent=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}