Book Image

Hands-On Financial Trading with Python

By : Jiri Pik, Sourav Ghosh
Book Image

Hands-On Financial Trading with Python

By: Jiri Pik, Sourav Ghosh

Overview of this book

Creating an effective system to automate your trading can help you achieve two of every trader’s key goals; saving time and making money. But to devise a system that will work for you, you need guidance to show you the ropes around building a system and monitoring its performance. This is where Hands-on Financial Trading with Python can give you the advantage. This practical Python book will introduce you to Python and tell you exactly why it’s the best platform for developing trading strategies. You’ll then cover quantitative analysis using Python, and learn how to build algorithmic trading strategies with Zipline using various market data sources. Using Zipline as the backtesting library allows access to complimentary US historical daily market data until 2018. As you advance, you will gain an in-depth understanding of Python libraries such as NumPy and pandas for analyzing financial datasets, and explore Matplotlib, statsmodels, and scikit-learn libraries for advanced analytics. As you progress, you’ll pick up lots of skills like time series forecasting, covering pmdarima and Facebook Prophet. By the end of this trading book, you will be able to build predictive trading signals, adopt basic and advanced algorithmic trading strategies, and perform portfolio optimization to help you get —and stay—ahead of the markets.
Table of Contents (15 chapters)
1
Section 1: Introduction to Algorithmic Trading
3
Section 2: In-Depth Look at Python Libraries for the Analysis of Financial Datasets
9
Section 3: Algorithmic Trading in Python

Creating NumPy ndarrays

An ndarray is an extremely high-performant and space-efficient data structure for multidimensional arrays.

First, we need to import the NumPy library, as follows:

import numpy as np

Next, we will start creating a 1D ndarray.

Creating 1D ndarrays

The following line of code creates a 1D ndarray:

arr1D = np.array([1.1, 2.2, 3.3, 4.4, 5.5]); 
arr1D

This will give the following output:

array([1.1, 2.2, 3.3, 4.4, 5.5])

Let's inspect the type of the array with the following code:

type(arr1D)

This shows that the array is a NumPy ndarray, as can be seen here:

numpy.ndarray

We can easily create ndarrays of two dimensions or more.

Creating 2D ndarrays

To create a 2D ndarray, use the following code:

arr2D = np.array([[1, 2], [3, 4]]); 
arr2D

The result has two rows and each row has two values, so it is a 2 x 2 ndarray, as illustrated in the following code snippet:

array([[1, 2],
      ...