Example #1
0
        public async Task <BitstampTicker> GetTickerAsync(BitstampTickerCode tickerCode)
        {
            var result = "";

            try
            {
                //var cts = new CancellationTokenSource();
                //cts.CancelAfter(2500);

                using (var client = new HttpClient())
                    using (var response = await client.GetAsync(ApiBaseUrl + "ticker/" + tickerCode.ToString().ToLower()))
                        using (var content = response.Content)
                        {
                            result = await content.ReadAsStringAsync();

                            _ticker = JsonConvert.DeserializeObject <BitstampTicker>(result);
                            UpdateMinMaxLogs(_ticker.Last);
                            if (tickerCode == BitstampTickerCode.BtcUsd) //todo: make beter storage of tickers, now code expects only BtcUsd ticker
                            {
                                UpdateTickerHistory(_ticker);
                            }

                            return(_ticker);
                        }
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
            catch (Exception e)
            {
                return(null);
                //throw new Exception("BitstampExchange.GetTickerAsync() : " + e.Message + Environment.NewLine + result);
            }
        }
Example #2
0
        public async Task <BitstampOrder> BuyLimitOrderAsync(BitstampTickerCode tickerCode, decimal amount, decimal price)
        {
            var result = "";

            try
            {
                // prepare post data
                var postData = GetAuthenticationPostData();
                postData.Add(new KeyValuePair <string, string>("amount", amount.ToString(CultureInfo.InvariantCulture)));
                postData.Add(new KeyValuePair <string, string>("price", price.ToString(CultureInfo.InvariantCulture)));

                using (var client = new HttpClient())
                    using (var response = await client.PostAsync(ApiBaseUrl + "buy/" + tickerCode.ToString().ToLower() + "/", new FormUrlEncodedContent(postData)))
                        using (var content = response.Content)
                        {
                            result = await content.ReadAsStringAsync();

                            var executedOrder = JsonConvert.DeserializeObject <BitstampOrder>(result);
                            if (executedOrder.Id != 0)
                            {
                                SaveBuyLimitOrderInDb(executedOrder);
                            }
                            else
                            {
                                throw new Exception("Executed buy order id==0!!!!");
                            }
                            return(executedOrder);
                        }
            }
            catch (Exception e)
            {
                throw new Exception("BitstampExchange.BuyLimitOrderAsync() : " + e.Message + Environment.NewLine + result);
            }
        }