/ docs / src / plotting.md
plotting.md
  1  # Plotting and Visualization
  2  
  3  The Plotting module utilizes [Makie](https://docs.makie.org/stable/) for creating visualizations. It is important to note that graphical backends are not automatically included with the module and must be installed manually:
  4  
  5  
  6  Planar enhances Makie with specialized plotting recipes for financial analysis:
  7  
  8  - [OHLCV](guides/../guides/data-management.md#ohlcv-data) (Open-High-Low-Close Volume) charts
  9  - Detailed trade history visualization
 10  - Summarized trade history with volume and balance insights
 11  - Custom indicator overlays and channels
 12  - Multi-asset portfolio visualization
 13  - Performance and [optimization](optimization.md) result analysis
 14  
 15  !!! info "Interactive Features"
 16      Interactive backends, such as GLMakie and WGLMakie, allow for dynamic plots that can be clicked-and-dragged or zoomed. Additionally, interactive elements like tooltips are available on hover for candlesticks, trades, and balance charts.
 17  
 18  ## Backend Selection and Setup
 19  
 20  ### GLMakie (Desktop Applications)
 21  
 22  GLMakie is the recommended backend for desktop applications and provides the best performance for interactive plots:
 23  
 24  
 25  ### WGLMakie (Web Applications)
 26  
 27  WGLMakie is ideal for web-based applications and Jupyter notebooks:
 28  
 29  
 30  ### CairoMakie (Static Images)
 31  
 32  For generating static images or when interactive features are not needed:
 33  
 34  For generating static images, use CairoMakie and save with `save("chart.png", figure)`.
 35  
 36  ## [OHLCV](guides/../guides/data-management.md#ohlcv-data) Charts
 37  
 38  [OHLCV data](guides/../guides/data-management.md#ohlcv-data) is represented using candlesticks to indicate price movement, with red signifying a price decrease and green an increase. Volume is depicted as bars in the background of the chart.
 39  
 40  ### Basic [OHLCV](guides/../guides/data-management.md#ohlcv-data) Chart
 41  
 42  ![OHLCV Plot](assets/ohlcv.gif)
 43  
 44  ### Customizing OHLCV Charts
 45  
 46  You can customize various aspects of OHLCV charts:
 47  
 48  Customize charts with parameters like `title`, `up_color`, `down_color`, `volume_color`, and `show_volume`.
 49  
 50  ### Time Range Selection
 51  
 52  Display specific time periods:
 53  
 54  
 55  ### Multiple Timeframes
 56  
 57  Compare different [timeframes](guides/../guides/data-management.md#timeframes) on the same chart:
 58  
 59  Create multi-timeframe layouts using `Figure()` and `Axis()` with `Plotting.ohlcv!()` for each subplot.
 60  
 61  ## Trading History Visualization
 62  
 63  The history of trades is illustrated using triangles, with upwards pointing triangles for buy transactions and downwards for sell transactions.
 64  
 65  ### Basic Trade Visualization
 66  
 67  ![TradesTicks](assets/tradesticks.jpg)
 68  
 69  ### Advanced Trade Analysis
 70  
 71  
 72  ### Trade Performance Overlay
 73  
 74  Combine trade markers with performance metrics:
 75  
 76  Add balance lines and statistics with `Plotting.balance_line!()` and `Plotting.add_stats_table!()`.
 77  
 78  ### Multi-Asset Trade Comparison
 79  
 80  Compare multiple assets using loops with `Plotting.tradesticks!()` in subplots.
 81  
 82  ## Aggregated Trade History for a Single Asset
 83  
 84  The `balloons` function provides an aggregated view of trade density within a specified timeframe. Each circle's size correlates with the trade volume—larger circles denote higher volume. Positive volume timeframes are labeled as _sells_ and negative ones as _buys_. Circle opacity reflects the number of trades within the timeframe. The accompanying balance chart indicates the quote currency held: the orange band represents actual cash (`Strategy.cash`), while the blue band represents the value of held assets in quote currency (`AssetInstance.cash * price`).
 85  
 86  ### Basic Balloons Chart
 87  
 88  ![Balloons](assets/balloons.jpg)
 89  
 90  ### Customizing Balloons Visualization
 91  
 92  Customize balloons with parameters like `tf`, `min_radius`, `max_radius`, `buy_color`, and `sell_color`.
 93  
 94  ### Advanced Balloons Analysis
 95  
 96  Add volume histograms and frequency overlays with `show_volume_histogram` and `Plotting.frequency_heatmap!()`.
 97  
 98  ## Aggregated Trade History for Multiple Assets
 99  
100  For a comprehensive analysis, aggregated trade history can plot multiple assets. Balloons are overlaid on the price line of each asset, with the same opacity and radius settings as single asset plots. To benchmark against a specific asset, pass a symbol or a dataframe using the `benchmark` keyword argument.
101  
102  ### Basic Multi-Asset Balloons
103  
104  ![Balloons Multi](assets/balloons-multi.jpg)
105  
106  ### Portfolio-Wide Analysis
107  
108  Use `benchmark`, `normalize_prices`, and `layout` parameters for multi-asset analysis.
109  
110  ### Advanced Multi-Asset Visualization
111  
112  Create portfolio heatmaps with `Plotting.portfolio_heatmap()` and add correlation analysis.
113  
114  ### Sector and Category Analysis
115  
116  Group assets by categories and plot with different colors using loops and subplots.
117  
118  ## Custom Indicators
119  
120  Custom indicators enhance chart analysis and can be integrated into plots. Planar provides several functions for adding [technical indicators](guides/../guides/strategy-development.md#technical-indicators) to your charts.
121  
122  ### Line Indicators
123  
124  Moving averages and other line-based indicators can be added using the `line_indicator` function:
125  
126  ![Line Indicator](assets/line-indicator.jpg)
127  
128  ### Advanced Line Indicators
129  
130  
131  ### Channel Indicators
132  
133  Channels or envelopes can be visualized using the `channel_indicator` function. This tool is useful for identifying trends and potential breakouts:
134  
135  ![Channel Indicator](assets/channel-indicator.jpg)
136  
137  ### Advanced Channel Indicators
138  
139  
140  ### Oscillator Indicators
141  
142  
143  ### Custom Indicator Development
144  
145  
146  ## Styling and Customization
147  
148  ### Color Schemes and Themes
149  
150  
151  ### Chart Layout and Sizing
152  
153  
154  ### Export and Saving Options
155  
156  ## Pe
157  rformance Analysis Visualization
158  
159  ### Strategy Performance Charts
160  
161  
162  ### Risk Metrics Visualization
163  
164  Create risk analysis charts using functions like `Plotting.rolling_sharpe()`, `Plotting.rolling_volatility()`, and `Plotting.calculate_var()`.
165  
166  ## Optimization Result Visualization
167  
168  ### Parameter Optimization Heatmaps
169  
170  
171  ### 3D Optimization Surface
172  
173  Use `Axis3()` and `surface!()` for 3D optimization visualization.
174  
175  ### Optimization Progress Tracking
176  
177  Track optimization progress using `lines!()` and `scatter!()` to show best values and parameter evolution.
178  
179  ## Large Dataset Visualization
180  
181  ### Large Dataset Handling
182  
183  For large datasets, use `Plotting.load_chunks()` for progressive loading or `Plotting.downsample_ohlcv()` for memory efficiency.
184  
185  ## Interactive Features and Widgets
186  
187  ### Interactive Features
188  
189  GLMakie supports interactive features like `Slider()`, `Button()`, and `lift()` for reactive plotting.
190  
191  ## Troubleshooting and Performance
192  
193  ### Common Plotting Issues
194  
195  #### Backend Problems
196  
197  
198  #### Performance Tips
199  
200  Monitor memory usage with `Base.gc_live_bytes()` and consider downsampling large datasets for better performance.e
201      GLMakie.set_theme!(
202          SSAO = (enabled = false,),
203          FXAA = (enabled = false,)
204      )
205      
206      # Use lower quality for interactive plots
207      GLMakie.set_theme!(
208          resolution = (1200, 800),  # Lower resolution
209          px_per_unit = 1            # Lower pixel density
210      )
211      
212      println("Performance optimizations applied")
213  end
214  
215  # Apply optimizations
216  optimize_plot_performance()
217  ```
218  
219  ### Best Practices
220  
221  1. **Data Preparation**: Clean and validate data before plotting
222  2. **Memory Management**: Use downsampling for large datasets
223  3. **Backend Selection**: Choose appropriate backend for your use case
224  4. **Color Accessibility**: Use colorblind-friendly palettes
225  5. **Performance**: Limit the number of data points for interactive plots
226  6. **Export Quality**: Use high DPI settings for publication-quality images
227  
228  dvanced Backend Configuration
229  
230  ### GLMakie Advanced Setup
231  
232  GLMakie provides the best performance for desktop applications with full GPU acceleration support.
233  
234  #### Installation and Configuration
235  
236  
237  #### Multi-Monitor Support
238  
239  ```julia
240  # Activate PlanarInteractive project for plotting features
241  import Pkg
242  Pkg.activate("PlanarInteractive")
243  
244  try
245      using GLMakie
246      
247      # Configure for multiple monitors
248      function setup_multi_monitor()
249          monitors = GLMakie.GLFW.GetMonitors()
250          println("Available monitors: $(length(monitors))")
251          
252          for (i, monitor) in enumerate(monitors)
253              name = GLMakie.GLFW.GetMonitorName(monitor)
254              mode = GLMakie.GLFW.GetVideoMode(monitor)
255              println("Monitor $i: $name ($(mode.width)x$(mode.height))")
256          end
257      end
258      
259      setup_multi_monitor()
260  catch e
261      @warn "GLMakie not available: $e"
262  end
263      resolution = (1200, 800),
264  
265  #### Jupyter Notebook Integration
266  
267  
268  ### Memory Usage Optimization
269  
270  
271  
272  ## See Also
273  
274  - **[Exchanges](exchanges.md)** - Exchange integration and configuration
275  - **[Config](config.md)** - Exchange integration and configuration
276  - **[Overview](troubleshooting/index.md)** - Troubleshooting: Troubleshooting and problem resolution
277  - **[Optimization](optimization.md)** - Performance optimization techniques
278  - **[Performance Issues](troubleshooting/performance-issues.md)** - Troubleshooting: Performance optimization techniques
279  - **[Data Management](guides/../guides/data-management.md)** - Guide: Data handling and management
280  
281  ## Advanced Interactive Features
282  
283  ### Custom Interaction Handlers
284  
285  
286  ### Real-time Data Streaming
287  
288  
289  ### Multi-Window Management
290  
291  
292  This completes the advanced plotting and backend documentation. The enhanced plotting.md file now includes:
293  
294  1. **Comprehensive OHLCV chart examples** with customization options
295  2. **Advanced trade visualization** with multiple analysis methods
296  3. **Enhanced balloons functionality** for single and multi-asset analysis
297  4. **Extensive custom indicator examples** including line, channel, volume, and oscillator indicators
298  5. **Styling and customization** options with themes and color schemes
299  6. **Performance analysis visualization** for strategy evaluation
300  7. **Optimization result visualization** with heatmaps and 3D surfaces
301  8. **Large dataset handling** with progressive loading and memory optimization
302  9. **Interactive features** with widgets and real-time updates
303  10. **Advanced backend configuration** for GLMakie, WGLMakie, and CairoMakie
304  11. **Performance optimization** and [troubleshooting](troubleshooting/index.md) guidance
305  12. **Multi-window management** for complex analysis workflows
306  
307  <function_calls>
308  <invoke name="taskStatus">
309  <parameter name="taskFilePath">.kiro/specs/docs-improvement/tasks.md