TCPDashboard/tasks/tasks-refactor-indicator-calculation.md
Vasily.onl b49e39dcb4 Implement multi-timeframe support for indicators
- Enhanced the `UserIndicator` class to include an optional `timeframe` attribute for custom indicator timeframes.
- Updated the `get_indicator_data` method in `MarketDataIntegrator` to fetch and calculate indicators based on the specified timeframe, ensuring proper data alignment and handling.
- Modified the `ChartBuilder` to pass the correct DataFrame for plotting indicators with different timeframes.
- Added UI elements in the indicator modal for selecting timeframes, improving user experience.
- Updated relevant JSON templates to include the new `timeframe` field for all indicators.
- Refactored the `prepare_chart_data` function to ensure it returns a DataFrame with a `DatetimeIndex` for consistent calculations.

This commit enhances the flexibility and usability of the indicator system, allowing users to analyze data across various timeframes.
2025-06-06 15:06:17 +08:00

3.1 KiB

Relevant Files

  • data/common/indicators.py - This is the primary file to be refactored. The TechnicalIndicators class will be modified to be DataFrame-centric.
  • components/charts/utils.py - The prepare_chart_data function in this file needs to be corrected to ensure it properly creates and returns a DataFrame with a DatetimeIndex.
  • components/charts/data_integration.py - This file's get_indicator_data method will be simplified to pass the correctly prepared DataFrame to the calculation engine.
  • app_new.py - The main application file, which will be used to run the dashboard and perform end-to-end testing.

Notes

  • The goal of this refactoring is to create a more robust and maintainable data pipeline for indicator calculations, preventing recurring data type and index errors.
  • Pay close attention to ensuring that DataFrames have a consistent DatetimeIndex with proper timezone information throughout the pipeline.

Tasks

  • 1.0 Refactor TechnicalIndicators Class in data/common/indicators.py to be DataFrame-centric.

    • 1.1 Modify sma, ema, rsi, macd, and bollinger_bands methods to accept a pre-formatted DataFrame as their primary input, not a list of candles.
    • 1.2 Remove the redundant prepare_dataframe call from within each individual indicator method.
    • 1.3 Rename prepare_dataframe to _prepare_dataframe_from_list to signify its new role as a private helper for converting list-based data.
    • 1.4 Update the main calculate method to be the single point of data preparation, handling both DataFrame and list inputs.
  • 2.0 Correct DataFrame Preparation in components/charts/utils.py.

    • 2.1 Review the prepare_chart_data function to identify why the DatetimeIndex is being dropped.
    • 2.2 Modify the function to ensure it returns a DataFrame with the timestamp column correctly set as the index, without a reset_index() call at the end.
  • 3.0 Simplify Data Flow in components/charts/data_integration.py.

    • 3.1 In the get_indicator_data function, remove the workaround that converts the DataFrame to a list of dictionaries (to_dict('records')).
    • 3.2 Ensure the function correctly handles both main and custom timeframes, passing the appropriate DataFrame to the calculation engine.
    • 3.3 Verify that the final reindex operation works correctly with the consistent DataFrame structure.
  • 4.0 End-to-End Testing and Validation.

    • 4.1 Run the dashboard and test the indicator plotting functionality with both matching and custom timeframes.
    • 4.2 Verify that no new errors appear in the console during chart interaction.
  • 5.0 Update Indicators documentation to reflect the new DataFrame-centric approach.

    • 5.1 Review the documentation in the /docs directory related to indicators.
    • 5.2 Update the documentation to explain that the calculation engine now uses DataFrames.
    • 5.3 Provide clear examples of how to use the refactored TechnicalIndicators class.