績效報告¶
以 quantstats 為基礎,針對 BacktestResult 產生的績效報告。
Warning
full_report() 和比較報表預設會寫出 HTML 並開啟瀏覽器。
cjtrade.pkgs.analytics.evaluation.quantstats
¶
pkgs/analytics/evaluation/quantstats.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Thin facade around BacktestResult → quantstats.
Usage (as the CJTrade app and the oneshot backtester do):
from cjtrade.pkgs.analytics.evaluation.quantstats import BacktestReport
report = BacktestReport(result)
report.summary() # print key stats to stdout
report.full_report() # open quantstats HTML report
report.save_html("out.html")
report.save_csv("out.csv")
BacktestReport(result)
¶
Wraps a BacktestResult and exposes reporting helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
BacktestResult
|
Backtest output to report on. |
required |
Examples:
>>> report = BacktestReport(result)
>>> report.summary()
>>> report.save_html("report.html")
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
90 91 92 | |
returns
property
¶
Daily returns pd.Series (cached).
summary()
¶
Print key performance metrics to stdout (no external deps).
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
print_round_trips()
¶
Print every round-trip (entry→exit) in a compact table.
Uses the same FIFO matching as BacktestResult.compute_round_trips(). Format mirrors the full-simulation trade history for easy comparison.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
metrics()
¶
Return key metrics as a plain dict (suitable for logging/JSON).
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
full_report(path=None, open_browser=True, title=None)
¶
Save a full quantstats HTML tearsheet and optionally open it in the browser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Output HTML file path. Defaults to |
None
|
open_browser
|
bool
|
If True (default), open the saved file in the default browser. |
True
|
title
|
str
|
Custom title to display at the top of the HTML report (e.g., strategy name). If None, defaults to "Strategy Tearsheet". |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved HTML file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If quantstats is not installed. |
ValueError
|
If the equity curve is empty. |
Warnings
Writes (overwrites) an HTML file and, by default, opens a browser. With no fills, a minimal "no trades" page is written instead.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
basic_report()
¶
Print a compact quantstats report to stdout.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
336 337 338 339 340 | |
save_html(path)
¶
Save a full quantstats HTML report to path.
Warnings
Writes (overwrites) path; does not open a browser.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
421 422 423 424 425 426 427 428 | |
save_csv(path)
¶
Save daily_summary() as a CSV to path.
Warnings
Writes (overwrites) path.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
430 431 432 433 434 435 436 437 438 439 | |
quick_report(result, output='html', path=None)
¶
Generate a quantstats report from a BacktestResult in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
BacktestResult
|
Backtest output to report on. |
required |
output
|
'html' | 'full' | 'basic'
|
"html" → save HTML file (requires |
'html'
|
path
|
str
|
File path for HTML output (used when output="html"). Defaults to
|
None
|
Warnings
"html" and "full" write an HTML file in the working directory unless
path is given; "full" also opens it in the default browser.
Source code in src/cjtrade/pkgs/analytics/evaluation/quantstats.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
cjtrade.pkgs.analytics.evaluation.multi_strategy_report
¶
pkgs/analytics/evaluation/multi_strategy_report.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Multi-strategy comparison report generator.
Run multiple strategies on the same kbars and generate side-by-side comparison reports using quantstats.
Usage
from cjtrade.pkgs.analytics.evaluation.multi_strategy_report import MultiStrategyBacktestReport
from cjtrade.pkgs.strategy.dca_monthly import DCA_Monthly
from cjtrade.pkgs.strategy.example_strategy import BollingerStrategy
msr = MultiStrategyBacktestReport(kbars, initial_balance=1_000_000, params={...})
msr.add_strategy("DCA_Monthly", DCA_Monthly())
msr.add_strategy("BollingerBands", BollingerStrategy())
msr.run()
msr.compare_summary()
msr.save_comparison_html("comparison.html")
MultiStrategyBacktestReport(kbars, initial_balance=1000000.0, params=None)
¶
Run multiple strategies on the same kbars and generate side-by-side comparison report.
Attributes:
| Name | Type | Description |
|---|---|---|
kbars |
list
|
Pre-fetched list of Kbar objects (time-sorted, all symbols, each tagged). |
initial_balance |
float
|
Starting cash for each strategy. |
params |
dict
|
Shared parameters passed to all strategies. |
_strategies |
dict
|
Map of strategy_name → strategy instance. |
_results |
dict
|
Map of strategy_name → BacktestResult. |
_returns_series |
dict
|
Map of strategy_name → returns pd.Series. |
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
62 63 64 65 66 67 68 69 | |
add_strategy(name, strategy)
¶
Register a strategy to be tested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Display name for this strategy (e.g., "DCA_Monthly", "BollingerBands"). |
required |
strategy
|
BaseStrategy
|
Strategy instance to run. |
required |
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
71 72 73 74 75 76 77 78 79 80 81 82 | |
run()
¶
Run all registered strategies on the same kbars.
Each strategy gets its own BacktestEngine and a random session id.
Raises:
| Type | Description |
|---|---|
ValueError
|
If no strategies are registered. |
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
compare_summary()
¶
Print a comparison table of all strategies' key metrics to stdout.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If quantstats is not installed. |
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
save_comparison_html(path='comparison.html', open_browser=True)
¶
Save a multi-strategy HTML comparison report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Output HTML file path. Default: "comparison.html" |
'comparison.html'
|
open_browser
|
bool
|
If True (default), open the saved file in the default browser. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved HTML file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If quantstats is not installed. |
Warnings
Writes (overwrites) path and, by default, opens a browser.
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
compare_returns_overlay(path='compare_returns.html', open_browser=True)
¶
Generate overlaid returns chart using plotly for direct comparison.
Plots daily returns % for each strategy on the same chart so you can directly compare their volatility and drawdowns side-by-side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Output HTML file path. |
'compare_returns.html'
|
open_browser
|
bool
|
If True, open the file in the default browser. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved HTML file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If plotly is not installed. |
ValueError
|
If |
Warnings
Writes (overwrites) path and, by default, opens a browser.
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
compare_cumulative_equity_overlay(path='compare_equity.html', open_browser=True)
¶
Generate overlaid cumulative equity chart using plotly.
Plots the equity curve (in TWD, not normalized) for each strategy on the same chart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Output HTML file path. |
'compare_equity.html'
|
open_browser
|
bool
|
If True, open the file in the default browser. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved HTML file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If plotly is not installed. |
ValueError
|
If |
Warnings
Writes (overwrites) path and, by default, opens a browser.
Source code in src/cjtrade/pkgs/analytics/evaluation/multi_strategy_report.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |