Visualizing Alt-Coin Correlation

in #money7 years ago (edited)

Yesterday I showed you how to download price data from Binance using Python. Today I will show you some of the cool stuff you can do with that data.

In today's tutorial, I want to show you how to download the price data for over 120 crypto pairs which trade on the Binance exchange. We will then look at the correlation between each cyrptocurrency and identify which cyrpto's trade most similarly to STEEM/USDT.

Finding the assets which are most similar to another asset is useful in a number of trading applications, including 1) price prediction, 2) hedging and 3) statistical arbitrage. We'll talk a little bit more about this in a second, but first, let's take a look at how to download so much data...

We could manually write down the list of symbols... but wait, fuck that!! We're programmers and the best programmers are lazy so let's get the symbols the lazy way using the Binance API:

symbols = json.loads(requests.get("https://api.binance.com/api/v1/exchangeInfo").text)
symbols = [symbol['symbol'] for symbol in symbols['symbols'] if symbol['quoteAsset'] == 'ETH']

What we're left with here is a list of any pair with ETH as the quote side of the pair. We will combine this data with the price history of ETH/USDT to create time series for each cryptocurrency quoted in USDT instead of ETH.

To do this we'll use our get_bars function to retrieve the data for ETH/USDT

ethusdt = get_bars('ETHUSDT')

We'll use this to create USDT weighted time series for each */ETH pair we have in our list of symbols.

Now let's download historical price data for all the symbols which trade against ETH on Binance:

price_data = []
new_symbols = []
for symbol in symbols:
    print(symbol)
    data = get_bars(symbol)
    new_symbols.append(symbol.replace('ETH','USDT'))
    price_data.append(data['c'].astype('float') * ethusdt['c'].astype('float'))

For each pair, we've stored the historical price data in a list named price_data

Thats not really terribly useful as is, so let's use pandas to merge all the individual pairs into one big dataframe:

combo = pd.concat(price_data, axis = 1)
combo.columns = new_symbols

You can see that we've got a shit ton of symbols in there:

combo.div(combo.ix[0]).plot(figsize=(16,9))

I cut off the legend but it goes on and on and on...

There's got to be a better way to visualize which crypto's flock together... And luckily for us there are much better ways to visualize this!! W00t

Consider the following graph:

mst = sm.MinimumSpanningTree(dataset = np.log(combo).diff().T)

This was created using the same price data that we tried to visualize before. But instead of plotting the raw price data, this graph plots each symbol as a circle or node on a graph. Nodes are connected via edges if their price history is extremely similar.

This type of graph is known as a Network Graph and this particular graph was created using an algorithm known as a Minimum Spanning Tree aka MST

Where is STEEM in this network graph?

We can see from the graph that the price history of STEEM / USDT is most similar to that of OMG / USDT. You might find that surprising, but a quick look at the side by side prices reveals a striking similarity over time:

There are other algorithms for creating networks out of correlations. Another algorithm is called the Correlation Filtered Graph. This is the graph which is produced from the same price data using the CFG algorithm.

cfg = sm.CorrelationFilteredGraph(dataset = np.log(combo).diff().T)

You can see that the CFG algo generates a lot more edges between nodes. The set of nodes which are directly connected to one particular node is known as the neighborhood of that node.

In the case of STEEM, its neighborhood consists of

cfg.neighborhood("STEEMUSDT")
"""
{'OMGUSDT': {'weight': 0.8315814103529764},
 'TNBUSDT': {'weight': 0.8197091679870396},
 'STORJUSDT': {'weight': 0.7506614670521796},
 'GRSUSDT': {'weight': 0.5237622731586296},
 'MANAUSDT': {'weight': 0.7429232474210377},
 'BNTUSDT': {'weight': 0.8303019286045419}}
"'"

We can plot the neighborhood of STEEM to verify the similarities of each price history:

This would suggest there exists a pretty good portfolio rebalancing trade between all of these crypto's, at least thats been the case over the time frame we looked at (the last month). Correlations are not constant in financial markets and cryptocurrencies are no exception to the rule. Thus it is important to update your model's internal correlation as market conditions evolve. However, this is a trivial process using our get_bars function combined with the script above. All one would have to do is download new data everyday and then you could update your trading models.

Another thing thats interesting about STEEM is that, in both graphs, it is positioned near the edge or periphery of the graph. Nodes which are near the periphery are some of the most unique nodes in the dataset, as nodes which are very similar to many other nodes tend to cluster towards the center of the graph.

The bad news is that STEEM appears to be one of the worst performing in its cluster over the last month.

Another way to look at portfolio diversification is really just maximizing the average distance between the nodes in your portfolio. If you have a portfolio which is clustered in the same part of the graph then you basically have multiple statistical copies of the same asset... When one goes down they all go down and visa versa. This is the opposite of diversification.

On the other hand, assembling a portfolio of symbols which are truly different from one another means when one crypto is down, its likely that another part of your portfolio is up, thus providing the hedging effect which is the foundation of successful diversification.

So where's EOS in all this madness?

Right next to Cardano, apparently!

If you're interested in creating these graphs yourself, please let me know. These particular network graphs we're created using proprietary software which requires an API key to use.


Follow @marketstack for more updates about trading, finance, and technology

Sort:  

I'd love to see a MST including BTC.

Then you could build a volatility harvesting portfolio for the entire crypto market.

great idea, i'll have to make that graph in a follow up post!

followed you btw, you had me at "volatility harvesting portfolio" and your top post has VWAP in it LOL i'll have to take a read later

Thanks for the follow! I follow your posts as well.

I'm definitely not a hard core quant like you seem to be, but I like to think I have a decent understanding of markets.

Very nice use of built-in network analysis libraries to show correlations. How would you convert this insight into a trading strategy? In other words, would you be able to correlate temporally with prior patterns and then project the one further along as a probable path for the one following? Very nice analysis again - really enjoyed this presentation. Do check my posts on Elliot waves of these crytpos and would appreciate your feedback.

easiest way to use something like this is using one of the members of a family of strats known as statistical arbitrage. the simplest version of that is pairs trading, where you find two highly correlation assets and then trade temporary "mispricings" in their long term equilibrium relationship

so for example, in the EOS / ADA example, you could build a linear model which says "given the price of ADA, EOS should be X" and when then real price of EOS is significantly different than X you take a position, either going long EOS and short ADA or visa versa.

of course in cash crypto markets its not always possible to really "short" and so to take advantage of the stat arb trade you would have to maintain inventories in both crypto's

I think you hit reply in the wrong place

Fascinating, a new way of date visualization.

It could be possible, that certain coins are influenced by human emotions similarly and therefore the price moves like that.

I know that Steem has a working product and may have been more stable through the bearish markets, while OMG has promising applications for ETH and it also stayed strong and unaffected as much.

Wonder what is similar to Tron and Verge. Or Bitconnect. That would be interesting to see (personal interest).

looks like TRX is hub of a big cluster here, and is directly connected to XVG

Very nice! I'm not sure I'll have time to use anything more practical you post, but I'd definitely like to read it anyway.

thanks for feedback! i'm gonna try to apply graph theory to some steem-specific data over the next few weeks e.g. mapping out voting networks, friend follow networks, witness voting... open to suggestions of areas to deep dive as well!

As a witness, I know that many are interested in the way that the top 20 witnesses keep their positions. It was also of interest to monitor the dead/inactive witnesses as it was speculated that some were deliberately voting for these dead witnesses to keep them in the top 50 to minimise the amount of real witnesses that steem users were seeing when they visited the witness voting page in steemit.com (which only displays the top 50). Currently though, this issue appears to have been resolved.

Identifying which witnesses receive votes from the same voters might be interesting in some way that isn't obvious to me at the moment.

Also, users who have received large delegations from Steemit Inc. should be a high priority, since it was claimed that the delegations were intended to help grow Steem but in some cases it is clear that the money has just been funnelled to the recipient and their friends.

thanks for the info

That will take me a while to absorb all of that knowledge but this will pay off for sure.Thanks for those guides sir.Informative and interesting posts are the reason why I followed you you.I really appreciate your effort in making those articles.Keep up your great work.

appreciate it

@marketstack Привет! Отлично, здорово, интересно и полезно!

rusteemteam!

mind-boggling.....
No other word to describe it. wow.

Thanks for those guides sir.Informative and interesting post.After visualizing alt coin we get more facilities..@marketstack

You write good articles for programmers. Please advise how to correctly withdraw money through the correct exchange. Otherwise, we will die of hunger.

not sure if i follow... u asking how u cash out?

I do not know how to withdraw money. Through which exchange? There is bittrex, blocktrades... What is the difference between exchanges ??

Great post,a very useful program,i support and strongly agree,thank you for sharing,@marketstack