Candlestick Patterns App
Relevant source files
The following files were used as context for generating this wiki page:
The Candlestick Patterns App is a specialized visualization and backtesting dashboard built using the Dash framework. It enables users to interactively explore market data, detect specific candlestick patterns using TA-Lib, and evaluate the performance of trading strategies derived from these patterns through the vectorbt backtesting engine. The application provides a responsive UI for managing complex simulation settings and visualizing financial metrics.
Sources: apps/candlestick-patterns/README.md:1-13, apps/candlestick-patterns/app.py:46-56
Architecture and Data Flow
The application follows a reactive architecture where user inputs in the sidebar trigger a series of callbacks. These callbacks handle data fetching, signal generation, portfolio simulation, and finally, the rendering of interactive graphs and tables.
System Flow
The diagram below illustrates the sequence of operations from data ingestion to performance reporting.
The application uses a hidden DIV signal mechanism (data_signal, index_signal) to share data between callbacks efficiently and avoid redundant API calls to Yahoo! Finance.
Sources: apps/candlestick-patterns/app.py:380-405, apps/candlestick-patterns/app.py:640-660
Key Components
Data Management
The application fetches OHLCV (Open, High, Low, Close, Volume) data via the yfinance library. This data is cached using flask_caching to optimize performance across different sessions or repeated requests.
| Function | Purpose | Source |
|---|---|---|
fetch_data | Downloads historical data from yfinance with auto-adjust options. | app.py:388-399 |
update_data | Updates hidden signals when the symbol or period changes. | app.py:402-414 |
Pattern Recognition
Pattern detection is handled by the TA-Lib abstract.Function interface. Users can select from a wide range of standard patterns (e.g., CDLHAMMER, CDL3WHITESOLDIERS) or specify custom entry/exit dates.
| Parameter | Type | Description |
|---|---|---|
entry_patterns | List | Patterns triggering a bullish (entry) signal. |
exit_patterns | List | Patterns triggering a bearish (exit) signal. |
candle_settings | DataTable | Global TA-Lib settings for body/shadow ratios. |
Sources: apps/candlestick-patterns/app.py:100-116, apps/candlestick-patterns/app.py:560-590
Portfolio Simulation
The core logic resides in simulate_portfolio, which utilizes vectorbt.Portfolio.from_signals. It compares three distinct entities:
- Main Strategy: Signals generated by selected candlestick patterns.
- Buy & Hold: A benchmark strategy that buys at the start and holds until the end.
- Random Strategy: A collection of strategies generated either by shuffling main signals or through random probability, used to determine the statistical significance (Z-Score) of the strategy.
Sources: apps/candlestick-patterns/app.py:640-754
Visualization and Reporting
The application leverages Plotly for high-fidelity financial charting.
Subplot Configurations
Users can toggle various subplots within the portfolio view:
- Orders: Visualization of buy and sell markers.
- Trade PnL: Profit and loss for individual closed trades.
- Cumulative Returns: Comparison of the strategy equity curve against the benchmark.
Sources: apps/candlestick-patterns/app.py:157-163, apps/candlestick-patterns/app.py:801-815
Performance Metrics
Statistical analysis is performed using the stats() method from the StatsBuilderMixin.
The app displays metrics such as Total Return, Sharpe Ratio, and Win Rate in a dash_table.DataTable, including a comparison between the strategy and the median of random runs.
Sources: vectorbt/generic/stats_builder.py:32-60, apps/candlestick-patterns/app.py:828-860
Configuration Options
The application supports extensive customization via its "Settings" sidebar.
| Category | Option | Default | Description |
|---|---|---|---|
| Data | Symbol | BTC-USD | The asset ticker for analysis. |
| Data | Period | 1y | Time window for the backtest. |
| Simulation | Fees | 0.1% | Transaction costs per trade. |
| Simulation | Slippage | 5.0% | Percentage of (High-Open) price variation. |
| Simulation | Conflict Mode | Opposite | Strategy for handling simultaneous entry/exit. |
| Random | N Random Strat | 50 | Number of random portfolios for Z-Score calculation. |
Sources: apps/candlestick-patterns/app.py:86-125
Style and Layout
The interface uses a dual-layer CSS approach. default.css provides a foundation based on the Skeleton framework (grid, typography, base styles), while style.css applies a modern UI theme using Dash Bootstrap Components.
- Responsive Grid: Implements a 12-column system for cross-device compatibility.
- Theming: Uses CSS variables for consistent primary colors (
#2563EB) and surface elevations. - Card Containers: Visual elements are wrapped in
pretty-containerclasses with specific shadow and border properties.
Sources: apps/candlestick-patterns/assets/default.css:40-100, apps/candlestick-patterns/assets/style.css:1-40
Conclusion
The Candlestick Patterns App serves as an interactive bridge between technical analysis and quantitative backtesting. By integrating TA-Lib for pattern recognition and vectorbt for high-performance simulation, it allows users to validate traditional candlestick theories against historical data and statistical benchmarks within a single responsive interface.
Sources: apps/candlestick-patterns/README.md:1-12, apps/candlestick-patterns/app.py:900-901