Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Introduction to NumPy – Python Tutorials for Traders

4 min read

NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices, along with a large collection of mathematical functions to operate on these arrays. NumPy is a popular library for data analysis and scientific computing and is used extensively in finance and trading.

Installing NumPy

You can install NumPy using pip, the Python package manager. Simply open a terminal and enter the following command:

pip install numpy

Importing NumPy

After Numpy installation, you need to import it into your Python script. You can do this using the following line of code:

import numpy as np

The np alias is commonly used for NumPy, and allows you to refer to the library in a shorter and more convenient way.

Creating Arrays

The core data structure in NumPy is the ndarray, which stands for “n-dimensional array”. You can create an array by passing a list or tuple to the np.array() function:

import numpy as np

# Create a one-dimensional array
a = np.array([1, 2, 3, 4, 5])
print('Create a one-dimensional array')
print(a)

# Create a two-dimensional array
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print('Create a two-dimensional array')
print(b)

Python Output

Create a one-dimensional array
[1 2 3 4 5]
Create a two-dimensional array
[[1 2 3]
 [4 5 6]
 [7 8 9]]

You can also create arrays of zeros or ones using the np.zeros() and np.ones() functions:

import numpy as np

# Create a one-dimensional array of zeros
a = np.zeros(5)
print('Create a one-dimensional array of zeros')
print(a)

# Create a two-dimensional array of ones
b = np.ones((3, 3))
print('Create a two-dimensional array of ones')
print(b)

Output

Create a one-dimensional array of zeros
[0. 0. 0. 0. 0.]
Create a two-dimensional array of ones
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

Indexing and Slicing Arrays

You can access individual elements of an array using indexing:

import numpy as np

a = np.array([1, 2, 3, 4, 5])

# Get the first element of the array
print(a[0])

# Get the last element of the array
print(a[-1])

Output

1
5

You can also slice arrays to get a subset of the elements:

import numpy as np

a = np.array([1, 2, 3, 4, 5])

# Get the first three elements of the array
print(a[:3])

# Get the last two elements of the array
print(a[-2:])

Output

[1 2 3]
[4 5]

Operations on Arrays

You can perform a wide variety of mathematical operations on arrays in NumPy. Here are a few examples:

import numpy as np

# Create two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Add the arrays together
c = a + b
print(c)

# Subtract the arrays
d = b - a
print(d)

# Multiply the arrays element-wise
e = a * b
print(e)

# Divide the arrays element-wise
f = b / a
print(f)

Output

[5 7 9]
[3 3 3]
[ 4 10 18]
[4. 2.5 2 ]

You can also perform mathematical operations on entire arrays, using functions like `np.sum()`, `np.mean()`, and `np.std()`:


import numpy as np

a = np.array([1, 2, 3, 4, 5])

# Get the sum of the array
print(np.sum(a))

# Get the mean of the array
print(np.mean(a))

# Get the standard deviation of the array
print(np.std(a))

Output

15
3.0
1.4142135623730951

Converting a Numpy array to pandas dataframe

To convert a NumPy array to a Pandas DataFrame, you can use the pd.DataFrame() function, which takes an array as its input and returns a DataFrame object. Here’s an example:

import numpy as np
import pandas as pd

# Create a NumPy array
data = np.array([[1, 2], [3, 4], [5, 6]])

# Convert the array to a DataFrame
df = pd.DataFrame(data, columns=['Column 1', 'Column 2'])

# Print the DataFrame
print(df)

Output

   Column 1  Column 2
0         1         2
1         3         4
2         5         6

In this example, we first create a NumPy array called data, which contains three rows and two columns of data. We then use the pd.DataFrame() function to convert the array to a DataFrame, and specify the column names using the columns parameter. Finally, we print the resulting DataFrame to the console.

Note that when converting a NumPy array to a DataFrame, it’s important to specify column names (either manually or by using the data parameter with named columns). Otherwise, the resulting DataFrame will have default column names like “0”, “1”, etc., which may not be very informative.

Reshaping Arrays

You can reshape arrays using the np.reshape() function:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6])

# Reshape the array into a 2x3 matrix
b = np.reshape(a, (2, 3))
print(b)

Output

[[1 2 3]
 [4 5 6]]

You can also use the np.ravel() function to flatten an array:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])

# Flatten the array
b = np.ravel(a)
print(b)

Output

[1 2 3 4 5 6]

Broadcasting

NumPy also supports broadcasting, which allows you to perform mathematical operations on arrays of different shapes and sizes:

import numpy as np

# Create an array
a = np.array([1, 2, 3])

# Multiply the array by 2
b = a * 2
print(b)

Output

[2 4 6]

Generating Random Numbers

NumPy also provides support for generating random numbers. Here’s an example:

import numpy as np

# Generate a random number between 0 and 1
a = np.random.rand()
print(a)

# Generate an array of random numbers
b = np.random.rand(5)
print(b)

Output

0.6232014363018096
[0.74894215 0.80172241 0.83527317 0.186123   0.31004827]

Filtering

import numpy as np

# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])

# Use np.where to find indices where a condition is true
indices = np.where(arr > 2)

# Print the result
print("Indices where elements are greater than 2:", indices)
print("Elements at those indices:", arr[indices])

Output

Indices where elements are greater than 2: (array([2, 3, 4], dtype=int64),)
Elements at those indices: [3 4 5]

Generate Arrays between Specific Range

import numpy as np

# Generate an array with values within a specified range with a specified step size
start = 0
stop = 11
step = 2
arange_arr = np.arange(start, stop, step)

# Print the result
print("Arange array:", arange_arr)

Output

Arange array: [ 0  2  4  6  8 10]
import numpy as np

# Generate evenly spaced values between start and stop (inclusive) with a specified number of points
start = 0
stop = 1
num_points = 5
linspace_arr = np.linspace(start, stop, num_points)

# Print the result
print("Linspace array:", linspace_arr)

Output

Linspace array: [0.0   0.25 0.5  0.75 1.0  ]

Here’s a list of some popular NumPy functions organized in a table format:

Function NameDescription
numpy.arrayCreate an array
numpy.arangeCreate an array with regularly spaced values
numpy.linspaceCreate an array with linearly spaced values
numpy.zerosCreate an array filled with zeros
numpy.onesCreate an array filled with ones
numpy.eyeCreate a 2-D identity matrix
numpy.random.randGenerate random values in a given shape (uniform dist.)
numpy.random.randnGenerate random values in a given shape (normal dist.)
numpy.shapeGet the shape of an array
numpy.reshapeReshape an array
numpy.sizeGet the number of elements in an array
numpy.ndimGet the number of dimensions in an array
numpy.meanCompute the mean of an array
numpy.medianCompute the median of an array
numpy.sumCompute the sum of array elements
numpy.minFind the minimum value in an array
numpy.maxFind the maximum value in an array
numpy.argminFind the index of the minimum value in an array
numpy.argmaxFind the index of the maximum value in an array
numpy.uniqueFind unique elements in an array
numpy.transposeTranspose of an array
numpy.dotDot product of two arrays
numpy.linalg.invCompute the inverse of a matrix
numpy.concatenateConcatenate arrays along a given axis
numpy.splitSplit an array into multiple sub-arrays
numpy.vstackStack arrays vertically
numpy.hstackStack arrays horizontally
numpy.sin, numpy.cos, numpy.tanTrigonometric functions
numpy.expCompute the exponent of each element
numpy.log, numpy.log10Natural logarithm, base-10 logarithm

NumPy is a powerful library for working with arrays and matrices in Python, and is especially useful for finance and trading applications. In this tutorial, we covered some of the basic features of NumPy, including creating arrays, indexing and slicing, performing mathematical operations, reshaping arrays, broadcasting, and generating random numbers. With these tools, you should be able to start using NumPy in your trading applications.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

[Course] Designing a Stock Market Trading Dashboard App using…

Designing a Stock Market App using Python is a hands-on course that guides you through the development of a functional stock market application. Over...
Rajandran R
1 min read

Exploring the Essential Python Libraries for Data Analytics

Python has emerged as a powerhouse due to its versatility, ease of use, and extensive library support. Whether you're manipulating data, visualizing trends, performing...
Rajandran R
3 min read

Integrating Tradingview Lightweight Charts with Yahoo Finance Data –…

Hey traders! If you’re looking for a way to visualize your stock data dynamically and interactively, you’re in for a treat. Today, we’ll explore...
Rajandran R
2 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More