Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

₿ Bitcoin Price Exploration and Strategy Experimentation

Bitcoin can be described as a peer-to-peer network that does not need a trusted third party to ensure the validity of electronic transactions. The bitcoin network started in early 2009 and the price of bitcoin at that time was hardly noteworthy. For the first few years it stayed under $5/bitcoin. But in the recent years, with decentralized money coming into vogue in the wider population, institutional adoption, such as ETF's provided by Fidelity, BlackRock, and the like, and other factors leading to people and companies alike to find alternative "risk on" assets during times of financial panic, bitcoin has shown that it might have a small-medium size place in ones portfolio in the coming decades.

This notebook follows both Peng Liu's Quantitative Trading Strategies Using Python: Technical Analysis, Statistical Testing, and Machine Learning and Yves Hilpisch's Python for Algorithmic Trading to look at metrics, scripts used for backtesting purposes, and trading strategies for bitcoin over the last 10 years; 2016 - 2020 as an exploratory timeframe, and 2021 - 2025 as a backtest period for different strategy implementations.

Below are two types of graphs, one showing different price movements and the other showing a boxplot of all the close prices during the same time frame. The top row is from January 2016 - December 2020. And the second, from January 2021 - December 2025.

bitcoin_price_chart (Data gathered from Yahoo! Finance)

Metrics

We will mainly look at the terminal return of bitcoin throughout this notebook, but if we were to compare bitcoin to other stocks, currencies, or commodities, the metrics below would give a good idea of the returns and risk associated with these assets.

  • terminal rate:

    • this approach ignores intermediate retuns and only considers the initial and terminal (final) asset prices; a simplified view of the asset price's grouth.
  • annualized return:

    • this metric compares different assets that might have different time-scales of returns. Bitcoin's returns are daily, but other assets might be monthly, quarterly, or yearly.
  • volatility:

    • this metric is the standard deviation (measure of volatility) of the returns. Measuring "how large the prices swing around the mean price and serves as a direct measure of the dispersion of returns." This metric plays an important role in assessing the risk tolerance in a portfolio.

    The higher the volatility, the higher the risk.

  • annualized volatility:

    • like the annualized return, this metric measures the volatility of different assets on different time-scale returns.
  • sharpe ratio:

    • More accurate assessment of investment's performance relative to overall market conditions.

    Higher ratio => investment yields higher returns for the same level of risk compared to other investments or the overall market.

  • max drawdown:

    • measures the worst case scenario - buy high, sell low. It is not a sure-fire metric for backtesting purposes because of it's sensativity to outliers, but might be interesting to look at because of how different the exploratory data and backtest data are.
Strategy Historical Prices (2016-2020) Backtest (2021-2025)
Terminal Return Annualized Returns Terminal Return Max Drawdown
Buy-n-Hold 6,540.27% 131.44% 126.00% -29.24%
Trend-Following (50 and 200-day SMA) 4,351.54% 113.65% 41.00% -54.77%
Momentum (2-day) 46,729.13% 241.92% 361.00% -51.59%
Mean Reversion -20.00% -4.36% 10.00% -63.32%
Advanced Strategy 3,066.67% 99.51% 15.00% -72.94%

Strategies

Moving Averages

Moving averages can also help identify support and resistance levels. Support level is a price/range where the price has had trouble falling below over a certain period, whereas the resistance level is one where the price has had trouble breaking past.

When short-term moving average crosses above the long-term moving average, it signals a buy action (enter long position).

When the oposite happens it signals a sell action (enter short position).

If only one trend line, the strategy works the same. If the price is greater than the moving average this signals a buy action, and vice versa.

Below, the chart shows the buy and sell signals:

sma-price-signals

This chart shows the cumulative returns of bitcoin and the cumulative returns of the trend-following strategy over the period 2016-2020.

sma-trend-following-chart

Backtest of 50 and 200-day moving averages over 2021-2025:

sma-backtest

This next chart shows the buy/sell signals for the EWMA strategy:

ewma-price-signals

This next chart shows the same as the last, but instead, using the .1 and .5-EWMA.

ewma-trend-following-chart

I do not include the backtested EWMA because of the exponential price chart that appears in both historical and backtested price ranges.

ALL charts do NOT include fees

Note The EWMA trend-following strategy gave a vastly better terminal return, but becuase it places more weight on the more recent prices, it is more prone to trigger buy/sell signals more often and thus eat any profits that might occur with the fees associated.

The EWMA had a total of 125 buy/sell signals whereas the SMA strategy only had 7.

Momentum

The momentum strategy focuses on exploiting market herding behavior that represents the most significant price movements.

Here, Strategy-0 is equivalent to the cumulative return of bitcoin over the 5 year period.

momentum-windows

momentum-backtest

Mean Reversion

Coming Soon

Advanced Trading Strategy

The advanced trading strategy will use the momentum indicator, as shown above, to capture the momentum of the market. Just because the market is moving in a certain direction on any given period, that doesn't necessarily mean that the trend should be bought/sold into. If there is high volatility then traders can get wiped out by fees, and that is where stop losses can play a role. One such indicator is the ATR or average true range, that gives a decent indicator of volatilty. Another indicator is the Directional Indicator (DI+/-) that gives a decent indication on where the market is trending. Using these as additional indicators let us see how the strategy compares to the others.

The first chart below might look messy at first, but when we look at the blue ATR line, notice that the buy/sell icons on the bottom hardly show up during the periods when the ATR line is above its mean, indicating that this is a period of high volatility in either the upward/downward direction. The DI+/- indicators in the background are added to show the messiness of price direction and why it is crutial to have additional idicators for stop loss purposes.

The second chart is the backtested advanced strategy.

advanced-trading-strategy-historical

advanced-trading-strategy-backtest

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages