QuantConnect
Bybit Crypto Future Margin Rate Data
Introduction
The Bybit Crypto Future Margin Rate Data by QuantConnect is for Cryptocurrency Futures margin interest data points. The data covers 631 Cryptocurrency pairs, starts in August 2020, and is delivered on a daily update frequency. This dataset is created by downloading data using Bybit API.
This dataset is an important companion to the Bybit Crypto Future Price Data dataset because it contains information on margin interest data to model margin costs.
For more information about the Bybit Crypto Future Margin Rate Data dataset, including CLI commands and pricing, see the dataset listing.
About the Provider
QuantConnect was founded in 2012 to serve quants everywhere with the best possible algorithmic trading technology. Seeking to disrupt a notoriously closed-source industry, QuantConnect takes a radically open-source approach to algorithmic trading. Through the QuantConnect web platform, more than 50,000 quants are served every month.
Getting Started
The following snippet demonstrates how to request data from the Bybit Crypto Future Margin Rate dataset:
def Initialize(self) -> None:
self.SetBrokerageModel(BrokerageName.Bybit, AccountType.Margin)
self.crypto_future_symbol = self.AddCryptoFuture("BTCBUSD", Resolution.Minute).Symbol
private Symbol _cryptoFutureSymbol;
public override void Initialize
{
SetBrokerageModel(BrokerageName.Bybit, AccountType.Margin);
// perpetual futures does not have a filter function
_cryptoFutureSymbol = AddCryptoFuture("BTCBUSD", Resolution.Minute).Symbol;
}
The Bybit Crypto Future Margin Rate data is added to your algorithm along with the market data when you add a crypto future subscription.
Data Summary
The following table describes the dataset properties:
| Property | Value |
|---|---|
| Start Date | August 2020 |
| Asset Coverage | 631 Crypto Futures Pairs |
| Data Density | Regular |
| Resolution | Daily |
| Timezone | UTC |
| Market Hours | Always Open |
Requesting Data
To add Bybit Crypto Future Margin Rate data to your algorithm, call the AddCryptoFutureadd_crypto_future method. Save a reference to the Crypto Future Symbol so you can access the data later in your algorithm.
from AlgorithmImports import *
from QuantConnect.DataSource import *
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 Tether
self.set_account_currency("USDT", 100000)
self.set_brokerage_model(BrokerageName.BYBIT, AccountType.MARGIN)
crypto_future = self.add_crypto_future("BTCUSDT", Resolution.MINUTE)
# perpetual futures does not have a filter function
self.btcusdt = 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 Tether
SetAccountCurrency("USDT", 100000);
SetBrokerageModel(BrokerageName.Bybit, AccountType.Margin);
var cryptoFuture = AddCryptoFuture("BTCUSDT", 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 Bybit Crypto Margin Rate data, index the MarginInterestRatesmargin_interest_rates property 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.btcusdt in slice.margin_interest_rates:
interest_rate = slice.margin_interest_rates[self.btcusdt].interest_rate
self.log(f"{self.btcusdt} close at {slice.time}: {interest_rate}") public override void OnData(Slice slice)
{
if (slice.MarginInterestRates.ContainsKey(_symbol))
{
var interestRate = slice.MarginInterestRates[_symbol].InterestRate;
Log($"{_symbol} price at {slice.Time}: {interestRate}");
}
}
You can also iterate through all of the data objects in the current Slice.
def on_data(self, slice: Slice) -> None:
for symbol, margin_interest_rate in slice.margin_interest_rates.items():
interest_rate = margin_interest_rate.interest_rate
self.log(f"{symbol} close at {slice.time}: {interest_rate}") public override void OnData(Slice slice)
{
foreach (var kvp in slice.MarginInterestRates)
{
var symbol = kvp.Key;
var marginInterestRate = kvp.Value;
var interestRate = marginInterestRate.InterestRate;
Log($"{symbol} price at {slice.Time}: {interestRate}");
}
}
For more information about accessing Crypto Future data, see Handling Data.
Historical Data
To get historical Bybit Crypto Future Margin Rate data, call the History<MarginInterestRate>history method with MarginInterestRate data type and the Crypto Future Symbol. If there is no data in the period you request, the history result is empty.
# DataFrame history_df = self.history(MarginInterestRate, self.btcusdt, 100, Resolution.DAILY) # MarginInterestRate objects history = self.history[MarginInterestRate](self.btcusdt, 100, Resolution.DAILY)
var history = History<MarginInterestRate>(_symbol, 100, Resolution.Daily);
For more information about historical data, see History Requests.
Remove Subscriptions
To unsubscribe from a Crypto pair that you added with the AddCryptoadd_crypto method, call the RemoveSecurityremove_security method.
self.remove_security(self.btcusdt)
RemoveSecurity(_symbol);
TheRemoveSecurityremove_security method cancels your open orders for the security and liquidates your holdings in the virtual pair.
Example Applications
The Binance Crypto Future Margin Rate dataset enables correct margin cost so you can 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 holds a long position in the BTCUSDT perpetual Future contract on the Bybit exchange. When the margin interest rate is relatively low, it flips to a short position.
from AlgorithmImports import *
class BybitCryptoFutureDataAlgorithm(QCAlgorithm):
# Add an indicator to track the low of trailing margin interest rates.
_low_interest_rate = Minimum(10)
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
# Set Account Currency to USDT, since USD cannot be used to trade.
self.set_cash("USDT", 100_000)
# Bybit accepts both Cash and Margin account types, select the one you need for the best reality modeling.
self.set_brokerage_model(BrokerageName.BYBIT, AccountType.MARGIN)
# Seed the price of each asset with its last known price to avoid trading errors.
self.set_security_initializer(
BrokerageModelSecurityInitializer(
self.brokerage_model,
FuncSecuritySeeder(self.get_last_known_prices)
)
)
# Requesting data, we only trade on BTCUSDT Future in Bybit exchange
# Perpetual futures does not have a filter function.
self._future = self.add_crypto_future("BTCUSDT", Resolution.DAILY)
# Historical data
history = self.history[MarginInterestRate](self._future.symbol, 10, Resolution.DAILY)
for data_point in history:
self._low_interest_rate.update(data_point.end_time, data_point.interest_rate)
def on_data(self, slice: Slice) -> None:
# Note that you may want to access the margin interest of the crypto future to calculate if it would impact a trade's PnL
# Or you can calculate the trade size on keeping the quote currency constant
if self._future.symbol not in slice.margin_interest_rates:
return
interest_rate = slice.margin_interest_rates[self._future.symbol].interest_rate
self.plot('Margin Interest Rate', str(self._future.symbol), interest_rate)
# Short when margin interest rates are relatively low. Otherwise, long.
if interest_rate <= self._low_interest_rate.current.value:
if not self._future.holdings.is_short:
self.set_holdings(self._future.symbol, -0.5)
elif not self._future.holdings.is_long:
self.set_holdings(self._future.symbol, 1)
# Update the indicator.
self._low_interest_rate.update(self.time, interest_rate) public class BybitCryptoFutureDataAlgorithm : QCAlgorithm
{
private CryptoFuture _future;
// Add an indicator to track the low of trailing margin interest rates.
private Minimum _lowInterestRate = new(10);
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
// Set Account Currency to USDT, since USD cannot be used to trade.
SetCash("USDT", 100000);
// Bybit accepts both Cash and Margin account types, select the one you need for the best reality modeling.
SetBrokerageModel(BrokerageName.Bybit, AccountType.Margin);
// Seed the price of each asset with its last known price to avoid trading errors.
SetSecurityInitializer(
new BrokerageModelSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))
);
// Requesting data, we only trade on BTCUSDT Future in Bybit exchange
// Perpetual futures does not have a filter function.
_future = AddCryptoFuture("BTCUSDT", Resolution.Daily);
// Historical data
var history = History<MarginInterestRate>(_future.Symbol, 10, Resolution.Daily);
foreach (var dataPoint in history)
{
_lowInterestRate.Update(dataPoint.EndTime, dataPoint.InterestRate);
}
}
public override void OnData(Slice slice)
{
// Note that you may want to access the margin interest of the crypto future to calculate if it would impact a trade's PnL
// Or you can calculate the trade size on keeping the quote currency constant
if (!slice.MarginInterestRates.TryGetValue(_future.Symbol, out var dataPoint))
{
return;
}
var interestRate = dataPoint.InterestRate;
Plot("Margin Interest Rate", _future.Symbol.ToString(), interestRate);
// Short when margin interest rates are relatively low. Otherwise, long.
if (interestRate <= _lowInterestRate)
{
if (!_future.Holdings.IsShort)
{
SetHoldings(_future.Symbol, -0.5);
}
}
else if (!_future.Holdings.IsLong)
{
SetHoldings(_future.Symbol, 1);
}
// Update the indicator.
_lowInterestRate.Update(Time, interestRate);
}
}
Framework Algorithm Example
The following example algorithm holds a long position in the BTCUSDT perpetual Future contract on the Bybit exchange. When the margin interest rate is relatively low, it flips to a short position.
from AlgorithmImports import *
class BybitCryptoFutureDataAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
self.settings.free_portfolio_value_percentage = 0.1
# Set Account Currency to USDT, since USD cannot be used to trade.
self.set_cash("USDT", 100000)
# Bybit accepts both Cash and Margin account types, select the one you need for the best reality modeling.
self.set_brokerage_model(BrokerageName.BYBIT, AccountType.MARGIN)
# Seed the price of each asset with its last known price to avoid trading errors.
self.set_security_initializer(
BrokerageModelSecurityInitializer(
self.brokerage_model,
FuncSecuritySeeder(self.get_last_known_prices)
)
)
self.universe_settings.resolution = Resolution.DAILY
self.universe_settings.leverage = 2
# We only trade on BTCUSDT Future in Bybit exchange
symbols = Symbol.create("BTCUSDT", SecurityType.CRYPTO_FUTURE, Market.BYBIT)
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):
_min_by_symbol = {}
def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]:
insights = []
for symbol, low_interest_rate in self._min_by_symbol.items():
# Note that you may want to access the margin interest of the crypto future to calculate if it would impact a trade's PnL
# Or you can calculate the trade size on keeping the quote currency constant
if symbol not in slice.margin_interest_rates:
continue
interest_rate = slice.margin_interest_rates[symbol].interest_rate
algorithm.plot('Margin Interest Rate', str(symbol), interest_rate)
# Short when margin interest rates are relatively low. Otherwise, long.
if interest_rate <= low_interest_rate.current.value:
direction = InsightDirection.DOWN
else:
direction = InsightDirection.UP
insights.append(Insight.price(symbol, timedelta(1), direction))
# Update the indicator.
low_interest_rate.update(slice.time, interest_rate)
return insights
def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None:
for security in changes.added_securities:
symbol = security.symbol
# Add an indicator to track the low of trailing margin interest rates.
self._min_by_symbol[symbol] = Minimum(10)
# Historical data
history = algorithm.history[MarginInterestRate](symbol, 10, Resolution.DAILY)
for data_point in history:
self._min_by_symbol[symbol].update(data_point.end_time, data_point.interest_rate)
for security in changes.removed_securities:
self._min_by_symbol.pop(security.symbol, None) public class BybitCryptoFutureDataAlgorithm : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
Settings.FreePortfolioValuePercentage = 0.1m;
// Set Account Currency to USDT, since USD cannot be used to trade.
SetCash("USDT", 100000);
// Bybit accepts both Cash and Margin account types, select the one you need for the best reality modeling.
SetBrokerageModel(BrokerageName.Bybit, AccountType.Margin);
// Seed the price of each asset with its last known price to avoid trading errors.
SetSecurityInitializer(
new BrokerageModelSecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices))
);
UniverseSettings.Resolution = Resolution.Daily;
UniverseSettings.Leverage = 2;
// We only trade on BTCUSDT Future in Bybit exchange
var symbols = new List<Symbol>{
QuantConnect.Symbol.Create("BTCUSDT", SecurityType.CryptoFuture, Market.Bybit)
};
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 Dictionary<Symbol, Minimum> _minBySymbol = new();
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice slice)
{
var insights = new List<Insight>();
foreach (var kvp in _minBySymbol)
{
var symbol = kvp.Key;
var lowInterestRate = kvp.Value;
// Note that you may want to access the margin interest of the crypto future to calculate if it would impact a trade's PnL
// Or you can calculate the trade size on keeping the quote currency constant
if (!slice.MarginInterestRates.TryGetValue(symbol, out var dataPoint))
continue;
var interestRate = dataPoint.InterestRate;
algorithm.Plot("Margin Interest Rate", symbol.ToString(), interestRate);
// Short when margin interest rates are relatively low. Otherwise, long.
var direction = interestRate <= lowInterestRate.Current.Value
? InsightDirection.Down
: InsightDirection.Up;
insights.Add(Insight.Price(symbol, TimeSpan.FromDays(1), direction));
// Update the indicator.
lowInterestRate.Update(slice.Time, interestRate);
}
return insights;
}
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
foreach (var security in changes.AddedSecurities)
{
var symbol = security.Symbol;
// Add an indicator to track the low of trailing margin interest rates.
var min = new Minimum(10);
_minBySymbol[symbol] = min;
// Historical data
var history = algorithm.History<MarginInterestRate>(symbol, 10, Resolution.Daily);
foreach (var dataPoint in history)
{
min.Update(dataPoint.EndTime, dataPoint.InterestRate);
}
}
foreach (var security in changes.RemovedSecurities)
{
_minBySymbol.Remove(security.Symbol);
}
}
}