PYTHON
[|]
#Part 1

Python Environment

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.
Discussion in Telegram
Screencasts on Youtube
Articles on Medium
Code on Github

Introduction

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
Executive Summary
In this chapter we found out how to install Python and libraries on your computer. We explored Google Colab as an alternative pure online way to work with the Python on-demand using powerful virtual environment and having less hassle how to make some code running on your machine.

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:
Py1. Installing Python (run in Terminal)
$ 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:
Py2. Python test run from the Terminal
$ python3.8

Python 3.8.3 (default, Jul 10 2020, 23:20:32)

[GCC 4.8.5 20150623 (Red Hat 4.8.5–28.0.1)] on linux

Type “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:
Py3. Run a program in Python from a command line
$ 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:
Py4. Run a program in Python from a command line (single Python version installed)
$ python3.8 yourscript.py
To invoke the interpreter in a terminal, you can use:
Py5. Invoke the Python interpreter from a command line
$ python
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):
Py6. Install a library for Python
$ 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:
Figure-1: Google Colaboratory notebook example
Figure-1: Google Colaboratory notebook example
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:
Py7. Installing yfinance
!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:
Py8. Successful library installation message in Colab
Successfully installed yfinance-0.1.54
Conclusion
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.

Do you find the article useful?

Do you like the content?
Consider to make a donation
Whether you're grateful for the content, or you wish to support one of the ideas in the wishlist (published on the BuyMeACoffee page)

Leave your feedback on the article

For example, is it easy to understand?
For example, could you run the code?
For example, do you have idea to improve the article ?

Here you'll find the best articles from PythonInvest. Only useful digests, no spam.