joabara / Iridium-Crypto-Trader

An cryptocurrency trading bot that uses automated machine learning for decision making to maximize returns.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Iridium Trading Bot

Executive Summary

  • Iridium trading algorithms acheive consistently strong results across diference currencies, like a 25% incremental alpha over holdout.

  • Appetite control algorithm significantly reduces risks from market volatility and cashflow issues.

  • By grouping many bots together into a Hive network we can diversify and smooth performance over time.

Data

Currency Index & Price

from coin_hist_pull import *

index = import_coin_list()
index.head(10)
id symbol name
0 bitcoin BTC Bitcoin
1 ethereum ETH Ethereum
2 cardano ADA Cardano
3 binancecoin BNB Binance Coin
4 tether USDT Tether
5 ripple XRP XRP
6 solana SOL Solana
7 dogecoin DOGE Dogecoin
8 polkadot DOT Polkadot
9 usd-coin USDC USD Coin
prices = coin_price_hist('bitcoin', 'usd', 85, 'hourly')
prices.head()
market_tms price mkt_cap total_volumes
2043 2021-09-06 13:36:27.000 51236.488363 9.635965e+11 9.635965e+11
2042 2021-09-06 13:03:42.420 51328.326739 9.653960e+11 9.653960e+11
2041 2021-09-06 12:00:58.225 51384.962032 9.664615e+11 9.664615e+11
2040 2021-09-06 11:06:58.999 51770.081701 9.730119e+11 9.730119e+11
2039 2021-09-06 10:01:58.312 51810.725514 9.744632e+11 9.744632e+11

Google Trends

from g_trends import *
btc_searches = g_trend_pull(['bitcoin'])
btc_searches.head()
bitcoin isPartial
date
2021-06-06 70 False
2021-06-07 67 False
2021-06-08 91 False
2021-06-09 86 False
2021-06-10 75 False

Methodology

Individual Bot Strategy

A trading bot is defined by the two currencies it's going to trade. A BTC/USD bot will be a bot that will buy BTC in USD based on the prices of BTC. This means the bot can also trade USD/BTC. Let's init a bot that starts with 1000 USD worth of Bitcoin and trades ~400 USD at a time.

from Trader import *
inv_amt = 1000
pct = 0.40
bot = Trader('Iridium_bitcoin_usd', 'bitcoin', 'usd', 'CoinBasePro', inv_amt, pct)
bot.learn_and_sim(42*24)
from sklearn.tree import plot_tree
from matplotlib.dates import DateFormatter, DayLocator
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.pyplot import figure
figure(figsize=(14, 12), dpi=225)
w = bot.go_build
plot_tree(w, filled=True)
plt.show()

png

Trading Strategy

Every hour, the bot looks at the previous 42 days and identifies what the best opportunities to buy and sell would have been. This will set up the training data for the machine learning algorithm later.

Specifically, there are two labels that the bot learns for:

  • Buy Ind: If the price change increases by more than 1 standard deviation over the next hour
  • Sell Ind: If the price change decreases by more than 1 standard deviation over the next hour

The features can be simplified to:

  • Prices and moving averages of prices (momentum)
  • Google Trends data (how much public interest is in a currency)

The training data will be the last 42 days minus the current hour, and then split on a 75/25 size. The current hour's data will then be run through the model build and it will return a decision: buy, sell, or do nothing.

Results From Individual Trader

After a couple weeks of trading, the bot's performance in terms of PNL is usually profitable (depending on the currency). Most of the value comes from increased asset size while maintaining a small deficit in cashflow.

Using the example from before, if the bot starts with 100,000 USD and 1 Bitcoin, the ending balance sheet might look like 87,000 USD and 4 Bitcoin. So in essence, you would have bought 3 Bitcoin for 13,000 USD, which is a pretty great deal.

Below, is a the simulation result from a BTC/USD trading bot.

bot.summary()
CASHFLOW
--------------------------------------
Cash spent (Total Buy Cost): 36680.6  usd
Cash earned (Total Sell Revenue): $38153.44  usd
Net Cashflow: 1472.84  usd
--------------------------------------

ASSET VALUES: 
--------------------------------------
Start Quantity: 0.028 bitcoin
Starting Asset Value: 1101.74  usd
Ending Quantity: 0.006 bitcoin
Ending Assets Value: 303.08  usd
Net Asset Value: $-798.66
--------------------------------------

TEST RETURNS
--------------------------------------
PNL: 674.18 usd
Return %: 60.19%
--------------------------------------

TEST VS HOLDOUT
--------------------------------------
PNL over Hold: 352.82 usd
Algo Return over Hold%: 24.79%
--------------------------------------

What this readout shows is that the BTC/USD bot was able to accumulate around 17,000 USD worth of BTC by making some smart trades and only spending a net of 9,300 USD. The bot, on paper, was able to double its asset value by adding 8,500 USD in value.

However, this would not be a true performance readout. It is important to note that during this time period, the price of BTC/USD did increase quite drastically. We need to able to compare the performance of the strategy vs the holdout (i.e if we didn't do anything).

In conclusion, the algorithm was able to create ~ 2,000 USD in net value, an incremental improvement rate of about 20%.

Resilience to Decreasing Prices

We validate the algorithm's robustness by see how algorithm return rate is affected by a drop in price. In this example, we will look at a USDT/ETH trading bot. Since the price of ETH in USD has gone up significantly in the past few weeks, the price of USDT in ETH will have gone down.

from Trader import *
inv_amt = 0.05
pct = 0.40
bot = Trader('Iridium_bitcoin_usd', 'tether', 'btc', 'CoinBasePro', inv_amt, pct)
bot.learn_and_sim(42*24)
bot.coin_hist.iloc[42*24:].to_csv('decisions2.csv')
bot.summary()
CASHFLOW
--------------------------------------
Cash spent (Total Buy Cost): 0.97  btc
Cash earned (Total Sell Revenue): $0.93  btc
Net Cashflow: -0.04  btc
--------------------------------------

ASSET VALUES: 
--------------------------------------
Start Quantity: 1795.742 tether
Starting Asset Value: 0.05  btc
Ending Quantity: 4449.449 tether
Ending Assets Value: 0.09  btc
Net Asset Value: $0.04
--------------------------------------

TEST RETURNS
--------------------------------------
PNL: -0.0 btc
Return %: -6.81%
--------------------------------------

TEST VS HOLDOUT
--------------------------------------
PNL over Hold: 0.01 btc
Algo Return over Hold%: 21.99%
--------------------------------------
from matplotlib.dates import DateFormatter, DayLocator
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
%matplotlib inline

ts = bot.coin_hist[['price', 'dt', 'curr_val', 'cash_flow']].iloc[42*24:]
ts = ts.groupby(['dt']).mean()
ts.index = pd.to_datetime(ts.index)
fig, ax = plt.subplots(figsize=(10, 6))
axb = ax.twinx()

ax.plot(ts.index.values, ts.price, color='tab:blue', linestyle='-', label='Price')
axb.plot(ts.index.values, ts.cash_flow, color='tab:green', linestyle='-', label='Cash Flow')

axb.set_ylabel('BTC Cashflow')

ax.set_xlabel('Date')
ax.set_ylabel('Price of $1 in BTC')
ax.set_title('USDT / BTC')

# format x axis labels
plt.setp(ax.get_xticklabels(), rotation=90)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d'))
ax.xaxis.set_major_locator(DayLocator())

ax.legend(loc='upper left')
axb.legend(loc='upper right')
<matplotlib.legend.Legend at 0x7f890ce42280>

png

The plot above shows that the price of USDT in ETH drops immediately as the bot starts trading. On the flipside, the bot starts selling USDT for ETH, increasing the ETH Cashflow. This is effectively the same thing as buying a lot of ETH when the price increases to USDT.

The performance below shows that the algorithm below suffers a loss of ~11%. However, when comparing the performance versus the holdout (if the bot had done nothing at all), the bot still performed 55% better.

This means that while the bot takes a loss, it is able to react quickly and make sure the losses are minimized. While the USDT price tanked, it sold a lot of ETH to reduce the losses.

Hive Network

Three's a Party

As we mentioned before, the bots usually have a pretty good performance, but in some cases they are quite bad and are designed to focus on one market at time. We could have multiple bots trading in different markets, but there is a lot more value in storing them in a network. This accomplishes two important goals:

  • Many bots trading differnt markets allows us to diversify investments. If one bot fails spectacularly it won't affect the overall performance.

  • By putting all the bots in a network, we open up future opportunities to simulate collaboration between bots and improve computing performance.

We're going to take a sample of the top 10 coins in terms of market cap and we're going to create a network of bots to simulate orders. The simulation process takes a long time, but in production it won't be a problem since we are running only one record (the current hour) through the model and processing any orders that need to go through.

We will give each bot around 1,000 USD and a standard trade size of 2.5%, or 25 USD.

from Hive import *
index['err'] = index['id'].apply(lambda x: 1 if '-' in x else 0)
index = index[index['err']==0]
index.head()
id symbol name err
0 bitcoin BTC Bitcoin 0
1 ethereum ETH Ethereum 0
2 cardano ADA Cardano 0
3 binancecoin BNB Binance Coin 0
4 tether USDT Tether 0
network = HiveNet()
network.loadBotNet(index.iloc[0:5], 500, 0.40)
network.setWindow(42)
network.learn_performance(42, 500, 0.4)
network.networkToPerf()

Results

Now that we have run the network simulation, we can aggregate the performance across all bots in the network and view their results.

network.simSummary()
CASHFLOW
--------------------------------------
Cash spent (Total Buy Cost): $90002.06
Cash earned (Total Sell Revenue): $90694.37
Net Cashflow: 692.32
--------------------------------------

ASSET VALUES: 
--------------------------------------
Starting Asset Value: $2449.01
Ending Assets Value: $3424.97
Net Asset Value: $975.97
--------------------------------------

TEST RETURNS
--------------------------------------
PNL: $1668.29
Return %: 68.12%
--------------------------------------

TEST VS HOLDOUT
--------------------------------------
PNL over Hold: $343.31
Algo Return over Hold%: 14.02%
--------------------------------------

Internal Collaboration (Next Steps)

While the trading algorithm is extremely effective on its own, it would be interesting to see if there are opportunities for collaborations between bots. Using a tool like networkx could help create links between bots.

One potential implementation would be to use an order correlation matrix to create links between bots. It would be interesting to see if some bots have high correlations of orders with each other. If that would be the case, we could save a lot of completxity by having only a few bots make decisions and others following.

About

An cryptocurrency trading bot that uses automated machine learning for decision making to maximize returns.


Languages

Language:Python 100.0%