Table of Contents
- Getting Started
- Loading data
- Backtesting algorithm
- Viewing the sentiment analysis plots
- Viewing the sentiment analysis results
Prerequisites
python 3.7 or later
Install requirements
pip install -r req_packages.txt --user
zlib (already included with MacOS)
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
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
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.
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
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
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
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.
Location: plots/article_sentiment.png
Location: plots/swap_sentiment.png
Location: plots/combined_sentiment.png
Location: plots/compare_sentiment.png
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
},
...
...
]
}
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
},
...
...
]
}
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
},
...
...
]
}