Beispiel #1
0
        public string PlaceOrderRaw(
            BlocktradeApiKey apiKey,
            int portfolioId,
            string direction,
            string type,
            int tradingPairId,
            decimal amount,
            decimal price,
            string timeInForce,
            string stopPrice)
        {
            var url = $"{ApiRoot}orders";

            var requestModel = new PlaceOrderRequest
            {
                PortfolioId   = portfolioId,
                Direction     = direction,
                Type          = type,
                TradingPairId = tradingPairId,
                Amount        = amount.ToString(),
                Price         = price.ToString(),
                TimeInForce   = timeInForce,
                StopPrice     = stopPrice
            };

            var json = JsonConvert.SerializeObject(requestModel);

            return(AuthenticatedPost(apiKey, url, json));
        }
Beispiel #2
0
        public List <BlocktradePortfolio> GetUserPortfolios(BlocktradeApiKey apiKey)
        {
            var contents = GetUserPortfoliosRaw(apiKey);

            return(!string.IsNullOrWhiteSpace(contents)
                ? JsonConvert.DeserializeObject <List <BlocktradePortfolio> >(contents)
                : new List <BlocktradePortfolio>());
        }
Beispiel #3
0
        public BlocktradeUser GetUser(BlocktradeApiKey apiKey)
        {
            var contents = GetUserRaw(apiKey);

            return(!string.IsNullOrWhiteSpace(contents)
                ? JsonConvert.DeserializeObject <BlocktradeUser>(contents)
                : new BlocktradeUser());
        }
Beispiel #4
0
        public BlocktradeMessage CancelOrder(BlocktradeApiKey apiKey, long orderId)
        {
            var contents = CancelOrderRaw(apiKey, orderId);

            return(!string.IsNullOrWhiteSpace(contents)
                ? JsonConvert.DeserializeObject <BlocktradeMessage>(contents)
                : new BlocktradeMessage());
        }
Beispiel #5
0
        public BlocktradeGetUserOrdersResponse GetUserOrders(BlocktradeApiKey apiKey)
        {
            var contents = GetUserOrdersRaw(apiKey);

            return(!string.IsNullOrWhiteSpace(contents)
                ? JsonConvert.DeserializeObject <BlocktradeGetUserOrdersResponse>(contents)
                : new BlocktradeGetUserOrdersResponse());
        }
Beispiel #6
0
        public BlocktradeMessage CancelActive(BlocktradeApiKey apiKey)
        {
            var contents = CancelActiveRaw(apiKey);

            return(!string.IsNullOrWhiteSpace(contents)
                ? JsonConvert.DeserializeObject <BlocktradeMessage>(contents)
                : new BlocktradeMessage());
        }
Beispiel #7
0
 public string PlaceLimitAskRaw(
     BlocktradeApiKey apiKey,
     string baseSymbol,
     string quoteSymbol,
     decimal quantity,
     decimal price)
 {
     return(PlaceLimitRaw(apiKey, false, baseSymbol, quoteSymbol, quantity, price));
 }
Beispiel #8
0
        public BlocktradePlaceOrderResponse PlaceLimitAsk(
            BlocktradeApiKey apiKey,
            string baseSymbol,
            string quoteSymbol,
            decimal quantity,
            decimal price)
        {
            var response = PlaceLimitAskRaw(apiKey, baseSymbol, quoteSymbol, quantity, price);

            return(!string.IsNullOrWhiteSpace(response)
                ? JsonConvert.DeserializeObject <BlocktradePlaceOrderResponse>(response)
                : new BlocktradePlaceOrderResponse());
        }
Beispiel #9
0
        public BlocktradePlaceOrderResponse PlaceOrder(
            BlocktradeApiKey apiKey,
            int portfolioId,
            string direction,
            string type,
            int tradingPairId,
            decimal amount,
            decimal price,
            string timeInForce,
            string stopPrice)
        {
            var response = PlaceOrderRaw(apiKey, portfolioId, direction, type, tradingPairId, amount, price, timeInForce, stopPrice);

            return(!string.IsNullOrWhiteSpace(response)
                ? JsonConvert.DeserializeObject <BlocktradePlaceOrderResponse>(response)
                : new BlocktradePlaceOrderResponse());
        }
Beispiel #10
0
        private string AuthenticatedGet(BlocktradeApiKey apiKey, string url)
        {
            var nonce     = GenerateNonce();
            var message   = $"{apiKey.PublicKey}.{nonce}.";
            var signature = Sign(apiKey.PrivateKey, message);

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.ContentType = "application/json";
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("X-Api-Key", apiKey.PublicKey);
            request.Headers.Add("X-nonce", nonce.ToString());
            request.Headers.Add("X-Signature", signature);

            using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        return(reader.ReadToEnd());
                    }
        }
Beispiel #11
0
        private string AuthenticatedPost(BlocktradeApiKey apiKey, string url, string body = null)
        {
            var nonce   = GenerateNonce();
            var message = $"{apiKey.PublicKey}.{nonce}.";

            if (!string.IsNullOrWhiteSpace(body))
            {
                message += body;
            }
            var signature = Sign(apiKey.PrivateKey, message);

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Method      = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("X-Api-Key", apiKey.PublicKey);
            request.Headers.Add("X-nonce", nonce.ToString());
            request.Headers.Add("X-Signature", signature);

            if (!string.IsNullOrWhiteSpace(body))
            {
                var encodedBody = Encoding.UTF8.GetBytes(body);

                request.ContentLength = encodedBody.Length;
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(encodedBody, 0, encodedBody.Length);
                }
            }

            using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        return(reader.ReadToEnd());
                    }
        }
Beispiel #12
0
        private string PlaceLimitRaw(
            BlocktradeApiKey apiKey,
            bool isBid,
            string baseSymbol,
            string quoteSymbol,
            decimal quantity,
            decimal price)
        {
            const int    PortfolioId = 1;
            const string Type        = BlocktradeOrderType.Limit;
            const string TimeInForce = BlocktradeTimeInForce.GoodUntilCancelled;
            var          direction   = isBid ? BlocktradeDirection.Buy : BlocktradeDirection.Sell;

            var tradingAssets = GetTradingAssets();

            var baseAsset  = tradingAssets.Single(queryTradingAsset => string.Equals(queryTradingAsset.IsoCode, baseSymbol, StringComparison.InvariantCultureIgnoreCase));
            var quoteAsset = tradingAssets.Single(queryTradingAsset => string.Equals(queryTradingAsset.IsoCode, quoteSymbol, StringComparison.InvariantCultureIgnoreCase));

            var tradingPairs        = GetTradingPairs();
            var matchingTradingPair = tradingPairs.Single(queryTradingPair => queryTradingPair.BaseAssetId == baseAsset.Id && queryTradingPair.QuoteAssetId == quoteAsset.Id);

            return(PlaceOrderRaw(apiKey, PortfolioId, direction, Type, matchingTradingPair.Id, quantity, price, TimeInForce, null));
        }
Beispiel #13
0
        public string GetUserPortfoliosRaw(BlocktradeApiKey apiKey)
        {
            const string Url = "https://trade.blocktrade.com/api/v1/portfolios";

            return(AuthenticatedGet(apiKey, Url));
        }
Beispiel #14
0
        public string CancelOrderRaw(BlocktradeApiKey apiKey, long orderId)
        {
            var url = $"{ApiRoot}orders/{orderId}/cancel";

            return(AuthenticatedPost(apiKey, url));
        }
Beispiel #15
0
        public string CancelActiveRaw(BlocktradeApiKey apiKey)
        {
            const string Url = "https://trade.blocktrade.com/api/v1/orders/cancel_active";

            return(AuthenticatedPost(apiKey, Url));
        }