This application is designed to analyze and simulate the performance of a stock portfolio under market stress conditions. It allows users to input stock tickers, set the number of stocks for each ticker, and apply a stress factor to simulate market drops. The application generates a comprehensive report that includes portfolio summaries and visualizations. Only usable for stocks listed on National Stock Exchange(NSE), India.
- Portfolio Input: Users can enter stock tickers and specify the number of stocks for each ticker.
- Market Stress Simulation: Apply a stress factor to simulate market drops and observe the impact on portfolio value.
- Portfolio Rebalancing: Automatically rebalance the portfolio based on target weights after applying market stress.
- Visualizations: Interactive line plots to visualize portfolio values over time.
- PDF Report Generation: Generate and download a PDF report summarizing the portfolio analysis.
- Streamlit: For building the web application interface.
- Pandas: For data manipulation and analysis.
- NumPy: For numerical operations.
- Seaborn & Matplotlib: For data visualization.
- Plotly: For interactive plotting.
- ReportLab: For generating PDF reports.
- NSEPython: For fetching historical stock data (simulated in this code).
To run this application, ensure you have Python installed along with the required libraries. You can install the necessary packages using pip in the requirements.txt file (the file needs to be in the same directory as the source code):
!pip install -r requirements.txt
Or you can just directly run the following command in the Terminal/Command Prompt:
pip install streamlit pandas numpy seaborn matplotlib plotly reportlab nsepython
- Clone the repository or download the code files.
- Navigate to the directory containing frame2.py.
- Run the Streamlit application using the following command:
streamlit run frame2.py
- Open the provided local URL in your web browser.
- Input the stock tickers, set the number of stocks, and adjust the stress factor as desired.
- Click on "Run Simulation" to see the results.
- Download the PDF report for a detailed analysis of your portfolio.
- Generates a PDF report summarizing the portfolio analysis.
generate_pdf_report(portfolio_data, start_date, end_date)
- Calculates custom weights based on the number of stocks for each ticker.
calculate_custom_weights(tickers, num_stocks)
- Fetches historical stock data for the specified tickers and date range.
fetch_historical_data(tickers, start_date, end_date)
- Calculates initial equal weights for the portfolio.
calculate_initial_weights(tickers)
- Applies a market stress factor to the portfolio prices.
apply_market_stress(prices, stress_factor)
- Calculates the total value of the portfolio based on prices and weights.
calculate_portfolio_value(prices, weights)
- Rebalances the portfolio towards target weights based on price changes.
rebalance_portfolio(prices, target_weights, initial_weights, trade_threshold)
- Displays a summary of the portfolio data in the Streamlit app.
display_portfolio_summary(portfolio_data, title)
###Note: This project is deployed publicly as a demo tester here: http://portfolio-rebalancing.streamlit.app/ . Only for educational purposes, not tested for commercial use.