Thursday, July 9, 2020

How BitMEX Wallets Impact the Price of Bitcoin


Part of building a profitable trading strategy is quickly testing novel ideas. These tend to be the money makers in the rare case that they prove useful once you can integrate them into your strategy.
Today, we will investigate one such idea. Here is the context: currently BitMEX — a large exchange for trading Bitcoin — allows withdraws from their wallets only one time per day. 
These withdraws — or outflows — can be quite high volume, so we would like to know if these so-called outflows of Bitcoin from BitMEX wallets are possibly an indicator of the Bitcoin market. Since I work for Amberdata, and Amberdata provides the best blockchain metrics and data, we will be using their product for today's analysis. All we need for this analysis is Amberdata’s Pro API and Python3 installed, so let us get started!

What is Inflow/Outflow?

Here we are looking at large movements of Bitcoin into and out of BitMEX wallets. More specifically, when somebody deposits money into, or makes a withdrawal from their BitMEX wallet; however, we do not have access to the Exchange’s data when users are depositing money, so we infer the flow of money into the wallets by watching their account balances on the blockchain. Simply put, inflow increases an address’ balance, while outflow decreases the address’ balance. 
Sanity check to make sure outflows only happen once a day
Just as a quick sanity check, I decided to look at the hourly account balances of a few recently active BitMEX BTC wallets. As expected, the wallet outflows line up to only once per day. Note: the value on the y-axis is in Shatoshi’s, not Bitcoin. We can also test this assumption directly with the code below. It groups the data from hourly to daily, and tests to see if there is more than one outflow per address per day. Testing this on our above data shows the assumption is safe for now. The full notebook for this analysis is available here.
combined = pd.DataFrame(data).T
combined.columns = [str(i) for i in range(N)]
combined = combined.applymap(lambda x: 1 if x > 0 else 0)
combined = combined.reset_index().rename({"index": "ts"}, axis=1)
combined['date'] = combined["ts"].apply(lambda df: datetime.datetime(year=df.year, month=df.month, day=df.day)) 
combined.set_index(combined["date"],inplace=True)
combined = combined.drop(["date", "ts"], axis=1)
combined = combined.resample('D').sum()
if combined.max(axis=1).max() == 1:
    print("Our assumtion is safe.")
else:
    print("Incorrect assumption!")
As we view the account balances of all addresses associated with BitMEX, we can start to get a picture of the net inflow/outflow of the exchange. Let us talk a little bit about the rationale behind our calculations.

The Calculation

Our primary interest is the money going in and coming out of BitMEX wallets, so let us talk a bit about how we calculate this. Imagine we have an eagle-eye view of the account balances on every BitMEX address. When one of the addresses, let’s call them A, makes a trade with another BitMEX address, B, for $100 worth of Bitcoin, then the transaction looks as such:
 A — 100 — >B.
In other words, address A has an outflow of $100, and address B has an inflow of $100. 
But, we are most interested in the flow of money in and out of the system: the BitMEX exchange. Hence, we take a look at the net flow of all addresses. We calculate the net flow as:
Net = In - Out.
In this view, we can compare the total money going into BitMEX Bitcoin wallets, and the total money going out. When there is a large move in either direction, this will show up on the net inflows/outflows.

The Analysis

Now that we have the calculation down, it is time to turn our attention to getting the data. As mentioned above, we are focusing in to BitMEX Bitcoin wallets, and since we want to capture as much activity as we can, we extend the look-back to one year from the current day. We collected as many addresses as we could for this analysis, approximately five hundred thousand in total. These addresses all start with either “3BMEX” or “3BitMEX”, and in our analysis “3BMEX” was much more common. In our case, the average activity addresses did not differ much between the two naming conventions.
Once you have your addresses — and these could be any set of addresses on supported blockchains — you can use this great little function to get the response from Amberdata:
def get_response(url, headers=None, queryString=None):
    "Get the REST response from the specified URL"
    if not headers:
        headers = {'x-api-key': api_key}
    if queryString:
        response = requests.request("GET", url, headers=headers, params=queryString)
    else:
        response = requests.request("GET", url, headers=headers)
    response = json.loads(response.text)
    try:
        if response["title"] == "OK":
            return response["payload"]
    except KeyError:
        return None
This works for all of the calls in the analysis, but using
response["payload"]
 may not be ideal for your use-case, depending on the endpoint. In this case we are accessing Amberdata’s Account Balance Historical endpoint, which looks a little like this:
url = "https://web3api.io/api/v2/addresses/" + address + "/account-balances/historical"

querystring = {"startDate": startTime, "endDate": endTime}

headers = {
    'x-amberdata-blockchain-id': "bitcoin-mainnet",
    'x-api-key': os.getenv("AMBERDATA_API_KEY")
}

payload = get_response(url, headers, querystring)
Where address is the address for which to get account balance. Our focus is on addresses on Bitcoin, so we use
bitcoin-mainnet
 for the blockchain ID. 
At five hundred thousand addresses over a year-long period, this computation can take upwards of 40 hours! This is where Python’s built-in support for concurrency comes in handy. Using a function called
daily_inflow_outflow
 in
utils.py
 to calculate the daily total inflow and outflow for an address, we can use Python’s
ThreadPoolExecutor
 to cut the execution time by 5x or more — depending on the speed of your machine. We save the aggregated daily flows as well as add them to the
gross_daily
 data frame. After running my analysis, the size of
all_activity
 was over 6 GB! This we can use later for digging deeper into the activity.
for i in tqdm.trange(len(addresses) // config.P):
        with ThreadPoolExecutor(max_workers=4) as executor:
            futures = {executor.submit(utils.daily_inflow_outflow, addresses[config.P*i+j], headers, querystring): addresses[config.P*i+j] for j in range(config.P) if i+j < len(addresses)}
            for future in as_completed(futures):
                address = futures[future]
                res = future.result()
                if res is not None:
                    gross_daily += res
                    all_activity[address] = res.to_json()
                else:
                    all_activity[address] = {}
We use some vectorized functions in Pandas to add together all of the inflows and outflows of these wallets, overlay the net with the price of bitcoin, and voila! 
Net inflow/outflow and Bitcoin price
First, note the obvious spikes in inflow/outflow. We might categorize these as anomalous behavior. These are the signals that we wish to evaluate in our strategy. We first difference the net flows, or — in time series terminology — introduce a lag of one. Simply put, we calculate the daily change in net flow. By plotting the lagged daily net flows on a histogram, we can get an idea of the distribution of daily change in net flow and can infer anomalies.
Net inflow/outflow differences, and standard deviations in red
Here, we are looking specifically for the outliers, so we plotted standard deviations from the mean by red lines. We choose the outer red line to indicate high changes in net flows. In plain English, if we observe a daily change in net inflow or outflow greater than 3 sigma, then it is a significant amount of flow out of or into BitMEX wallets. If we choose 3 standard deviations, we end up excluding 97.521% of the days from our analysis. Some sharp-eyed statisticians may notice that 97.521% is not the 99.7% we would expect from 3 sigma. Since this is just an empirical estimation based on historical values and our daily change in net flows do not look normally distributed, we cannot expect to see exact quantiles.
Now that we can identify abnormally high net flow days, we can plot our days of high inflow/outflow over the price of Bitcoin.
High inflow/outflow days in BitMEX and Bitcoin
Here we can see that high BitMEX outflow is typically accompanied by a sharp downturn in price. The event in October 2019, March 2020, and late May 2020 were all followed by huge outflows of Bitcoin in BitMEX wallets. We can drill down into these periods further and see what was going on.
A outflow period in 2019, where outflows followed a large price dip of 20%
The second high outflow period, in 2020
But, notice that it takes the current algorithm a day to recognize the change in flows. By that time it is likely already too late, so we can do better with a rolling standard deviation SMA, or Bollinger Bands. These will help us quickly identify when we are experiencing sufficiently larger flows than usual in either direction. Using slightly modified code from a Python website for Bollinger Bands:
def bollinger_bands(df,lookback,std,plot='no'):
    sym = list(df.columns.values)[0]
    rolling_mean = df.rolling(window=lookback,center=False).mean()
    rolling_std = df.rolling(window=lookback,center=False).std()
    upper_band = rolling_mean + std*rolling_std
    lower_band = rolling_mean - std*rolling_std
    lower_band.rename(columns={sym:'lower band'}, inplace=True)
    upper_band.rename(columns={sym:'upper band'}, inplace=True)
    rolling_mean.rename(columns={sym:'SMA'}, inplace=True)
if plot == 'yes':
        ax = df.plot(title="Net Flow BB", label=sym,color = 'blue')
        rolling_mean.plot(ax=ax, color='gold')
        upper_band.plot(ax=ax, color = 'cyan')
        lower_band.plot(ax=ax, color = 'cyan')
        ax.set_xlabel("Date")
        ax.set_ylabel("Price")
        ax.legend(loc='upper left', prop={'size':8})
        plt.savefig(sym+'_BollingerBands.png')
        plt.show()
    return rolling_mean,upper_band,lower_band
rolling_mean, upper, lower = bollinger_bands(data[["net"]], lookback=60, std=3, plot="yes")
Gives us this sort of painful looking graph:
60 SMA and 3 sigma BB
Where the cyan lines are the Bollinger Bands. When our series passes above or below those bands, it is a signal of anomalous behavior. Now we see just how much quicker we are at identifying large flows. The results differ drastically depending on the look-back window and and choice of sigma, so I encourage you to try that on your own. Plotting the days that exceed the Bollinger bands, we find a much tighter fit with price movements:
Bollinger Band Anomalous Flows
Zooming in to the two periods of high flows:
First period — 2019
Second period — 2020
We see that we have a lagging indicator and more false positives than we had in the previous method. False positives are a common downfall of Bollinger Bands, and they are usually paired with another indicator for this reason. Fortunately, we have a very clear picture of the highest activity period in mid March 2020. People rushed in after a drop of over 37.5% and bought as much as they could for two days, and many people also left on the third day after the drop. In the first period, we can see everybody selling after a drop of roughly 20%. Another great lesson to just HODL and keep moving forward.
Now, let us just have fun and look at the largest flows over the period. These are all of the flows greater than three standard deviations from normal values:
Flows > 3 sigma
Zooming in for more context:
3 Sigma Flows for the first period
3 Sigma Flows for the second period
This method is equivalent to drawing a horizontal line above and below the net flows and marking all days with net inflow above and net outflow below the line as anomalies. While the most direct way to view the activity we describe, here we are also the most prone to data snooping bias. Clearly, these outflow activities happen around the largest periods of price movements. However, since we cannot know the average standard deviations of net flow in advance we cannot say for sure that this sort of analysis is even possible. The only feasible way of running this live would be to use Bollinger Bands with a look-back window of 365 days, but we do not have enough data here to back-test it.
One drawback of this analysis is that we observe only daily flows. As we know, Bitcoin is traded live at all times of the day and night across the world, so it is to our detriment to split the prices by day. An interesting way to move this research further would be to drill down into the periods of high inflow/outflow and do an hourly or minute-by-minute analysis, and see how quickly we get the signal and can act on it.
In order to implement this concept in a live strategy, we can take advantage of Amberdata’s Addresses Balance Batch endpoint to monitor the live inflow and outflow of BitMEX wallets. When the outflows reach a certain threshold, we have an indication that a large market movement will precipitate. 




The full code for this post is available here.

Security Measures for Cryptocurrency Owners


Cryptocurrencies have so far become one of the most innovative forms of technology. They bring all the perks of new-age innovation to the financial space, essentially allowing people to make quick, anonymous, and much cheaper transactions that can travel over long distances.
Since breaking out on the scene in 2008, Bitcoin has been able to achieve a value close to $10,000 per token. At the same time, several other altcoins have seen surges in their prices as well. cryptocurrencies have finally solidified themselves as part of our financial system, and there’s truly no stopping them at this point.
While they do bring a wide array of benefits, there are also some risks for you to keep in mind. Of course, the first thing that most crypto detractors point out is the fact that cryptocurrencies remain unregulated and have a past with helping criminals to launder money.
In truth, there are a lot of countries that have failed to regulate cryptocurrencies to this day. However, this hasn’t stopped the assets from being even more practical and useful – even in those countries! As for the issue of criminal activity, pretty much the same thing can be said about traditional cash. Money laundering has been around long before Bitcoin was invented, and it will be around for as long as money exists.
Then, there is the risk of security. Most cryptocurrencies are stored on exchanges or individual wallets. However, like traditional bank accounts, there is the propensity for these asset custodians to suffer hacks.
In fact, there have been some significant hacks in the history of the crypto space.

Coincheck: 2018

In January 2018, Japanese crypto exchange Coincheck suffered an attack in which about $534 million in NWM coins were stolen. It’s worth noting that a hot wallet was the culprit in this theft. Still, the fact that the hackers were able to move such an amount was incredible.
Lon Wong, the President of the NEM Foundation, called it the largest theft in the history of the world. While that might be debatable, this hack remains the most significant in the crypto space.

Mt. Gox: 2011 to 2014

Another significant hack was that of Mt. Gox, a Japanese exchange that was the largest in the world at the time. In 2013, a staggering 70 percent of all Bitcoin exchanges in the world happened through this platform. Still, even then, the platform had developed some significant troubles.
The first major hack of Mt. Gox happened in 2011. At the time, 80,000 BTC was stolen from the exchange’s founder Jed McCaleb, as he tried to sell the exchange. A hacker was able to get into McCaleb’s account and use admin access to artificially drop the price of Bitcoin from about $17 to $0.01. this allowed for the transfer of about 2,000 BTC before the attack was found.
However, the most significant Mt. Gox attack happened between 2011 and 2014. At the end of those three years, information came out that the exchange had no funds in its cold wallets. About 850,000 BTC was stolen from the exchange within those three years, of which users owned about 750,000. At the time, the value of the entire stash of Bitcoins stolen as $469 million.
To be fair, there have been other significant hacks in the crypto industry since then. However, it’s also worth noting that events such as these have caused exchange operators to be more careful with how they operate their platforms. Security features have improved markedly since then, so the chances of such a hack happening again are, quite frankly, slim to none.
However, there is always the risk of a hack. Almost everyone who goes into cryptocurrencies has a clear understanding of that. So, it’s worth understanding how you can help yourself and keep you safe.
While exchanges like StormGain, Binance, and Coinbase are known for their high levels of security, cryptocurrency holders can also take some steps to be safer.
StormGain has particularly built up a reputation for providing optimal security for users. The exchange provides effective two-factor authentication, thus ensuring that users can effectively secure their accounts with multiple layers of verification.
In addition, StormGain’s platform is backed by top-notch encryption and can enhance your security based on your device. Like several other exchanges, StormGain remains a top choice for security.

Hardware Wallets: The Only Way Out

So far, one of the best ways to keep yourself safe is to store your valuable coins in a hardware wallet. A hardware wallet is essentially a physical device that stores your wallet keys. It’s not connected to the Internet, so there is no risk of someone hacking anything.
Most experts advise against storing massive amounts of cryptocurrency on an exchange or in any digital wallet app. All of these offer attackers various inroads to try infiltrating your wallet.
Hardware wallets cost no more than $150, and you can set them up quite easily. They also work great because you can recover a lost wallet and the details that it contains quite easily. So, you have all of the things you need in one place – easy handling, easy setup, optimal security, and a clear path to recovering a lost wallet.
With a hardware wallet, the only significant threat that you might end up suffering is if the details get in the hands of the wrong person. Keep your wallet well and you have no problems.
Another downside is that hardware wallets make transactions less quick. You can’t get a hardware wallet to issue transactions as quickly as the ones connected to the Internet. So, if you’re the type of cryptocurrency owner who prefers to make transactions regularly, you can transfer some assets from your hardware wallet to a digital one. At the very least, this minimizes the risk you are exposed to.

A Friendly Reminder that Cryptocurrency is the Future of Money


So far, the cryptocurrency industry has grown to be one of the most lucrative in the world. After a decade of existence, Bitcoin has so far become the best performing asset – period. Statistics from last year show that Bitcoin was the best –performing investment class of the year, as well as the past decade.
At this point, it has become clear to all that cryptocurrencies are the future of money. They might not have the level of regulation that industry insiders would have hoped by now, but that doesn’t mean that they’re worthless.

A Road to Recovery for Bitcoin and the Crypto Market

Last year, Bitcoin’s performance beat out that of bonds, stocks, treasury bills, as well as alternative assets like oil and gold. This was despite the fact that the asset began the year still trying to dig itself out of the hole it sank under in the 2018 crypto winter.
Recall that Bitcoin rose to a high of 19,700 in December 2017. At the time, the asset was on a roll that no one had seen, and everyone believed that this wouldn’t stop. Then, 2018 came and it was a different story. Bitcoin lost over 80 percent of its market value in the year, and people dumped the asset as they tried to save themselves from what seemed like a bubble.
Thus, Bitcoin had to begin 2019 with a price that was barely enough to look at. The asset was, in every sense of the word, a shadow of itself. Bitcoin began 2019 trading a little over the $3,000 mark, and there was little hope of a rise. However, the middle of the year came and it managed to surge. Soon enough, Bitcoin topped the $14,000 mark and the bulls were back.
To be fair, the asset wasn’t able to hold this price for long. It soon slipped back to $7,000, and it struggled to break the $7,500 comfort point before the year ended. Still, the fact that it managed to gain over 100 percent over the course of the year meant that it beat the returns of every major digital asset – and traditional investment vehicle in the world.
Other digital assets might not have had performances as stellar as that of Bitcoin, but they did pretty good in their own right as well.

2020: Holding Strong Despite the Coronavirus

All of these reinforced the belief that cryptocurrencies were the best bet for investors who had a high level of capital and were looking to earn the best returns. Not to be outgunned, institutions have also flocked into the space.
Both individual and institutional investors are so far very interested in this asset class, and with the coronavirus causing global economies – yes, even the strongest ones – to be on the brink of recessions, everyone has been looking for a way to hedge their wealth and protect themselves from a potential global economic meltdown.
It’s a no-brainer that people have been running into cryptocurrencies for the financial protection that they need. While traditional stocks and alternative assets took a beating as a result of the coronavirus, cryptocurrencies remained largely unaffected. Prices have maintained a steady level of growth, and investors are more confident that they have made a right choice.
Bitcoin itself has also rewarded the faith that many had in it. The asset started the year strong, quickly racing past the $10,000 mark and gunning for the highs of December 2017 – or, at the very least, a price peg that was similar to what it got in the middle of last year.
Sadly, the coronavirus came in and disrupted that momentum. The pandemic caused businesses and economies to shut down, with capital and stock markets closing with it. While cryptocurrencies tried to stave off this downturn, it was inevitable that they’d get affected too.
In the middle of March, Bitcoin dropped its price from $8,000 to $3,800, and many were in panic mode once more. Several believed that it would be an experience akin to that of 2018, but this wasn’t to be. The asset soon picked and returned to the $8,500 price point, and the halving also helped it to maintain a price that was well above comfortable for nervous investors.

Comparing with Traditional Markets

To be fair, Bitcoin hasn’t been able to break the $10,000 mark convincingly since then. However, the asset’s performance has still been a laudable one. And other digital assets have still been performing quite well too.
All of these have essentially made something clear – cryptocurrency investments are the best bet for anyone looking to steadily and consistently grow their wealth. Currently, traditional stocks are teetering on the brink, with countries themselves trying to stave off the effect of a recession.
In the United States, over 40 million have lost their jobs as companies continue to work around the effect of the virus. It is estimated that more people will lose their jobs, especially now with what seems to be a second wave of the pandemic coming in.
Several states have recorded surging coronavirus numbers in the past week, leading many to believe that the worst might not be over yet.
In all of this, Bitcoin and other cryptocurrencies have been able to maintain their value. They might not be growing so much, but they at least provide price and value stability – a great asset in a time when investors are simply trying to protect what they have.
A New Opportunity for Potential Investors
At the same time, there are some significant impediments to investors who would like to get into crypto. Many view Bitcoin and several large-cap altcoins as too expensive, and others tend to have difficulties concerning how they work.
For those investors that might not have so much money or who believe that Bitcoin and other large-cap assets are too “establishment,” then perhaps it’s time to take a gaze at the X-Token.
The X-Token is a unique platform that allows users to build social networking services, enjoy gaming and make some significant amounts of money while they’re at it. The description might sound like something that’s too good to be true, but it is.
How the it Works
Primarily, the X-Token platform is represented by two digital assets – the X-token and the M-token. The X-token functions as the main token, with investors purchasing it and gaining value for the money they put in. participants in the platform will also be able to gain money by holding the token, and they can earn some sizable passive income through it as well.
On the flip side, the M-token is an internal coin that is owned by the platform itself. Users use the M-token to conduct transactions between each other, whether to purchase services or to reward activities to other users who complete tasks on their niches.
When put together, the X-toke platform lets each participant to engage in promotions on existing social media channels. These include Vkontakte, Instagram, YouTube, and Telegram. Each investor immediately becomes a full member of the X-token ecosystem, and they receive a specific amount of tokens – of course, this amount is tied to the level of investment that they make.
Crypto investments are the surest way to protect your wealth at this point. With a myriad of options everywhere, it’s a buyer’s market at this point.