Skip to content

Commit 2127a7a

Browse files
Added new private BitMEX methods: GetHistoricalTrades, GetCurrentPositions, CancelAllOrders, and Deadman (#528)
1 parent df5cbfd commit 2127a7a

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/ExchangeSharp/API/Exchanges/BitMEX/ExchangeBitMEXAPI.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,47 @@ protected override async Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(strin
389389
return candles;
390390
}
391391

392+
public async Task<IEnumerable<ExchangeTrade>> GetHistoricalTradesAsync(
393+
string marketSymbol = null,
394+
DateTime? startDate = null,
395+
DateTime? endDate = null,
396+
int? startingIndex = null,
397+
int? limit = 1000)
398+
{
399+
List<ExchangeTrade> trades = new List<ExchangeTrade>();
400+
Dictionary<string, object> payload = await GetNoncePayloadAsync();
401+
string url = "/trade?";
402+
url += "&columns=[\"symbol\", \"size\", \"price\", \"side\", \"timestamp\", \"trdMatchID\"]";
403+
if (!string.IsNullOrWhiteSpace(marketSymbol))
404+
{
405+
url += "&symbol=" + NormalizeMarketSymbol(marketSymbol);
406+
}
407+
if (startDate != null)
408+
{
409+
url += "&startTime=" + startDate.Value.ToString("yyyy-MM-ddTHH:mm:ss.fff") + "Z";
410+
}
411+
if (endDate != null)
412+
{
413+
url += "&endTime=" + endDate.Value.ToString("yyyy-MM-ddTHH:mm:ss.fff") + "Z";
414+
}
415+
if (limit != null)
416+
{
417+
url += "&count=" + (limit.Value.ToStringInvariant());
418+
}
419+
if (startingIndex != null)
420+
{
421+
url += "&start=" + (startingIndex.Value.ToStringInvariant());
422+
}
423+
424+
var obj = await MakeJsonRequestAsync<JToken>(url);
425+
foreach (var t in obj)
426+
{
427+
trades.Add(t.ParseTrade("size", "price", "side", "timestamp", TimestampType.Iso8601, "trdMatchID"));
428+
}
429+
430+
return trades;
431+
}
432+
392433
protected override async Task<Dictionary<string, decimal>> OnGetAmountsAsync()
393434
{
394435
/*
@@ -482,6 +523,28 @@ protected override async Task<Dictionary<string, decimal>> OnGetAmountsAvailable
482523
return amounts;
483524
}
484525

526+
public async Task<IEnumerable<ExchangePosition>> GetCurrentPositionsAsync()
527+
{
528+
var payload = await GetNoncePayloadAsync();
529+
string url = "/position?";
530+
url += "&columns=[\"symbol\", \"currentQty\", \"avgEntryPrice\", \"liquidationPrice\", \"leverage\"]";
531+
JToken token = await MakeJsonRequestAsync<JToken>(url, BaseUrl, payload);
532+
List<ExchangePosition> positions = new List<ExchangePosition>();
533+
foreach (var item in token)
534+
{
535+
ExchangePosition position = new ExchangePosition
536+
{
537+
MarketSymbol = item["symbol"].ToStringUpperInvariant(),
538+
Amount = item["currentQty"].ConvertInvariant<decimal>(),
539+
AveragePrice = item["avgEntryPrice"].ConvertInvariant<decimal>(),
540+
LiquidationPrice = item["liquidationPrice"].ConvertInvariant<decimal>(),
541+
Leverage = item["leverage"].ConvertInvariant<decimal>(),
542+
};
543+
positions.Add(position);
544+
}
545+
return positions;
546+
}
547+
485548
protected override async Task<IEnumerable<ExchangeOrderResult>> OnGetOpenOrderDetailsAsync(string marketSymbol = null)
486549
{
487550
List<ExchangeOrderResult> orders = new List<ExchangeOrderResult>();
@@ -521,7 +584,25 @@ protected override async Task OnCancelOrderAsync(string orderId, string marketSy
521584
payload["orderID"] = orderId;
522585
JToken token = await MakeJsonRequestAsync<JToken>("/order", BaseUrl, payload, "DELETE");
523586
}
587+
588+
public async Task CancelAllOrdersAsync(string marketSymbol = null)
589+
{
590+
Dictionary<string, object> payload = await GetNoncePayloadAsync();
591+
string query = "/order/all";
592+
if (!string.IsNullOrWhiteSpace(marketSymbol))
593+
{
594+
payload["symbol"] = NormalizeMarketSymbol(marketSymbol);
595+
}
596+
JToken token = await MakeJsonRequestAsync<JToken>(query, BaseUrl, payload, "DELETE");
597+
}
524598

599+
public async Task Deadman(int timeoutMS)
600+
{
601+
Dictionary<string, object> payload = await GetNoncePayloadAsync();
602+
payload["timeout"] = timeoutMS;
603+
JToken token = await MakeJsonRequestAsync<JToken>("/order/cancelAllAfter", BaseUrl, payload, "POST");
604+
}
605+
525606
protected override async Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
526607
{
527608
Dictionary<string, object> payload = await GetNoncePayloadAsync();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
MIT LICENSE
3+
4+
Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
*/
12+
13+
namespace ExchangeSharp
14+
{
15+
/// <summary>
16+
/// Contains information about a position on exchange
17+
/// </summary>
18+
public class ExchangePosition
19+
{
20+
/// <summary>
21+
/// Market Symbol
22+
/// </summary>
23+
public string MarketSymbol { get; set; }
24+
25+
/// <summary>
26+
/// Amount
27+
/// </summary>
28+
public decimal Amount { get; set; }
29+
30+
/// <summary>
31+
/// Average Price
32+
/// </summary>
33+
public decimal AveragePrice { get; set; }
34+
35+
/// <summary>
36+
/// Liquidation Price
37+
/// </summary>
38+
public decimal LiquidationPrice { get; set; }
39+
40+
/// <summary>
41+
/// Leverage
42+
/// </summary>
43+
public decimal Leverage { get; set; }
44+
}
45+
}

0 commit comments

Comments
 (0)