Codementor Events

How to Perform Technical Analysis on Cryptocurrencies Using Ruby

Published Nov 11, 2019Last updated May 08, 2020
How to Perform Technical Analysis on Cryptocurrencies Using Ruby

Overview

Technical analysis is a form of analysis that uses patterns in market data to identify trends and make predictions. While it’s common to perform technical analysis on forex or security prices, let’s see what technical analysis looks like on cryptocurrencies.

I have some 1-minute interval prices for Bitcoin/USD in a CSV file and I’ll use some Ruby to perform some quick technical analysis by calculating the Simple Moving Average (SMA).

Getting started

First things first, go ahead and create a new folder to save your work. Change directory into this folder on your command line by doing:

cd YourNewFolderPath
cd Documents/Projects/crypto/technical_analysis

You’ll need to install 2 gems, csv and technical-analysis. You can do this by running:

gem install csv
gem install technical-analysis

The csv gem is to help us parse CSV files in Ruby. The technical-analysis gem is a nice open source project to perform technical analysis on various datasets in Ruby.

Okay, now we’re ready to get started. Create a file (bitcoin_technical_analysis.rb) in the folder you made earlier.

Let’s require the gems we just installed by adding the following 2 lines to the top of the file:

require 'csv'
require 'technical-analysis'

Parsing the data from the CSV

The next step is to get the data we need from our CSV file.
If you need cryptocurrency price data in a CSV, check out Intrinio, they offer bulk downloads for all kinds of data.

There are many ways to grab data from a CSV file using Ruby. For the sake of this exercise, I have the data in a file named btcusd_1m.csv in the same folder we created earlier (Documents/Projects/crypto/technical_analysis).
The headers of my columns in this CSV from Intrinio are:

  • EXCHANGE_CODE
  • PAIR_CODE
  • TIMEFRAME
  • TIME
  • OPEN
  • HIGH
  • LOW
  • CLOSE
  • VOLUME

We only need some of these columns and some will need to be renamed. You can see here that the technical-analysis gem is looking for data formatted certain ways, depending on which indicator you’re trying to use.

We’re going to be calculating SMA, so let’s parse the CSV to get the data that we need for this indicator. For SMA, we need 2 values for each observation — the date_time of the observation and the value of the observation. In this case, we are going to calculate the SMA based on the closing price of BTC/USD from our CSV file, in 1-minute intervals.

Let’s create a method to grab the data from the CSV file, parse it, and format it.

This method, get_data, accepts a parameter which is a string of the CSV file btcusd_1m.csv. Here is an example:

get_data('./btcusd_1m.csv')

Then, we use the csv gem to parse the CSV. With each row, we’re changing the time key to be date_time and turning the value of close to a Float to format it for use in the technical-analysis gem. You’ll want to adjust your code to fit how your CSV data is formatted.

For example, if your CSV data had a header of CLOSE_PRICE instead of CLOSE, you would want to do something like this:

formatted_row[:close] = row[:close_price]&.to_f

Once we run get_data our result will be an Array of Hashes that looks something like this*:

[
  { date_time: "2019-04-22 18:43:00 UTC", close: 5001.00},
  { date_time: "2019-04-22 18:42:00 UTC", close: 5002.00},
  { date_time: "2019-04-22 18:41:00 UTC", close: 5003.00}
]

The closing prices in the above example are made up

Now that we have the data formatted the way we need it, let’s get to doing some technical analysis.

Technical Analysis

It’s time to use the 1technical-analysis1 gem to calculate the SMA.

Continuing with our same code from before:

First, we get the data from our CSV as we mentioned before by executing:

data = get_data('./btcusd_1m.csv')

Then we set the options for the indicator. Options vary by indicator. For SMA, you can see here that options for SMA include period and price_key.

I’ll leave period to its default value of 30. Since we are using 1-minute interval prices it means we’re calculating the 30-minute SMA. price_key is the column name that the data is in. It defaults to :value in the gem, but we have the data in a column named :close. We need to tell the gem to look for our data in the close column instead of value. So we set price_key to be :close:

options = { price_key: :close }

Finally, we use the technical-analysis gem to perform the analysis, passing in the data and options we just defined by doing:

TechnicalAnalysis::Indicator.calculate(
  'sma',
  data,
  :technicals,
  options
)

This returns an Array of [SmaValue](https://github.com/intrinio/technical-analysis/blob/ffd9b120d8c4a749df4b77c9e6c74cc86723b3f5/lib/technical_analysis/indicators/sma.rb#L79) objects. Each indicator returns its own object with its own attributes. SmaValue has date_time and sma.

Now that we have the SMA calculated, let’s analyze the results.

Analyzing the results

The final step is to analyze the SMA calculations by graphing them. Most technical indicators are graphed. I quickly dropped the results into a new CSV and graphed the results in Excel for demonstration purposes. In doing so, I took a look at the last month or so of data and found April 23rd, 2019 to be interesting.

btc_usd_30_min_sma.png30-minute SMA for BTC/USD on 04/23/2019

Here you can see that starting early on April 23rd, the 30-minute SMA begin to act as a support line for BTC/USD as it rose from 5,375 to 5,550. In a perfect world, that would’ve been roughly a 3.26% gain in just a few hours.

Summary

And that’s it! It’s quick and easy to do some technical analysis with Ruby using the technical-analysis gem. A couple of resources that are helpful or referenced above:

Give it a try by adding another SMA and changing the period to 200. Or try calculating a different indicator like Bollinger Bands.


This post is for informational purposes only and is not investment advice. Please seek a licensed professional for advice.

Also, I work for Intrinio and I’m a primary contributor to the technical-analysis gem!

Discover and read more posts from Alex
get started
post comments1Reply
Eal Mcgowan
4 years ago

I found this explanation very detailed and simple, thanks for posting. Recently I found this that explains a lot about Bitcoin trading and I would like to read more information like that. What would you recommend?