{"id":10374,"date":"2025-01-09T15:39:09","date_gmt":"2025-01-09T07:39:09","guid":{"rendered":"https:\/\/blog.alltick.co\/?p=10374"},"modified":"2025-04-23T16:25:48","modified_gmt":"2025-04-23T08:25:48","slug":"momentum-based-quant-strategy","status":"publish","type":"post","link":"https:\/\/blog.alltick.co\/zh-CN\/momentum-based-quant-strategy\/","title":{"rendered":"\u57fa\u4e8e\u52a8\u91cf\u6548\u5e94\u7684\u91cf\u5316\u7b56\u7565"},"content":{"rendered":"\n<p>\u52a8\u91cf\u6548\u5e94\u662f\u91d1\u878d\u5e02\u573a\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u73b0\u8c61\uff0c\u6307\u7684\u662f\u8fc7\u53bb\u8868\u73b0\u8f83\u597d\u7684\u8d44\u4ea7\u5728\u672a\u6765\u4e00\u6bb5\u65f6\u95f4\u5185\u7ee7\u7eed\u8868\u73b0\u826f\u597d\u7684\u8d8b\u52bf\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u79cd\u57fa\u4e8e\u52a8\u91cf\u6548\u5e94\u7684\u91cf\u5316\u7b56\u7565\uff0c\u5e76\u4f7f\u7528Python\u4ee3\u7801\u5c55\u793a\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b56\u7565\u903b\u8f91<\/strong><\/h3>\n\n\n\n<p>\u8be5\u7b56\u7565\u7684\u6838\u5fc3\u903b\u8f91\u662f\uff1a<strong>\u9009\u53d6\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4\u5185\u6da8\u5e45\u6700\u5927\u7684N\u53ea\u80a1\u7968\uff0c\u6301\u6709\u5b83\u4eec\u5728\u672a\u6765\u4e00\u6bb5\u65f6\u95f4\u5185\uff0c\u671f\u671b\u5b83\u4eec\u80fd\u591f\u7ee7\u7eed\u4fdd\u6301\u4e0a\u6da8\u8d8b\u52bf\u3002<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6570\u636e\u51c6\u5907<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u4ece<strong><a href=\"https:\/\/alltick.co\/\" target=\"_blank\" rel=\"noopener\">AllTick<\/a><\/strong>\u7684\u63a5\u53e3\u83b7\u53d6\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import time\nimport requests\nimport pandas as pd\n\n# \u5b9a\u4e49\u83b7\u53d6\u5386\u53f2K\u7ebf\u6570\u636e\u7684\u51fd\u6570\ndef get_historical_data(symbol, kline_type=1, kline_timestamp_end=0, query_kline_num=100, adjust_type=0):\n    url = 'http:\/\/quote.alltick.io\/quote-stock-b-api\/kline'\n    token = 'e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806'\n    \n    # \u6784\u9020\u67e5\u8be2\u53c2\u6570\n    query = {\n        \"trace\": \"python_http_test1\",\n        \"data\": {\n            \"code\": symbol,\n            \"kline_type\": kline_type,  # K\u7ebf\u7c7b\u578b\uff1a1\u4e3a\u65e5\u7ebf\n            \"kline_timestamp_end\": kline_timestamp_end,  # \u7ed3\u675f\u65f6\u95f4\u6233\uff0c0\u8868\u793a\u6700\u65b0\u65f6\u95f4\n            \"query_kline_num\": query_kline_num,  # \u83b7\u53d6\u7684K\u7ebf\u6570\u91cf\n            \"adjust_type\": adjust_type  # \u590d\u6743\u7c7b\u578b\uff1a0\u4e3a\u4e0d\u590d\u6743\n        }\n    }\n    \n    # \u53d1\u9001\u8bf7\u6c42\n    response = requests.get(\n        url=url,\n        params={\"token\": token, \"query\": json.dumps(query)},\n        headers={'Content-Type': 'application\/json'}\n    )\n    \n    # \u89e3\u6790\u8fd4\u56de\u7684\u6570\u636e\n    if response.status_code == 200:\n        data = response.json()\n        if data.get(\"code\") == 0:  # \u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6210\u529f\n            kline_data = data[\"data\"][\"kline\"]\n            df = pd.DataFrame(kline_data, columns=[\"timestamp\", \"open\", \"high\", \"low\", \"close\", \"volume\"])\n            df[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"], unit=\"s\")  # \u8f6c\u6362\u65f6\u95f4\u6233\u4e3a\u65e5\u671f\n            df.set_index(\"timestamp\", inplace=True)\n            return df\n        else:\n            print(f\"Error: {data.get('message')}\")\n            return None\n    else:\n        print(f\"Request failed with status code: {response.status_code}\")\n        return None\n\n# \u83b7\u53d6\u591a\u53ea\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\nsymbols = [\"700.HK\", \"UNH.US\", \"AAPL.US\"]  # \u793a\u4f8b\u80a1\u7968\u4ee3\u7801\nstart_date = \"2022-01-01\"\nend_date = \"2023-01-01\"\n\n# \u83b7\u53d6\u5386\u53f2\u6570\u636e\u5e76\u8ba1\u7b97\u6536\u76ca\u7387\ndata = {}\nfor symbol in symbols:\n    df = get_historical_data(symbol, query_kline_num=252)  # \u83b7\u53d6252\u4e2a\u4ea4\u6613\u65e5\u7684\u6570\u636e\n    if df is not None:\n        df['returns'] = df['close'].pct_change()  # \u8ba1\u7b97\u6bcf\u65e5\u6536\u76ca\u7387\n        data[symbol] = df\n\n# \u5c06\u6240\u6709\u80a1\u7968\u6570\u636e\u5408\u5e76\u5230\u4e00\u4e2aDataFrame\u4e2d\nall_data = pd.concat(data.values(), keys=data.keys(), names=['symbol'])<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u52a8\u91cf\u8ba1\u7b97<\/strong><\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u6bcf\u53ea\u80a1\u7968\u5728\u8fc7\u53bb\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u7d2f\u8ba1\u6536\u76ca\u7387\uff0c\u4f5c\u4e3a\u5176\u52a8\u91cf\u6307\u6807\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lookback_period = 20  # \u52a8\u91cf\u8ba1\u7b97\u5468\u671f\nall_data['momentum'] = all_data.groupby('symbol')['returns'].rolling(window=lookback_period).apply(lambda x: (x + 1).prod() - 1).reset_index(level=0, drop=True)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b56\u7565\u6784\u5efa<\/strong><\/h3>\n\n\n\n<p>\u5728\u6bcf\u4e2a\u8c03\u4ed3\u65e5\uff0c\u6211\u4eec\u9009\u53d6\u52a8\u91cf\u6700\u5927\u7684N\u53ea\u80a1\u7968\uff0c\u7b49\u6743\u91cd\u6301\u6709\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np\n\nN = 5  # \u6301\u6709\u80a1\u7968\u6570\u91cf\nrebalance_freq = 'M'  # \u6bcf\u6708\u8c03\u4ed3\u4e00\u6b21\n\n# \u8ba1\u7b97\u6bcf\u4e2a\u8c03\u4ed3\u65e5\u7684\u52a8\u91cf\u6392\u540d\nall_data['rank'] = all_data.groupby('date')['momentum'].rank(ascending=False)\n\n# \u751f\u6210\u4ea4\u6613\u4fe1\u53f7\nall_data['signal'] = np.where(all_data['rank'] &lt;= N, 1, 0)\n\n# \u8ba1\u7b97\u7b56\u7565\u6536\u76ca\nall_data['strategy_return'] = all_data.groupby('symbol')['signal'].shift(1) * all_data['returns']\n\n# \u8ba1\u7b97\u7ec4\u5408\u6536\u76ca\nportfolio_return = all_data.groupby('date')['strategy_return'].sum() \/ N<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7b56\u7565\u8bc4\u4f30<\/strong><\/h3>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u7b56\u7565\u7684\u5e74\u5316\u6536\u76ca\u7387\u3001\u6700\u5927\u56de\u64a4\u7b49\u6307\u6807\uff0c\u5e76\u7ed8\u5236\u7b56\u7565\u7684\u51c0\u503c\u66f2\u7ebf\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u8ba1\u7b97\u5e74\u5316\u6536\u76ca\u7387\nannual_return = portfolio_return.mean() * 252\n\n# \u8ba1\u7b97\u6700\u5927\u56de\u64a4\ncumulative_return = (1 + portfolio_return).cumprod()\npeak = cumulative_return.cummax()\ndrawdown = (cumulative_return - peak) \/ peak\nmax_drawdown = drawdown.min()\n\n# \u6253\u5370\u7b56\u7565\u8868\u73b0\nprint(f\"\u5e74\u5316\u6536\u76ca\u7387: {annual_return:.2%}\")\nprint(f\"\u6700\u5927\u56de\u64a4: {max_drawdown:.2%}\")\n\n# \u7ed8\u5236\u51c0\u503c\u66f2\u7ebf\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 6))\nplt.plot(cumulative_return)\nplt.title('Strategy Cumulative Return')\nplt.xlabel('Date')\nplt.ylabel('Cumulative Return')\nplt.show()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>\u52a8\u91cf\u6548\u5e94\u662f\u91cf\u5316\u4ea4\u6613\u4e2d\u4e00\u4e2a\u7ecf\u5178\u4e14\u6709\u6548\u7684\u7b56\u7565\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u4e00\u79cd\u57fa\u4e8e\u52a8\u91cf\u6548\u5e94\u7684\u7b80\u5355\u7b56\u7565\uff0c\u5e76\u4f7f\u7528Python\u4ee3\u7801\u5c55\u793a\u4e86\u5176\u5b9e\u73b0\u8fc7\u7a0b\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u7b56\u7565\u4ec5\u4f5c\u4e3a\u793a\u4f8b\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u8981\u8003\u8651\u66f4\u591a\u56e0\u7d20\uff0c\u5982\u4ea4\u6613\u6210\u672c\u3001\u98ce\u9669\u63a7\u5236\u7b49\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u91cf\u6548\u5e94\u662f\u91d1\u878d\u5e02\u573a\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u73b0\u8c61\uff0c\u6307\u7684\u662f\u8fc7\u53bb\u8868\u73b0\u8f83\u597d\u7684\u8d44\u4ea7\u5728\u672a\u6765\u4e00\u6bb5\u65f6\u95f4\u5185\u7ee7\u7eed\u8868\u73b0\u826f\u597d\u7684\u8d8b\u52bf\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u79cd&#8230;<\/p>\n","protected":false},"author":1,"featured_media":10379,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-10374","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tick-data-wiki"],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/10374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/comments?post=10374"}],"version-history":[{"count":5,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/10374\/revisions"}],"predecessor-version":[{"id":10393,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/10374\/revisions\/10393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media\/10379"}],"wp:attachment":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media?parent=10374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/categories?post=10374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/tags?post=10374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}