Financial Trading in Python for Beginners

Victor Montanez
June 8, 2020

Successful trading requires a great sense of analysis, high-quality financial data, and a programming language that makes the data work for you. In the universe of finance, the two most popular programming languages are Python and R. Today, we’ll be focusing on Python. My name is Victor Montanez, and if you’re a Python beginner, I’ll be leading you through the process of getting started with trading in Python.

Organizing Your Data Collection

The very first thing we need is a data set, in this case stock price data. We can save and call from a .csv format, or call by a programming code, then organize what we need from it and apply it to a programming algorithm.

That will result in a specific data frame, such as in Python and one of its libraries, Pandas. Once we have gathered the data, we can then proceed to analyze its trending sense or direction to plot a chart that will show technical indicators within a price chart.

For our data calls, we will be using the Intrinio US Core Market Data package. You can find documentation for the Intrinio Python software development kit (SDK) here, which, along with my tutorial, will help you get set up so you can start making data calls.

Setting Up the Python Environment

First, you need to install the Intrinio Python SDK API v2 as follows:

Type in your jupyter notebook or Python IDE (Integrated Development Environment) such as VS Code, Anaconda, or Python terminal as shown below

Type

"pip install intrinio-sdk" in Windows or

"pip3 install intrinio-sdk" in a Linux operating system such as Unbutu

This next step will import the library to your notebook or IDE, which will allow you to call the collection of classes, methods, and response objects as outlined in the Intrinio Python SDK documentation.

"import intrinio_sdk"

Making the Data Call and Code

Importing needed libraries

from future import print_function
import time
import intrinio_sdk
from intrinio_sdk.rest import ApiException
from pprint import pprint

Notice, if such packages or libraries are needed but not installed, your Python environment will ask for them.

Getting Your API Key

In order to use the Python API SDK, you need an API key as a token to allow you to use the data. You can get your Intrinio API key by creating a free account. Once you have created an account, visit this page to retrieve your API keys (this article explains the difference between the sandbox and production environments, and which key to use).

intrinio_sdk.ApiClient().configuration.api_key['api_key'] = 'YOUR_API_KEY'

security_api = intrinio_sdk.SecurityApi()
Calling a Specific Stock or Instrument

The following format will call for data - in this example, I'm retrieving data for AMD (American Micro Devices).

identifier = 'AMD' # str | A Security identifier (Ticker, FIGI, ISIN, CUSIP, Intrinio ID)

Now, a great feature of the Intrinio Python SDK is the following lines where you can state a specific start and end date for the data you want as per the company symbol, in this case AMD.

start_date = '2020-01-01' # date | Return prices on or after the date (optional)
end_date = '2020-05-06' # date | Return prices on or before the date (optional)

The code above means you are stating the specific year, month, and day of the month as a start or end date.

Other features include the capability to specify the data frequency (such as daily), page size, and what to do next.

frequency = 'daily' # str | Return stock prices in the given frequency (optional) (default to daily)
page_size = 100 # int | The number of results to return (optional) (default to 100)
next_page = '' # str | Gets the next page of data from a previous API call (optional)

Error exception catch can be also possible, as shown in the lines of code below.

try:
api_response = security_api.get_security_stock_prices(identifier, start_date=start_date, end_date=end_date, frequency=frequency, page_size=page_size, next_page=next_page)
pprint(api_response)
except ApiException as e:
print("Exception when calling SecurityApi->get_security_stock_prices: %s\r\n" % e)

Within the data results, you can receive and view the printed format as shown. Frequency of data, high, low, open, close, and volume, to mention a few, that you can use to print the candles on a chart.

'frequency': 'daily',
'high': 56.78,
'intraperiod': False,
'low': 54.42,
'open': 55.1,
'volume': 72854752.0},
{'adj_close': 55.9,
'adj_high': 57.285,
'adj_low': 55.64,
'adj_open': 56.65,
'adj_volume': 68223356.0,
'close': 55.9,
'date': datetime.date(2020, 4, 23),
'frequency': 'daily',
'high': 57.285,

We can use the adj_close for example, which is the price of the stock after the market had closed and settled for the day, and may include any dividends adjustments on the price. That data makes this Intrinio Python SDK an amazing tool to get more accurate data for your charts and trading algorithms.

Please take a look at the Python SDK documentation, so you can see how all of this code is put together, with helpful notes.

Excellent Libraries in Python

There are other external libraries we can use to plot charts and trading signals according to the data we pulled. The Pandas library can help in converting, reading, and saving as .csv. The Backtrader library is useful for a simulated portfolio with technical indicators and trading signals included.

For examples of charts that may include technical indicators and related signals, feel free to visit my blog.

Flexible Access

Intrinio's data is available via Web API (what we used for the code above), Excel, .csv, and widgets. Having this advanced set of tools make our programming life and data manipulation very easy.

With an Excel or .csv sheet, we can call the data from a local folder in our computer, then arrange it with Python or R. We use the Pandas library with Python so we can remove or add rows or columns in a very professional way and with just few lines of code.

Most Valuable Player

Some of the most valuable players in Intrinio financial data are the financial statements, covered exchanges, accessibility, and fast server response, as we have tested. It took one second or less to get a response from the data servers to see the AMD adjusted closing price and volume at the moment of the call.

Intrinio US Core Market Data includes features for real-time and historical pricing, and data components such as the "adjusted_close" so we can plot a more accurate chart or test. We can also build stock trading algorithms and manipulate the data with Python libraries such as Pandas and Backtrader. If you haven't already, start by signing up for a free Intrinio account.

In this series of blogs, I will follow up with more advanced tutorials so you can follow along as we go.

Enjoy and thanks for reading.