Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a script to find highs and lows of a OHLC data. I am following a long way to find highs and lows and it looks like it kinda works. But does not give me a great solution. Here is the code i wrote so far:

df['Highs'] = (df['High'] > df['High'].shift(1)) & (df['High'] > df['High'].shift(2)) & (df['High'] > df['High'].shift(-1)) & (df['High'] > df['High'].shift(-2)), 1, 0)

df['Lows'] = (df['Low'] < df['Low'].shift(1)) & (df['Low'] < df['Low'].shift(2)) & (df['Low'] < df['Low'].shift(-1)) & (df['Low'] < df['Low'].shift(-2)), 1, 0)

I am simply following five finger rule. If middle finger is higer than other four fingers it is a high and if middle finger is lower than all other four fingers it is low. This code find highs and lows for me but as i said i am not quite sure if this is the best approach.

So the main reason i am trying to find highs and lows is to draw a trend line virtually and show an alert when the new price crossover a high resistance line or crossunder the support line if any exists at that point.

I really don't need to draw and plot these lines. All i need is to detect the cross over and under.

Let me explain how i would like to use this:

First of all, i will get a time frame. I am working on a 5 minutes historical / intraday data. When I get a new bar's prices, I want to check if the Close price of new bar crossover or crossunder any trend line in the chart. Again it is a virtual line. So all i need is virtually connect highs and lows to eachother. I don't want to plot them. Script will check all high and low points and will detect if it is crossing those lines.

Here is the screenshot to give you the idea.

EURUSD Chart

like image 208
Don Coder Avatar asked Sep 05 '25 02:09

Don Coder


1 Answers

Perhaps I'm a bit late to the party, but this article nicely describes how to detect highs and lows using scipy.signal.argrelextrema and how to draw trendlines between consecutive peaks.

like image 57
sega.dev Avatar answered Sep 07 '25 19:09

sega.dev