The min
and max
functions are the answer to our requirement. Perform the following steps to find highest and lowest values:
h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True)
The only thing that changed is the usecols
parameter, since the high and low prices are situated in different columns.
print "highest =", np.max(h) print "lowest =", np.min(l)
These are the values returned:
highest = 364.9 lowest = 333.53
Now, it's trivial to get a midpoint, so it is left as an exercise for the reader to attempt.
ptp
. The ptp
function returns the difference between the maximum and minimum values of an array. In other words, it is equal to max(array) - min(array). Call the ptp
function.print "Spread high price", np.ptp(h) print "Spread low price", np.ptp(l)
You will see the following:
Spread high price 24.86 Spread low price 26.97
We defined a range of highest to lowest values for the price. The highest value was given by applying the max
function
to the high price array. Similarly, the lowest value was found by calling the min
function to the low price array. We also calculated the peak-to-peak distance with the ptp
function.
import numpy as np h,l=np.loadtxt('data.csv', delimiter=',', usecols=(4,5), unpack=True) print "highest =", np.max(h) print "lowest =", np.min(l) print (np.max(h) + np.min(l)) /2 print "Spread high price", np.ptp(h) print "Spread low price", np.ptp(l)