In recent years, cryptocurrencies have gained significant attention as both a medium of exchange and an investment asset. Unlike traditional fiat currencies—such as the US Dollar or the Euro, which are issued and regulated by central authorities like national banks—cryptocurrencies operate within decentralized frameworks. These digital assets rely on cryptographic protocols and distributed ledger technologies, such as blockchain, to maintain transaction records and network integrity.
While both fiat and digital currencies serve similar economic functions, they differ fundamentally in governance and infrastructure. This distinction has contributed to the growing interest in the potential benefits and challenges associated with cryptocurrency adoption.
From an investment perspective, trading cryptocurrencies can offer lucrative opportunities. However, the process often demands considerable time and attention, particularly in monitoring market trends and making timely decisions. To address this, algorithmic trading presents a compelling alternative. By encoding specific trading rules into software, it becomes possible to automate buy and sell decisions, enabling systematic participation in financial markets. When carefully designed and tested, such strategies may offer increased efficiency and consistency in trade execution.
This project serves as an introduction to algorithmic trading through the development and evaluation of a basic strategy, specifically the Simple Moving Average (SMA) Crossover strategy.
The core idea of the SMA Crossover strategy is:
- A Buy Signal is generated when a short-term SMA crosses above a long-term SMA.
- A Sell Signal is generated when a short-term SMA crosses below a long-term SMA.
This project includes:
- Fetching historical cryptocurrency price data using
yfinance
. - Calculating SMAs and generating trading signals.
- Implementing a backtesting engine to simulate trades.
- Evaluating strategy performance against a "Buy and Hold" baseline using metrics like Total Return, Annualized Return, Sharpe Ratio, Max Drawdown, and Win Rate.
- Visualizing price data, trading signals, and portfolio performance.
crypto_trading.ipynb
: The main Jupyter Notebook containing all the code, analysis, and visualizations.README.md
: This file.requirements.txt
: A list of Python packages required to run the notebook..gitignore
: Specifies intentionally untracked files that Git should ignore.images/
: A directory containing plots generated by the notebook.LICENSE
: the license for the project.
-
Clone the repository:
git clone https://github.com/codebywiam/algorithmic-crypto-trading.git
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the Jupyter Notebook:
jupyter notebook crypto_trading_project.ipynb
-
Inside the notebook, you can modify the parameters in Project Parameters:
TICKER = "BTC-USD" START_DATE = "2024-01-01" END_DATE = "2025-01-01" INTERVAL = "1d" SHORT_SMA_WINDOW = 10 LONG_SMA_WINDOW = 40 INITIAL_CAPITAL = 10000.0 RISK_FREE_RATE = 0.02
- Performance Metrics:
- Strategy Total Return: 44.93%
- Buy & Hold Total Return: 111.53%
- Strategy Annualized Return: 29.11%
- Buy & Hold Annualized Return: 67.51%
- Strategy Sharpe Ratio: 0.85
- Buy & Hold Sharpe Ratio: 1.34
- Strategy Max Drawdown: -36.78%
- Buy & Hold Max Drawdown: -26.18%
- Strategy Win Rate: 33.33% (1 out of 3 trades)
For the 2024 period analyzed, the SMA Crossover strategy (10/40) significantly underperformed a simple Buy & Hold approach for Bitcoin. While it exhibited slightly lower volatility, it failed to capture the upside and resulted in a lower risk-adjusted return (Sharpe Ratio) and a higher maximum drawdown. The low win rate (33.33%) over a small number of trades (3) indicates that the chosen parameters were not well-suited for the market conditions during this period.
-
Performance Metrics (2020):
- Strategy Total Return: 288.31%
- Buy & Hold Total Return: 302.79%
- Strategy Annualized Return: 154.48%
- Buy & Hold Annualized Return: 160.98%
- Strategy Annual Volatility: 41.18%
- Buy & Hold Annual Volatility: 59.86%
- Strategy Sharpe Ratio: 2.43
- Buy & Hold Sharpe Ratio: 1.89
- Strategy Max Drawdown: -23.60%
- Buy & Hold Max Drawdown: -51.86%
- Strategy Win Rate: 100.00% (3 out of 3 trades)
-
Interpretation (2020 Data): In a strong bull market like 2020, the SMA 10/40 strategy showed competitive performance. It delivered a higher Sharpe Ratio and lower drawdown than Buy & Hold, despite a slightly lower total return. This demonstrates how trend-following strategies may excel in trending markets but underperform in volatile or sideways conditions.
This outcome underscores the challenge of market timing and emphasizes that a strategy’s effectiveness—and its optimal parameters—can vary significantly across different market conditions and timeframes, highlighting the importance of robustness and adaptability in strategy design.
- Past performance is not indicative of future results.
- Transaction Costs: This simulation excludes trading fees or slippage.
- Market Impact: Assumes trades do not move market prices.
- Overfitting Risk: SMA windows may not generalize across timeframes.
- Simplicity: Real-world strategies typically involve more factors.
Potential Future Enhancements:
- Parameter optimization via grid/random search.
- Additional indicators (RSI, MACD, Bollinger Bands).
- Model realistic transaction costs and slippage.
- Use dynamic risk management (e.g., stop-loss, position sizing).
- Broaden asset universe and test across timeframes.
- Implement walk-forward optimization for robustness.
This project is licensed under the MIT License. See the LICENSE file for details.
This project is for educational purposes only and does not constitute financial advice.