It seems like the margin calculation(s) and therefore the order size calculation is not correct. I'm not sure if it also affects other crypto exchanges but it's true for Bybit.
There are two factors which come into play:
The CryptoFutureMarginModel uses 5% as a default margin rate, while for bybit the base rate is 0.5%. This can be manually changed in the algorithm but it still would be good to have it as a default.
QC is using the available margin based on the used maintenance margin for calculating position sizes. While bybit isn't using the MM for position size changes but instead the available initial margin (which is based on the position entry price)
Expected Behavior
The available Initial Margin should be used for order size calculations instead of the available maintenance margin.
This screenshot represents approximately the same state as the algorithm below (The entry price differs by 1k but it doesn't change that much). You can see that the initial margin is 70% utilized while the maintenance margin is only at 5% utilization. As the MM is made available and is used for order size calculations.
The margin calculation in this log is already correct due to manually setting the BuyingPowerModel with a margin rate of 0.5% but the margin available is "wrong". For ByBit
Reproducing the Problem
public class Example : QCAlgorithm
{
private CryptoFuture _btc;
public override void Initialize()
{
SetStartDate(2024, 02, 14); // Set Start Date
SetEndDate(2024, 02, 16); // Set End Date
SetBrokerageModel(new BybitBrokerageModel(AccountType.Margin));
SetAccountCurrency("USDT", 957);
// On bybit the base margin rate is 0.5% compared to the 5% which is set as the default in the CryptoFutureMarginModel
_btc = AddCryptoFuture("BTCUSDT", Resolution.Daily, Market.Bybit, leverage: 10);
_btc.SetBuyingPowerModel(new CryptoFutureMarginModel(10, 0.005m));
}
public override void OnData(Slice data)
{
if (!Portfolio.Invested)
{
PrintInfo();
var order = MarketOrder(_btc.Symbol, 0.146m);
Log($"Holding info: Cost: {_btc.Holdings.HoldingsCost} Value: {_btc.Holdings.HoldingsValue} Quantity: {_btc.Holdings.Quantity}");
}
PrintInfo();
}
private void PrintInfo()
{
Portfolio.LogMarginInformation();
Log($"MR: {Portfolio.MarginRemaining} MU: {Portfolio.TotalMarginUsed} Portfolio: {Portfolio.TotalPortfolioValue} Holdings: {Portfolio.TotalHoldingsValue} IMU: {InitialMarginUsed()} IMR: {Portfolio.TotalPortfolioValue - InitialMarginUsed()}");
}
private decimal InitialMarginUsed()
{ var reserved = 0m;
foreach (var (_,security) in Portfolio.Securities)
{
if(!security.Invested) continue;
reserved += (security.QuoteCurrency.ConversionRate * security.SymbolProperties.ContractMultiplier * security.Holdings.AveragePrice * security.Holdings.AbsoluteQuantity * 0.1m);
}
return reserved;
}
System Information
n/a
Checklist
[x] I have completely filled out this template
[x] I have confirmed that this issue exists on the current master branch
[x] I have confirmed that this is not a duplicate issue by searching issues
[x] I have provided detailed steps to reproduce the issue
It seems like the margin calculation(s) and therefore the order size calculation is not correct. I'm not sure if it also affects other crypto exchanges but it's true for Bybit. There are two factors which come into play:
CryptoFutureMarginModel
uses5%
as a default margin rate, while for bybit the base rate is0.5%
. This can be manually changed in the algorithm but it still would be good to have it as a default.Expected Behavior
The available Initial Margin should be used for order size calculations instead of the available maintenance margin. This screenshot represents approximately the same state as the algorithm below (The entry price differs by 1k but it doesn't change that much). You can see that the initial margin is 70% utilized while the maintenance margin is only at 5% utilization. As the MM is made available and is used for order size calculations.![image](https://github.com/QuantConnect/Lean/assets/18667020/04a5a2ea-375a-4d84-8602-7d6055a65726)
Actual Behavior
The margin calculation in this log is already correct due to manually setting the
BuyingPowerModel
with a margin rate of0.5%
but the margin available is "wrong". For ByBitReproducing the Problem
System Information
n/a
Checklist
master
branch