forked from BitMEX/api-connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitMEXAPI.cs
202 lines (175 loc) · 6.83 KB
/
BitMEXAPI.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
//using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace BitMEX
{
public class OrderBookItem
{
public string Symbol { get; set; }
public int Level { get; set; }
public int BidSize { get; set; }
public decimal BidPrice { get; set; }
public int AskSize { get; set; }
public decimal AskPrice { get; set; }
public DateTime Timestamp { get; set; }
}
public class BitMEXApi
{
private const string domain = "https://testnet.bitmex.com";
private string apiKey;
private string apiSecret;
private int rateLimit;
public BitMEXApi(string bitmexKey = "", string bitmexSecret = "", int rateLimit = 5000)
{
this.apiKey = bitmexKey;
this.apiSecret = bitmexSecret;
this.rateLimit = rateLimit;
}
private string BuildQueryData(Dictionary<string, string> param)
{
if (param == null)
return "";
StringBuilder b = new StringBuilder();
foreach (var item in param)
b.Append(string.Format("&{0}={1}", item.Key, WebUtility.UrlEncode(item.Value)));
try { return b.ToString().Substring(1); }
catch (Exception) { return ""; }
}
private string BuildJSON(Dictionary<string, string> param)
{
if (param == null)
return "";
var entries = new List<string>();
foreach (var item in param)
entries.Add(string.Format("\"{0}\":\"{1}\"", item.Key, item.Value));
return "{" + string.Join(",", entries) + "}";
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
private long GetNonce()
{
DateTime yearBegin = new DateTime(1990, 1, 1);
return DateTime.UtcNow.Ticks - yearBegin.Ticks;
}
private string Query(string method, string function, Dictionary<string, string> param = null, bool auth = false, bool json = false)
{
string paramData = json ? BuildJSON(param) : BuildQueryData(param);
string url = "/api/v1" + function + ((method == "GET" && paramData != "") ? "?" + paramData : "");
string postData = (method != "GET") ? paramData : "";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(domain + url);
webRequest.Method = method;
if (auth)
{
string nonce = GetNonce().ToString();
string message = method + url + nonce + postData;
byte[] signatureBytes = hmacsha256(Encoding.UTF8.GetBytes(apiSecret), Encoding.UTF8.GetBytes(message));
string signatureString = ByteArrayToString(signatureBytes);
webRequest.Headers.Add("api-nonce", nonce);
webRequest.Headers.Add("api-key", apiKey);
webRequest.Headers.Add("api-signature", signatureString);
}
try
{
if (postData != "")
{
webRequest.ContentType = json ? "application/json" : "application/x-www-form-urlencoded";
var data = Encoding.UTF8.GetBytes(postData);
using (var stream = webRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
using (WebResponse webResponse = webRequest.GetResponse())
using (Stream str = webResponse.GetResponseStream())
using (StreamReader sr = new StreamReader(str))
{
return sr.ReadToEnd();
}
}
catch (WebException wex)
{
using (HttpWebResponse response = (HttpWebResponse)wex.Response)
{
if (response == null)
throw;
using (Stream str = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
return sr.ReadToEnd();
}
}
}
}
}
//public List<OrderBookItem> GetOrderBook(string symbol, int depth)
//{
// var param = new Dictionary<string, string>();
// param["symbol"] = symbol;
// param["depth"] = depth.ToString();
// string res = Query("GET", "/orderBook", param);
// return JsonSerializer.DeserializeFromString<List<OrderBookItem>>(res);
//}
public string GetOrders()
{
var param = new Dictionary<string, string>();
param["symbol"] = "XBTUSD";
//param["filter"] = "{\"open\":true}";
//param["columns"] = "";
//param["count"] = 100.ToString();
//param["start"] = 0.ToString();
//param["reverse"] = false.ToString();
//param["startTime"] = "";
//param["endTime"] = "";
return Query("GET", "/order", param, true);
}
public string PostOrders()
{
var param = new Dictionary<string, string>();
param["symbol"] = "XBTUSD";
param["side"] = "Buy";
param["orderQty"] = "1";
param["ordType"] = "Market";
return Query("POST", "/order", param, true);
}
public string DeleteOrders()
{
var param = new Dictionary<string, string>();
param["orderID"] = "de709f12-2f24-9a36-b047-ab0ff090f0bb";
param["text"] = "cancel order by ID";
return Query("DELETE", "/order", param, true, true);
}
private byte[] hmacsha256(byte[] keyByte, byte[] messageBytes)
{
using (var hash = new HMACSHA256(keyByte))
{
return hash.ComputeHash(messageBytes);
}
}
#region RateLimiter
private long lastTicks = 0;
private object thisLock = new object();
private void RateLimit()
{
lock (thisLock)
{
long elapsedTicks = DateTime.Now.Ticks - lastTicks;
var timespan = new TimeSpan(elapsedTicks);
if (timespan.TotalMilliseconds < rateLimit)
Thread.Sleep(rateLimit - (int)timespan.TotalMilliseconds);
lastTicks = DateTime.Now.Ticks;
}
}
#endregion RateLimiter
}
}