forked from ridicoulous/HitBtc.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HitBtcClient.cs
722 lines (595 loc) · 43.8 KB
/
HitBtcClient.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.ExchangeInterfaces;
using HitBtc.Net.Interfaces;
using HitBtc.Net.Objects.AccountManagement;
using HitBtc.Net.Objects.AccountManagement.SubAccount;
using HitBtc.Net.Objects.MarginTrading;
using HitBtc.Net.Objects.MarketData;
using HitBtc.Net.Objects.Trading;
using HitBtc.Net.Objects.TradingHistory;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using HitBtc.Net.Extensions;
using HitBtc.Net.Enums;
namespace HitBtc.Net
{
public class HitBtcClient : RestClient, IHitBtcClient, IExchangeClient
{
#region Endpoints
private const string SymbolsUrl = "public/symbol";
private const string SymbolWithSymbolUrl = "public/symbol/{}";
private const string CurrencyUrl = "public/currency";
private const string CurrencyWithCurrencyUrl = "public/currency/{}";
private const string TickerUrl = "public/ticker";
private const string TickerWithSymbolUrl = "public/ticker/{}";
private const string TradesUrl = "public/trades";
private const string TradesWithSymbolUrl = "public/trades/{}";
private const string OrderBookUrl = "public/orderbook";
private const string OrderBookWithSymbolUrl = "public/orderbook/{}";
private const string CandlesUrl = "public/candles";
private const string CandlesWithSymbolUrl = "public/candles/{}";
private const string SubAccountsUrl = "sub-acc";
private const string ActivateSubAccountsUrl = "sub-acc/activate";
private const string SubAccFreezeUrl = "sub-acc/freeze";
private const string SubAccTransferUrl = "sub-acc/transfer";
private const string SubAccWithdrawSettingUrl = "sub-acc/acl";
private const string SubAccWithdrawSettingWithUserIdUrl = "sub-acc/acl/{}";
private const string SubAccBalanceWithUserIdUrl = "sub-acc/balance/{}";
private const string SubAccDepositAddressWithUserIdCurrencyUrl = "sub-acc/deposit-address/{}/{}";
private const string OrderWithClientOrderIdUrl = "order/{}";
private const string OrderUrl = "order";
private const string TradingBalanceUrl = "trading/balance";
private const string TradingFeeWithSymbolUrl = "trading/fee/{}";
private const string MarginOrderWithClientOrderIdUrl = "margin/order/{}";
private const string MarginOrderUrl = "margin/order";
private const string MarginAccountUrl = "margin/account";
private const string MarginAccountWithSymbolUrl = "margin/account/{}";
private const string MarginPositionUrl = "margin/position";
private const string MarginPositionWithSymbolUrl = "margin/position/{}";
private const string AccountBalanceUrl = "account/balance";
private const string AccountCryptoWithdrawUrl = "account/crypto/withdraw";
private const string AccountCryptoCheckOffchainUrl = "account/crypto/check-offchain-available";
private const string AccountCryptoWithdrawWithIdUrl = "account/crypto/withdraw/{}";
private const string AccountCryptoTransferConvertUrl = "account/crypto/transfer-convert";
private const string AccountCryptoEstimateWithrdawFeesUrl = "account/crypto/estimate-withdraw";
private const string AccountCryptoEstimateWithrdawFeesLevelsUrl = "account/crypto/estimate-withdraw-levels";
private const string AccountCryptoAddressWithCurrencyUrl = "account/crypto/address/{}";
private const string AccountCryptoAddressesWithCurrencyUrl = "account/crypto/addresses/{}";
private const string AccountCryptoIsMineAddressUrl = "account/crypto/is-mine/{}";
private const string AccountCryptoUsedAddressesWithCurrencyUrl = "account/crypto/used-addresses/{}";
private const string AccountTransferUrl = "account/transfer";
private const string AccountTransferInternalUrl = "account/transfer/internal";
private const string AccountTransactionsUrl = "account/transactions";
private const string AccountTransactionWithIdUrl = "account/transactions/{}";
private const string HistoryTradesUrl = "history/trades";
private const string HistoryOrderUrl = "history/order";
private const string HistoryOrderWithOrderIdUrl = "/history/order/{}/trades";
#endregion
static HitBtcClientOptions defaultOptions = new HitBtcClientOptions();
public event Action<ICommonOrderId> OnOrderPlaced;
public event Action<ICommonOrderId> OnOrderCanceled;
public HitBtcClient() : this("HitBtcClient", defaultOptions, null) { }
public HitBtcClient(string key, string secret, string clientName = "HitBtcClient", bool sandBox = false) : this(clientName, new HitBtcClientOptions(sandBox), new HitBtcAuthenticationProvider(new ApiCredentials(key, secret)))
{
}
public HitBtcClient(string clientName, HitBtcClientOptions exchangeOptions, HitBtcAuthenticationProvider authenticationProvider) : base(clientName, exchangeOptions, authenticationProvider)
{
requestBodyFormat = RequestBodyFormat.FormData;
}
private Uri GetUrl(string path)
{
return new Uri(BaseAddress + path);
}
public WebCallResult<HitBtcRequestsBoolResult> ActivateSubAccounts(List<long> ids) => ActivateSubAccountsAsync(ids, default).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> ActivateSubAccountsAsync(List<long> ids, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("ids", ids.AsStringParameterOrNull());
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(ActivateSubAccountsUrl), HttpMethod.Post, ct, null, true, true);
}
public WebCallResult<HitBtcOrder> CancelMarginOrderByClientOrderId(string clientOrderId) => CancelMarginOrderByClientOrderIdAsync(clientOrderId).Result;
public async Task<WebCallResult<HitBtcOrder>> CancelMarginOrderByClientOrderIdAsync(string clientOrderId, CancellationToken ct = default)
{
var result = await SendRequestAsync<HitBtcOrder>(GetUrl(FillPathParameter(MarginOrderWithClientOrderIdUrl, clientOrderId)), HttpMethod.Delete, ct, null, true, true);
if (result.Success)
{
OnOrderCanceled?.Invoke(result.Data);
}
return result;
}
public WebCallResult<IEnumerable<HitBtcOrder>> CancelMarginOrders(string symbol = null) => CancelMarginOrdersAsync(symbol).Result;
public async Task<WebCallResult<IEnumerable<HitBtcOrder>>> CancelMarginOrdersAsync(string symbol = null, CancellationToken ct = default)
{
var result = await SendRequestAsync<IEnumerable<HitBtcOrder>>(GetUrl(MarginOrderUrl), HttpMethod.Delete, ct, symbol == null ? null : new Dictionary<string, object> { { "symbol", symbol } }, true, true);
if (result.Success && OnOrderCanceled != null)
{
foreach (var o in result.Data)
{
OnOrderCanceled.Invoke(o);
}
}
return result;
}
public WebCallResult<HitBtcOrder> CancelOrderByClientOrderId(string clientOrderId) => CancelOrderByClientOrderIdAsync(clientOrderId).Result;
public async Task<WebCallResult<HitBtcOrder>> CancelOrderByClientOrderIdAsync(string clientOrderId, CancellationToken ct = default)
{
var result = await SendRequestAsync<HitBtcOrder>(GetUrl(FillPathParameter(OrderWithClientOrderIdUrl, clientOrderId)), HttpMethod.Delete, ct, null, true, true);
if (result.Success)
{
OnOrderCanceled?.Invoke(result.Data);
}
return result;
}
public WebCallResult<IEnumerable<HitBtcOrder>> CancelOrders(string symbol = null) => CancelOrdersAsync(symbol).Result;
public async Task<WebCallResult<IEnumerable<HitBtcOrder>>> CancelOrdersAsync(string symbol = null, CancellationToken ct = default)
{
var result = await SendRequestAsync<IEnumerable<HitBtcOrder>>(GetUrl(OrderUrl), HttpMethod.Delete, ct, symbol == null ? null : new Dictionary<string, object> { { "symbol", symbol } }, true, true);
if (result.Success && OnOrderCanceled != null)
{
foreach (var o in result.Data)
{
OnOrderCanceled.Invoke(o);
}
}
return result;
}
public WebCallResult<IEnumerable<HitBtcIsolatedMarginAccount>> CloseAllIsolatedMarginAccounts() => CloseAllIsolatedMarginAccountsAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcIsolatedMarginAccount>>> CloseAllIsolatedMarginAccountsAsync(CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcIsolatedMarginAccount>>(GetUrl(MarginAccountUrl), HttpMethod.Delete, ct, null, true, true);
}
public WebCallResult<HitBtcIsolatedMarginAccount> CloseIsolatedMarginAccount(string symbol) => CloseIsolatedMarginAccountAsync(symbol).Result;
public async Task<WebCallResult<HitBtcIsolatedMarginAccount>> CloseIsolatedMarginAccountAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcIsolatedMarginAccount>(GetUrl(FillPathParameter(MarginAccountWithSymbolUrl, symbol)), HttpMethod.Delete, ct, null, true, true);
}
public WebCallResult<HitBtcPosition> CloseMarginPosition(string symbol, decimal? price = null, bool strictValidate = false) => CloseMarginPositionAsync(symbol, price, strictValidate).Result;
public async Task<WebCallResult<HitBtcPosition>> CloseMarginPositionAsync(string symbol, decimal? price = null, bool strictValidate = false, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("price", price);
parameters.AddOptionalParameter("strictValidate", strictValidate);
return await SendRequestAsync<HitBtcPosition>(GetUrl(FillPathParameter(MarginPositionWithSymbolUrl, symbol)), HttpMethod.Delete, ct, parameters, true, true);
}
public WebCallResult<IEnumerable<HitBtcPosition>> CloseMarginPositions() => CloseMarginPositionsAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcPosition>>> CloseMarginPositionsAsync(CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcPosition>>(GetUrl(MarginPositionUrl), HttpMethod.Delete, ct, null, true, true);
}
public WebCallResult<HitBtcRequestsBoolResult> CommitWithdrawCrypto(string id) => CommitWithdrawCryptoAsync(id).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> CommitWithdrawCryptoAsync(string id, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(FillPathParameter(AccountCryptoWithdrawWithIdUrl, id)), HttpMethod.Put, ct, null, true, true);
}
public WebCallResult<HitBtcRequestsBoolResult> FreezeSubAccounts(List<long> ids) => FreezeSubAccountsAsync(ids).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> FreezeSubAccountsAsync(List<long> ids, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("ids", ids.AsStringParameterOrNull());
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(SubAccFreezeUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcDepositAddress> GenerateNewDepositAddress(string currency) => GenerateNewDepositAddressAsync(currency).Result;
public async Task<WebCallResult<HitBtcDepositAddress>> GenerateNewDepositAddressAsync(string currency, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcDepositAddress>(GetUrl(FillPathParameter(AccountCryptoAddressWithCurrencyUrl, currency)), HttpMethod.Post, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcAccountBalance>> GetAccountBalance() => GetAccountBalanceAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcAccountBalance>>> GetAccountBalanceAsync(CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcAccountBalance>>(GetUrl(AccountBalanceUrl), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcOrder> GetActiveOrderByClientOrderId(string clientOrderId, long? wait = null) => GetActiveOrderByClientOrderIdAsync(clientOrderId, wait).Result;
public async Task<WebCallResult<HitBtcOrder>> GetActiveOrderByClientOrderIdAsync(string clientOrderId, long? wait = null, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("wait", wait);
return await SendRequestAsync<HitBtcOrder>(GetUrl(FillPathParameter(OrderWithClientOrderIdUrl, clientOrderId)), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<IEnumerable<HitBtcOrder>> GetActiveOrders(string symbol = null) => GetActiveOrdersAsync(symbol).Result;
public async Task<WebCallResult<IEnumerable<HitBtcOrder>>> GetActiveOrdersAsync(string symbol = null, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("symbol", symbol);
return await SendRequestAsync<IEnumerable<HitBtcOrder>>(GetUrl(OrderUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcRequestsBoolResult> GetAddressIsMine(string address) => GetAddressIsMineAsync(address).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> GetAddressIsMineAsync(string address, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(FillPathParameter(AccountCryptoIsMineAddressUrl, address)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcCandle>> GetCandlesForSymbol(string symbol, HitBtcCandlesFilterRequest filter) => GetCandlesForSymbolAsync(symbol, filter).Result;
public async Task<WebCallResult<IEnumerable<HitBtcCandle>>> GetCandlesForSymbolAsync(string symbol, HitBtcCandlesFilterRequest filter, CancellationToken ct = default)
{
var parameters = filter.AsDictionary();
return await SendRequestAsync<IEnumerable<HitBtcCandle>>(GetUrl(FillPathParameter(CandlesWithSymbolUrl, symbol)), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<HitBtcCandlesResponse> GetCandles(HitBtcCandlesFilterRequest filter, params string[] symbols) => GetCandlesAsync(filter, symbols: symbols).Result;
public async Task<WebCallResult<HitBtcCandlesResponse>> GetCandlesAsync(HitBtcCandlesFilterRequest filter, CancellationToken ct = default, params string[] symbols)
{
var parameters = filter.AsDictionary();
return await SendRequestAsync<HitBtcCandlesResponse>(GetUrl(CandlesUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<IEnumerable<HitBtcCurrency>> GetCurrencies(params string[] currencies) => GetCurrenciesAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcCurrency>>> GetCurrenciesAsync(CancellationToken ct = default, params string[] currencies)
{
var parameters = new Dictionary<string, object>();
parameters.Add("currencies", currencies.AsStringParameterOrNull());
return await SendRequestAsync<IEnumerable<HitBtcCurrency>>(GetUrl(CurrencyUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<HitBtcCurrency> GetCurrency(string currency) => GetCurrencyAsync(currency).Result;
public async Task<WebCallResult<HitBtcCurrency>> GetCurrencyAsync(string currency, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcCurrency>(GetUrl(FillPathParameter(CurrencyWithCurrencyUrl, currency)), HttpMethod.Get, ct, null, false);
}
public WebCallResult<HitBtcDepositAddress> GetDepositAddress(string currency) => GetDepositAddressAsync(currency).Result;
public async Task<WebCallResult<HitBtcDepositAddress>> GetDepositAddressAsync(string currency, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcDepositAddress>(GetUrl(FillPathParameter(AccountCryptoAddressWithCurrencyUrl, currency)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcFee> GetEstimateWithdrawFee(string currency, decimal amount) => GetEstimateWithdrawFeeAsync(currency, amount).Result;
public async Task<WebCallResult<HitBtcFee>> GetEstimateWithdrawFeeAsync(string currency, decimal amount, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("currency", currency);
parameters.Add("amount", amount);
return await SendRequestAsync<HitBtcFee>(GetUrl(AccountCryptoEstimateWithrdawFeesUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<IEnumerable<HitBtcFeeLevel>> GetEstimateWithdrawFeeLevels(string currency, decimal amount) => GetEstimateWithdrawFeeLevelsAsync(currency, amount).Result;
public async Task<WebCallResult<IEnumerable<HitBtcFeeLevel>>> GetEstimateWithdrawFeeLevelsAsync(string currency, decimal amount, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("currency", currency);
parameters.Add("amount", amount);
return await SendRequestAsync<IEnumerable<HitBtcFeeLevel>>(GetUrl(AccountCryptoEstimateWithrdawFeesLevelsUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcIsolatedMarginAccount> GetIsolatedMarginAccount(string symbol) => GetIsolatedMarginAccountAsync(symbol).Result;
public async Task<WebCallResult<HitBtcIsolatedMarginAccount>> GetIsolatedMarginAccountAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcIsolatedMarginAccount>(GetUrl(FillPathParameter(MarginAccountWithSymbolUrl, symbol)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcIsolatedMarginAccount>> GetIsolatedMarginAccounts() => GetIsolatedMarginAccountsAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcIsolatedMarginAccount>>> GetIsolatedMarginAccountsAsync(CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcIsolatedMarginAccount>>(GetUrl(MarginAccountUrl), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcDepositAddress>> GetLastDepositAddresses(string currency) => GetLastDepositAddressesAsync(currency).Result;
public async Task<WebCallResult<IEnumerable<HitBtcDepositAddress>>> GetLastDepositAddressesAsync(string currency, CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcDepositAddress>>(GetUrl(FillPathParameter(AccountCryptoAddressesWithCurrencyUrl, currency)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcDepositAddress>> GetLastUsedDepositAddresses(string currency) => GetLastUsedDepositAddressesAsync(currency).Result;
public async Task<WebCallResult<IEnumerable<HitBtcDepositAddress>>> GetLastUsedDepositAddressesAsync(string currency, CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcDepositAddress>>(GetUrl(FillPathParameter(AccountCryptoUsedAddressesWithCurrencyUrl, currency)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcOrder> GetMarginOrder(string clientOrderId) => GetMarginOrderAsync(clientOrderId).Result;
public async Task<WebCallResult<HitBtcOrder>> GetMarginOrderAsync(string clientOrderId, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcOrder>(GetUrl(FillPathParameter(MarginOrderWithClientOrderIdUrl, clientOrderId)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcOrder>> GetMarginOrders(string symbol = null) => GetMarginOrdersAsync(symbol).Result;
public async Task<WebCallResult<IEnumerable<HitBtcOrder>>> GetMarginOrdersAsync(string symbol = null, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("symbol", symbol);
return await SendRequestAsync<IEnumerable<HitBtcOrder>>(GetUrl(MarginOrderUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcPosition> GetMarginPosition(string symbol) => GetMarginPositionAsync(symbol).Result;
public async Task<WebCallResult<HitBtcPosition>> GetMarginPositionAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcPosition>(GetUrl(FillPathParameter(MarginPositionWithSymbolUrl, symbol)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcPosition>> GetMarginPositions() => GetMarginPositionsAsync().Result;
public async Task<WebCallResult<IEnumerable<HitBtcPosition>>> GetMarginPositionsAsync(CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcPosition>>(GetUrl(MarginPositionUrl), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcOrderBook> GetOrderBook(string symbol, int limit = 100, decimal? volume = null) => GetOrderBookAsync(symbol, limit, volume).Result;
public async Task<WebCallResult<HitBtcOrderBook>> GetOrderBookAsync(string symbol, int limit = 100, decimal? volume = null, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("limit", limit);
parameters.AddOptionalParameter("volume", volume);
return await SendRequestAsync<HitBtcOrderBook>(GetUrl(FillPathParameter(OrderBookWithSymbolUrl, symbol)), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<HitBtcOrderBooksResponse> GetOrderBooks(int limit = 100, params string[] symbols) => GetOrderBooksAsync(limit, symbols: symbols).Result;
public async Task<WebCallResult<HitBtcOrderBooksResponse>> GetOrderBooksAsync(int limit = 100, CancellationToken ct = default, params string[] symbols)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("limit", limit);
parameters.AddOptionalParameter("symbols", symbols.AsStringParameterOrNull());
return await SendRequestAsync<HitBtcOrderBooksResponse>(GetUrl(OrderBookUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<IEnumerable<HitBtcOrder>> GetOrdersHistory(HitBtcOrdersFilterRequest filter) => GetOrdersHistoryAsync(filter).Result;
public async Task<WebCallResult<IEnumerable<HitBtcOrder>>> GetOrdersHistoryAsync(HitBtcOrdersFilterRequest filter, CancellationToken ct = default)
{
var parameters = filter.AsDictionary();
return await SendRequestAsync<IEnumerable<HitBtcOrder>>(GetUrl(HistoryOrderUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcSubAccAddressResponse> GetSubAccAddress(long subAccountUserId, string currency) => GetSubAccAddressAsync(subAccountUserId, currency).Result;
public async Task<WebCallResult<HitBtcSubAccAddressResponse>> GetSubAccAddressAsync(long subAccountUserId, string currency, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcSubAccAddressResponse>(GetUrl(FillPathParameter(SubAccDepositAddressWithUserIdCurrencyUrl, subAccountUserId.ToString(), currency)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcSubAccBalanceResponse> GetSubAccBalance(long subAccountUserID) => GetSubAccBalanceAsync(subAccountUserID).Result;
public async Task<WebCallResult<HitBtcSubAccBalanceResponse>> GetSubAccBalanceAsync(long subAccountUserID, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcSubAccBalanceResponse>(GetUrl(FillPathParameter(SubAccBalanceWithUserIdUrl, subAccountUserID.ToString())), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcSubAccountResponse> GetSubAccounts() => GetSubAccountsAsync().Result;
public async Task<WebCallResult<HitBtcSubAccountResponse>> GetSubAccountsAsync(CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcSubAccountResponse>(GetUrl(SubAccountsUrl), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcSubAccSettingResponse> GetSubAccWithdrawalSettings(List<long> subAccountIds) => GetSubAccWithdrawalSettingsAsync(subAccountIds).Result;
public async Task<WebCallResult<HitBtcSubAccSettingResponse>> GetSubAccWithdrawalSettingsAsync(List<long> subAccountIds, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("subAccountIds", subAccountIds.AsStringParameterOrNull());
return await SendRequestAsync<HitBtcSubAccSettingResponse>(GetUrl(SubAccWithdrawSettingUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcSymbol> GetSymbol(string symbol) => GetSymbolAsync(symbol).Result;
public async Task<WebCallResult<HitBtcSymbol>> GetSymbolAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcSymbol>(GetUrl(FillPathParameter(SymbolWithSymbolUrl, symbol)), HttpMethod.Get, ct, null, false);
}
public WebCallResult<IEnumerable<HitBtcSymbol>> GetSymbols(params string[] symbols) => GetSymbolsAsync(default, symbols).Result; //JoinableTaskFactory.Run(async delegate { await GetSymbolsAsync(default, symbols); });
public async Task<WebCallResult<IEnumerable<HitBtcSymbol>>> GetSymbolsAsync(CancellationToken ct = default, params string[] symbols)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("symbols", symbols.AsStringParameterOrNull());
return await SendRequestAsync<IEnumerable<HitBtcSymbol>>(GetUrl(SymbolsUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<HitBtcTicker> GetTicker(string symbol) => GetTickerAsync(symbol).Result;
public async Task<WebCallResult<HitBtcTicker>> GetTickerAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcTicker>(GetUrl(FillPathParameter(TickerWithSymbolUrl, symbol)), HttpMethod.Get, ct, null, false);
}
public WebCallResult<IEnumerable<HitBtcTicker>> GetTickers(params string[] symbols) => GetTickersAsync(symbols: symbols).Result;
public async Task<WebCallResult<IEnumerable<HitBtcTicker>>> GetTickersAsync(CancellationToken ct = default, params string[] symbols)
{
var parameters = new Dictionary<string, object>();
parameters.AddOptionalParameter("symbols", symbols.AsStringParameterOrNull());
return await SendRequestAsync<IEnumerable<HitBtcTicker>>(GetUrl(TickerUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<IEnumerable<HitBtcPublicTrade>> GetTradesForSymbol(string symbol, HitbtcPublicTradesFilterRequest filter) => GetTradesForSymbolAsync(symbol, filter).Result;
public async Task<WebCallResult<IEnumerable<HitBtcPublicTrade>>> GetTradesForSymbolAsync(string symbol, HitbtcPublicTradesFilterRequest filter, CancellationToken ct = default)
{
var parameters = filter.AsDictionary();
return await SendRequestAsync<IEnumerable<HitBtcPublicTrade>>(GetUrl(FillPathParameter(TradesWithSymbolUrl, symbol)), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<Dictionary<string, IEnumerable<HitBtcPublicTrade>>> GetTrades(HitbtcPublicTradesFilterRequest filter, params string[] symbols) => GetTradesAsync(filter: filter, symbols: symbols).Result;
public async Task<WebCallResult<Dictionary<string, IEnumerable<HitBtcPublicTrade>>>> GetTradesAsync(HitbtcPublicTradesFilterRequest filter, CancellationToken ct = default, params string[] symbols)
{
var parameters = filter.AsDictionary();
parameters.AddOptionalParameter("symbols", symbols.AsStringParameterOrNull());
return await SendRequestAsync<Dictionary<string, IEnumerable<HitBtcPublicTrade>>>(GetUrl(TradesUrl), HttpMethod.Get, ct, parameters, false);
}
public WebCallResult<IEnumerable<HitBtcTrade>> GetTradesByOrderId(long orderId) => GetTradesByOrderIdAsync(orderId).Result;
public async Task<WebCallResult<IEnumerable<HitBtcTrade>>> GetTradesByOrderIdAsync(long orderId, CancellationToken ct = default)
{
return await SendRequestAsync<IEnumerable<HitBtcTrade>>(GetUrl(FillPathParameter(HistoryOrderWithOrderIdUrl, orderId.ToString())), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcTrade>> GetTradesHistory(HitBtcTradesFilterRequest filter) => GetTradesHistoryAsync(filter).Result;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="filter"></param>
/// <param name="ct"></param>
/// <returns></returns>
public async Task<WebCallResult<IEnumerable<HitBtcTrade>>> GetTradesHistoryAsync(HitBtcTradesFilterRequest filter, CancellationToken ct = default)
{
var parameters = filter.AsDictionary();
return await SendRequestAsync<IEnumerable<HitBtcTrade>>(GetUrl(HistoryTradesUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<List<HitBtcTradingBalance>> GetTradingBalance() => GetTradingBalanceAsync().Result;
public async Task<WebCallResult<List<HitBtcTradingBalance>>> GetTradingBalanceAsync(CancellationToken ct = default)
{
return await SendRequestAsync<List<HitBtcTradingBalance>>(GetUrl(TradingBalanceUrl), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcTradingCommission> GetTradingCommission(string symbol) => GetTradingCommissionAsync(symbol).Result;
public async Task<WebCallResult<HitBtcTradingCommission>> GetTradingCommissionAsync(string symbol, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcTradingCommission>(GetUrl(FillPathParameter(TradingFeeWithSymbolUrl, symbol)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<HitBtcTransaction> GetTransactionById(string transactionId) => GetTransactionByIdAsync(transactionId).Result;
public async Task<WebCallResult<HitBtcTransaction>> GetTransactionByIdAsync(string transactionId, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcTransaction>(GetUrl(FillPathParameter(AccountTransactionWithIdUrl, transactionId)), HttpMethod.Get, ct, null, true, true);
}
public WebCallResult<IEnumerable<HitBtcTransaction>> GetTransactions(HitBtcTransactionsHistoryRequest request) => GetTransactionsAsync(request).Result;
public async Task<WebCallResult<IEnumerable<HitBtcTransaction>>> GetTransactionsAsync(HitBtcTransactionsHistoryRequest request, CancellationToken ct = default)
{
var parameters = request.AsDictionary();
return await SendRequestAsync<IEnumerable<HitBtcTransaction>>(GetUrl(AccountTransactionsUrl), HttpMethod.Get, ct, parameters, true, true);
}
public WebCallResult<HitBtcOrder> PlaceMarginOrder(HitbtcPlaceOrderRequest order) => PlaceMarginOrderAsync(order).Result;
public async Task<WebCallResult<HitBtcOrder>> PlaceMarginOrderAsync(HitbtcPlaceOrderRequest order, CancellationToken ct = default)
{
var parameters = order.AsDictionary();
var result = await SendRequestAsync<HitBtcOrder>(GetUrl(MarginOrderUrl), HttpMethod.Post, ct, parameters, true, true);
if (result.Success)
{
OnOrderPlaced?.Invoke(result.Data);
}
return result;
}
public WebCallResult<HitBtcOrder> PlaceOrder(HitbtcPlaceOrderRequest order) => PlaceOrderAsync(order).Result;
public async Task<WebCallResult<HitBtcOrder>> PlaceOrderAsync(HitbtcPlaceOrderRequest order, CancellationToken ct = default)
{
var parameters = order.AsDictionary();
var result = await SendRequestAsync<HitBtcOrder>(GetUrl(OrderUrl), HttpMethod.Post, ct, parameters, true, false);
if (result.Success)
{
OnOrderPlaced?.Invoke(result.Data);
}
return result;
}
public WebCallResult<HitBtcRequestsBoolResult> RollbackWithdrawCrypto(string id) => RollbackWithdrawCryptoAsync(id).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> RollbackWithdrawCryptoAsync(string id, CancellationToken ct = default)
{
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(FillPathParameter(AccountCryptoWithdrawWithIdUrl, id)), HttpMethod.Delete, ct, null, true, true);
}
public WebCallResult<HitBtcIsolatedMarginAccount> SetIsolatedMarginAccount(string symbol, decimal marginBalance, bool strictValidate) => SetIsolatedMarginAccountAsync(symbol, marginBalance, strictValidate).Result;
public async Task<WebCallResult<HitBtcIsolatedMarginAccount>> SetIsolatedMarginAccountAsync(string symbol, decimal marginBalance, bool strictValidate, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("marginBalance", marginBalance);
parameters.Add("strictValidate", strictValidate);
return await SendRequestAsync<HitBtcIsolatedMarginAccount>(GetUrl(FillPathParameter(MarginAccountWithSymbolUrl, symbol)), HttpMethod.Put, ct, parameters, true, true);
}
public WebCallResult<HitBtcSubAccSettingResponse> SetSubAccWithdrawalSettings(HitBtcSubAccWithdrawalSetting request) => SetSubAccWithdrawalSettingsAsync(request).Result;
public async Task<WebCallResult<HitBtcSubAccSettingResponse>> SetSubAccWithdrawalSettingsAsync(HitBtcSubAccWithdrawalSetting request, CancellationToken ct = default)
{
var parameters = request.AsDictionary();
return await SendRequestAsync<HitBtcSubAccSettingResponse>(GetUrl(FillPathParameter(SubAccWithdrawSettingWithUserIdUrl, request.SubAccountId.ToString())), HttpMethod.Put, ct, parameters, true, true);
}
public WebCallResult<HitBtcTransactionIdResponse> TransferBetweenAcc(HitBtcTransferRequest request) => TransferBetweenAccAsync(request).Result;
public async Task<WebCallResult<HitBtcTransactionIdResponse>> TransferBetweenAccAsync(HitBtcTransferRequest transferRequest, CancellationToken ct = default)
{
var parameters = transferRequest.AsDictionary();
return await SendRequestAsync<HitBtcTransactionIdResponse>(GetUrl(AccountTransferUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcTransferConvertCryptoResponse> TransferConvertCrypto(string fromCurrency, string toCurrency, decimal amount) => TransferConvertCryptoAsync(fromCurrency, toCurrency, amount).Result;
public async Task<WebCallResult<HitBtcTransferConvertCryptoResponse>> TransferConvertCryptoAsync(string fromCurrency, string toCurrency, decimal amount, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("fromCurrency", fromCurrency);
parameters.Add("toCurrency", toCurrency);
parameters.Add("amount", amount);
return await SendRequestAsync<HitBtcTransferConvertCryptoResponse>(GetUrl(AccountCryptoTransferConvertUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcTransactionIdResponse> TransferToAnotherUser(HitBtcTransferToAnotherUserRequest request) => TransferToAnotherUserAsync(request).Result;
public async Task<WebCallResult<HitBtcTransactionIdResponse>> TransferToAnotherUserAsync(HitBtcTransferToAnotherUserRequest request, CancellationToken ct = default)
{
var parameters = request.AsDictionary();
return await SendRequestAsync<HitBtcTransactionIdResponse>(GetUrl(AccountTransferInternalUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcRequestsBoolResult> TrasferFound(HitBtcSubAccTransferRequest request) => TrasferFoundAsync(request).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> TrasferFoundAsync(HitBtcSubAccTransferRequest request, CancellationToken ct = default)
{
var parameters = request.AsDictionary();
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(SubAccTransferUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcTransactionIdResponse> WithdrawCrypto(HitBtcWithdrawRequest request) => WithdrawCryptoAsync(request).Result;
public async Task<WebCallResult<HitBtcTransactionIdResponse>> WithdrawCryptoAsync(HitBtcWithdrawRequest request, CancellationToken ct = default)
{
var parameters = request.AsDictionary();
return await SendRequestAsync<HitBtcTransactionIdResponse>(GetUrl(AccountCryptoWithdrawUrl), HttpMethod.Post, ct, parameters, true, true);
}
public WebCallResult<HitBtcRequestsBoolResult> IsOffchainTransactionAvialable(string currency, string address, string paymentId = null) => IsOffchainTransactionAvialableAsync(currency, address, paymentId, default).Result;
public async Task<WebCallResult<HitBtcRequestsBoolResult>> IsOffchainTransactionAvialableAsync(string currency, string address, string paymentId = null, CancellationToken ct = default)
{
var parameters = new Dictionary<string, object>();
parameters.Add("currency", currency);
parameters.Add("address", address);
parameters.AddOptionalParameter("paymentId", paymentId);
return await SendRequestAsync<HitBtcRequestsBoolResult>(GetUrl(AccountCryptoCheckOffchainUrl), HttpMethod.Post, ct, parameters, true, true);
}
public string GetSymbolName(string baseAsset, string quoteAsset)
{
return baseAsset + quoteAsset;
}
async Task<WebCallResult<IEnumerable<ICommonSymbol>>> IExchangeClient.GetSymbolsAsync()
{
var foo = await GetSymbolsAsync();
return foo.As<IEnumerable<ICommonSymbol>>(foo.Data); ;
}
async Task<WebCallResult<IEnumerable<ICommonTicker>>> IExchangeClient.GetTickersAsync()
{
var foo = await GetTickersAsync();
return foo.As<IEnumerable<ICommonTicker>>(foo.Data); ;
}
async Task<WebCallResult<ICommonTicker>> IExchangeClient.GetTickerAsync(string symbol)
{
var foo = await GetTickerAsync(symbol);
return foo.As<ICommonTicker>(foo.Data);
}
public async Task<WebCallResult<IEnumerable<ICommonKline>>> GetKlinesAsync(string symbol, TimeSpan timespan, DateTime? startTime = null, DateTime? endTime = null, int? limit = null)
{
var request = new HitBtcCandlesFilterRequest(timespan.AsStringTimeFrame(), "ASC", startTime, endTime, limit ?? 100, 0);
var foo = await GetCandlesForSymbolAsync(symbol, request);
return foo.As<IEnumerable<ICommonKline>>(foo.Data);
}
async Task<WebCallResult<ICommonOrderBook>> IExchangeClient.GetOrderBookAsync(string symbol)
{
var foo = await GetOrderBookAsync(symbol);
return foo.As<ICommonOrderBook>(foo.Data);
}
public async Task<WebCallResult<IEnumerable<ICommonRecentTrade>>> GetRecentTradesAsync(string symbol)
{
int maxEntryCount = 1000;
var request = new HitBtcTradesFilterRequest(symbol, limit: maxEntryCount);
var foo = await GetTradesForSymbolAsync(symbol,request);
if (foo)
{
return foo.As<IEnumerable<ICommonRecentTrade>>(foo.Data);
}
else
{
return new WebCallResult<IEnumerable<ICommonRecentTrade>>(foo.ResponseStatusCode, foo.ResponseHeaders, null, foo.Error);
}
}
public async Task<WebCallResult<ICommonOrderId>> PlaceOrderAsync(string symbol, IExchangeClient.OrderSide side, IExchangeClient.OrderType type, decimal quantity, decimal? price = null, string accountId = null)
{
var hitBtcSide = (HitBtcTradeSide)Enum.Parse(typeof(HitBtcTradeSide), side.ToString());
HitBtcOrderType hitBtcOType = type switch
{
IExchangeClient.OrderType.Market => HitBtcOrderType.Market,
_ => HitBtcOrderType.Limit
};
var request = new HitbtcPlaceOrderRequest(symbol, hitBtcSide, quantity, hitBtcOType, price);
var foo = await PlaceOrderAsync(request);
return foo.As<ICommonOrderId>(foo.Data);
}
public async Task<WebCallResult<ICommonOrder>> GetOrderAsync(string orderId, string symbol = null)
{
// string clientOrderId = await GetClientOrderIdById(orderId, symbol);
var foo = await GetActiveOrderByClientOrderIdAsync(orderId);
return foo.As<ICommonOrder>(foo.Data);
}
public async Task<WebCallResult<IEnumerable<ICommonTrade>>> GetTradesAsync(string orderId, string symbol = null)
{
long id;
if (long.TryParse(orderId, out id))
{
var foo = await GetTradesByOrderIdAsync(id);
return foo.As<IEnumerable<ICommonTrade>>(foo.Data);
}
else
{
return WebCallResult<IEnumerable<ICommonTrade>>.CreateErrorResult(new ServerError($"Can't convert orderId {orderId} to type long"));
}
}
public async Task<WebCallResult<IEnumerable<ICommonOrder>>> GetOpenOrdersAsync(string symbol = null)
{
var foo = await GetActiveOrdersAsync(symbol);
return foo.As<IEnumerable<ICommonOrder>>(foo.Data);
}
public async Task<WebCallResult<IEnumerable<ICommonOrder>>> GetClosedOrdersAsync(string symbol = null)
{
var request = HitBtcOrdersFilterRequest.CreateFilterBySymbolRequest(symbol);
var foo = await GetOrdersHistoryAsync(request);
return foo.As<IEnumerable<ICommonOrder>>(foo.Data);
}
public async Task<WebCallResult<ICommonOrderId>> CancelOrderAsync(string orderId, string symbol = null)
{
var foo = await CancelOrderByClientOrderIdAsync(orderId);
if (!foo && foo.Error.Message.Contains("Order not found"))
{
return new WebCallResult<ICommonOrderId>(System.Net.HttpStatusCode.OK, foo.ResponseHeaders, new HitBtcOrder() { ClientOrderId = orderId }, null);
}
return foo.As<ICommonOrderId>(foo.Data);
}
public async Task<WebCallResult<IEnumerable<ICommonBalance>>> GetBalancesAsync(string accountId = null)
{
var foo = await GetTradingBalanceAsync();
return foo.As<IEnumerable<ICommonBalance>>(foo.Data);
}
}
}