How I Coded Technical Indicators for Bitcoin | Towards Data Science

When it comes to trading, there are a variety of techniques that can be applied to find the most optimum time to invest. Some could look at the financials and see if there is value behind the curtains. Others might look to the daily news and observe how it may influence the current price. Another group might actually look at the price movements in the past and try to discern possible patterns to determine future price movements.
Technical Analysis concerns that group that observes past prices and movements to predict future prices. By utilizing technical analysis, we can derive many different patterns just from using an element’s price history. Mathematical formulas that have been developed throughout the past half century have been used to derive novel values just from a stock’s price and volume history. These formula’s returning values are what we call Technical Indicators.
When trading a resource such as Bitcoin, there is not much to analyze besides its price history and volume. If you take a look at its Yahoo Finance page and compare it to any random stock’s page, you can see that it is missing some financial information. This is because Bitcoin is not a company but a currency. There are no Balance Sheets, Profit and Loss Statements, etcetera when it comes to Bitcoin.
With no fundamental information to go on, we can resort to utilizing Technical Indicators when trading Bitcoin. If we wanted to add more variables to a trading algorithm or a machine learning forecasting model, then we could introduce more features (technical indicators) to the dataset instead of the usual price and volume history. This tactic may help in forecasting future prices when utilizing a machine learning model.
Next up, we will be coding out these technical indicators in Python by utilizing its multiple libraries.
Sign up for a Medium Membership here to gain unlimited access and support content like mine! With your support I earn a small portion of the membership fee. Thanks!
The technical indicators that we will be experimenting with are:
Relative Strength Index
Stochastic Oscillator
Simple Moving Average
These indicators appear simple enough for us to develop a function to create and introduce new values and features to our dataset.
In order to develop these functions, let’s first import the necessary libraries as well as download the most up-to-date price history for Bitcoin. Another option for price history would be to use a financial data API such as EOD Historical Data. It is free to sign up and you’ll have access to vast amounts of financial data. Disclosure: I earn a small commission from any purchases made through the link above.
import pandas as pdimport _pickle as pickleimport numpy as npimport matplotlib as pltplt.style.use('bmh')df = pd.read_csv('BTC-USD.csv')
Take a look at the Pandas DataFrame when you run this code:
Another step after this would be to convert the index into a Datetime format. This is a simple process requiring only a couple lines of code:
# Datetime conversiondf['Date'] = pd.to_datetime(df.Date)# Setting the indexdf.set_index('Date', inplace=True)
By running these lines of code, visualizing any column or feature from this dataset will be easier.
With the data ready for us to manipulate, let’s start by coding out some simple functions that will give us the technical indicator values we need. (The formulas for these indicators can be found in the links with the technical indicators).
Once we run these functions on the appropriate columns, we will receive a new set of features derived from these technical indicators.
Nice! We were able to successfully feature engineer and introduce this new data into our dataset.
But, how do we verify these values? Well, there are options to view online such as stock charts with indicators that will show us the right values at specific times. One option is to compare our values to the Yahoo Finance chart.
As we compare our technical indicator values to the chart, we can see that do not match. There may have been an issue with the functions or we missed a calculation here and there. Anyways, our functions are not performing as well as we wanted.
Where do we go from here? We could go back and try to fix our code or we could try a more practical approach. This approach will be to use a Technical Analysis library already written and provided for us. Most of the time, using a Python library for these sort of calculations are better than us writing our own functions.
The library we will be utilizing is simply called — ta. You can click on the link below for instructions on how to get started with the library:
technical-analysis-library-in-python.readthedocs.io
The functions provided here are much more versatile than the ones we created so let’s put them to use.
# Importing Libraryimport ta# TA's RSIdf['ta_rsi'] = ta.momentum.rsi(df.Close)# TA's Stochastic Oscillatordf['ta_stoch_k'] = ta.momentum.stoch(df.High, df.Low, df.Close)df['ta_stoch_d'] = ta.momentum.stoch_signal(df.High, df.Low, df.Close)
Once run the above piece of code we will end up with the DataFrame below:
As we can see, the values we created differ quite significantly from TA’s values. We can also check the validity of these values again with Yahoo Finance’s chart and see that they do indeed match.
After seeing attempting to create our own technical indicator functions, we found a more optimal approach by importing a technical analysis library. Generally, it is better to utilize a Python library instead of your own written functions because they are usually much more optimized than anything we could possibly code. The library also gives us the option of adding more indicators instead of trying to code it all out ourselves. In the end, we were able to practice coding out some simple algorithms and functions even though they weren’t as accurate as we had hoped.
Once we have constructed this new DataFrame filled with new features and values, we now may be able to feed this extra data into machine learning model. The data is in an easily digestible Pandas format for a potential time series forecasting model. Hopefully, we can improve forecasting results by using these extra technical indicators.
Follow me on Twitter: @_Marco_Santos_