Backtraderというライブラリを使って株取引のバックテストをやってみたが、結果を取り出すのに難儀したので自分の備忘録を兼ねて公開。
import backtrader as bt
import backtrader.analyzers as btanalyzers
(中略)
data = bt.feeds.PandasData(dataname=df)
cerebro = bt.Cerebro()
cerebro.addstrategy(MyStrategy)
cerebro.adddata(data)
cerebro.addanalyzer(btanalyzers.TradeAnalyzer, _name='myTradeAnalyzer')
result = cerebro.run()
res = result[0].analyzers.myTradeAnalyzer.get_analysis()
この後、res.total.totalなどとすると下記のデータが取り出せる。
| カテゴリ | 項目 | 説明 |
|---|---|---|
| 合計 (Total) | total.total | 総取引数(開いている取引と閉じた取引の合計) |
| total.open | 開いている取引の数 | |
| total.closed | 閉じた取引の数 | |
| 連勝・連敗 (Streak) | streak.won.current | 現在の連勝数 |
| streak.won.longest | 最長連勝数 | |
| streak.lost.current | 現在の連敗数 | |
| streak.lost.longest | 最長連敗数 | |
| 損益 (PnL) | pnl.gross.total | 総損益(手数料を含まない) |
| pnl.gross.average | 平均損益(手数料を含まない) | |
| pnl.net.total | 総損益(手数料を含む) | |
| pnl.net.average | 平均損益(手数料を含む) | |
| 勝ち取引 (Won) | won.total | 勝ち取引の総数 |
| won.pnl.total | 勝ち取引からの総損益 | |
| won.pnl.average | 勝ち取引の平均損益 | |
| won.pnl.max | 勝ち取引からの最大損益 | |
| 負け取引 (Lost) | lost.total | 負け取引の総数 |
| lost.pnl.total | 負け取引からの総損益 | |
| lost.pnl.average | 負け取引の平均損益 | |
| lost.pnl.max | 負け取引からの最大損益 | |
| ロング取引 (Long) | long.total | ロング取引の総数 |
| long.pnl.total | ロング取引からの総損益 | |
| long.pnl.average | ロング取引の平均損益 | |
| long.won.total | 勝ちロング取引の総損益 | |
| long.won.average | 勝ちロング取引の平均損益 | |
| long.won.max | 勝ちロング取引からの最大損益 | |
| long.lost.total | 負けロング取引からの総損益 | |
| long.lost.average | 負けロング取引の平均損益 | |
| long.lost.max | 負けロング取引からの最大損益 | |
| long.won | 勝ちロング取引の数 | |
| long.lost | 負けロング取引の数 | |
| ショート取引 (Short) | short.total | ショート取引の総数 |
| short.pnl.total | ショート取引からの総損益 | |
| short.pnl.average | ショート取引の平均損益 | |
| short.won.total | 勝ちショート取引の総損益 | |
| short.won.average | 勝ちショート取引の平均損益 | |
| short.won.max | 勝ちショート取引からの最大損益 | |
| short.lost.total | 負けショート取引からの総損益 | |
| short.lost.average | 負けショート取引の平均損益 | |
| short.lost.max | 負けショート取引からの最大損益 | |
| short.won | 勝ちショート取引の数 | |
| short.lost | 負けショート取引の数 | |
| 取引期間 (Len) | len.total | 総取引期間 |
| len.average | 平均取引期間 | |
| len.max | 最長取引期間 | |
| len.min | 最短取引期間 | |
| len.won.total | 勝ち取引の総取引期間 | |
| len.won.average | 勝ち取引の平均取引期間 | |
| len.won.max | 勝ち取引の最長取引期間 | |
| len.won.min | 勝ち取引の最短取引期間 | |
| len.lost.total | 負け取引の総取引期間 | |
| len.lost.average | 負け取引の平均取引期間 | |
| len.lost.max | 負け取引の最長取引期間 | |
| len.lost.min | 負け取引の最短取引期間 | |
| len.long.total | ロング取引の総取引期間 | |
| len.long.average | ロング取引の平均取引期間 | |
| len.long.max | ロング取引の最長取引期間 | |
| len.long.min | ロング取引の最短取引期間 | |
| len.long.won.total | 勝ちロング取引の総取引期間 | |
| len.long.won.average | 勝ちロング取引の平均取引期間 | |
| len.long.won.max | 勝ちロング取引の最長取引期間 | |
| len.long.won.min | 勝ちロング取引の最短取引期間 | |
| len.long.lost.total | 負けロング取引の総取引期間 | |
| len.long.lost.average | 負けロング取引の平均取引期間 | |
| len.long.lost.max | 負けロング取引の最長取引期間 | |
| len.long.lost.min | 負けロング取引の最短取引期間 | |
| len.short.total | ショート取引の総取引期間 | |
| len.short.average | ショート取引の平均取引期間 | |
| len.short.max | ショート取引の最長取引期間 | |
| len.short.min | ショート取引の最短取引期間 | |
| len.short.won.total | 勝ちショート取引の総取引期間 | |
| len.short.won.average | 勝ちショート取引の平均取引期間 | |
| len.short.won.max | 勝ちショート取引の最長取引期間 | |
| len.short.won.min | 勝ちショート取引の最短取引期間 | |
| len.short.lost.total | 負けショート取引の総取引期間 | |
| len.short.lost.average | 負けショート取引の平均取引期間 | |
| len.short.lost.max | 負けショート取引の最長取引期間 | |
| len.short.lost.min | 負けショート取引の最短取引期間 |