Dear Alpaca community members,
I would like to share with you an interesting Python script that leverages the Alpaca API and the power of machine learning to determine the optimal support and resistance levels for a given stock. The code repository is public and available on GitHub.
Purpose of the Script:
The primary purpose of this script is to analyze stock price data, identify the peaks and troughs, and determine the optimal support and resistance levels by clustering these peaks and troughs. This information is then used to visualize the stock data, highlighting the support and resistance levels over time. By identifying these levels, traders can make more informed decisions when analyzing the price movement of stocks.
The script is written in Python and utilizes the following libraries:
- Alpaca Trade API
- Pandas Market Calendars
The script follows these steps:
- Retrieve historical stock data from the Alpaca API.
- Calculate the peaks and troughs of the stock data.
- Determine the optimal number of clusters for peaks and troughs using either the Agglomerative Clustering or K-Means clustering algorithms, based on the silhouette scores.
- Cluster the peaks and troughs using the optimal number of clusters.
- Identify the last peak and trough and their corresponding cluster group.
- Calculate the maximum high of the last peak cluster group and the minimum low of the last trough cluster group.
- Plot the stock data with the support and resistance levels, as well as other relevant information.
Unique and Innovative Features:
The script employs a machine learning approach to determine the optimal support and resistance levels. This innovative approach sets it apart from traditional methods that are based on heuristic or manual analysis. The use of clustering algorithms allows the script to automatically find the most suitable number of clusters, providing a more accurate and data-driven representation of the stock price movement.
In addition, the script allows users to customize various parameters, such as the stock symbol, the number of trading days, the minimum number of clusters, and the choice between Agglomerative Clustering and K-Means clustering algorithms. This flexibility allows users to tailor the analysis according to their specific needs and preferences.
Please feel free to explore this script and provide any feedback or suggestions for improvements. Let’s continue to work together to build a thriving Alpaca community and develop innovative solutions for stock trading and investment.
Github repository for stock_support_resistance_analysis