Skip to content

Commit

Permalink
Fix bugs related with order types and quantity sign (#15)
Browse files Browse the repository at this point in the history
* Fix bugs related with order types and quantity sign

* Improve implementation

* Solve another potential bug
  • Loading branch information
Marinovsky authored Jan 29, 2024
1 parent 395b16c commit f653c24
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions QuantConnect.BybitBrokerage/BybitBrokerage.Brokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,48 @@ public override List<Order> GetOpenOrders()
var orders = new List<Order>();
foreach (var category in SupportedBybitProductCategories)
{
orders.AddRange(ApiClient.Trade.GetOpenOrders(category)
.Select(bybitOrder =>
{
var symbol = _symbolMapper.GetLeanSymbol(bybitOrder.Symbol, GetSecurityType(category), MarketName);
var price = bybitOrder.Price!.Value;
Order order;
if (bybitOrder.StopOrderType != null)
orders.AddRange(ApiClient.Trade.GetOpenOrders(category)
.Select(bybitOrder =>
{
if (bybitOrder.StopOrderType == StopOrderType.TrailingStop)
var symbol = _symbolMapper.GetLeanSymbol(bybitOrder.Symbol, GetSecurityType(category), MarketName);
var price = bybitOrder.Price!.Value;

// Set the correct sign of the quantity
if (bybitOrder.Side == OrderSide.Sell && bybitOrder.Quantity > 0)
{
throw new NotSupportedException();
bybitOrder.Quantity *= -1;
}

order = bybitOrder.OrderType == OrderType.Limit
? new StopLimitOrder(symbol, bybitOrder.Quantity, price, bybitOrder.Price!.Value, bybitOrder.CreateTime)
: new StopMarketOrder(symbol, bybitOrder.Quantity, price, bybitOrder.CreateTime);
}
else
{
order = bybitOrder.OrderType == OrderType.Limit
? new LimitOrder(symbol, bybitOrder.Quantity, price, bybitOrder.CreateTime)
: new MarketOrder(symbol, bybitOrder.Quantity, bybitOrder.CreateTime);
}

order.BrokerId.Add(bybitOrder.OrderId);
order.Status = ConvertOrderStatus(bybitOrder.Status);
return order;
}));
}
Order order;
if (bybitOrder.StopOrderType != null)
{
if (bybitOrder.StopOrderType == StopOrderType.TrailingStop)
{
throw new NotSupportedException();
}

// Bybit does not have a direct option for placing
// Stop Orders To create one, we place a TP/SL order
// that triggers a market order when the trigger price
// is reached. Therefore, since Bybit API returns 0
// as price for Stop Orders, we instead take the trigger
// price.
order = bybitOrder.OrderType == OrderType.Limit
? new StopLimitOrder(symbol, bybitOrder.Quantity, price, bybitOrder.TriggerPrice!.Value, bybitOrder.CreateTime)
: new StopMarketOrder(symbol, bybitOrder.Quantity, bybitOrder.TriggerPrice!.Value, bybitOrder.CreateTime);
}
else
{
order = bybitOrder.OrderType == OrderType.Limit
? new LimitOrder(symbol, bybitOrder.Quantity, price, bybitOrder.CreateTime)
: new MarketOrder(symbol, bybitOrder.Quantity, bybitOrder.CreateTime);
}

order.BrokerId.Add(bybitOrder.OrderId);
order.Status = ConvertOrderStatus(bybitOrder.Status);
return order;
}));
}

return orders;
}
Expand Down

0 comments on commit f653c24

Please sign in to comment.