This project is primarily a Python application that performs financial data analysis, specifically focusing on volume profile and trading range identification. It has been extended with a FastAPI web server to expose its analysis capabilities as a remote API.
The core functionality of this project is provided by the api02.py script. This script analyzes volume profile data from JSON files to:
- Identify the Point of Control (POC).
- Detect Low-Volume Nodes (LVNs).
- Define and highlight the primary trading range (Value Area).
- Generate a visual representation of the volume profile and trading range as a PDF chart.
The script has been enhanced to function in two primary modes:
- Command-Line Interface (CLI): Users can execute the script directly to process a specified JSON file and output a PDF.
- FastAPI Web Server: When run with the
--apiflag, the script starts a web server that exposes a/getpdfendpoint. This endpoint acceptsfile_pathandoutput_pdf_pathas query parameters, allowing remote clients to trigger the analysis and receive a generated PDF.
To run the analysis directly from the command line, execute the script with the input JSON file path and the desired output PDF file path as arguments:
python analyze_volume_profile_phase2.py <input_json_file_path> <output_pdf_file_path>Example:
python analyze_volume_profile_phase2.py MGC1.json MGC1_volprofile.pdfTo start the API server, run the script with the --api flag:
python analyze_volume_profile_phase2.py --apiBy default, the server will start on http://127.0.0.1:8000. You can specify a different host and port using --host and --port arguments (e.g., python analyze_volume_profile_phase2.py --api --host 0.0.0.0 --port 8080).
Once the API server is running, you can interact with the /getpdf endpoint using HTTP GET requests. The file_path and output_pdf_path parameters must be provided as query parameters in the URL.
Example using curl:
curl "http://127.0.0.1:8000/getpdf?file_path=MGC1.json&output_pdf_path=MGC1_api_volprofile.pdf"This command will trigger the volume profile analysis for MGC1.json and save the resulting PDF to MGC1_api_volprofile.pdf on the server's filesystem.
- Language: Python 3.
- Core Libraries:
pandas: For data manipulation and analysis.matplotlib: For generating plots and saving them as PDFs. TheAggbackend is used, which is suitable for non-interactive environments like servers.scipy: For scientific computing, specifically forfind_peaksin volume analysis.numpy: For numerical operations.
- Web Framework:
fastapi: For building the modern, fast, web API.uvicorn: An ASGI server for running FastAPI applications.
- Code Structure:
- The main analysis logic is encapsulated within the
analyze_volume_profilefunction. - The script is designed to be executable as a standalone CLI tool or as a FastAPI web server using command-line arguments (
--api).
- The main analysis logic is encapsulated within the
- Error Handling:
- The
analyze_volume_profilefunction includes specifictry-exceptblocks forFileNotFoundError,json.JSONDecodeError, and general exceptions. - The FastAPI endpoint includes error handling to catch exceptions during function execution and return JSON error responses.
- The
- Dependencies:
- The
package.jsonandpackage-lock.jsonfiles are present but empty. This suggests that Node.js/npm is not being used for dependency management for this Python project. - A
requirements.txtfile is expected for managing Python dependencies.
- The
- Logging: Implement more comprehensive logging for the FastAPI server to track requests, errors, and application events effectively.
- Empty
package.json: Investigate the purpose and origin of the emptypackage.jsonandpackage-lock.jsonfiles. They might be remnants of a previous setup or intended for a specific, unconfigured tool. - PDF Serving: Currently, the API saves the PDF to a file path. Consider implementing functionality to serve the generated PDF directly as a response or provide a link to it.
- Configuration Management: For production use, consider externalizing configuration (like host, port, default paths) rather than hardcoding them in the script.
- Input Validation: Add more robust input validation for
file_pathandoutput_pdf_pathin the API endpoint to ensure they are valid and safe paths.