Deployment: https://stocks-dashboard-qz33.onrender.com/
The primary goal of this application is to provide users with a simple and interactive tool to visualize historical stock data, including technical indicators like Exponential Moving Averages (EMAs).
- Allow users to specify a ticker symbol (e.g., MSFT, AAPL, BTC-USD, etc).
- Allow users to select a historical time period for the data (e.g., 1 year, 5 years).
- Allow users to configure the window sizes for short-term and long-term EMAs.
- Display a candlestick chart of the stock's price history.
- Overlay the short-term and long-term EMAs on the chart.
- Volatility option.
- Anyone interested in basic technical analysis of stock trends.
The application follows a classic client-server architecture.
- Technology: Single-Page Application (SPA) built with React.
- Core Libraries:
axios: For making HTTP requests to the backend API.plotly.js&react-plotly.js: For rendering interactive charts.
- Responsibility: Renders the user interface, captures user input, communicates with the backend API, and displays the returned chart data.
- Technology: Python web server with Flask.
- Core Libraries:
yfinance: To fetch historical stock data from Yahoo Finance.pandas: For data manipulation and calculating technical indicators.plotly: To create the chart figure object.gunicorn: As the WSGI server for running the application in production (indicated by theProcfile).
- Responsibility: Exposes a single API endpoint to receive user parameters, fetches the relevant data, performs calculations, generates a chart object, and returns it to the frontend. It also serves the static files for the built React application.
The backend exposes a single endpoint to generate and retrieve stock chart data.
-
Endpoint:
/get_stock_data -
Method:
POST -
Request Body (JSON):
{ "stock_symbol": "string", "selected_period": "string", "short_window": "integer", "long_window": "integer", "show_volatility": "bool", "volatility_window": "integer" }stock_symbol: The ticker symbol for the stock (e.g.,"MSFT").selected_period: A string representing the time period, compatible withyfinance(e.g.,"1y","5d","max").short_window: An integer for the short-term EMA window size.long_window: An integer for the long-term EMA window size.show_volatility: A boolean for option to display volatility.volatility_window: An integer for the volatility window size.
-
Response Body (JSON):
- On Success (200 OK): The response is a JSON object representing a Plotly figure. This object can be directly passed to the
react-plotly.jscomponent on the frontend.{ "data": [ { "type": "candlestick", "name": "Candlestick", "x": ["2023-01-01", ...], "open": [150.00, ...], "high": [152.50, ...], "low": [149.00, ...], "close": [151.75, ...] }, { "type": "scatter", "mode": "lines", "name": "10-EWMA", "x": ["2023-01-01", ...], "y": [150.50, ...] }, ... ], "layout": { "title": "MSFT Candlestick Plot with Exponential Moving Average Crossover", "xaxis_title": "Date", "yaxis_title": "Price in $", ... } }
- On Success (200 OK): The response is a JSON object representing a Plotly figure. This object can be directly passed to the
- Backend: The Flask app defines the
/get_stock_dataroute. Inside this route, it retrieves the JSON payload, calls theyfinancelibrary to get data, uses pandas.ewm().mean()to calculate the moving averages, and then constructs ago.Figureobject from theplotlylibrary. This figure is then converted to a JSON-serializable dictionary and returned. - Frontend: The main
App.jscomponent uses React hooks (useState) to manage the state of the input fields. When the "Refresh Visual" button is clicked, thehandleSubmitfunction makes an asyncaxios.postcall to the backend. The response data is then stored in thegraphDatastate variable, triggering a re-render of the<Plot>component with the new data.
- The presence of a
Procfilewith agunicorncommand indicates that the application is designed for deployment on a PaaS. - The deployment workflow is:
- Install frontend dependencies (
npm install). - Build the static React application (
npm run build). This creates an optimized set of files infrontend/build. - Install backend dependencies (
pip install -r requirements.txt). - Start the Gunicorn server, which runs the Flask app. The Flask app is configured to serve the static files from
frontend/buildas well as handle API requests.
- Install frontend dependencies (
- The application is dependent on the external Yahoo Finance API. Any downtime or changes to this API will affect the application.
- The application is stateless, simplifying scaling and management.
- Python 3.x
- Node.js and npm
-
Create a virtual environment:
python3 -m venv venv source venv/bin/activate -
Install dependencies:
pip install -r backend/requirements.txt
- Install dependencies:
npm install --prefix frontend
- Start the server:
The backend server will start on
python3 backend/app.py
http://localhost:10000.
- Start the development server:
The frontend development server will start on
npm start --prefix frontend
http://localhost:3000and will proxy API requests to the backend.
-
Refresh Visualization: Click the "Refresh Visual" button to update the graph with the selected parameters.
-
Select a Stock and Time Period: Use the dropdown menus and input fields to choose a stock symbol (e.g., AAPL for Apple Inc.) and a time period for analysis (e.g., 1 year).
-
Specify EWMA Windows: Input the number of days for the short-term and long-term EWMA windows to tailor the analysis to your trading strategy.
-
Analyze Volatility: Optionally, enable the volatility plot and set the rolling window to visualize the stock's volatility over time.
-
Analyze Signals: Observe the Up (green triangles) and Down (red triangles) signals on the stock chart based on EWMA crossovers.
The Exponential Weighted Moving Average (EWMA) is a type of moving average that places a greater weight and significance on the most recent data points. The application uses EWMA crossover points as signals for potential buying or selling opportunities:
-
Upward Trend signal:
- Generated when the short-term EWMA crosses above the long-term EWMA, indicating an upward trend.
-
Downward Trend signal:
- Generated when the short-term EWMA crosses below the long-term EWMA, indicating a downward trend.
This strategy helps traders identify moments when a stock's momentum is changing, potentially offering opportunities for profitable trades.
EWMA(t) = α * C(t) + (1 - α) * EWMA(t - 1)
Where:
EWMA(t) = Exponential Weighted Moving Average at time t
C(t) = Closing price at time t
EWMA(t - 1) = Exponential Weighted Moving Average at time t-1
α = 2 / (N + 1)
N = number of days in the moving average period.
