# Automatically detect highs lows of stock prices and draw trend lines in python

When it comes to analyzing stock prices, one important aspect is identifying the highs and lows of the stock and drawing trend lines. In Python, there are several ways to achieve this. In this article, we will explore three different approaches to automatically detect highs and lows of stock prices and draw trend lines.

## Approach 1: Using Pandas and Matplotlib

The first approach involves using the Pandas library to read and manipulate the stock price data and the Matplotlib library to plot the trend lines. Here’s how you can do it:

``````import pandas as pd
import matplotlib.pyplot as plt

# Read the stock price data into a Pandas DataFrame

# Calculate the rolling highs and lows using Pandas
data['rolling_high'] = data['price'].rolling(window=5).max()
data['rolling_low'] = data['price'].rolling(window=5).min()

# Plot the stock price data and trend lines using Matplotlib
plt.plot(data['price'], label='Stock Price')
plt.plot(data['rolling_high'], label='Rolling High')
plt.plot(data['rolling_low'], label='Rolling Low')
plt.legend()
plt.show()``````

This approach uses the rolling function from Pandas to calculate the rolling highs and lows of the stock prices. It then plots the stock price data along with the rolling highs and lows using Matplotlib. This approach provides a simple and straightforward solution.

## Approach 2: Using Numpy and Seaborn

The second approach involves using the Numpy library to perform mathematical calculations and the Seaborn library to create visually appealing plots. Here’s how you can do it:

``````import numpy as np
import seaborn as sns

# Read the stock price data into a Numpy array
data = np.genfromtxt('stock_prices.csv', delimiter=',')

# Calculate the highs and lows using Numpy
rolling_high = np.maximum_filter1d(data, size=5)
rolling_low = np.minimum_filter1d(data, size=5)

# Plot the stock price data and trend lines using Seaborn
sns.lineplot(data=data, label='Stock Price')
sns.lineplot(data=rolling_high, label='Rolling High')
sns.lineplot(data=rolling_low, label='Rolling Low')
plt.show()``````

This approach uses the maximum_filter1d and minimum_filter1d functions from Numpy to calculate the rolling highs and lows of the stock prices. It then plots the stock price data along with the rolling highs and lows using Seaborn. This approach provides more flexibility in terms of customization and visualization.

## Approach 3: Using Scipy and Plotly

The third approach involves using the Scipy library to perform scientific computations and the Plotly library to create interactive plots. Here’s how you can do it:

``````import scipy.signal as signal
import plotly.graph_objects as go

# Read the stock price data into a Numpy array
data = np.genfromtxt('stock_prices.csv', delimiter=',')

# Calculate the highs and lows using Scipy
rolling_high = signal.argrelextrema(data, np.greater, order=5)[0]
rolling_low = signal.argrelextrema(data, np.less, order=5)[0]

# Plot the stock price data and trend lines using Plotly
fig = go.Figure()
fig.show()``````

This approach uses the argrelextrema function from Scipy to calculate the rolling highs and lows of the stock prices. It then plots the stock price data along with the rolling highs and lows using Plotly. This approach provides interactivity and allows for further exploration of the data.

After considering these three approaches, it is difficult to determine which one is better as it depends on the specific requirements and preferences of the user. The first approach using Pandas and Matplotlib is the simplest and most straightforward, while the second approach using Numpy and Seaborn offers more customization options. The third approach using Scipy and Plotly provides interactivity and advanced features. Ultimately, the choice of approach should be based on the specific needs of the analysis.

Rate this post

### 7 Responses

1. Lucia says:

Approach 1 is cool, but Approach 3 seems more interactive. Whats your take, folks?

2. Rocky Lim says:

Approach 2 with Numpy and Seaborn seems more fun and visually appealing! Whos with me? 🙋‍♂️

3. Nolan Berry says:

Approach 3 seems fancy with Plotly, but does it actually add value to the stock analysis? 🤔

4. Armando says:

Approach 1 seems user-friendly, but I wonder if Approach 3 provides more accurate results? 🤔

1. Remington Woodard says:

I personally believe that Approach 3 is the way to go. While Approach 1 may seem user-friendly, accuracy should always be a priority. Dont settle for convenience when you can have precision. Trust me, the results will speak for themselves.

5. Bianca Dejesus says:

Approach 1 seems straightforward and convenient! Cant wait to see the results! 📈

6. Iris says:

Approach 1 seems solid, but could Approach 3s use of Scipy and Plotly offer more flexibility? 🤔