---
title: "Bitcoin Metadata"
description: "Bitcoin Metadata dataset from Blockchain."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/blockchain/bitcoin-metadata
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/blockchain/bitcoin-metadata.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/14%20Datasets/07%20Blockchain/01%20Bitcoin%20Metadata
modified: 2026-10-29
index: https://www.quantconnect.com/llms.txt
---

# Bitcoin Metadata

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Datasets > [Blockchain](https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/blockchain.md) > Bitcoin Metadata

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

The Bitcoin Metadata dataset by Blockchain provides 23 fundamental metadata of Bitcoin directly fetched from the Bitcoin blockchain. The data starts in January 2009 and delivered on a daily frequency. This dataset contains mining statistics like hash rate and miner revenue; transaction metadata like transaction per block, transaction fee, and number of addresses; and blockchain metadata like blockchain size and block size.

For more information about the Bitcoin Metadata dataset, including CLI commands and pricing, see the [dataset listing](https://www.quantconnect.com/datasets/bitcoin-metadata).

## About the Provider

[Blockchain](https://www.blockchain.com) is a website that publishes data related to Bitcoin. It has been online since 2011 and publishes the Bitcoin Metadata history back to 2009.

## Getting Started

The following snippet demonstrates how to request data from the Bitcoin Metadata dataset:

```python
self.btcusd = self.add_crypto("BTCUSD", Resolution.DAILY, Market.BITFINEX).symbol
self.dataset_symbol = self.add_data(BitcoinMetadata, self.btcusd).symbol
```

## Data Summary

The following table describes the dataset properties:

| Property | Value |
| --- | --- |
| Start Date | January 2009 |
| Coverage | Bitcoin blockchain |
| Data Density | Regular |
| Resolution | Daily |
| Timezone | UTC |

## Requesting Data

To add Bitcoin Metadata data to your algorithm, call the `add_data` method with the BTCUSD `Symbol`. Save a reference to the dataset `Symbol` so you can access the data later in your algorithm.

```python
class BlockchainBitcoinMetadataAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2019, 1, 1)
        self.set_end_date(2020, 6, 1)
        self.set_cash(100000)

        self.btcusd = self.add_crypto("BTCUSD", Resolution.DAILY, Market.BITFINEX).symbol
        self.dataset_symbol = self.add_data(BitcoinMetadata, self.btcusd).symbol
```

## Accessing Data

To get the current Bitcoin Metadata data, index the current [`Slice`](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) with the dataset `Symbol`. Slice objects deliver unique events to your algorithm as they happen, but the `Slice` may not contain data for your dataset at every time step. To avoid issues, check if the `Slice` contains the data you want before you index it.

```python
def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self.dataset_symbol):
        data_point = slice[self.dataset_symbol]
        self.log(f"{self.dataset_symbol} miner revenue at {slice.time}: {data_point.miners_revenue}")
```

To iterate through all of the dataset objects in the current `Slice`, call the `get` method.

```python
def on_data(self, slice: Slice) -> None:
    for dataset_symbol, data_point in slice.get(BlockchainBitcoinData).items():
        self.log(f"{dataset_symbol} miner revenue at {slice.time}: {data_point.miners_revenue}")
```

## Historical Data

To get historical Bitcoin Metadata data, call the `history` method with the dataset `Symbol`. If there is no data in the period you request, the history result is empty.

```python
# DataFrame
history_df = self.history(self.dataset_symbol, 100, Resolution.DAILY)

# Dataset objects
history_bars = self.history[BlockchainBitcoinData](self.dataset_symbol, 100, Resolution.DAILY)
```

For more information about historical data, see [History Requests](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/history-requests.md).

## Remove Subscriptions

To remove a subscription, call the `remove_security` method.

```python
self.remove_security(self.dataset_symbol)
```

## Example Applications

The Bitcoin Metadata dataset enables you to incorporate metadata from the Bitcoin blockchain into your strategies. Examples include the following strategies:

- Comparing mining and transaction statistics to provide insight on the supply-demand relationship of the Bitcoin blockchain service.
- Measuring the activity and popularity of the Bitcoin blockchain to predict the price movements of the Cryptocurrency.

### Classic Algorithm Example

The following example algorithm tracks the transaction-to-hash-rate ratio of the Bitcoin network. The algorithm holds Bitcoin when the ratio increases. Otherwise, it holds dollars.

```python
from AlgorithmImports import *

class BlockchainBitcoinMetadataAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)

        # Request BTCUSD as the trading vehicle on Bitcoin Metadata
        self.btcusd = self.add_crypto("BTCUSD", Resolution.MINUTE, Market.BITFINEX).symbol
        # Request Bitcoin Metadata for trade signal generation
        self.bitcoin_metadata_symbol = self.add_data(BitcoinMetadata, self.btcusd).symbol

        # Historical data
        history = self.history(BitcoinMetadata, self.bitcoin_metadata_symbol, 60, Resolution.DAILY)
        self.debug(f"We got {len(history)} items from our history request for {self.btcusd} Blockchain Bitcoin Metadata")

        # Cache the last supply-demand ratio for comparison
        self.last_demand_supply = None

    def on_data(self, slice: Slice) -> None:
        # Trade only based on updated Bitcoin Metadata
        data = slice.get(BitcoinMetadata)

        if self.bitcoin_metadata_symbol in data and data[self.bitcoin_metadata_symbol] != None and data[self.bitcoin_metadata_symbol].hash_rate:
            # Calculate the supply-demand ratio to estimate the microeconomy structure of Bitcoin for scalp-trading
            # Transaction number as demand, hash production rate as supply
            current_demand_supply = data[self.bitcoin_metadata_symbol].numberof_transactions / data[self.bitcoin_metadata_symbol].hash_rate

            # Comparing the average transaction-to-hash-rate ratio changes, buy Bitcoin if demand is higher than supply, sell vice versa
            if self.last_demand_supply != None and current_demand_supply > self.last_demand_supply:
                self.set_holdings(self.btcusd, 1)
            else:
                self.set_holdings(self.btcusd, 0)

            self.last_demand_supply = current_demand_supply
```

### Framework Algorithm Example

The following example algorithm tracks the transaction-to-hash-rate ratio of the Bitcoin network. The algorithm holds Bitcoin when the ratio increases. Otherwise, it holds dollars.

```python
from AlgorithmImports import *

class BlockchainBitcoinMetadataFrameworkAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)

        # Universe contains only BTCUSD as the trading vehicle on Bitcoin Metadata
        self.add_universe_selection(
            ManualUniverseSelectionModel(
            Symbol.create("BTCUSD", SecurityType.CRYPTO, Market.BITFINEX)
        ))
        # Custom alpha model that emit insights based on Bitcoin Metadata
        self.add_alpha(BlockchainBitcoinMetadataAlphaModel())
        # Equally invest to evenly dissipate the capital concentration risk from non-sysmtematic risky events
        self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel())

class BlockchainBitcoinMetadataAlphaModel(AlphaModel):

    def __init__(self) -> None:
        self.bitcoin_metadata_symbol_by_symbol = {}
        # Cache the last supply-demand ratio for comparison
        self.last_demand_supply = {}

    def update(self, algorithm:QCAlgorithm, slice: Slice) -> List[Insight]:
        insights = []

        # Trade only based on updated Bitcoin Metadata
        data = slice.get(BitcoinMetadata)

        for symbol, bitcoin_metadata_symbol in self.bitcoin_metadata_symbol_by_symbol.items():
            if data.contains_key(bitcoin_metadata_symbol) and data[bitcoin_metadata_symbol] != None and data[bitcoin_metadata_symbol].hash_rate:
                # Calculate the supply-demand ratio to estimate the microeconomy structure of the crypto pair for scalp-trading
                # Transaction number as demand, hash production rate as supply
                current_demand_supply = data[bitcoin_metadata_symbol].numberof_transactions / data[bitcoin_metadata_symbol].hash_rate

                # Comparing the average transaction-to-hash-rate ratio changes, buy coin if demand is higher than supply
                if symbol in self.last_demand_supply and current_demand_supply > self.last_demand_supply[symbol]:
                    insights.append(Insight.price(symbol, timedelta(1), InsightDirection.UP))

                self.last_demand_supply[symbol] = current_demand_supply

        return insights

    def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None:
        for security in changes.added_securities:
            symbol = security.symbol

            # Request Bitcoin Metadata for trade signal generation
            bitcoin_metadata_symbol = algorithm.add_data(BitcoinMetadata, symbol).symbol

            self.bitcoin_metadata_symbol_by_symbol[symbol] = bitcoin_metadata_symbol

            # Historical data
            history = algorithm.history(BitcoinMetadata, bitcoin_metadata_symbol, 60, Resolution.DAILY)
```

## Data Point Attributes

The Bitcoin Metadata dataset provides `BitcoinMetadata` objects, which have the following attributes:

*Members of `QuantConnect.DataSource.BitcoinMetadata` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.DataSource.BitcoinMetadata&language=python*
