新增backtesting框架用于回测
This commit is contained in:
420
回测/backtesting.ipynb
Normal file
420
回测/backtesting.ipynb
Normal file
File diff suppressed because one or more lines are too long
@@ -3,8 +3,8 @@
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-17T14:33:50.205817Z",
|
||||
"start_time": "2025-02-17T14:33:50.160964Z"
|
||||
"end_time": "2025-02-18T02:25:15.671867Z",
|
||||
"start_time": "2025-02-18T02:25:15.090164Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
@@ -12,10 +12,10 @@
|
||||
"import backtrader as bt\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"source_df = \\\n",
|
||||
"pd.read_csv(\"C:\\\\Users\\\\lanyuanxiaoyao\\\\SynologyDrive\\\\data\\\\Tushare\\\\日线行情 1990-2024\\\\分组行情\\\\000001.SZ.csv\") \\\n",
|
||||
"# source_df = \\\n",
|
||||
"# pd.read_csv(\"C:\\\\Users\\\\lanyuanxiaoyao\\\\SynologyDrive\\\\data\\\\Tushare\\\\日线行情 1990-2024\\\\分组行情\\\\000001.SZ.csv\") \\\n",
|
||||
"source_df = pd.read_csv(\"/Users/lanyuanxiaoyao/SynologyDrive/data/Tushare/日线行情 1990-2024/分组行情/000001.SZ.csv\") \\\n",
|
||||
" [[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\"]]\n",
|
||||
"# source_df = pd.read_csv(\"/Users/lanyuanxiaoyao/SynologyDrive/data/Tushare/日线行情 1990-2024/分组行情/000001.SZ.csv\") \\\n",
|
||||
"df = pd.DataFrame()\n",
|
||||
"df[[\"date\", \"volume\", \"open\", \"close\", \"high\", \"low\"]] = \\\n",
|
||||
" source_df[[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\"]]\n",
|
||||
@@ -216,18 +216,18 @@
|
||||
"</div>"
|
||||
]
|
||||
},
|
||||
"execution_count": 42,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"execution_count": 42
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-17T14:33:50.231103Z",
|
||||
"start_time": "2025-02-17T14:33:50.225919Z"
|
||||
"end_time": "2025-02-18T02:29:43.443218Z",
|
||||
"start_time": "2025-02-18T02:29:43.436601Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
@@ -260,17 +260,17 @@
|
||||
" print('夏普比率:', strat.analyzers.sharpe_ratio.get_analysis()['sharperatio'])\n",
|
||||
" print('最大回撤:', strat.analyzers.draw_down.get_analysis()['max']['drawdown'])\n",
|
||||
" # 绘制结果图表\n",
|
||||
" return cerebro.plot(style='candlestick', width=20, height=10)"
|
||||
" return cerebro.plot(style='candlestick')"
|
||||
],
|
||||
"id": "d25f6e4d51cdfb20",
|
||||
"outputs": [],
|
||||
"execution_count": 43
|
||||
"execution_count": 5
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-17T14:33:50.274745Z",
|
||||
"start_time": "2025-02-17T14:33:50.269232Z"
|
||||
"end_time": "2025-02-18T02:25:16.582436Z",
|
||||
"start_time": "2025-02-18T02:25:15.803336Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
@@ -291,21 +291,7 @@
|
||||
"df['hist'] = hist\n",
|
||||
"df['hist'] = df['hist'].fillna(0)\n",
|
||||
"\n",
|
||||
"# df"
|
||||
],
|
||||
"id": "5774f2437186c503",
|
||||
"outputs": [],
|
||||
"execution_count": 44
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-02-17T14:33:50.412752Z",
|
||||
"start_time": "2025-02-17T14:33:50.327691Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"\n",
|
||||
"class EnhancePandasData(bt.feeds.PandasData):\n",
|
||||
" lines = (\"dif\", \"dea\", \"hist\")\n",
|
||||
" params = (\n",
|
||||
@@ -343,7 +329,7 @@
|
||||
"\n",
|
||||
"run_backtrader(EnhancePandasData(dataname=df), MACDStrategy)"
|
||||
],
|
||||
"id": "45f8133af2348b95",
|
||||
"id": "5774f2437186c503",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -373,7 +359,7 @@
|
||||
"<IPython.core.display.HTML object>"
|
||||
],
|
||||
"text/html": [
|
||||
"<div id='75a19a97-0e96-4034-a26a-d8ac2a51d841'></div>"
|
||||
"<div id='ba76dac9-3ef2-4dbb-a2b4-b75774d0b531'></div>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
@@ -385,12 +371,12 @@
|
||||
"[[<Figure size 640x480 with 4 Axes>]]"
|
||||
]
|
||||
},
|
||||
"execution_count": 45,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"execution_count": 45
|
||||
"execution_count": 4
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user