How to Perform Technical Analysis on Cryptocurrencies Using Ruby

How to Perform Technical Analysis on Cryptocurrencies Using RubyAlex SmithBlockedUnblockFollowFollowingMay 17Photo by M.



on UnsplashOverviewTechnical 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 startedFirst 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 YourNewFolderPathcd Documents/Projects/crypto/technical_analysisYou’ll need to install 2 gems, csv and technical-analysis.

You can do this by running:gem install csvgem install technical-analysisThe 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 requirethe gems we just installed by adding the following 2 lines to the top of the file:Parsing the data from the CSVThe 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_CODEPAIR_CODETIMEFRAMETIMEOPENHIGHLOWCLOSEVOLUMEWe 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.


Here is an example:get_data('.


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_fOnce 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 upNow that we have the data formatted the way we need it, let’s get to doing some technical analysis.

Technical AnalysisIt’s time to use the technical-analysis 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('.


csv')Then we set the options for the indicator.

Options vary by indicator.

For SMA, you can see here that options for SMA includeperiod 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 objects.

Each indicator returns its own object with its own attributes.

SmaValue has date_time and sma.

To see this more clearly, you can print out the results like so:Now that we have the SMA calculated, let’s analyze the results.

Analyzing the resultsThe 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.

30-minute SMA for BTC/USD on 04/23/2019Here 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.

SummaryAnd 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:csv gemtechnical-analysis gemIntrinio CSV bulk downloadsIntrinio API endpoints (another way to get data besides CSV)Intrinio Technical Indicator API endpointsGive 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!.

. More details

Leave a Reply