Time for action – balancing volume

In other words, we need to multiply the sign of the close price and the volume. In this section, we look at two approaches to this problem: one using the NumPy sign() function and the other using the NumPy piecewise() function.

  1. Load the BHP data into a close and volume array:
    c, v=np.loadtxt('BHP.csv', delimiter=',', usecols=(6, 7), unpack=True)

    Compute the absolute value changes. Calculate the change of the closing price with the diff() function. The diff() function computes the difference between two sequential array elements and returns an array containing these differences:

    change = np.diff(c)
    print("Change", change)

    The changes of the close price are shown as follows:

    Change [ 1.92 -1.08 -1.26 0.63 -1.54 -0.28 ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.