Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Introduction to NumPy – Python Tutorials for Traders

3 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]

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 Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

How to Perform Machine Learning Using Amibroker and Python

Amibroker is a powerful technical analysis and trading system development platform, used extensively by traders and analysts for developing and deploying trading strategies. Python,...
Rajandran R
5 min read

Top Quant Python Libraries for Quantitative Finance

quantitative-finance, python, pandas, NumPy, SciPy, scikit-learn, statsmodels, QuantLib, zipline, TensorFlow, pyfolio, yfinance, seaborn, Plotly, Streamlit, TA-Lib, pandas_ta
Rajandran R
4 min read

Introduction to NumPy – Python Tutorials for Traders

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...
Rajandran R
3 min read

Leave a Reply

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