1
0

完成macd回测

This commit is contained in:
2025-02-16 23:24:34 +08:00
parent 46a539cf72
commit 9689625fc1
3 changed files with 1021 additions and 452 deletions

View File

@@ -3,8 +3,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-13T09:38:30.408310Z",
"start_time": "2025-02-13T09:38:30.328797Z"
"end_time": "2025-02-16T08:09:25.176397Z",
"start_time": "2025-02-16T08:09:25.125996Z"
}
},
"cell_type": "code",
@@ -12,49 +12,596 @@
"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 = pd.read_csv(\"/Users/lanyuanxiaoyao/SynologyDrive/data/Tushare/日线行情 1990-2024/分组行情/600519.SH.csv\") \\\n",
" [[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\", \"macd\", \"macd_dif\", \"macd_dea\"]]\n",
"source_df = \\\n",
" pd.read_csv(\"C:\\\\Users\\\\lanyuanxiaoyao\\\\SynologyDrive\\\\data\\\\Tushare\\\\日线行情 1990-2024\\\\分组行情\\\\000001.SZ.csv\") \\\n",
" [[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\", \"macd\", \"macd_dif\", \"macd_dea\"]]\n",
"# source_df = pd.read_csv(\"/Users/lanyuanxiaoyao/SynologyDrive/data/Tushare/日线行情 1990-2024/分组行情/600519.SH.csv\") \\\n",
"df = pd.DataFrame()\n",
"df[[\"date\", \"volume\", \"open\", \"close\", \"high\", \"low\", \"macd\", \"macd_dif\", \"macd_dea\"]] = \\\n",
" source_df[[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\", \"macd\", \"macd_dif\", \"macd_dea\"]]\n",
"df[\"datetime\"] = pd.to_datetime(df[\"date\"], format=\"%Y%m%d\")\n",
"df[\"datetime_text\"] = df[\"datetime\"].apply(lambda x: x.strftime(\"%Y%m%d\"))\n",
"df = df[df[\"datetime\"].dt.year > 2010]\n",
"df = df[df[\"datetime\"].dt.year > 2023]\n",
"df.sort_values(by='datetime', inplace=True)\n",
"df.set_index('datetime', inplace=True)\n",
"# df"
"df"
],
"id": "157a87ab33037c6e",
"outputs": [],
"execution_count": 46
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "12fde7bfaddbdf4"
"outputs": [
{
"data": {
"text/plain": [
" date volume open close high low \\\n",
"datetime \n",
"2024-01-02 20240102 1158366.45 8.57646 8.41205 8.60386 8.41205 \n",
"2024-01-03 20240103 733610.31 8.39379 8.40292 8.42119 8.35725 \n",
"2024-01-04 20240104 864193.99 8.39379 8.32072 8.39379 8.29332 \n",
"2024-01-05 20240105 1991622.16 8.31158 8.46686 8.62213 8.28418 \n",
"2024-01-08 20240108 1121156.19 8.43032 8.35725 8.49426 8.32072 \n",
"... ... ... ... ... ... ... \n",
"2024-12-25 20241225 1475282.94 11.86000 11.92000 12.02000 11.84000 \n",
"2024-12-26 20241226 1000074.70 11.92000 11.86000 11.93000 11.78000 \n",
"2024-12-27 20241227 1290012.28 11.87000 11.83000 11.90000 11.66000 \n",
"2024-12-30 20241230 1351846.36 11.78000 11.95000 11.97000 11.78000 \n",
"2024-12-31 20241231 1475367.33 11.93000 11.70000 11.99000 11.70000 \n",
"\n",
" macd macd_dif macd_dea datetime_text \n",
"datetime \n",
"2024-01-02 0.107 -0.162 -0.216 20240102 \n",
"2024-01-03 0.098 -0.155 -0.203 20240103 \n",
"2024-01-04 0.080 -0.153 -0.193 20240104 \n",
"2024-01-05 0.087 -0.139 -0.183 20240105 \n",
"2024-01-08 0.076 -0.135 -0.173 20240108 \n",
"... ... ... ... ... \n",
"2024-12-25 0.050 0.080 0.055 20241225 \n",
"2024-12-26 0.051 0.087 0.062 20241226 \n",
"2024-12-27 0.044 0.089 0.067 20241227 \n",
"2024-12-30 0.052 0.100 0.074 20241230 \n",
"2024-12-31 0.020 0.086 0.076 20241231 \n",
"\n",
"[242 rows x 10 columns]"
],
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>volume</th>\n",
" <th>open</th>\n",
" <th>close</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>macd</th>\n",
" <th>macd_dif</th>\n",
" <th>macd_dea</th>\n",
" <th>datetime_text</th>\n",
" </tr>\n",
" <tr>\n",
" <th>datetime</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2024-01-02</th>\n",
" <td>20240102</td>\n",
" <td>1158366.45</td>\n",
" <td>8.57646</td>\n",
" <td>8.41205</td>\n",
" <td>8.60386</td>\n",
" <td>8.41205</td>\n",
" <td>0.107</td>\n",
" <td>-0.162</td>\n",
" <td>-0.216</td>\n",
" <td>20240102</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-03</th>\n",
" <td>20240103</td>\n",
" <td>733610.31</td>\n",
" <td>8.39379</td>\n",
" <td>8.40292</td>\n",
" <td>8.42119</td>\n",
" <td>8.35725</td>\n",
" <td>0.098</td>\n",
" <td>-0.155</td>\n",
" <td>-0.203</td>\n",
" <td>20240103</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-04</th>\n",
" <td>20240104</td>\n",
" <td>864193.99</td>\n",
" <td>8.39379</td>\n",
" <td>8.32072</td>\n",
" <td>8.39379</td>\n",
" <td>8.29332</td>\n",
" <td>0.080</td>\n",
" <td>-0.153</td>\n",
" <td>-0.193</td>\n",
" <td>20240104</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-05</th>\n",
" <td>20240105</td>\n",
" <td>1991622.16</td>\n",
" <td>8.31158</td>\n",
" <td>8.46686</td>\n",
" <td>8.62213</td>\n",
" <td>8.28418</td>\n",
" <td>0.087</td>\n",
" <td>-0.139</td>\n",
" <td>-0.183</td>\n",
" <td>20240105</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-08</th>\n",
" <td>20240108</td>\n",
" <td>1121156.19</td>\n",
" <td>8.43032</td>\n",
" <td>8.35725</td>\n",
" <td>8.49426</td>\n",
" <td>8.32072</td>\n",
" <td>0.076</td>\n",
" <td>-0.135</td>\n",
" <td>-0.173</td>\n",
" <td>20240108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-25</th>\n",
" <td>20241225</td>\n",
" <td>1475282.94</td>\n",
" <td>11.86000</td>\n",
" <td>11.92000</td>\n",
" <td>12.02000</td>\n",
" <td>11.84000</td>\n",
" <td>0.050</td>\n",
" <td>0.080</td>\n",
" <td>0.055</td>\n",
" <td>20241225</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-26</th>\n",
" <td>20241226</td>\n",
" <td>1000074.70</td>\n",
" <td>11.92000</td>\n",
" <td>11.86000</td>\n",
" <td>11.93000</td>\n",
" <td>11.78000</td>\n",
" <td>0.051</td>\n",
" <td>0.087</td>\n",
" <td>0.062</td>\n",
" <td>20241226</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-27</th>\n",
" <td>20241227</td>\n",
" <td>1290012.28</td>\n",
" <td>11.87000</td>\n",
" <td>11.83000</td>\n",
" <td>11.90000</td>\n",
" <td>11.66000</td>\n",
" <td>0.044</td>\n",
" <td>0.089</td>\n",
" <td>0.067</td>\n",
" <td>20241227</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-30</th>\n",
" <td>20241230</td>\n",
" <td>1351846.36</td>\n",
" <td>11.78000</td>\n",
" <td>11.95000</td>\n",
" <td>11.97000</td>\n",
" <td>11.78000</td>\n",
" <td>0.052</td>\n",
" <td>0.100</td>\n",
" <td>0.074</td>\n",
" <td>20241230</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-31</th>\n",
" <td>20241231</td>\n",
" <td>1475367.33</td>\n",
" <td>11.93000</td>\n",
" <td>11.70000</td>\n",
" <td>11.99000</td>\n",
" <td>11.70000</td>\n",
" <td>0.020</td>\n",
" <td>0.086</td>\n",
" <td>0.076</td>\n",
" <td>20241231</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>242 rows × 10 columns</p>\n",
"</div>"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 158
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-13T09:36:52.737860Z",
"start_time": "2025-02-13T09:36:51.459915Z"
"end_time": "2025-02-16T08:09:25.209704Z",
"start_time": "2025-02-16T08:09:25.199080Z"
}
},
"cell_type": "code",
"source": [
"import calendar\n",
"import talib\n",
"\n",
"# 指标计算\n",
"close_prices = df['close'].values\n",
"\n",
"# 计算MACD\n",
"dif, dea, hist = talib.MACD(close_prices, fastperiod=12, slowperiod=26, signalperiod=9)\n",
"\n",
"# 将结果添加到数据框\n",
"df['dif'] = dif\n",
"df['dif'] = df['dif'].fillna(0)\n",
"df['dea'] = dea\n",
"df['dea'] = df['dea'].fillna(0)\n",
"df['hist'] = hist\n",
"df['hist'] = df['hist'].fillna(0)\n",
"\n",
"df"
],
"id": "12fde7bfaddbdf4",
"outputs": [
{
"data": {
"text/plain": [
" date volume open close high low \\\n",
"datetime \n",
"2024-01-02 20240102 1158366.45 8.57646 8.41205 8.60386 8.41205 \n",
"2024-01-03 20240103 733610.31 8.39379 8.40292 8.42119 8.35725 \n",
"2024-01-04 20240104 864193.99 8.39379 8.32072 8.39379 8.29332 \n",
"2024-01-05 20240105 1991622.16 8.31158 8.46686 8.62213 8.28418 \n",
"2024-01-08 20240108 1121156.19 8.43032 8.35725 8.49426 8.32072 \n",
"... ... ... ... ... ... ... \n",
"2024-12-25 20241225 1475282.94 11.86000 11.92000 12.02000 11.84000 \n",
"2024-12-26 20241226 1000074.70 11.92000 11.86000 11.93000 11.78000 \n",
"2024-12-27 20241227 1290012.28 11.87000 11.83000 11.90000 11.66000 \n",
"2024-12-30 20241230 1351846.36 11.78000 11.95000 11.97000 11.78000 \n",
"2024-12-31 20241231 1475367.33 11.93000 11.70000 11.99000 11.70000 \n",
"\n",
" macd macd_dif macd_dea datetime_text dif dea \\\n",
"datetime \n",
"2024-01-02 0.107 -0.162 -0.216 20240102 0.000000 0.000000 \n",
"2024-01-03 0.098 -0.155 -0.203 20240103 0.000000 0.000000 \n",
"2024-01-04 0.080 -0.153 -0.193 20240104 0.000000 0.000000 \n",
"2024-01-05 0.087 -0.139 -0.183 20240105 0.000000 0.000000 \n",
"2024-01-08 0.076 -0.135 -0.173 20240108 0.000000 0.000000 \n",
"... ... ... ... ... ... ... \n",
"2024-12-25 0.050 0.080 0.055 20241225 0.080475 0.055293 \n",
"2024-12-26 0.051 0.087 0.062 20241226 0.087385 0.061712 \n",
"2024-12-27 0.044 0.089 0.067 20241227 0.089411 0.067251 \n",
"2024-12-30 0.052 0.100 0.074 20241230 0.099551 0.073711 \n",
"2024-12-31 0.020 0.086 0.076 20241231 0.086419 0.076253 \n",
"\n",
" hist \n",
"datetime \n",
"2024-01-02 0.000000 \n",
"2024-01-03 0.000000 \n",
"2024-01-04 0.000000 \n",
"2024-01-05 0.000000 \n",
"2024-01-08 0.000000 \n",
"... ... \n",
"2024-12-25 0.025182 \n",
"2024-12-26 0.025674 \n",
"2024-12-27 0.022159 \n",
"2024-12-30 0.025840 \n",
"2024-12-31 0.010166 \n",
"\n",
"[242 rows x 13 columns]"
],
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>volume</th>\n",
" <th>open</th>\n",
" <th>close</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>macd</th>\n",
" <th>macd_dif</th>\n",
" <th>macd_dea</th>\n",
" <th>datetime_text</th>\n",
" <th>dif</th>\n",
" <th>dea</th>\n",
" <th>hist</th>\n",
" </tr>\n",
" <tr>\n",
" <th>datetime</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2024-01-02</th>\n",
" <td>20240102</td>\n",
" <td>1158366.45</td>\n",
" <td>8.57646</td>\n",
" <td>8.41205</td>\n",
" <td>8.60386</td>\n",
" <td>8.41205</td>\n",
" <td>0.107</td>\n",
" <td>-0.162</td>\n",
" <td>-0.216</td>\n",
" <td>20240102</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-03</th>\n",
" <td>20240103</td>\n",
" <td>733610.31</td>\n",
" <td>8.39379</td>\n",
" <td>8.40292</td>\n",
" <td>8.42119</td>\n",
" <td>8.35725</td>\n",
" <td>0.098</td>\n",
" <td>-0.155</td>\n",
" <td>-0.203</td>\n",
" <td>20240103</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-04</th>\n",
" <td>20240104</td>\n",
" <td>864193.99</td>\n",
" <td>8.39379</td>\n",
" <td>8.32072</td>\n",
" <td>8.39379</td>\n",
" <td>8.29332</td>\n",
" <td>0.080</td>\n",
" <td>-0.153</td>\n",
" <td>-0.193</td>\n",
" <td>20240104</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-05</th>\n",
" <td>20240105</td>\n",
" <td>1991622.16</td>\n",
" <td>8.31158</td>\n",
" <td>8.46686</td>\n",
" <td>8.62213</td>\n",
" <td>8.28418</td>\n",
" <td>0.087</td>\n",
" <td>-0.139</td>\n",
" <td>-0.183</td>\n",
" <td>20240105</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-01-08</th>\n",
" <td>20240108</td>\n",
" <td>1121156.19</td>\n",
" <td>8.43032</td>\n",
" <td>8.35725</td>\n",
" <td>8.49426</td>\n",
" <td>8.32072</td>\n",
" <td>0.076</td>\n",
" <td>-0.135</td>\n",
" <td>-0.173</td>\n",
" <td>20240108</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-25</th>\n",
" <td>20241225</td>\n",
" <td>1475282.94</td>\n",
" <td>11.86000</td>\n",
" <td>11.92000</td>\n",
" <td>12.02000</td>\n",
" <td>11.84000</td>\n",
" <td>0.050</td>\n",
" <td>0.080</td>\n",
" <td>0.055</td>\n",
" <td>20241225</td>\n",
" <td>0.080475</td>\n",
" <td>0.055293</td>\n",
" <td>0.025182</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-26</th>\n",
" <td>20241226</td>\n",
" <td>1000074.70</td>\n",
" <td>11.92000</td>\n",
" <td>11.86000</td>\n",
" <td>11.93000</td>\n",
" <td>11.78000</td>\n",
" <td>0.051</td>\n",
" <td>0.087</td>\n",
" <td>0.062</td>\n",
" <td>20241226</td>\n",
" <td>0.087385</td>\n",
" <td>0.061712</td>\n",
" <td>0.025674</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-27</th>\n",
" <td>20241227</td>\n",
" <td>1290012.28</td>\n",
" <td>11.87000</td>\n",
" <td>11.83000</td>\n",
" <td>11.90000</td>\n",
" <td>11.66000</td>\n",
" <td>0.044</td>\n",
" <td>0.089</td>\n",
" <td>0.067</td>\n",
" <td>20241227</td>\n",
" <td>0.089411</td>\n",
" <td>0.067251</td>\n",
" <td>0.022159</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-30</th>\n",
" <td>20241230</td>\n",
" <td>1351846.36</td>\n",
" <td>11.78000</td>\n",
" <td>11.95000</td>\n",
" <td>11.97000</td>\n",
" <td>11.78000</td>\n",
" <td>0.052</td>\n",
" <td>0.100</td>\n",
" <td>0.074</td>\n",
" <td>20241230</td>\n",
" <td>0.099551</td>\n",
" <td>0.073711</td>\n",
" <td>0.025840</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2024-12-31</th>\n",
" <td>20241231</td>\n",
" <td>1475367.33</td>\n",
" <td>11.93000</td>\n",
" <td>11.70000</td>\n",
" <td>11.99000</td>\n",
" <td>11.70000</td>\n",
" <td>0.020</td>\n",
" <td>0.086</td>\n",
" <td>0.076</td>\n",
" <td>20241231</td>\n",
" <td>0.086419</td>\n",
" <td>0.076253</td>\n",
" <td>0.010166</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>242 rows × 13 columns</p>\n",
"</div>"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 159
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-16T08:09:25.361947Z",
"start_time": "2025-02-16T08:09:25.270309Z"
}
},
"cell_type": "code",
"source": [
"cerebro = bt.Cerebro()\n",
"\n",
"data_feed = bt.feeds.PandasData(dataname=df)\n",
"cerebro.adddata(data_feed)\n",
"\n",
"class EnhancePandasData(bt.feeds.PandasData):\n",
" lines = (\"dif\", \"dea\", \"hist\")\n",
" params = (\n",
" (\"dif\", -1),\n",
" (\"dea\", -1),\n",
" (\"hist\", -1),\n",
" )\n",
"\n",
"\n",
"cerebro.adddata(EnhancePandasData(dataname=df))\n",
"# 初始资金 100,000,000\n",
"cerebro.broker.setcash(100000000.0)\n",
"cerebro.broker.setcash(10000.0)\n",
"# 佣金,双边各 0.0003\n",
"cerebro.broker.setcommission(commission=0.0003)\n",
"# 滑点:双边各 0.0001\n",
@@ -68,12 +615,20 @@
"# 创建策略类\n",
"class TestStrategy(bt.Strategy):\n",
" def next(self):\n",
" curr_date = self.datetime.date()\n",
" _, last_day_month = calendar.monthrange(curr_date.year, curr_date.month)\n",
" if curr_date.day == 1:\n",
" self.buy()\n",
" elif curr_date.day == last_day_month:\n",
" self.sell()\n",
" now_dif = self.data.dif[0]\n",
" now_dea = self.data.dea[0]\n",
" now_hist = self.data.hist[0]\n",
"\n",
" pre_dif = self.data.dif[-1]\n",
" pre_dea = self.data.dea[-1]\n",
" pre_hist = self.data.hist[-1]\n",
"\n",
" hold = self.getposition(self.data).size\n",
"\n",
" if now_dif > now_dea and pre_dif <= pre_dea and now_dif > 0 and now_dea > 0:\n",
" self.buy(size=100)\n",
" elif now_dif < now_dea and pre_dif >= pre_dea and hold > 0:\n",
" self.sell(size=hold)\n",
"\n",
" def log(self, txt, dt=None):\n",
" \"\"\"\n",
@@ -113,10 +668,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"初始资金: 100000000.00\n",
"最终资金: 99982677.31\n",
"夏普比率: -249.45833106775427\n",
"最大回撤: 0.03266399519865984\n"
"初始资金: 10000.00\n",
"最终资金: 10015.80\n",
"夏普比率: None\n",
"最大回撤: 0.48930011171324134\n"
]
},
{
@@ -135,7 +690,7 @@
"<IPython.core.display.HTML object>"
],
"text/html": [
"<div id='8a8873d7-5615-4456-bb60-d011d91f07a9'></div>"
"<div id='02240639-900b-4c9e-bace-e1c596384da2'></div>"
]
},
"metadata": {},
@@ -147,12 +702,12 @@
"[[<Figure size 640x480 with 4 Axes>]]"
]
},
"execution_count": 45,
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 45
"execution_count": 160
}
],
"metadata": {