Use Your Computer to Make Informed Decisions in Stock Trading: Practical Introduction — Part 1: Python Environment
If you want to take advantage of your computer to make informed decisions in trading stocks, this article series is definitely worth reading. Don’t worry if you have little or no experience with programming. In this series, we will use Python, one of the most human-friendly programming languages, despite its fearsome name. If you have written at least one program at school, you’ll be able to keep up with the code samples provided in this series. My goal is not to turn you into a programmer, but rather to show how you might take advantage of your computer to improve your investment results.
As you work through the series’ hands-on examples, you’ll learn to make decisions regarding investments based on computer analysis of the stock market. After reading this series, you will have a good knowledge of cutting-edge techniques for stock trading, as well as a set of tools on your computer to trade more efficiently.
This first part in the series covers how to do the very first steps required to get started with stock data analysis on your computer. In particular, you will be instructed how to install Python on your computer and then how to setup Python libraries needed for obtaining and analysing stock data. Since many examples in this series will be accomplished in Google Colab, you’ll also learn how to get started with this outstandingPython development environment
Installing Python on Your Computer
For samples in this article series, you’ll need a latest Python version installed on your machine. If you have it already, you can skip this section.
Whether on Windows, Linux, or macOS, installing Python is a breeze. To start with, visit the official Python download page. Under the ‘Download the latest source release’ header on this page, find and click a link to the Python download page for your operating system.
For Windows, download the Windows x86–64 executable installer for the latest stable release, and run the installer. In the dialog, just click Install Now to complete the installation on your machine. That is it.
As to Linux, many distributions come with Python already installed in the system. However, you may still need to install the latest version. One clear way to do this is with the help of the source code for the latest release, which you can find at the Python download source page. Below are the steps to install Python on your Linux system, using the source code:
$ cd /usr/src$ sudo wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz$ sudo tar xzf Python-3.8.3.tgz$ cd Python-3.8.3$ sudo ./configure — enable-optimizations$ sudo make altinstall
The altinstall parameter instructs the make command to install the distribution as an alternative version of Python in your system. This will allow you and your system still to use the other version of Python that has been already installed in the system.
Now that you have Python installed on your machine, you should be able to run the Python interpreter from your system terminal:
$ python3.8Python 3.8.3 (default, Jul 10 2020, 23:20:32)[GCC 4.8.5 20150623 (Red Hat 4.8.5–28.0.1)] on linuxType “help”, “copyright”, “credits” or “license” for more information.>>>A quick test might look as follows:>>> print(‘Hello world!’)Hello world!To close the interpreter session, use the exit() function:>>> exit()$
We will use the Python interpreter to run small to medium scripts in this series. And we will use the python command to run larger scripts:
$ python3.8 yourscript.py
Note that if you have a single Python installation in your system, there will be no version number following the python command:
$ python yourscript.py
To invoke the interpreter in a terminal, you can use:
For clarity, we will refer to the python command as python throughout the rest of this series.
Turning back to the discussion on Python installation, I would also recommend to check out this tutorial for further details. In this same tutorial, you’ll find installation details for macOS / Mac OS X operating systems.
Python Libraries to Install
Python has a strong set of inbuilt data structures that make implementations of data processing operations extremely easy and fast to complete. These structures include lists, dictionaries, tuples, and sets. In addition, there are a huge number of libraries you can install in your Python installation to obtain the necessary functionality.
If you’ve already performed some data analysis operations in Python, chances are you’re already familiar with the Pandas and Numpy libraries. These libraries play a key role in many data analysis scenarios, and stock data analysis is no exception. Since they are so important, you may not even need to install them explicitly — they often will be implicitly installed for you during the installation of a library designed for a specific data analysis field, say, stock data analysis. Thus, when installing yfinance library, which is designed for obtaining stock data from Yahoo Finance, you will have both the Pandas and Numpy libraries installed for you implicitly.
To install yfinance, you can use the pip command, which is installed during your Python installation (it can be somewhat like pip3.8 in your particular system — refer back to the previous section):
$ pip install yfinance
After the successful installation, you can start using the yfinance library, as discussed in the next part of this series.
Getting Started with Google Colab
Google Colaboratory (Colab for short) lets you write and run Python code in your browser in so-called Colab notebooks. Using notebooks, you can combine executable code, rich text, and images within a single document that will be stored in your Google Drive account and, therefore, can be used later.
Listed as short bullets, the main advantages/disadvantages of using Google Colab are the following:
+ good for visualisation and you can see the author’s output straight away
+ just copy it and save to your drive — you’ll be able to execute it
+ easy to install all the env with !pip install command, runs directly from browser
+ you can enable gpu and tpu easily — to increase the speed for deep learning models
+ easy to upload your code to GitHub
+ notebooks can be easily shared with colleagues and partners
- disconnects when browser is inactive
To get started with Google Colab, you’ll need a Google account. Once you have it, point your browser to https://colab.research.google.com and follow these steps to create a new notebook and get started with it immediately:
1. In the dialog shown in the foreground, click the NEW NOTEBOOK button.
2. In a new notebook opened in your browser, click +Text button to add a text cell to the notebook.
3. In the text cell, type in this text: ‘This is our first test of Google Colab’.
4. Click +Code button to add a new code cell.
5. In the code cell, type in a simple Python command like: print(‘Hello World!’)
6. Run the code cell by clicking the button on its right.
The following screenshot illustrates what you should see after performing the above steps:
The next task is to install the required libraries in our Colab environment. This can be done with the !pip install command in a new code cell. For example, you might install the yfiinance library by issuing the following command:
!pip install yfinance
When you run this command, you’ll see a series of messages informing you about the installation process progress. The final message should look as follows:
Successfully installed yfinance-0.1.54
By following the instructions provided in this first part of the series, you should have a Python environment installed on your local machine and have an initial understanding of how to run your Python code in Google Colab. In the next part, you’ll start using those environments, obtaining stock data programmatically.