/CryptoTradingAlgo

Cryptocurrency Trading Algorithm with Sentiment Analysis

Primary LanguagePython

Cryptocurrency Trading Algorithm with Sentiment Analysis

Table of Contents

Getting started

Prerequisites

python 3.7 or later

Install requirements

pip install -r req_packages.txt --user
zlib (already included with MacOS)

Loading data

Loading Price data

The script load_price_data.py fetches price data from https://www.bitmex.com/api/v1/trade/bucketed (BitMex API endpoint) and saves the data in data/bitmex_data.msgpack.zlib. Variables datetime_start and datetime_end defines the date range of the price data that has to be fetched. Variable symbol is the symbol of the cryptocurrency you want to trade (Example: XBTUSD - Bitcoin). Parameter binSize is the time difference between 2 successive data values (Bin size of 1 hour is used in this algorithm. Don't change this value!)

python load_price_data.py

Loading Article data

The script load_article_data.py fetches Trending News data from public/trending-news-data (Executium Trending News API endpoint) and saves the data in data/article_data.json. Variables datetime_start and datetime_end defines the date range of the article data that has to be fetched. Variable symbol is the symbol of the cryptocurrency you want to trade (Example: XBTUSD - Bitcoin).

python load_article_data.py

Loading Swap Funding Rate data

The script load_funding_data.py fetches Swap Funding Rate data from https://www.bitmex.com/api/v1/funding (BitMex API endpoint) and saves the data in data/funding_data.msgpack.zlib. Variables datetime_start and datetime_end defines the date range of the article data that has to be fetched. Variable symbol is the symbol of the cryptocurrency you want to trade (Example: XBTUSD - Bitcoin).

python load_funding_data.py

Note: The variables datetime_start, datetime_end, and symbol inside the above data loading scripts should all match for the backtesting to work.

Backtesting algorithm

Articles Sentiment Analysis

The script article_sentiment.py uses article data saved in data/article_data.json and runs an NLP (Natural Language Processing) algorithm to classify the sentiment of the articles as either positive, negative or neutral. By using an indicator called Bull/Bear Ratio, the algorithm generates a trading signal.

python article_sentiment.py

Swap Funding Rate Sentiment Analysis

The script swap_sentiment.py uses swap funding rate data saved in data/funding_data.msgpack.zlib and generates a trading signal. A positive funding rate implies the most people are holding a long position and vice versa for a short position. Note: An extreme positive or negative funding rate usually implies a forthcoming mean reversion. This has been added as a feature in the trading signal.

python swap_sentiment.py

Combined Sentiment Analysis

Note: Run this script after running article_sentiment.py and swap_sentiment.py.

The script combined_sentiment.py uses both the Independent Trading Signals generated by article_sentiment.py and swap_sentiment.py (saved in results/article_results.json and results/funding_results.json respectively). These signals are combined to generate the best possible combined Trading Signal.

python combined_sentiment.py

Compare Sentiment Analysis

Note: Run this script after running article_sentiment.py, swap_sentiment.py, and combined_sentiment.py.

The script compare_sentiment.py plots the profit differences of different sentiment analysis strategies.

python compare_sentiment.py

Note: Plots are automatically saved after running the above scripts.

Viewing the sentiment analysis plots

Article Sentiment Analysis plots

Location: plots/article_sentiment.png

Swap Funding Rate Sentiment Analysis plots

Location: plots/swap_sentiment.png

Combined Sentiment Analysis plots

Location: plots/combined_sentiment.png

Compare Sentiment Analysis plots

Location: plots/compare_sentiment.png

Viewing the sentiment analysis results

Article Sentiment Analysis results

Location: results/article_results.json

JSON Structure:

{
	"symbol": "XBTUSD",
	"data": [
			{
				"date": "2019-06-01T05:30:00Z",
			 	"bbratio": 3.2, 
			 	"mvngavg_ratio": 3.2, 
			 	"ratio_signal": 0.5240053208269925, 
			 	"wallet": 1.0
			 }, 
			 {
			 	"date": "2019-06-02T05:30:00Z", 
			 	"bbratio": 1.5714285714285714, 
			 	"mvngavg_ratio": 2.5485714285714285, 
			 	"ratio_signal": 0.5240053208269925, 
			 	"wallet": 1.0011116311724784
			 },
			 ...
			 ...
		]
}

Swap Funding Rate Sentiment Analysis results

Location: results/funding_results.json

JSON Structure:

{
	"symbol": "XBTUSD",
	"data": [
			{
				"date": "2019-06-01T09:30:00Z", 
				"funding_rate": 0.000116, 
				"mvngavg_funding": 0.000116, 
				"funding_signal": 0.0, 
				"wallet": 1.0
			}, 
			{
				"date": "2019-06-01T17:30:00Z", 
				"funding_rate": 0.000141, 
				"mvngavg_funding": 0.00011713636363636365, 
				"funding_signal": 2.386363636363636e-05, 
				"wallet": 1.0
			},
			...
			...
		]
}

Combined Sentiment Analysis results

Location: results/combined_results.json

JSON Structure:

{
	"symbol": "XBTUSD", 
	"scaling_factor": 0.35, 
	"data": [
			{
				"date": "2019-06-01T05:30:00Z", 
				"combined_signal": 0.18340186228944735, 
				"wallet": 1.0
			}, 
			{
				"date": "2019-06-02T05:30:00Z", 
				"combined_signal": 0.2648541601311871, 
				"wallet": 1.0011116311724784
			},
			...
			...
		]
}