CoinAPI
Binance Crypto Future Price Data
Introduction
The Binance Crypto Future Price Data by CoinAPI is for Crypto-currency futures price and volume data points. The data covers 421 Cryptocurrency pairs, starts in August 2020, and is delivered on any frequency from tick to daily. This dataset is created by monitoring the trading activity on Binance.
The Binance Crypto Future Margin Rate Data dataset provides margin interest data to model margin costs.
For more information about the Binance Crypto Future Price Data dataset, including CLI commands and pricing, see the dataset listing.
About the Provider
CoinAPI was founded by Artur Pietrzyk in 2016 with the goal of providing real-time and historical cryptocurrency market data, collected from hundreds of exchanges. CoinAPI provides access to Cryptocurrencies for traders, market makers, and developers building third-party applications.
Getting Started
The following snippet demonstrates how to request data from the Binance Crypto Future Price dataset:
def initialize(self) -> None: self.set_brokerage_model(BrokerageName.BINANCE_FUTURES, AccountType.MARGIN) self.set_brokerage_model(BrokerageName.BINANCE_COIN_FUTURES, AccountType.MARGIN) self.crypto_future_symbol = self.add_crypto_future("BTCBUSD", Resolution.MINUTE).symbol
private Symbol _cryptoFutureSymbol; public override void Initialize { SetBrokerageModel(BrokerageName.BinanceFutures, AccountType.Margin); SetBrokerageModel(BrokerageName.BinanceCoinFutures, AccountType.Margin); // perpetual futures does not have a filter function _cryptoFutureSymbol = AddCryptoFuture("BTCBUSD", Resolution.Minute).Symbol; }
Data Summary
The following table describes the dataset properties:
Property | Value |
---|---|
Start Date | August 2020 |
Asset Coverage | 421 Crypto Futures Pairs |
Data Density | Dense |
Resolution | Tick, Second, Minute, Hourly, & Daily |
Timezone | UTC |
Market Hours | Always Open |
Requesting Data
To add Binance Crypto Future Price data to your algorithm, call the AddCryptoFuture
add_crypto_future
method. Save a reference to the Crypto Future Symbol
so you can access the data later in your algorithm.
class CoinAPIDataAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2020, 6, 1) self.set_end_date(2021, 6, 1) # Set Account Currency to Binance Stable Coin for USD self.set_account_currency("BUSD") self.set_cash(100000) self.set_brokerage_model(BrokerageName.BINANCE_FUTURES, AccountType.MARGIN) self.set_brokerage_model(BrokerageName.BINANCE_COIN_FUTURES, AccountType.MARGIN) crypto_future = self.add_crypto_future("BTCBUSD", Resolution.MINUTE) # perpetual futures does not have a filter function self.btcbusd = crypto_future.symbol
public class CoinAPIDataAlgorithm : QCAlgorithm { private Symbol _symbol ; public override void Initialize() { SetStartDate(2020, 6, 1); SetEndDate(2021, 6, 1); // Set Account Currency to Binance Stable Coin for USD SetAccountCurrency("BUSD"); SetCash(100000); SetBrokerageModel(BrokerageName.BinanceFutures, AccountType.Margin); SetBrokerageModel(BrokerageName.BinanceCoinFutures, AccountType.Margin); var cryptoFuture = AddCryptoFuture("BTCBUSD", Resolution.Minute); // perpetual futures does not have a filter function _symbol = cryptoFuture.Symbol; } }
For more information about creating Crypto Future subscriptions, see Requesting Data.
Accessing Data
To get the current Binance Crypto Future Price data, index the Bars
bars
, QuoteBars
quote_bars
, or Ticks
ticks
properties of the current Slice
with the Crypto Future Symbol
. Slice
objects deliver unique events to your algorithm as they happen, but the Slice
may not contain data for your security 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 self.btcbusd in slice.bars: trade_bar = slice.bars[self.btcbusd] self.log(f"{self.btcbusd} close at {slice.time}: {trade_bar.close}") if self.btcbusd in slice.quote_bars: quote_bar = slice.quote_bars[self.btcbusd] self.log(f"{self.btcbusd} bid at {slice.time}: {quote_bar.bid.close}") if self.btcbusd in slice.ticks: ticks = slice.ticks[self.btcbusd] for tick in ticks: self.log(f"{self.btcbusd} price at {slice.time}: {tick.price}")
public override void OnData(Slice slice) { if (slice.Bars.ContainsKey(_symbol)) { var tradeBar = slice.Bars[_symbol]; Log($"{_symbol} price at {slice.Time}: {tradeBar.Close}"); } if (slice.QuoteBars.ContainsKey(_symbol)) { var quoteBar = slice.QuoteBars[_symbol]; Log($"{_symbol} bid at {slice.Time}: {quoteBar.Bid.Close}"); } if (slice.Ticks.ContainsKey(_symbol)) { var ticks = slice.Ticks[_symbol]; foreach (var tick in ticks) { Log($"{_symbol} price at {slice.Time}: {tick.Price}"); } } }
You can also iterate through all of the data objects in the current Slice
.
def on_data(self, slice: Slice) -> None: for symbol, trade_bar in slice.bars.items(): self.log(f"{symbol} close at {slice.time}: {trade_bar.close}") for symbol, quote_bar in slice.quote_bars.items(): self.log(f"{symbol} bid at {slice.time}: {quote_bar.bid.close}") for symbol, ticks in slice.ticks.items(): for tick in ticks: self.log(f"{symbol} price at {slice.time}: {tick.price}")
public override void OnData(Slice slice) { foreach (var kvp in slice.Bars) { var symbol = kvp.Key; var tradeBar = kvp.Value; Log($"{symbol} price at {slice.Time}: {tradeBar.Close}"); } foreach (var kvp in slice.QuoteBars) { var symbol = kvp.Key; var quoteBar = kvp.Value; Log($"{symbol} bid at {slice.Time}: {quoteBar.Bid.Close}"); } foreach (var kvp in slice.Ticks) { var symbol = kvp.Key; var ticks = kvp.Value; foreach (var tick in ticks) { Log($"{symbol} price at {slice.Time}: {tick.Price}"); } } }
For more information about accessing Crypto Future data, see Handling Data.
Historical Data
To get historical Binance Crypto Future Price data, call the History
history
method with the Crypto Future Symbol
. If there is no data in the period you request, the history result is empty.
# DataFrame history_df = self.history(self.btcbusd, 100, Resolution.DAILY) # TradeBar objects history_trade_bars = self.history[TradeBar](self.btcbusd, 100, Resolution.MINUTE) # QuoteBar objects history_quote_bars = self.history[QuoteBar](self.btcbusd, 100, Resolution.MINUTE) # Tick objects history_ticks = self.history[Tick](self.btcbusd, timedelta(seconds=10), Resolution.TICK)
// TradeBar objects var historyTradeBars = History(_symbol, 100, Resolution.Daily); // QuoteBar objects var historyQuoteBars = History<QuoteBar>(_symbol, 100, Resolution.Minute); // Tick objects var historyTicks = History<Tick>(_symbol, TimeSpan.FromSeconds(10), Resolution.Tick);
For more information about historical data, see History Requests.
Remove Subscriptions
To unsubscribe from a Crypto Future contract that you added with the AddCryptoFuture
add_crypto_future
method, call the RemoveSecurity
remove_security
method.
self.remove_security(self.btcbusd)
RemoveSecurity(_symbol);
The RemoveSecurity
remove_security
method cancels your open orders for the security and liquidates your Crypto Future holdings.
Example Applications
The Binance Crypto Future Price dataset enables you to accurately design strategies for Cryptocurrencies with term structure. Examples include the following strategies:
- Horizontal/Diagonal arbitrage with the underlying cryptocurrencies
- Trade Contango/Backwardation predictions
- Hedge for illiquid cryptocurrencies
Classic Algorithm Example
The following example algorithm buys BTCBUSD perpetual future contract if the last day's close price was close to ask close price than bid close price, sells short of that in opposite, through the Binance exchange:
from AlgorithmImports import * class BinanceCryptoFutureDataAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2022, 10, 1) self.set_end_date(2022, 10, 10) # Set Account Currency to Binance Stable Coin, since USD-BUSD will not auto-convert and USD cannot be used to trade self.set_cash("BUSD", 100000) # Binance Futures Exchange accepts both Cash and Margin account types, select the one you need for the best reality modeling. self.set_brokerage_model(BrokerageName.BINANCE_FUTURES, AccountType.MARGIN) # Requesting data, we only trade on BTCBUSD Future in Binance Future exchange crypto_future = self.add_crypto_future("BTCBUSD", Resolution.DAILY) # perpetual futures does not have a filter function self.btcbusd = crypto_future.symbol # Historical data history = self.history(self.btcbusd, 10, Resolution.DAILY) self.debug(f"We got {len(history)} from our history request for {self.btcbusd}") def on_data(self, slice: Slice) -> None: # Trade only based on updated price data if not slice.bars.contains_key(self.btcbusd) or not slice.quote_bars.contains_key(self.btcbusd): return quote = slice.quote_bars[self.btcbusd] price = slice.bars[self.btcbusd].price # Scalp-trade the bid-ask spread based on the supply-demand strength if price - quote.bid.close > quote.ask.close - price: self.set_holdings(self.btcbusd, -1) else: self.set_holdings(self.btcbusd, 1)
public class BinanceCryptoFutureDataAlgorithm : QCAlgorithm { public Symbol _symbol; public override void Initialize() { SetStartDate(2022, 1, 1); SetEndDate(2023, 1, 1); // Set Account Currency to Binance Stable Coin, since USD-BUSD will not auto-convert and USD cannot be used to trade SetCash("BUSD", 100000); // Binance Futures Exchange accepts both Cash and Margin account types, select the one you need for the best reality modeling. SetBrokerageModel(BrokerageName.BinanceFutures, AccountType.Margin); // Requesting data, we only trade on BTCBUSD Future in Binance Future exchange var cryptoFuture = AddCryptoFuture("BTCBUSD", Resolution.Daily); // perpetual futures does not have a filter function _symbol = cryptoFuture.Symbol; // Historical data var history = History(_symbol, 10, Resolution.Daily); Debug($"We got {history.Count()} from our history request for {_symbol}"); } public override void OnData(Slice slice) { // Trade only based on updated price data if (!slice.Bars.ContainsKey(_symbol) || !slice.QuoteBars.TryGet(_symbol, out var quote)) { return; } var price = slice.Bars[_symbol].Price; // Scalp-trade the bid-ask spread based on the supply-demand strength if (price - quote.Bid.Close > quote.Ask.Close - price) { SetHoldings(_symbol, -1m); } else { SetHoldings(_symbol, 1m); } } }
Framework Algorithm Example
The following example algorithm hold a 100% long BTCBUSD future portfolio if the last day's close price was close to ask close price than bid close price, while hold short of that in opposite, through the Binance exchange using the algorithm framework implementation:
from AlgorithmImports import * class BinanceCryptoFutureDataAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2022, 10, 1) self.set_end_date(2022, 10, 10) # Set Account Currency to Binance Stable Coin, since USD-BUSD will not auto-convert and USD cannot be used to trade self.set_cash("BUSD", 100000) # Binance Futures Exchange accepts both Cash and Margin account types, select the one you need for the best reality modeling. self.set_brokerage_model(BrokerageName.BINANCE_FUTURES, AccountType.MARGIN) self.universe_settings.resolution = Resolution.DAILY self.universe_settings.leverage = 2 # We only trade on BTCBUSD Future in Binance Future exchange symbols = Symbol.create("BTCBUSD", SecurityType.CRYPTO_FUTURE, Market.BINANCE) self.add_universe_selection(ManualUniverseSelectionModel(symbols)) # Custom alpha model to emit insights based on the Crypto Future price data self.add_alpha(CryptoFutureAlphaModel()) # Equally invest to evenly dissipate the capital concentration risk of inidividual crypto pair self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel()) self.set_execution(ImmediateExecutionModel()) class CryptoFutureAlphaModel(AlphaModel): def __init__(self) -> None: self.symbols = [] def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]: insights = [] for symbol in self.symbols: # Trade only based on updated price data if not slice.bars.contains_key(symbol) or not slice.quote_bars.contains_key(symbol): continue quote = slice.quote_bars[symbol] price = slice.bars[symbol].price # Scalp-trade the bid-ask spread based on the supply-demand strength if price - quote.bid.close > quote.ask.close - price: insights.append(Insight.price(symbol, timedelta(1), InsightDirection.DOWN)) else: insights.append(Insight.price(symbol, timedelta(1), InsightDirection.UP)) return insights def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None: for security in changes.added_securities: symbol = security.symbol self.symbols.append(symbol) # Historical data history = algorithm.history(symbol, 10, Resolution.DAILY) algorithm.debug(f"We got {len(history)} from our history request for {symbol}") for security in changes.removed_securities: symbol = security.symbol if symbol in self.symbols: self.symbols.remove(symbol)
public class BinanceCryptoFutureDataAlgorithm : QCAlgorithm { public Symbol _symbol; public override void Initialize() { SetStartDate(2022, 10, 1); SetEndDate(2022, 10, 10); // Set Account Currency to Binance Stable Coin, since USD-BUSD will not auto-convert and USD cannot be used to trade SetCash("BUSD", 100000); // Binance Futures Exchange accepts both Cash and Margin account types, select the one you need for the best reality modeling. SetBrokerageModel(BrokerageName.BinanceFutures, AccountType.Margin); UniverseSettings.Resolution = Resolution.Daily; UniverseSettings.Leverage = 2; // We only trade on BTCBUSD Future in Binance Future exchange var symbols = new List{ QuantConnect.Symbol.Create("BTCBUSD", SecurityType.CryptoFuture, Market.Binance) }; AddUniverseSelection(new ManualUniverseSelectionModel(symbols)); // Custom alpha model to emit insights based on the Crypto Future price data AddAlpha(new CryptoFutureAlphaModel()); // Equally invest to evenly dissipate the capital concentration risk of inidividual crypto pair SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel()); SetExecution(new ImmediateExecutionModel()); } } public class CryptoFutureAlphaModel : AlphaModel { private List _symbols = new(); public override List Update(QCAlgorithm algorithm, Slice slice) { var insights = new List(); foreach (var symbol in _symbols) { // Trade only based on updated price data if (!slice.Bars.ContainsKey(symbol) || !slice.QuoteBars.TryGet(symbol, out var quote)) { continue; } var price = slice.Bars[symbol].Price; // Scalp-trade the bid-ask spread based on the supply-demand strength if (price - quote.Bid.Close > quote.Ask.Close - price) { insights.Add( Insight.Price(symbol, TimeSpan.FromDays(1), InsightDirection.Down) ); } else { insights.Add( Insight.Price(symbol, TimeSpan.FromDays(1), InsightDirection.Up) ); } } return insights; } public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { foreach (var security in changes.AddedSecurities) { var symbol = security.Symbol; _symbols.Add(symbol); // Historical data var history = algorithm.History(symbol, 10, Resolution.Daily); algorithm.Debug($"We got {history.Count()} from our history request for {symbol}"); } foreach (var security in changes.RemovedSecurities) { _symbols.Remove(security.Symbol); } } }
Data Point Attributes
The Binance Crypto Future Price dataset provides TradeBar
, QuoteBar
, and Tick
objects.
TradeBar Attributes
TradeBar
objects have the following attributes:
QuoteBar Attributes
QuoteBar
objects have the following attributes:
Tick Attributes
Tick
objects have the following attributes: