Blockchain
Bitcoin Metadata
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.
About the Provider
Blockchain 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:
self.btcusd = self.add_crypto("BTCUSD", Resolution.DAILY, Market.BITFINEX).symbol
self.dataset_symbol = self.add_data(BitcoinMetadata, self.btcusd).symbol _symbol = AddCrypto("BTCUSD", Resolution.Daily, Market.Bitfinex).Symbol;
_datasetSymbol = AddData<BitcoinMetadata>(_symbol).Symbol;
Requesting Data
To add Bitcoin Metadata data to your algorithm, call the AddDataadd_data method with the BTCUSD Symbol. Save a reference to the dataset Symbol so you can access the data later in your algorithm.
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 public class BlockchainBitcoinMetadataAlgorithm: QCAlgorithm
{
private Symbol _symbol, _datasetSymbol;
public override void Initialize()
{
SetStartDate(2019, 1, 1);
SetEndDate(2020, 6, 1);
SetCash(100000);
_symbol = AddCrypto("BTCUSD", Resolution.Daily, Market.Bitfinex).Symbol;
_datasetSymbol = AddData<BitcoinMetadata>(_symbol).Symbol;
}
}
Accessing Data
To get the current Bitcoin Metadata data, index the current Slice 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.
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}") public override void OnData(Slice slice)
{
if (slice.ContainsKey(_datasetSymbol))
{
var dataPoint = slice[_datasetSymbol];
Log($"{_datasetSymbol} miner revenue at {slice.Time}: {dataPoint.MinersRevenue}");
}
}
To iterate through all of the dataset objects in the current Slice, call the Getget method.
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}")
public override void OnData(Slice slice)
{
foreach (var kvp in slice.Get<BlockchainBitcoinData>())
{
var datasetSymbol = kvp.Key;
var dataPoint = kvp.Value;
Log($"{datasetSymbol} miner revenue at {slice.Time}: {dataPoint.MinersRevenue}");
}
}
Historical Data
To get historical Bitcoin Metadata data, call the Historyhistory method with the dataset Symbol. If there is no data in the period you request, the history result is empty.
# DataFrame history_df = self.history(self.dataset_symbol, 100, Resolution.DAILY) # Dataset objects history_bars = self.history[BlockchainBitcoinData](self.dataset_symbol, 100, Resolution.DAILY)
var history = History<BlockchainBitcoinData>(_datasetSymbol, 100, Resolution.Daily);
For more information about historical data, see History Requests.
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.
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
public class BlockchainBitcoinMetadataAlgorithm : QCAlgorithm
{
private Symbol _bitcoinMetadataSymbol, _btcSymbol;
// Cache the last supply-demand ratio for comparison
private decimal? _lastDemandSupply = null;
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
SetCash(100000);
// Request BTCUSD as the trading vehicle on Bitcoin Metadata
_btcSymbol = AddCrypto("BTCUSD", Resolution.Minute, Market.Bitfinex).Symbol;
// Request Bitcoin Metadata for trade signal generation
_bitcoinMetadataSymbol = AddData<BitcoinMetadata>(_btcSymbol).Symbol;
// Historical data
var history = History(new[]{_bitcoinMetadataSymbol}, 60, Resolution.Daily);
Debug($"We got {history.Count()} items from our history request for {_btcSymbol} Blockchain Bitcoin Metadata");
}
public override void OnData(Slice slice)
{
// Trade only based on updated Bitcoin Metadata
var data = slice.Get<BitcoinMetadata>();
if (!data.IsNullOrEmpty())
{
// Calculate the supply-demand ratio to estimate the microeconomy structure of Bitcoin for scalp-trading
// Transaction number as demand, hash production rate as supply
if (data[_bitcoinMetadataSymbol].HashRate == 0)
{
return;
}
var currentDemandSupply = data[_bitcoinMetadataSymbol].NumberofTransactions / data[_bitcoinMetadataSymbol].HashRate;
// Comparing the average transaction-to-hash-rate ratio changes, buy Bitcoin if demand is higher than supply, sell vice versa
if (_lastDemandSupply != null && currentDemandSupply > _lastDemandSupply)
{
SetHoldings(_btcSymbol, 1);
}
else
{
SetHoldings(_btcSymbol, 0);
}
_lastDemandSupply = currentDemandSupply;
}
}
}
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.
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) public class BlockchainBitcoinMetadataFrameworkAlgorithm : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
SetCash(100000);
// Universe contains only BTCUSD as the trading vehicle on Bitcoin Metadata
AddUniverseSelection(
new ManualUniverseSelectionModel(
QuantConnect.Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Bitfinex)
));
// Custom alpha model that emit insights based on Bitcoin Metadata
AddAlpha(new BlockchainBitcoinMetadataAlphaModel());
// Equally invest to evenly dissipate the capital concentration risk from non-sysmtematic risky events
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
}
}
public class BlockchainBitcoinMetadataAlphaModel: AlphaModel
{
private Dictionary<Symbol, Symbol> _bitcoinMetadataSymbolBySymbol = new Dictionary<Symbol, Symbol>();
// Cache the last supply-demand ratio for comparison
private Dictionary<Symbol, decimal> _lastDemandSupply = new Dictionary<Symbol, decimal>();
public BlockchainBitcoinMetadataAlphaModel(){}
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice slice)
{
var insights = new List<Insight>();
// Trade only based on updated Bitcoin Metadata
var data = slice.Get<BitcoinMetadata>();
if (!data.IsNullOrEmpty())
{
foreach(var kvp in _bitcoinMetadataSymbolBySymbol)
{
var symbol = kvp.Key;
var bitcoinMetadataSymbol = kvp.Value;
// 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
if (data[bitcoinMetadataSymbol].HashRate == 0) continue;
var currentDemandSupply = data[bitcoinMetadataSymbol].NumberofTransactions / data[bitcoinMetadataSymbol].HashRate;
// Comparing the average transaction-to-hash-rate ratio changes, buy coin if demand is higher than supply
if (_lastDemandSupply.ContainsKey(symbol) && currentDemandSupply > _lastDemandSupply[symbol])
{
insights.Add(Insight.Price(symbol, TimeSpan.FromDays(1), InsightDirection.Up));
}
_lastDemandSupply[symbol] = currentDemandSupply;
}
}
return insights;
}
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
foreach (var security in changes.AddedSecurities)
{
var symbol = security.Symbol;
// Request Bitcoin Metadata for trade signal generation
var bitcoinMetadataSymbol = algorithm.AddData<BitcoinMetadata>(symbol).Symbol;
_bitcoinMetadataSymbolBySymbol.Add(symbol, bitcoinMetadataSymbol);
// Historical data
var history = algorithm.History(new[]{bitcoinMetadataSymbol}, 60, Resolution.Daily);
}
}
}