Created
September 9, 2025 03:00
-
-
Save oxfordyang2016/103b3badd92728f944c4ad62b2315380 to your computer and use it in GitHub Desktop.
showstart.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyOJmgiGZYtPx+2orZok66hS", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/oxfordyang2016/103b3badd92728f944c4ad62b2315380/showstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "dZU3GWggY4uX" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from pyqlib import init\n", | |
| "from qlib.data import D\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import pandas as pd\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "# 初始化\n", | |
| "provider = r\"D:\\investmentresearch\\qlibsystem\\data_commod_daily\\qlib_data_demo\"\n", | |
| "init(provider_uri=provider)\n", | |
| "\n", | |
| "symbol = \"CU_DOMINANT_DAILY\"\n", | |
| "\n", | |
| "# 取收盘价\n", | |
| "df = D.features([symbol], [\"$close\"], start_time=\"2018-01-02\", end_time=\"2019-12-31\")\n", | |
| "close = df[\"$close\"]\n", | |
| "\n", | |
| "# 计算短期/长期均线\n", | |
| "ma5 = close.rolling(5).mean()\n", | |
| "ma20 = close.rolling(20).mean()\n", | |
| "\n", | |
| "# 策略信号\n", | |
| "signal = (ma5 > ma20).astype(int)\n", | |
| "\n", | |
| "# 日收益率\n", | |
| "ret = close.pct_change().fillna(0)\n", | |
| "\n", | |
| "# 策略收益\n", | |
| "strategy_ret = signal.shift(1) * ret\n", | |
| "nav_strategy = (1 + strategy_ret).cumprod()\n", | |
| "\n", | |
| "# 基准:买入持有\n", | |
| "nav_bh = (1 + ret).cumprod()\n", | |
| "\n", | |
| "# 夏普比率计算函数\n", | |
| "def sharpe_ratio(returns, freq=252):\n", | |
| " mean_ret = returns.mean()\n", | |
| " vol = returns.std()\n", | |
| " return (mean_ret / vol) * np.sqrt(freq) if vol > 0 else np.nan\n", | |
| "\n", | |
| "# 计算夏普\n", | |
| "sharpe_strategy = sharpe_ratio(strategy_ret)\n", | |
| "sharpe_bh = sharpe_ratio(ret)\n", | |
| "\n", | |
| "print(f\"Sharpe Ratio (MA策略): {sharpe_strategy:.2f}\")\n", | |
| "print(f\"Sharpe Ratio (买入持有): {sharpe_bh:.2f}\")\n", | |
| "\n", | |
| "# 对比画图\n", | |
| "plt.figure(figsize=(10,5))\n", | |
| "nav_strategy.plot(label=\"MA Crossover Strategy\")\n", | |
| "nav_bh.plot(label=\"Buy & Hold\")\n", | |
| "plt.title(f\"Strategy vs Buy&Hold - {symbol}\")\n", | |
| "plt.ylabel(\"Net Value\")\n", | |
| "plt.legend()\n", | |
| "plt.grid(True)\n", | |
| "plt.show()" | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment