-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.cs
1520 lines (1353 loc) · 76 KB
/
test.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT LICENSE
Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
*/
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ExchangeSharp.Model;
using ExchangeSharp.Runtime;
using ExchangeSharp.API.Exchanges.BitMart;
using Humanizer;
namespace ExchangeSharp
{
/// <summary>
/// Base class for all exchange API
/// </summary>
public abstract partial class ExchangeAPI : BaseAPI, IExchangeAPI
{
public static decimal MAX_PRICE { get; set; } = 99999999m;
public MachinaTunnelInfo MachinaTunnelInfo { get; set; } = new MachinaTunnelInfo();
/// <summary>
/// Separator for global symbols (char)
/// </summary>
public const char GlobalMarketSymbolSeparator = '-';
/// <summary>
/// Separator for global symbols (string)
/// </summary>
public const string GlobalMarketSymbolSeparatorString = "-";
/// <summary>
/// Whether to use the default method cache policy, default is true.
/// The default cache policy caches things like get symbols, tickers, order book, order details, etc. See ExchangeAPI constructor for full list.
/// </summary>
public static bool UseDefaultMethodCachePolicy { get; set; } = true;
#region Private methods
private static readonly Dictionary<string, IExchangeAPI?> apis = new Dictionary<string, IExchangeAPI?>(StringComparer.OrdinalIgnoreCase);
private bool disposed;
private static readonly Dictionary<string, string> classNamesToApiName = new Dictionary<string, string>();
#endregion Private methods
#region API Implementation
protected virtual async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>> OnGetTickersAsync()
{
List<KeyValuePair<string, ExchangeTicker>> tickers = new List<KeyValuePair<string, ExchangeTicker>>();
var marketSymbols = await GetMarketSymbolsAsync();
foreach (string marketSymbol in marketSymbols)
{
var ticker = await GetTickerAsync(marketSymbol);
tickers.Add(new KeyValuePair<string, ExchangeTicker>(marketSymbol, ticker));
}
return tickers;
}
protected virtual async Task<IEnumerable<KeyValuePair<string, ExchangeOrderBook>>> OnGetOrderBooksAsync(
int maxCount = 100
)
{
var marketSymbols = await GetMarketSymbolsAsync();
var orderBooks = await Task.WhenAll(
marketSymbols.Select(async ms =>
{
var orderBook = await GetOrderBookAsync(ms, maxCount);
orderBook.MarketSymbol ??= ms;
return orderBook;
})
);
return orderBooks.ToDictionary(k => k.MarketSymbol, v => v);
}
/// <summary>
/// When possible, the sort order will be Descending (newest trades first)
/// </summary>
/// <param name="marketSymbol">name of symbol</param>
/// <param name="limit">max number of results returned, if limiting is supported by the exchange</param>
/// <returns></returns>
protected virtual async Task<IEnumerable<ExchangeTrade>> OnGetRecentTradesAsync(string marketSymbol, int? limit = null)
{
marketSymbol = NormalizeMarketSymbol(marketSymbol);
List<ExchangeTrade> trades = new List<ExchangeTrade>();
await GetHistoricalTradesAsync((e) =>
{
trades.AddRange(e);
return true;
}, marketSymbol, limit: limit); //KK2020
return trades;
}
protected virtual Task<IReadOnlyDictionary<string, ExchangeCurrency>> OnGetCurrenciesAsync() => throw new NotImplementedException();
protected virtual Task<IEnumerable<string>> OnGetMarketSymbolsAsync() => throw new NotImplementedException();
protected internal virtual Task<IEnumerable<ExchangeMarket>> OnGetMarketSymbolsMetadataAsync() => throw new NotImplementedException();
protected virtual Task<ExchangeTicker> OnGetTickerAsync(string marketSymbol) => throw new NotImplementedException();
protected virtual Task<ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100) => throw new NotImplementedException();
protected virtual Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null) => throw new NotImplementedException();
//protected virtual Task OnGetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null) => throw new NotImplementedException();
protected virtual Task<ExchangeDepositDetails> OnGetDepositAddressAsync(string currency, bool forceRegenerate = false) => throw new NotImplementedException();
protected virtual Task<IEnumerable<ExchangeTransaction>> OnGetDepositHistoryAsync(string currency) => throw new NotImplementedException();
protected virtual Task<IEnumerable<MarketCandle>> OnGetCandlesAsync(string marketSymbol, int periodSeconds, DateTime? startDate = null, DateTime? endDate = null, int? limit = null) => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetAmountsAsync() => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetFeesAsync() => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetAmountsAvailableToTradeAsync() => throw new NotImplementedException();
protected virtual Task<ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order) => throw new NotImplementedException();
protected virtual Task<ExchangeOrderResult[]> OnPlaceOrdersAsync(params ExchangeOrderRequest[] order) => throw new NotImplementedException();
protected virtual Task<ExchangeOrderResult> OnGetOrderDetailsAsync(string orderId, string? marketSymbol = null) => throw new NotImplementedException();
protected virtual Task<IEnumerable<ExchangeOrderResult>> OnGetOpenOrderDetailsAsync(string? marketSymbol = null) => throw new NotImplementedException();
protected virtual Task<IEnumerable<ExchangeOrderResult>> OnGetCompletedOrderDetailsAsync(string? marketSymbol = null, DateTime? afterDate = null) => throw new NotImplementedException();
protected virtual Task OnCancelOrderAsync(string orderId, string? marketSymbol = null) => throw new NotImplementedException();
protected virtual Task<ExchangeWithdrawalResponse> OnWithdrawAsync(ExchangeWithdrawalRequest withdrawalRequest) => throw new NotImplementedException();
protected virtual Task<IEnumerable<ExchangeTransaction>> OnGetWithdrawHistoryAsync(string currency) => throw new NotImplementedException();
protected virtual Task<Dictionary<string, decimal>> OnGetMarginAmountsAvailableToTradeAsync(bool includeZeroBalances) => throw new NotImplementedException();
protected virtual Task<ExchangeMarginPositionResult> OnGetOpenPositionAsync(string marketSymbol) => throw new NotImplementedException();
protected virtual Task<ExchangeCloseMarginPositionResult> OnCloseMarginPositionAsync(string marketSymbol) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetTickersWebSocketAsync(Action<IReadOnlyCollection<KeyValuePair<string, ExchangeTicker>>> tickers, params string[] marketSymbols) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(Action<ExchangeOrderBook> callback, int maxCount = 20, params string[] marketSymbols) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetOrderDetailsWebSocketAsync(Action<ExchangeOrderResult> callback) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetCompletedOrderDetailsWebSocketAsync(Action<ExchangeOrderResult> callback) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnUserDataWebSocketAsync(Action<object> callback, string listenKey) => throw new NotImplementedException();
protected virtual Task<IWebSocket> OnGetCandlesWebSocketAsync(Action<KeyValuePair<string, MarketCandle>> candles, int periodSeconds, params string[] marketSymbols) => throw new NotImplementedException();
#endregion API implementation
#region Protected methods
/// <summary>
/// Clamp price using market info. If necessary, a network request will be made to retrieve symbol meta-data.
/// </summary>
/// <param name="marketSymbol">Market Symbol</param>
/// <param name="outputPrice">Price</param>
/// <returns>Clamped price</returns>
public async Task<decimal> ClampOrderPrice(string globalSymbol, decimal outputPrice)
{
if (outputPrice == 0)
return outputPrice;
if (!ExchangeDescription.Options.ClampPrice)
return outputPrice;
ExchangeMarket? em = GetExchangeMarket(globalSymbol);
return em == null ? outputPrice : CryptoUtility.ClampDecimal(em.MinPrice, em.MaxPrice, em.PriceStepSize, outputPrice);
}
/// <summary>
/// Clamp quantity using market info. If necessary, a network request will be made to retrieve symbol meta-data.
/// </summary>
/// <param name="marketSymbol">Market Symbol</param>
/// <param name="outputQuantity">Quantity</param>
/// <returns>Clamped quantity</returns>
public async Task<decimal> ClampOrderQuantity(string globalSymbol, decimal outputQuantity)
{
if (outputQuantity == 0)
return outputQuantity;
if (!ExchangeDescription.Options.ClampQuantity)
return outputQuantity;
ExchangeMarket? market = GetExchangeMarket(globalSymbol);
return market == null ? outputQuantity : CryptoUtility.ClampDecimal(market.MinTradeSize, market.MaxTradeSize, market.QuantityStepSize == null ? 1 : market.QuantityStepSize, outputQuantity);
}
/// <summary>
/// Convert an exchange symbol into a global symbol, which will be the same for all exchanges.
/// Global symbols are always uppercase and separate the currency pair with a hyphen (-).
/// Global symbols list the base currency first (i.e. BTC) and quote/conversion currency
/// second (i.e. USD). Global symbols are of the form BASE-QUOTE. BASE-QUOTE is read as
/// 1 BASE is worth y QUOTE.
///
/// Examples:
/// On 1/25/2020,
/// - BTC-USD: $8,371; 1 BTC (base) is worth $8,371 USD (quote)
/// - ETH-BTC: 0.01934; 1 ETH is worth 0.01934 BTC
/// - EUR-USD: 1.2; 1 EUR worth 1.2 USD
///
/// A value greater than 1 means one unit of base currency is more valuable than one unit of
/// quote currency.
///
/// </summary>
/// <param name="marketSymbol">Exchange market symbol</param>
/// <param name="separator">Separator</param>
/// <returns>Global symbol</returns>
protected async Task<string> ExchangeMarketSymbolToGlobalMarketSymbolWithSeparatorAsync(string marketSymbol, char separator = GlobalMarketSymbolSeparator)
{
if (string.IsNullOrEmpty(marketSymbol))
{
throw new ArgumentException("Symbol must be non null and non empty");
}
string[] pieces = marketSymbol.Split(separator);
if (MarketSymbolIsReversed == false) //if reversed then put quote currency first
{
return (await ExchangeCurrencyToGlobalCurrencyAsync(pieces[0])).ToUpperInvariant() + GlobalMarketSymbolSeparator + (await ExchangeCurrencyToGlobalCurrencyAsync(pieces[1])).ToUpperInvariant();
}
return (await ExchangeCurrencyToGlobalCurrencyAsync(pieces[1])).ToUpperInvariant() + GlobalMarketSymbolSeparator + (await ExchangeCurrencyToGlobalCurrencyAsync(pieces[0])).ToUpperInvariant();
}
/// <summary>
/// Split a market symbol into currencies. For weird exchanges like Bitthumb, they can override and hard-code the other pair
/// </summary>
/// <param name="marketSymbol">Market symbol</param>
/// <returns>Base and quote currency</returns>
protected virtual (string baseCurrency, string quoteCurrency) OnSplitMarketSymbolToCurrencies(string marketSymbol)
{
var pieces = marketSymbol.Split(MarketSymbolSeparator[0]);
if (pieces.Length < 2)
{
throw new InvalidOperationException($"Splitting {Name} symbol '{marketSymbol}' with symbol separator '{MarketSymbolSeparator}' must result in at least 2 pieces.");
}
string baseCurrency = MarketSymbolIsReversed ? pieces[1] : pieces[0];
string quoteCurrency = MarketSymbolIsReversed ? pieces[0] : pieces[1];
return (baseCurrency, quoteCurrency);
}
/// <summary>
/// Override to dispose of resources when the exchange is disposed
/// </summary>
protected virtual void OnDispose() { }
#endregion Protected methods
#region Other
/// <summary>
/// Static constructor
/// </summary>
static ExchangeAPI()
{
foreach (Type type in typeof(ExchangeAPI).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(ExchangeAPI)) && !type.IsAbstract))
{
// lazy create, we just create an instance to get the name, nothing more
// we don't want to pro-actively create all of these because an API
// may be running a timer or other house-keeping which we don't want
// the overhead of if a user is only using one or a handful of the apis
if ((Activator.CreateInstance(type) is ExchangeAPI api))
{
api.Dispose();
apis[api.Name] = null;
classNamesToApiName[type.Name] = api.Name;
}
// in case derived class is accessed first, check for existence of key
if (!ExchangeGlobalCurrencyReplacements.ContainsKey(type))
{
ExchangeGlobalCurrencyReplacements[type] = new KeyValuePair<string, string>[0];
}
}
}
/// <summary>
/// Constructor
/// </summary>
public ExchangeAPI()
{
if (UseDefaultMethodCachePolicy)
{
MethodCachePolicy.Add(nameof(GetCurrenciesAsync), TimeSpan.FromHours(1.0));
MethodCachePolicy.Add(nameof(GetMarketSymbolsAsync), TimeSpan.FromHours(1.0));
MethodCachePolicy.Add(nameof(GetMarketSymbolsMetadataAsync), TimeSpan.FromHours(4.0));
MethodCachePolicy.Add(nameof(GetTickerAsync), TimeSpan.FromSeconds(10.0));
MethodCachePolicy.Add(nameof(GetTickersAsync), TimeSpan.FromSeconds(10.0));
//MethodCachePolicy.Add(nameof(GetOrderBookAsync), TimeSpan.FromSeconds(1)); // from 10s to 1s ;)
MethodCachePolicy.Add(nameof(GetOrderBooksAsync), TimeSpan.FromSeconds(10.0));
MethodCachePolicy.Add(nameof(GetCandlesAsync), TimeSpan.FromSeconds(10.0));
MethodCachePolicy.Add(nameof(GetAmountsAsync), TimeSpan.FromMinutes(1.0));
MethodCachePolicy.Add(nameof(GetAmountsAvailableToTradeAsync), TimeSpan.FromMinutes(1.0));
MethodCachePolicy.Add(nameof(GetCompletedOrderDetailsAsync), TimeSpan.FromMinutes(2.0));
}
}
/// <summary>
/// Finalize
/// </summary>
~ExchangeAPI()
{
Dispose();
}
/// <summary>
/// Dispose and cleanup all resources
/// </summary>
public void Dispose()
{
if (!disposed)
{
disposed = true;
OnDispose();
Cache?.Dispose();
// take out of global api dictionary if disposed
lock (apis)
{
if (apis.TryGetValue(Name, out var cachedApi) && cachedApi == this)
{
apis[Name] = null;
}
}
}
}
public static IExchangeAPI? GetExchangeAPIFromClassName(string className)
{
if (!classNamesToApiName.TryGetValue(className, out string exchangeName))
throw new ArgumentException("No API available with class name " + className);
return GetExchangeAPI(exchangeName);
}
/// <summary>
/// Get an exchange API given an exchange name (see ExchangeName class)
/// </summary>
/// <param name="exchangeName">Exchange name</param>
/// <returns>Exchange API or null if not found</returns>
public static IExchangeAPI? GetExchangeAPI(string exchangeName)
{
if (exchangeName == null)
return null;
// note: this method will be slightly slow (milliseconds) the first time it is called and misses the cache
// subsequent calls with cache hits will be nanoseconds
lock (apis)
{
if (!apis.TryGetValue(exchangeName, out IExchangeAPI? api))
{
throw new ArgumentException("No API available with name " + exchangeName);
}
if (api == null)
{
// find an API with the right name
foreach (Type type in typeof(ExchangeAPI).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(ExchangeAPI)) && !type.IsAbstract))
{
if (!((api = Activator.CreateInstance(type) as IExchangeAPI) is null))
{
if (api.Name.Equals(exchangeName, StringComparison.OrdinalIgnoreCase))
{
// found one with right name, add it to the API dictionary
apis[exchangeName] = api;
// break out, we are done
break;
}
else
{
// name didn't match, dispose immediately to stop timers and other nasties we don't want running, and null out api variable
api.Dispose();
api = null;
}
}
}
}
return api;
}
}
public static IExchangeAPI? GetExchange(string exchange, TradeType tradeType)
{
if (exchange == null)
return null;
switch (exchange.ToLower())
{
case "binance": return new ExchangeBinanceAPI { TradeType = tradeType };
case "bittrex": return new ExchangeBittrexAPI { TradeType = tradeType };
case "poloniex": return new ExchangePoloniexAPI { TradeType = tradeType };
case "coinbasepro": return new ExchangeCoinbaseAPI { TradeType = tradeType };
case "bitpanda": return new ExchangeBitpandaAPI { TradeType = tradeType };
case "bitmart": return new ExchangeBitMartAPI { TradeType = tradeType };
// Lykke API is under development and is hence awaiting fixes on the API by the developers
case "lykke": return new ExchangeLykkeAPI { TradeType = tradeType };
// Untested below here
case "bitfinex": return new ExchangeBitfinexAPI { TradeType = tradeType };
case "hitbtc": return new ExchangeHitBTCAPI { TradeType = tradeType };
case "kraken": return new ExchangeKrakenAPI { TradeType = tradeType };
case "huobi": return new ExchangeHuobiAPI { TradeType = tradeType };
case "bitmex": return new ExchangeBitMEXAPI { TradeType = tradeType };
case "okex": return new ExchangeOKExAPI { TradeType = tradeType };
case "coinbase": return new ExchangeCoinbaseAPI { TradeType = tradeType };
// MachinaTrader 'download tunnel'
case "machinatunnel": return new MachinaTunnelAPI { TradeType = tradeType };
}
return null;
}
/// <summary>
/// Get all exchange APIs
/// </summary>
/// <returns>All APIs</returns>
public static IExchangeAPI[] GetExchangeAPIs()
{
lock (apis)
{
List<IExchangeAPI> apiList = new List<IExchangeAPI>();
foreach (var kv in apis.ToArray())
{
if (kv.Value == null)
{
apiList.Add(GetExchangeAPI(kv.Key));
}
else
{
apiList.Add(kv.Value);
}
}
return apiList.ToArray();
}
}
public static List<ExchangeConnectionInfo> GetSupportedExchanges()
{
List<ExchangeConnectionInfo> einfo = new List<ExchangeConnectionInfo>();
foreach (IExchangeAPI api in GetExchangeAPIs())
{
if (api.ExchangeDescription != null && api.ExchangeDescription.Enabled)
{
einfo.Add(new ExchangeConnectionInfo
{
ExchangeName = api.Name,
UsesAPIKey = api.ExchangeDescription.RequiredCredentials.Apikey,
UsesAPISecret = api.ExchangeDescription.RequiredCredentials.Secret,
UsesPassPhrase = api.ExchangeDescription.RequiredCredentials.PassPhrase,
ExchangeReferralUrl = api.ExchangeDescription.Urls.Referral,
Fees = api.ExchangeDescription.Fees,
ExchangeDescription = api.ExchangeDescription
});
}
}
return einfo;
}
public static List<string> GetSupportedExchangeNames()
{
List<string> einfo = new List<string>();
foreach (IExchangeAPI api in GetExchangeAPIs())
{
if (api.ExchangeDescription != null && api.ExchangeDescription.Enabled)
{
einfo.Add(api.Name);
}
}
return einfo;
}
/// <summary>
/// Convert an exchange currency to a global currency. For example, on Binance,
/// BCH (Bitcoin Cash) is BCC but in most other exchanges it is BCH, hence
/// the global symbol is BCH.
/// </summary>
/// <param name="currency">Exchange currency</param>
/// <returns>Global currency</returns>
public Task<string> ExchangeCurrencyToGlobalCurrencyAsync(string currency)
{
currency = (currency ?? string.Empty);
foreach (KeyValuePair<string, string> kv in ExchangeGlobalCurrencyReplacements[GetType()])
{
currency = currency.Replace(kv.Key, kv.Value);
}
return Task.FromResult(currency.ToUpperInvariant());
}
/// <summary>
/// Convert a global currency to exchange currency. For example, on Binance,
/// BCH (Bitcoin Cash) is BCC but in most other exchanges it is BCH, hence
/// the global symbol BCH would convert to BCC for Binance, but stay BCH
/// for most other exchanges.
/// </summary>
/// <param name="currency">Global currency</param>
/// <returns>Exchange currency</returns>
public string GlobalCurrencyToExchangeCurrency(string currency)
{
currency = (currency ?? string.Empty);
foreach (KeyValuePair<string, string> kv in ExchangeGlobalCurrencyReplacements[GetType()])
{
currency = currency.Replace(kv.Value, kv.Key);
}
return (MarketSymbolIsUppercase ? currency.ToUpperInvariant() : currency.ToLowerInvariant());
}
/// <summary>
/// Normalize an exchange specific symbol. The symbol should already be in the correct order,
/// this method just deals with casing and putting in the right separator.
/// </summary>
/// <param name="marketSymbol">Symbol</param>
/// <returns>Normalized symbol</returns>
public virtual string NormalizeMarketSymbol(string? marketSymbol)
{
marketSymbol = (marketSymbol ?? string.Empty).Trim();
marketSymbol = marketSymbol.Replace("-", MarketSymbolSeparator)
.Replace("/", MarketSymbolSeparator)
.Replace("_", MarketSymbolSeparator)
.Replace(" ", MarketSymbolSeparator)
.Replace(":", MarketSymbolSeparator);
if (MarketSymbolIsUppercase)
{
return marketSymbol.ToUpperInvariant();
}
return marketSymbol.ToLowerInvariant();
}
/// <summary>
/// Convert an exchange symbol into a global symbol, which will be the same for all exchanges.
/// Global symbols are always uppercase and separate the currency pair with a hyphen (-).
/// Global symbols list the base currency first (i.e. BTC) and quote/conversion currency
/// second (i.e. USD). Global symbols are of the form BASE-QUOTE. BASE-QUOTE is read as
/// 1 BASE is worth y QUOTE.
///
/// Examples:
/// On 1/25/2020,
/// - BTC-USD: $8,371; 1 BTC (base) is worth $8,371 USD (quote)
/// - ETH-BTC: 0.01934; 1 ETH is worth 0.01934 BTC
/// - EUR-USD: 1.2; 1 EUR worth 1.2 USD
///
/// A value greater than 1 means one unit of base currency is more valuable than one unit of
/// quote currency.
/// </summary>
/// <param name="marketSymbol">Exchange symbol</param>
/// <returns>Global symbol</returns>
public virtual async Task<string> ExchangeMarketSymbolToGlobalMarketSymbolAsync(string marketSymbol)
{
string modifiedMarketSymbol = marketSymbol;
char separator;
// if no separator, we must query meta-data and build the pair
if (string.IsNullOrWhiteSpace(MarketSymbolSeparator))
{
// we must look it up via meta-data, most often this call will be cached and fast
ExchangeMarket? em = GetExchangeMarket(ExchangeMarketSymbolToGlobalMarketSymbolAsync(marketSymbol).Sync());
if (em == null)
{
throw new InvalidDataException($"No market symbol meta-data returned or unable to find symbol meta-data for {marketSymbol}");
}
modifiedMarketSymbol = em.BaseCurrency + GlobalMarketSymbolSeparatorString + em.QuoteCurrency;
separator = GlobalMarketSymbolSeparator;
}
else
{
separator = MarketSymbolSeparator[0];
}
return await ExchangeMarketSymbolToGlobalMarketSymbolWithSeparatorAsync(modifiedMarketSymbol, separator);
}
/// <summary>
/// Convert currencies to exchange market symbol
/// </summary>
/// <param name="baseCurrency">Base currency</param>
/// <param name="quoteCurrency">Quote currency</param>
/// <returns>Exchange market symbol</returns>
public virtual Task<string> CurrenciesToExchangeMarketSymbol(string baseCurrency, string quoteCurrency)
{
string symbol = (MarketSymbolIsReversed ? $"{quoteCurrency}{MarketSymbolSeparator}{baseCurrency}" : $"{baseCurrency}{MarketSymbolSeparator}{quoteCurrency}");
return Task.FromResult(MarketSymbolIsUppercase ? symbol.ToUpperInvariant() : symbol);
}
/// <summary>
/// Get currencies from exchange market symbol. This method will call GetMarketSymbolsMetadataAsync if no MarketSymbolSeparator is defined for the exchange.
/// </summary>
/// <param name="marketSymbol">Market symbol</param>
/// <returns>Base and quote currency</returns>
public virtual async Task<(string baseCurrency, string quoteCurrency)> ExchangeMarketSymbolToCurrenciesAsync(string marketSymbol)
{
marketSymbol.ThrowIfNullOrWhitespace(nameof(marketSymbol));
// no separator logic...
if (string.IsNullOrWhiteSpace(MarketSymbolSeparator))
{
// we must look it up via meta-data, most often this call will be cached and fast
ExchangeMarket? market = null;
try
{
market = GetExchangeMarket(ExchangeMarketSymbolToGlobalMarketSymbolAsync(marketSymbol).Sync());
}
catch (Exception)
{
Console.WriteLine("Failed to retrieve GlobalMarketSymbol for {marketSymbol}", marketSymbol);
}
if (market == null)
{
market = await GetExchangeMarketFromCacheAsync(marketSymbol);
}
if (market == null)
{
throw new InvalidDataException($"No market symbol meta-data returned or unable to find symbol meta-data for {marketSymbol}");
}
return (market.BaseCurrency, market.QuoteCurrency);
}
// default behavior with separator
return OnSplitMarketSymbolToCurrencies(marketSymbol);
}
/// <summary>
/// Convert a global symbol into an exchange symbol, which will potentially be different from other exchanges.
/// </summary>
/// <param name="marketSymbol">Global market symbol</param>
/// <returns>Exchange market symbol</returns>
public virtual Task<string> GlobalMarketSymbolToExchangeMarketSymbolAsync(string marketSymbol)
{
if (string.IsNullOrWhiteSpace(marketSymbol))
{
throw new ArgumentException("Market symbol must be non null and non empty");
}
int pos = marketSymbol.IndexOf(GlobalMarketSymbolSeparator);
if (pos < 0)
{
throw new ArgumentException($"Market symbol {marketSymbol} is missing the global symbol separator '{GlobalMarketSymbolSeparator}'");
}
if (MarketSymbolIsReversed == false)
{
marketSymbol = GlobalCurrencyToExchangeCurrency(marketSymbol.Substring(0, pos)) + MarketSymbolSeparator + GlobalCurrencyToExchangeCurrency(marketSymbol.Substring(pos + 1));
}
else
{
marketSymbol = GlobalCurrencyToExchangeCurrency(marketSymbol.Substring(pos + 1)) + MarketSymbolSeparator + GlobalCurrencyToExchangeCurrency(marketSymbol.Substring(0, pos));
}
return Task.FromResult(MarketSymbolIsUppercase ? marketSymbol.ToUpperInvariant() : marketSymbol.ToLowerInvariant());
}
/// <summary>
/// Convert seconds to a period string, or throw exception if seconds invalid. Example: 60 seconds becomes 1m.
/// </summary>
/// <param name="seconds">Seconds</param>
/// <returns>Period string</returns>
public virtual string PeriodSecondsToString(int seconds) => CryptoUtility.SecondsToPeriodString(seconds);
#endregion Other
#region Exchange Configuration Methods
public ExchangeDescriptionModel ExchangeDescription { get; set; }
public string TunnelIp { get; set; } = ""; // MachinaTunnel IP Address
public int TunnelPort { get; set; } = 6000; // MachinaTunnel Port
#endregion
#region REST API
/// <summary>
/// Gets currencies and related data such as IsEnabled and TxFee (if available)
/// </summary>
/// <returns>Collection of Currencies</returns>
public virtual async Task<IReadOnlyDictionary<string, ExchangeCurrency>> GetCurrenciesAsync()
{
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetCurrenciesAsync(), nameof(GetCurrenciesAsync));
}
/// <summary>
/// Get exchange symbols
/// </summary>
/// <returns>Array of symbols</returns>
public virtual async Task<IEnumerable<string>> GetMarketSymbolsAsync()
{
return await Cache.CacheMethod(MethodCachePolicy, async () => (await OnGetMarketSymbolsAsync()).ToArray(), nameof(GetMarketSymbolsAsync));
}
/// <summary>
/// Get exchange symbols including available meta-data such as min trade size and whether the market is active
/// </summary>
/// <returns>Collection of ExchangeMarkets</returns>
public virtual async Task<IEnumerable<ExchangeMarket>> GetMarketSymbolsMetadataAsync()
{
return await Cache.CacheMethod(MethodCachePolicy, async () => (await OnGetMarketSymbolsMetadataAsync()).ToArray(), nameof(GetMarketSymbolsMetadataAsync));
}
/// <summary>
/// Gets the exchange market from this exchange's SymbolsMetadata cache. This will make a network request if needed to retrieve fresh markets from the exchange using GetSymbolsMetadataAsync().
/// Please note that sending a symbol that is not found over and over will result in many network requests. Only send symbols that you are confident exist on the exchange.
/// </summary>
/// <param name="marketSymbol">The market symbol. Ex. ADA/BTC. This is assumed to be normalized and already correct for the exchange.</param>
/// <returns>The ExchangeMarket or null if it doesn't exist in the cache or there was an error</returns>
public virtual async Task<ExchangeMarket?> GetExchangeMarketFromCacheAsync(string marketSymbol)
{
try
{
// *NOTE*: custom caching, do not wrap in CacheMethodCall...
// *NOTE*: vulnerability exists where if spammed with not found symbols, lots of network calls will happen, stalling the application
// TODO: Add not found dictionary, or some mechanism to mitigate this risk
// not sure if this is needed, but adding it just in case
await new SynchronizationContextRemover();
Dictionary<string, ExchangeMarket>? lookup = null;
ExchangeMarket? em = GetExchangeMarket(ExchangeMarketSymbolToGlobalMarketSymbolAsync(marketSymbol).Sync());
if (em != null)
{
return em;
}
lookup = await this.GetExchangeMarketDictionaryFromCacheAsync();
if (lookup != null && lookup.TryGetValue(marketSymbol, out ExchangeMarket market1))
{
return market1;
}
// try again with a fresh request
Cache.Remove(nameof(GetMarketSymbolsMetadataAsync));
Cache.Remove(nameof(ExchangeAPIExtensions.GetExchangeMarketDictionaryFromCacheAsync));
lookup = await this.GetExchangeMarketDictionaryFromCacheAsync();
if (lookup != null && lookup.TryGetValue(marketSymbol, out ExchangeMarket market2))
{
return market2;
}
return null;
}
catch
{
// TODO: Report the error somehow, for now a failed network request will just return null symbol which will force the caller to use default handling
}
return null;
}
/// <summary>
/// Get exchange ticker
/// </summary>
/// <param name="marketSymbol">Symbol to get ticker for</param>
/// <returns>Ticker</returns>
public virtual async Task<ExchangeTicker> GetTickerAsync(string globalSymbol)
{
var marketSymbol = await GlobalMarketSymbolToExchangeMarketSymbolAsync(globalSymbol);
var tick = await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetTickerAsync(marketSymbol), nameof(GetTickerAsync), nameof(marketSymbol), marketSymbol);
if (tick == null)
return null;
// Add the global symbol to the object!
tick.GlobalSymbol = globalSymbol;
return tick;
}
/// <summary>
/// Get all tickers in one request. If the exchange does not support this, a ticker will be requested for each symbol.
/// </summary>
/// <returns>Key value pair of symbol and tickers array</returns>
public virtual async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>> GetTickersAsync()
{
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetTickersAsync(), nameof(GetTickersAsync));
}
/// <summary>
/// Get exchange order book
/// </summary>
/// <param name="marketSymbol">Symbol to get order book for</param>
/// <param name="maxCount">Max count, not all exchanges will honor this parameter</param>
/// <returns>Exchange order book or null if failure</returns>
public virtual async Task<ExchangeOrderBook> GetOrderBookAsync(string globalSymbol, int maxCount = 100)
{
var marketSymbol = await GlobalMarketSymbolToExchangeMarketSymbolAsync(globalSymbol);
marketSymbol = NormalizeMarketSymbol(marketSymbol);
var result = await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetOrderBookAsync(marketSymbol, maxCount), nameof(GetOrderBookAsync), nameof(marketSymbol), marketSymbol, nameof(maxCount), maxCount);
result.Exchange = Name;
result.GlobalSymbol = globalSymbol;
result.MarketSymbol = marketSymbol;
return result;
}
/// <summary>
/// Get all exchange order book symbols in one request. If the exchange does not support this, an order book will be requested for each symbol. Depending on the exchange, the number of bids and asks will have different counts, typically 50-100.
/// </summary>
/// <param name="maxCount">Max count of bids and asks - not all exchanges will honor this parameter</param>
/// <returns>Symbol and order books pairs</returns>
public virtual async Task<IEnumerable<KeyValuePair<string, ExchangeOrderBook>>> GetOrderBooksAsync(int maxCount = 100)
{
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetOrderBooksAsync(maxCount), nameof(GetOrderBooksAsync), nameof(maxCount), maxCount);
}
/// <summary>
/// Get historical trades for the exchange. TODO: Change to async enumerator when available.
/// </summary>
/// <param name="callback">Callback for each set of trades. Return false to stop getting trades immediately.</param>
/// <param name="marketSymbol">Symbol to get historical data for</param>
/// <param name="startDate">Optional UTC start date time to start getting the historical data at, null for the most recent data. Not all exchanges support this.</param>
/// <param name="endDate">Optional UTC end date time to start getting the historical data at, null for the most recent data. Not all exchanges support this.</param>
public virtual async Task GetHistoricalTradesAsync(Func<IEnumerable<ExchangeTrade>, bool> callback, string marketSymbol, DateTime? startDate = null, DateTime? endDate = null, int? limit = null)
{
// *NOTE*: Do not wrap in CacheMethodCall, uses a callback with custom queries, not easy to cache
await new SynchronizationContextRemover();
await OnGetHistoricalTradesAsync(callback, NormalizeMarketSymbol(marketSymbol), startDate, endDate, limit);
}
/// <summary>
/// Get recent trades on the exchange - the default implementation simply calls GetHistoricalTrades with a null sinceDateTime.
/// </summary>
/// <param name="marketSymbol">Symbol to get recent trades for</param>
/// <returns>An enumerator that loops through all recent trades</returns>
public virtual async Task<IEnumerable<ExchangeTrade>> GetRecentTradesAsync(string globalSymbol, int? limit = null)
{
var marketSymbol = await GlobalMarketSymbolToExchangeMarketSymbolAsync(globalSymbol);
marketSymbol = NormalizeMarketSymbol(marketSymbol);
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetRecentTradesAsync(marketSymbol, limit), nameof(GetRecentTradesAsync), nameof(marketSymbol), marketSymbol, nameof(limit), limit);
//return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetRecentTradesAsync(marketSymbol), nameof(GetRecentTradesAsync), nameof(marketSymbol), marketSymbol);
}
/// <summary>
/// Gets the address to deposit to and applicable details.
/// </summary>
/// <param name="currency">Currency to get address for.</param>
/// <param name="forceRegenerate">Regenerate the address</param>
/// <returns>Deposit address details (including tag if applicable, such as XRP)</returns>
public virtual async Task<ExchangeDepositDetails> GetDepositAddressAsync(string currency, bool forceRegenerate = false)
{
if (forceRegenerate)
{
// force regenerate, do not cache
return await OnGetDepositAddressAsync(currency, forceRegenerate);
}
else
{
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetDepositAddressAsync(currency, forceRegenerate), nameof(GetDepositAddressAsync), nameof(currency), currency);
}
}
/// <summary>
/// Gets the deposit history for a symbol
/// </summary>
/// <returns>Collection of ExchangeCoinTransfers</returns>
public virtual async Task<IEnumerable<ExchangeTransaction>> GetDepositHistoryAsync(string currency)
{
return await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetDepositHistoryAsync(currency), nameof(GetDepositHistoryAsync), nameof(currency), currency);
}
/// <summary>
/// Get candles (open, high, low, close)
/// </summary>
/// <param name="marketSymbol">Symbol to get candles for</param>
/// <param name="periodSeconds">Period in seconds to get candles for. Use 60 for minute, 3600 for hour, 3600*24 for day, 3600*24*30 for month.</param>
/// <param name="startDate">Optional start date to get candles for</param>
/// <param name="endDate">Optional end date to get candles for</param>
/// <param name="limit">Max results, can be used instead of startDate and endDate if desired</param>
/// <param name="isHarvester">Is this being run in the context of the candle harvester</param>
/// <returns>Candles</returns>
public virtual async Task<IEnumerable<MarketCandle>> GetCandlesAsync(string globalSymbol, int period, DateTime? startDate = null, DateTime? endDate = null, int? limit = null, bool isHarvester = false)
{
if (isHarvester)
{
return await OnGetCandlesAsync(globalSymbol, period, startDate, endDate);
}
else
{
#if DEBUG
// Check if the requested 'start -> end' is greater than what the exchange allows, if so we need to adjust the caller to request 'buckets' vs requesting too many candles
if (startDate != null && endDate != null)
{
var rateLimit = ExchangeDescription.RateLimit;
var sd = (DateTime)startDate;
// NOTE: The period can vary here (not limited to the min size!)
var maxAllowedEndDate = sd.AddSeconds(rateLimit * period);
if (endDate > maxAllowedEndDate)
{
var duration = ((DateTime)endDate).Subtract((DateTime)startDate).Humanize();
var maxAllowed = maxAllowedEndDate.Subtract((DateTime)startDate).Humanize();
throw new Exception($"Requested too large a timeframe - please adjust the caller - {duration} was requested when {maxAllowed} is the maximum the exchange supports");
}
}
#endif
var cacheFile = ExchangeRuntime.CacheFileName(Name, globalSymbol, startDate, endDate, period);
if (ExchangeRuntime.AllowCandleJsonCache && File.Exists(cacheFile))
{
var cachedCandles = ExchangeRuntime.LoadCandleCacheFile(cacheFile);
if (cachedCandles != null)
return cachedCandles;
}
var candles = await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetCandlesAsync(globalSymbol, period, startDate, endDate, limit), nameof(GetCandlesAsync),
nameof(globalSymbol), globalSymbol, nameof(period), period, nameof(startDate), startDate, nameof(endDate), endDate, nameof(limit), limit);
if (ExchangeRuntime.AllowCandleJsonCache)
{
ExchangeRuntime.CacheCandles(cacheFile, candles, startDate, endDate, period);
}
return candles;
}
}
/// <summary>
/// Get total amounts, symbol / amount dictionary
/// </summary>
/// <returns>Dictionary of symbols and amounts</returns>
public virtual async Task<Dictionary<string, decimal>> GetAmountsAsync()
{
if (TradeType == TradeType.Live)
{
return await Cache.CacheMethod(MethodCachePolicy, async () => (await OnGetAmountsAsync()), nameof(GetAmountsAsync));
}
throw new Exception("Backtesting and Paper trading are handled in Lean - Upgrade the Lean Paper broker to use these features when required!");
//// Paper Trading and Back-testing
//var balances = ExchangeRuntime.ExchangeAccountSimulator.GetAmountsAsync(VirtualAccountId);
//// LogApiBalances("GetAmountsAsync", balances, "");
//return balances;
}
/// <summary>
/// Get fees
/// </summary>
/// <returns>The customer trading fees</returns>
public virtual async Task<List<ExchangeDescriptionMarketFee>> GetFeesAync()
{
// NOTE: This is not supported by most exchanges so we simply return the data we have from our exchange description information
return ExchangeDescription.Fees.Trading;
}
public void SetSelectedFees(ExchangeDescriptionMarketFee fees, bool useFees = false)
{
LogApiCall(ApiLogType.Fees, "SetSelectedFees", "", TradeType.ToString());
UseFees = useFees;
if (!UseFees)
return;
ExchangeDescriptionMarketFee = fees;
}
public ExchangeDescriptionMarketFee GetSelectedFees()
{
LogApiCall(ApiLogType.Fees, "GetSelectedFees", TradeType.ToString());
if (ExchangeDescriptionMarketFee == null)
{
ExchangeDescriptionMarketFee = ExchangeDescription.Fees.Trading.FirstOrDefault();
}
return ExchangeDescriptionMarketFee;
}
/// <summary>
/// Get amounts available to trade, symbol / amount dictionary
/// </summary>
/// <returns>Symbol / amount dictionary</returns>
public virtual async Task<Dictionary<string, decimal>> GetAmountsAvailableToTradeAsync()
{
if (TradeType == TradeType.Live)
{
var exchangeBalances = await Cache.CacheMethod(MethodCachePolicy, async () => await OnGetAmountsAvailableToTradeAsync(), nameof(GetAmountsAvailableToTradeAsync));
if (PublicApiKey != null)
{
ExchangeRuntime.UpdateBalanceCache(PublicApiKey.ToUnsecureString(), exchangeBalances);
}
return exchangeBalances;
}
throw new Exception("Backtesting and Paper trading are handled in Lean - Upgrade the Lean Paper broker to use these features when required!");
//// Paper Trading and Back-testing
//if (ExchangeRuntime.ExchangeAccountSimulator == null)
// return new Dictionary<string, decimal>();
//var balances = ExchangeRuntime.ExchangeAccountSimulator.GetAmountsAvailableToTradeAsync(VirtualAccountId);
//ExchangeRuntime.UpdateBalanceCache(VirtualAccountId, balances);
//// LogApiBalances("GetAmountsAvailableToTradeAsync", balances, "");
//return balances;
}
/// <summary>
/// Place an order
/// </summary>
/// <param name="order">The order request</param>
/// <returns>Result</returns>
public virtual async Task<ExchangeOrderResult> PlaceOrderAsync(ExchangeOrderRequest order)
{
// *NOTE* do not wrap in CacheMethodCall
await new SynchronizationContextRemover();
ExchangeOrderResult eor = new ExchangeOrderResult();
var marketSymbol = NormalizeMarketSymbol(order.MarketSymbol);
if (order.TradeType == TradeType.Live)
{
order.MarketSymbol = marketSymbol;
if (order.OrderType == OrderType.Market && !ExchangeDescription.Options.MarketOrdersSupported)
{
eor = await OnPlaceSafeMarketOrderAsync(order.GlobalSymbol, order.Amount, order.IsBuy, order.TradeType);
if (eor != null)
{