generated from QuantConnect/Lean.Brokerages.Template
/
BybitBrokerage.Brokerage.cs
253 lines (221 loc) · 9.23 KB
/
BybitBrokerage.Brokerage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Brokerages;
using QuantConnect.BybitBrokerage.Models.Enums;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Securities;
using OrderStatus = QuantConnect.Orders.OrderStatus;
using OrderType = QuantConnect.BybitBrokerage.Models.Enums.OrderType;
namespace QuantConnect.BybitBrokerage;
public partial class BybitBrokerage
{
#region Brokerage
/// <summary>
/// Gets all open orders on the account.
/// NOTE: The order objects returned do not have QC order IDs.
/// </summary>
/// <returns>The open orders returned from Bybit</returns>
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)
{
if (bybitOrder.StopOrderType == StopOrderType.TrailingStop)
{
throw new NotSupportedException();
}
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;
}));
}
return orders;
}
/// <summary>
/// Gets all holdings for the account
/// </summary>
/// <returns>The current holdings from the account</returns>
public override List<Holding> GetAccountHoldings()
{
var holdings = new List<Holding>();
foreach (var category in SupportedBybitProductCategories)
{
holdings.AddRange(ApiClient.Position.GetPositions(category)
.Select(bybitPosition => new Holding
{
Symbol = _symbolMapper.GetLeanSymbol(bybitPosition.Symbol, GetSecurityType(category), MarketName),
AveragePrice = bybitPosition.AveragePrice,
Quantity = bybitPosition.Side == Models.Enums.PositionSide.Buy ? bybitPosition.Size : bybitPosition.Size * -1,
MarketValue = bybitPosition.PositionValue,
UnrealizedPnL = bybitPosition.UnrealisedPnl,
MarketPrice = bybitPosition.MarkPrice
}));
}
return holdings.Count > 0 ? holdings : base.GetAccountHoldings(_job?.BrokerageData, _algorithm?.Securities?.Values);
}
/// <summary>
/// Gets the current cash balance for each currency held in the brokerage account
/// </summary>
/// <returns>The current cash balance for each currency available for trading</returns>
public override List<CashAmount> GetCashBalance()
{
return ApiClient.Account
.GetWalletBalances().Assets
.Select(x => new CashAmount(x.WalletBalance, x.Asset)).ToList();
}
/// <summary>
/// Places a new order and assigns a new broker ID to the order
/// </summary>
/// <param name="order">The order to be placed</param>
/// <returns>True if the request for a new order has been placed, false otherwise</returns>
public override bool PlaceOrder(Order order)
{
if (!CanSubscribe(order.Symbol))
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, -1,
$"Symbol is not supported {order.Symbol}"));
return false;
}
var submitted = false;
_messageHandler.WithLockedStream(() =>
{
var result = ApiClient.Trade.PlaceOrder(GetBybitProductCategory(order.Symbol), order,
useMargin: _algorithm.BrokerageModel.AccountType == AccountType.Margin);
order.BrokerId.Add(result.OrderId);
OnOrderEvent(new OrderEvent(order, DateTime.UtcNow, OrderFee.Zero, "Bybit Order Event")
{
Status = OrderStatus.Submitted
});
submitted = true;
});
return submitted;
}
/// <summary>
/// Updates the order with the same id
/// </summary>
/// <param name="order">The new order information</param>
/// <returns>True if the request was made for the order to be updated, false otherwise</returns>
public override bool UpdateOrder(Order order)
{
var category = GetBybitProductCategory(order.Symbol);
if (category == BybitProductCategory.Spot)
{
throw new NotSupportedException("BybitBrokerage.UpdateOrder: Order update not supported for spot. Please cancel and re-create.");
}
if (!CanSubscribe(order.Symbol))
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, -1,
$"Symbol is not supported {order.Symbol}"));
return false;
}
var submitted = false;
_messageHandler.WithLockedStream(() =>
{
ApiClient.Trade.UpdateOrder(category, order);
OnOrderEvent(new OrderEvent(order, DateTime.UtcNow, OrderFee.Zero, "Bybit Order Event")
{
Status = OrderStatus.UpdateSubmitted
});
submitted = true;
//OnOrderIdChangedEvent(new BrokerageOrderIdChangedEvent(){BrokerId = cachedOrder.BrokerId, OrderId = order.Id});
});
return submitted;
}
/// <summary>
/// Cancels the order with the specified ID
/// </summary>
/// <param name="order">The order to cancel</param>
/// <returns>True if the request was made for the order to be canceled, false otherwise</returns>
public override bool CancelOrder(Order order)
{
if (!CanSubscribe(order.Symbol))
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, -1,
$"Symbol is not supported {order.Symbol}"));
return false;
}
var canceled = false;
_messageHandler.WithLockedStream(() =>
{
if (order.Status == OrderStatus.Filled || order.Type == Orders.OrderType.Market)
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, -1, "Order already filled"));
return;
}
if (order.Status is OrderStatus.Canceled or OrderStatus.CancelPending)
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Warning, -1,
"Order already canceled or cancellation submitted"));
return;
}
ApiClient.Trade.CancelOrder(GetBybitProductCategory(order.Symbol), order);
canceled = true;
});
return canceled;
}
/// <summary>
/// Wss message handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnMessage(object sender, WebSocketMessage e)
{
_messageHandler.HandleNewMessage(e);
}
/// <summary>
/// Connects the client to the broker's remote servers
/// </summary>
public override void Connect()
{
if (IsConnected)
return;
// cannot reach this code if rest api client is not created
// WebSocket is responsible for Bybit UserData stream only
// as a result we don't need to connect user data stream if BybitBrokerage is used as DQH only
// or until Algorithm is actually initialized
Connect(null);
}
/// <summary>
/// Disconnects the client from the broker's remote servers
/// </summary>
public override void Disconnect()
{
if (WebSocket?.IsOpen != true) return;
WebSocket.Close();
}
#endregion
}