{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2025-02-17T10:16:26.170873Z", "start_time": "2025-02-17T10:16:26.058931Z" } }, "cell_type": "code", "source": [ "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/分组行情/000001.SZ.csv\") \\\n", " [[\"trade_date\", \"vol\", \"open_qfq\", \"close_qfq\", \"high_qfq\", \"low_qfq\", \"macd\", \"macd_dif\", \"macd_dea\"]]\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 > 2023]\n", "df.sort_values(by='datetime', inplace=True)\n", "df.set_index('datetime', inplace=True)\n", "df" ], "id": "157a87ab33037c6e", "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datevolumeopenclosehighlowmacdmacd_difmacd_deadatetime_text
datetime
2024-01-02202401021158366.458.576468.412058.603868.412050.107-0.162-0.21620240102
2024-01-0320240103733610.318.393798.402928.421198.357250.098-0.155-0.20320240103
2024-01-0420240104864193.998.393798.320728.393798.293320.080-0.153-0.19320240104
2024-01-05202401051991622.168.311588.466868.622138.284180.087-0.139-0.18320240105
2024-01-08202401081121156.198.430328.357258.494268.320720.076-0.135-0.17320240108
.................................
2024-12-25202412251475282.9411.8600011.9200012.0200011.840000.0500.0800.05520241225
2024-12-26202412261000074.7011.9200011.8600011.9300011.780000.0510.0870.06220241226
2024-12-27202412271290012.2811.8700011.8300011.9000011.660000.0440.0890.06720241227
2024-12-30202412301351846.3611.7800011.9500011.9700011.780000.0520.1000.07420241230
2024-12-31202412311475367.3311.9300011.7000011.9900011.700000.0200.0860.07620241231
\n", "

242 rows × 10 columns

\n", "
" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 72 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-17T10:16:26.845470Z", "start_time": "2025-02-17T10:16:26.839164Z" } }, "cell_type": "code", "source": [ "def run_backtrader(data, strategy):\n", " cerebro = bt.Cerebro()\n", " cerebro.adddata(data)\n", " cerebro.broker.setcash(10000.0)\n", " # 佣金,双边各 0.0003\n", " cerebro.broker.setcommission(commission=0.0003)\n", " # 滑点:双边各 0.0001\n", " cerebro.broker.set_slippage_perc(perc=0.0001)\n", " cerebro.addanalyzer(bt.analyzers.TimeReturn, _name='pnl') # 返回收益率时序数据\n", " cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='annual_return') # 年化收益率\n", " cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='sharpe_ratio') # 夏普比率\n", " cerebro.addanalyzer(bt.analyzers.DrawDown, _name='draw_down') # 回撤\n", "\n", " # 将编写的策略添加给大脑,别忘了 !\n", " cerebro.addstrategy(strategy)\n", "\n", " print('初始资金: %.2f' % cerebro.broker.getvalue())\n", " results = cerebro.run()\n", " print('最终资金: %.2f' % cerebro.broker.getvalue())\n", " # 获取分析结果\n", " strat = results[0]\n", " print('年化收益率')\n", " pairs = strat.analyzers.annual_return.get_analysis()\n", " for key in pairs.keys():\n", " print(key, pairs[key])\n", " print('夏普比率:', strat.analyzers.sharpe_ratio.get_analysis()['sharperatio'])\n", " print('最大回撤:', strat.analyzers.draw_down.get_analysis()['max']['drawdown'])\n", " # 绘制结果图表\n", " cerebro.plot(style='candlestick', width=20, height=10)" ], "id": "d25f6e4d51cdfb20", "outputs": [], "execution_count": 73 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-17T10:16:26.920783Z", "start_time": "2025-02-17T10:16:26.900580Z" } }, "cell_type": "code", "source": [ "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": "5774f2437186c503", "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
datevolumeopenclosehighlowmacdmacd_difmacd_deadatetime_textdifdeahist
datetime
2024-01-02202401021158366.458.576468.412058.603868.412050.107-0.162-0.216202401020.0000000.0000000.000000
2024-01-0320240103733610.318.393798.402928.421198.357250.098-0.155-0.203202401030.0000000.0000000.000000
2024-01-0420240104864193.998.393798.320728.393798.293320.080-0.153-0.193202401040.0000000.0000000.000000
2024-01-05202401051991622.168.311588.466868.622138.284180.087-0.139-0.183202401050.0000000.0000000.000000
2024-01-08202401081121156.198.430328.357258.494268.320720.076-0.135-0.173202401080.0000000.0000000.000000
..........................................
2024-12-25202412251475282.9411.8600011.9200012.0200011.840000.0500.0800.055202412250.0804750.0552930.025182
2024-12-26202412261000074.7011.9200011.8600011.9300011.780000.0510.0870.062202412260.0873850.0617120.025674
2024-12-27202412271290012.2811.8700011.8300011.9000011.660000.0440.0890.067202412270.0894110.0672510.022159
2024-12-30202412301351846.3611.7800011.9500011.9700011.780000.0520.1000.074202412300.0995510.0737110.025840
2024-12-31202412311475367.3311.9300011.7000011.9900011.700000.0200.0860.076202412310.0864190.0762530.010166
\n", "

242 rows × 13 columns

\n", "
" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "execution_count": 74 }, { "metadata": { "ExecuteTime": { "end_time": "2025-02-17T10:23:20.037206Z", "start_time": "2025-02-17T10:23:19.913993Z" } }, "cell_type": "code", "source": [ "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", "# 创建策略类\n", "class MACDStrategy(bt.Strategy):\n", " def __init__(self):\n", " print(bt.talib.MACD.__doc__)\n", " print(bt.talib.MACD(self.data.close, fastperiod=12, slowperiod=26, signalperiod=9))\n", " self.macd, self.signal, self.hist = bt.talib.MACD(self.data.close, fastperiod=12, slowperiod=26, signalperiod=9)\n", " # print(dif, dea, macd)\n", " # self.dif = dif\n", " # self.dea = dea\n", " # self.macd = macd\n", "\n", " def next(self):\n", " pre_dif = self.dif[-1]\n", " pre_dea = self.dea[-1]\n", " pre_hist = self.hist[-1]\n", "\n", " hold = self.getposition(self.data).size\n", "\n", " if dif > dea and pre_dif <= pre_dea and dif > 0 and dea > 0:\n", " self.buy(size=100)\n", " elif dif < dea and pre_dif >= pre_dea and hold > 0:\n", " self.sell(size=hold)\n", "\n", " def log(self, txt, dt=None):\n", " \"\"\"\n", " 输出包含时间的日志。\n", " \"\"\"\n", " dt = dt or self.datas[0].datetime.date(0)\n", " print(f'{dt.isoformat()}, {txt}')\n", "\n", "\n", "run_backtrader(bt.feeds.PandasData(dataname=df), MACDStrategy)" ], "id": "45f8133af2348b95", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "初始资金: 10000.00\n", "MACD([input_arrays], [fastperiod=12], [slowperiod=26], [signalperiod=9])\n", "\n", "Moving Average Convergence/Divergence (Momentum Indicators)\n", "\n", "Inputs:\n", " price: (any ndarray)\n", "Parameters:\n", " fastperiod: 12\n", " slowperiod: 26\n", " signalperiod: 9\n", "Outputs:\n", " macd\n", " macdsignal\n", " macdhist\n", "\n" ] }, { "ename": "ValueError", "evalue": "not enough values to unpack (expected 3, got 0)", "output_type": "error", "traceback": [ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", "Cell \u001B[0;32mIn[84], line 41\u001B[0m\n\u001B[1;32m 37\u001B[0m dt \u001B[38;5;241m=\u001B[39m dt \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdatas[\u001B[38;5;241m0\u001B[39m]\u001B[38;5;241m.\u001B[39mdatetime\u001B[38;5;241m.\u001B[39mdate(\u001B[38;5;241m0\u001B[39m)\n\u001B[1;32m 38\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mdt\u001B[38;5;241m.\u001B[39misoformat()\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m, \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mtxt\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m---> 41\u001B[0m \u001B[43mrun_backtrader\u001B[49m\u001B[43m(\u001B[49m\u001B[43mbt\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfeeds\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mPandasData\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdataname\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mdf\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mMACDStrategy\u001B[49m\u001B[43m)\u001B[49m\n", "Cell \u001B[0;32mIn[73], line 18\u001B[0m, in \u001B[0;36mrun_backtrader\u001B[0;34m(data, strategy)\u001B[0m\n\u001B[1;32m 15\u001B[0m cerebro\u001B[38;5;241m.\u001B[39maddstrategy(strategy)\n\u001B[1;32m 17\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m初始资金: \u001B[39m\u001B[38;5;132;01m%.2f\u001B[39;00m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m%\u001B[39m cerebro\u001B[38;5;241m.\u001B[39mbroker\u001B[38;5;241m.\u001B[39mgetvalue())\n\u001B[0;32m---> 18\u001B[0m results \u001B[38;5;241m=\u001B[39m \u001B[43mcerebro\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrun\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 19\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m最终资金: \u001B[39m\u001B[38;5;132;01m%.2f\u001B[39;00m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m%\u001B[39m cerebro\u001B[38;5;241m.\u001B[39mbroker\u001B[38;5;241m.\u001B[39mgetvalue())\n\u001B[1;32m 20\u001B[0m \u001B[38;5;66;03m# 获取分析结果\u001B[39;00m\n", "File \u001B[0;32m~/Library/Caches/pypoetry/virtualenvs/finance-G8vNaf2C-py3.13/lib/python3.13/site-packages/backtrader/cerebro.py:1132\u001B[0m, in \u001B[0;36mCerebro.run\u001B[0;34m(self, **kwargs)\u001B[0m\n\u001B[1;32m 1128\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_dooptimize \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mp\u001B[38;5;241m.\u001B[39mmaxcpus \u001B[38;5;241m==\u001B[39m \u001B[38;5;241m1\u001B[39m:\n\u001B[1;32m 1129\u001B[0m \u001B[38;5;66;03m# If no optimmization is wished ... or 1 core is to be used\u001B[39;00m\n\u001B[1;32m 1130\u001B[0m \u001B[38;5;66;03m# let's skip process \"spawning\"\u001B[39;00m\n\u001B[1;32m 1131\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m iterstrat \u001B[38;5;129;01min\u001B[39;00m iterstrats:\n\u001B[0;32m-> 1132\u001B[0m runstrat \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrunstrategies\u001B[49m\u001B[43m(\u001B[49m\u001B[43miterstrat\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1133\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mrunstrats\u001B[38;5;241m.\u001B[39mappend(runstrat)\n\u001B[1;32m 1134\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_dooptimize:\n", "File \u001B[0;32m~/Library/Caches/pypoetry/virtualenvs/finance-G8vNaf2C-py3.13/lib/python3.13/site-packages/backtrader/cerebro.py:1222\u001B[0m, in \u001B[0;36mCerebro.runstrategies\u001B[0;34m(self, iterstrat, predata)\u001B[0m\n\u001B[1;32m 1220\u001B[0m sargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdatas \u001B[38;5;241m+\u001B[39m \u001B[38;5;28mlist\u001B[39m(sargs)\n\u001B[1;32m 1221\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m-> 1222\u001B[0m strat \u001B[38;5;241m=\u001B[39m \u001B[43mstratcls\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43msargs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mskwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 1223\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m bt\u001B[38;5;241m.\u001B[39merrors\u001B[38;5;241m.\u001B[39mStrategySkipError:\n\u001B[1;32m 1224\u001B[0m \u001B[38;5;28;01mcontinue\u001B[39;00m \u001B[38;5;66;03m# do not add strategy to the mix\u001B[39;00m\n", "File \u001B[0;32m~/Library/Caches/pypoetry/virtualenvs/finance-G8vNaf2C-py3.13/lib/python3.13/site-packages/backtrader/metabase.py:88\u001B[0m, in \u001B[0;36mMetaBase.__call__\u001B[0;34m(cls, *args, **kwargs)\u001B[0m\n\u001B[1;32m 86\u001B[0m _obj, args, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39mdonew(\u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[1;32m 87\u001B[0m _obj, args, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39mdopreinit(_obj, \u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m---> 88\u001B[0m _obj, args, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mcls\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdoinit\u001B[49m\u001B[43m(\u001B[49m\u001B[43m_obj\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 89\u001B[0m _obj, args, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39mdopostinit(_obj, \u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[1;32m 90\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m _obj\n", "File \u001B[0;32m~/Library/Caches/pypoetry/virtualenvs/finance-G8vNaf2C-py3.13/lib/python3.13/site-packages/backtrader/metabase.py:78\u001B[0m, in \u001B[0;36mMetaBase.doinit\u001B[0;34m(cls, _obj, *args, **kwargs)\u001B[0m\n\u001B[1;32m 77\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mdoinit\u001B[39m(\u001B[38;5;28mcls\u001B[39m, _obj, \u001B[38;5;241m*\u001B[39margs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs):\n\u001B[0;32m---> 78\u001B[0m \u001B[43m_obj\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[38;5;21;43m__init__\u001B[39;49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 79\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m _obj, args, kwargs\n", "Cell \u001B[0;32mIn[84], line 15\u001B[0m, in \u001B[0;36mMACDStrategy.__init__\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 13\u001B[0m \u001B[38;5;28mprint\u001B[39m(bt\u001B[38;5;241m.\u001B[39mtalib\u001B[38;5;241m.\u001B[39mMACD\u001B[38;5;241m.\u001B[39m\u001B[38;5;18m__doc__\u001B[39m)\n\u001B[1;32m 14\u001B[0m \u001B[38;5;28mprint\u001B[39m(bt\u001B[38;5;241m.\u001B[39mtalib\u001B[38;5;241m.\u001B[39mMACD(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdata\u001B[38;5;241m.\u001B[39mclose, fastperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m12\u001B[39m, slowperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m26\u001B[39m, signalperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m9\u001B[39m))\n\u001B[0;32m---> 15\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mmacd, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39msignal, \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhist \u001B[38;5;241m=\u001B[39m bt\u001B[38;5;241m.\u001B[39mtalib\u001B[38;5;241m.\u001B[39mMACD(\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdata\u001B[38;5;241m.\u001B[39mclose, fastperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m12\u001B[39m, slowperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m26\u001B[39m, signalperiod\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m9\u001B[39m)\n", "\u001B[0;31mValueError\u001B[0m: not enough values to unpack (expected 3, got 0)" ] } ], "execution_count": 84 } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }