Innovative Script for Stock Support and Resistance Level Analysis

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.

Technical Summary:

The script is written in Python and utilizes the following libraries:

  • Alpaca Trade API
  • NumPy
  • Pandas
  • Matplotlib
  • Scikit-learn
  • Pandas Market Calendars
  • SciPy

The script follows these steps:

  1. Retrieve historical stock data from the Alpaca API.
  2. Calculate the peaks and troughs of the stock data.
  3. 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.
  4. Cluster the peaks and troughs using the optimal number of clusters.
  5. Identify the last peak and trough and their corresponding cluster group.
  6. Calculate the maximum high of the last peak cluster group and the minimum low of the last trough cluster group.
  7. 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

Best regards,

Joe O.


Thanks for posting this. I really do like your script and your approach.

However, since you asked for feedback, what I don’t like is the hyperbole in your write-up, such as “unique” and “innovative”. If you take quick look at github there are several older repositories that take the same approach to finding support and resistance. For example:

And there are many, many, many more. So please do your research and tone down the hype a bit next time. Or at least give credit to those that came before you. Regardless, kudos for a nice piece of code.

1 Like

Dear arctodus,

Thank you for taking the time to provide feedback on my script. I appreciate your constructive criticism, and I understand your point about the hyperbole in my write-up. I will take your feedback into account and use more accurate language in future projects.

I’m grateful for the links to the other GitHub repositories you provided. While I did do some research before developing my script, I didn’t come across these specific repositories. I’ll make sure to give credit to those who have contributed to this area in the future.

Thank you again for your input, and I’m glad you found the script useful despite the overstatements. I’m always open to feedback and ideas, so feel free to reach out if you have any further thoughts.

Best regards,

Joe O.