I've been searching high and low (no pun intended) for a formula, or more likely, a loop that can pick out the higher highs and lower lows so as to be able to draw a trend line across them. This is often used in technical analysis of stocks. At first it seems like such a simple problem but I'm stuck. I'm using python but any pseudo code would probably suffice for myself and others that stumble across this thread in the future.
Asked
Active
Viewed 3,840 times
1 Answers
2
try this I guess (after your comment)
import numpy as np
from matplotlib import pyplot as plt
data = {"x": np.arange(50), "y": np.random.random(50)}
avg, sigma = data['y'].mean(), data['y'].std()
mask_highs = data['y'] > avg + sigma
mask_lows = data['y'] < avg - sigma
mask_middle = ~ mask_highs & ~ mask_lows
plt.scatter(data['x'][mask_highs],data['y'][mask_highs],c="green")
plt.scatter(data['x'][mask_lows],data['y'][mask_lows],c="black")
plt.scatter(data['x'][mask_middle],data['y'][mask_middle],c="blue")
plt.axhline(avg + sigma, c="red")
plt.axhline(avg - sigma, c="red")
plt.show()
If you want to connect the highs and lows you can do
mask_extrema = mask_highs | mask_lows
plt.plot(data['x'][mask_extrema ],data['x'][mask_extrema],color="cyan")
before you do plt.show()

Joran Beasley
- 110,522
- 12
- 160
- 179
-
I'm interested in connecting the dots between the all the high and then again between all lows so as to encompass the entire dataset between the two lines and not have any outlying data points. I'll look deeper into matplotlib and see if maybe they have something similar to this, thanks for the input. – Andy Visser Mar 04 '17 at 04:19
-
This post is somewhat on the same idea, though the results aren't quite there. See: http://stackoverflow.com/questions/31070563/find-all-local-maxima-and-minima-when-x-and-y-values-are-given-as-numpy-arrays – Andy Visser Mar 04 '17 at 04:31