예제 #1
0
        public void ConfigureMap(IMapperConfigurationExpression cfg)
        {
            cfg.CreateMap <CandleResponse, CandleDto>()
            .ForMember(d => d.ExchangeName, o => o.MapFrom(s => ExchangeConfig.ExchangeName))
            .ForMember(d => d.Instrument, o => o.MapFrom(s => _exchangeApi.ToInstrumentType(s.Symbol)))
            .ForMember(d => d.Period, o => o.Ignore());

            cfg.CreateMap <TradeResponse, TradeDto>()
            .ForMember(d => d.ExchangeName, o => o.MapFrom(s => ExchangeConfig.ExchangeName))
            .ForMember(d => d.Instrument, o => o.MapFrom(s => _exchangeApi.ToInstrumentType(s.Symbol)))
            .ForMember(d => d.Side, o => o.MapFrom(s => BitmexUtils.ToTradeType(s.Side)))
            .ForMember(d => d.Timestamp, o => o.MapFrom(s => BitmexUtils.Now()));

            cfg.CreateMap <BookResponse, BookDto>()
            .ForMember(d => d.Instrument, o => o.MapFrom(s => _exchangeApi.ToInstrumentType(s.Symbol)))
            .ForMember(d => d.Side, o => o.MapFrom(s => BitmexUtils.ToTradeType(s.Side)));

            cfg.CreateMap <OrderResponse, OrderDto>()
            .ForMember(d => d.ExchangeName, o => o.MapFrom(s => ExchangeConfig.ExchangeName))
            .ForMember(d => d.Instrument, o => o.MapFrom(s => _exchangeApi.ToInstrumentType(s.Symbol)))
            .ForMember(d => d.Side, o => o.MapFrom(s => BitmexUtils.ToTradeType(s.Side)))
            .ForMember(d => d.Timestamp, o => o.MapFrom(s => BitmexUtils.Now()));

            cfg.CreateMap <PositionResponse, PositionDto>()
            .ForMember(d => d.ExchangeName, o => o.MapFrom(s => ExchangeConfig.ExchangeName))
            .ForMember(d => d.Instrument, o => o.MapFrom(s => _exchangeApi.ToInstrumentType(s.Symbol)))
            .ForMember(d => d.Timestamp, o => o.MapFrom(s => BitmexUtils.Now()));
        }
예제 #2
0
        private static string CreateSignature(string secret)
        {
            var message        = $"GET/realtime{BitmexUtils.Expires()}";
            var signatureBytes = StorageUtils.EncryptHMACSHA256(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(message));

            return(StorageUtils.ByteArrayToHexString(signatureBytes));
        }
예제 #3
0
        public static Dictionary <string, string> GetHttpAuthHeaders(string baseUrl, HttpMethod httpMethod, string requestPath, string urlQuery, string key, string secret)
        {
            var signatureMessage = $"{httpMethod.ToString()}/api/v1{requestPath}{urlQuery}{BitmexUtils.Expires()}";
            var signatureBytes   = StorageUtils.EncryptHMACSHA256(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(signatureMessage));

            return(new Dictionary <string, string>
            {
                { "api-expires", BitmexUtils.Expires().ToString() },
                { "api-key", key },
                { "api-signature", StorageUtils.ByteArrayToHexString(signatureBytes) }
            });
        }
예제 #4
0
        private async Task <List <T> > GetAllAsync <T, TY, TTy>(string path, TTy request)
            where T : ExchangeModel where TTy : ICollectionRequest
        {
            var stopwatch = new Stopwatch();
            var total     = 0;
            var count     = 0;

            var returnModels = new List <T>();

            do
            {
                using (var client = new ExchangeHttpClient(ExchangeConfig.RestUrl))
                {
                    stopwatch.Restart();
                    total          += count;
                    request.StartAt = total;

                    var query = request.AsDictionary().BuildQueryString();

                    var response = await client.GetAsync <TY>(path, query,
                                                              BitmexUtils.GetHttpAuthHeaders(ExchangeConfig.RestUrl, HttpMethod.GET, path, query,
                                                                                             ExchangeConfig.UserConfig.Key, ExchangeConfig.UserConfig.Secret));

                    if (response == null)
                    {
                        throw new NullReferenceException();
                    }

                    var models = response.Select(x => Mapper.Map <T>(x)).ToArray();

                    returnModels.AddRange(models);

                    count = response.Length;

                    if (count < request.Count)
                    {
                        count = 0;
                    }

                    if (stopwatch.Elapsed < TimeSpan.FromSeconds(1.01))
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1.5).Subtract(stopwatch.Elapsed));
                    }
                }
            } while (count > 0);

            return(returnModels);
        }
예제 #5
0
        private string GetSocketRequest(ExchangeChannel channel)
        {
            switch (channel)
            {
            case ExchangeChannel.Trade:
                return(BitmexUtils.GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Trade], ToSymbol(Instrument.BTCUSD)));

            case ExchangeChannel.Book:
                return(BitmexUtils.GetSocketRequest(ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Book], ToSymbol(Instrument.BTCUSD)));

            case ExchangeChannel.Candle:
                var paths = ExchangeConfig.SupportedTimePeriods.Values.Select(x => $"{ExchangeConfig.SupportedWebsocketChannels[ExchangeChannel.Candle]}{x}").ToList();
                var args  = paths.Select(x => new[] { ToSymbol(Instrument.BTCUSD) }).ToList();

                return(BitmexUtils.GetSocketRequest(paths, args));

            default:
                throw new ArgumentOutOfRangeException(nameof(channel), channel, null);
            }
        }
예제 #6
0
 public static string GetSocketAuthCommand(string key, string secret)
 {
     return($@"{{""op"": ""authKeyExpires"", ""args"": [""{key}"", {BitmexUtils.Expires()}, ""{CreateSignature(secret)}""]}}");
 }