Exemplo n.º 1
0
        public void GetMarketData_All_Fields()
        {
            // Arrange
            var client = new JsonServiceClient(_webServiceUrl);

            var request = new GetMarketData();

            request.DataSource           = "GOOG";
            request.Ticker               = "NASDAQ_TSLA";
            request.From                 = new DateTime(2000, 01, 01, 00, 00, 00);
            request.To                   = new DateTime(2014, 01, 01, 00, 00, 00);
            request.RowsNumber           = 300;
            request.Timeframe            = TimeframeType.DAILY;
            request.Transformation       = TransformationType.RDIFF;
            request.SortOrder            = SortOrderType.DESC;
            request.ExcludeHeaders       = false;
            request.SpecificColumnNumber = null;

            // Act
            var response = client.Get(request);

            client.Dispose();

            // Assert
            Assert.True(response.Data.Any());
        }
		public void GetMarketData_Invalid_Ticker()
		{
			// Arrange
			var request = new GetMarketData();
			request.DataSource = "GOOG";
			request.Ticker = "AAPL";

			// Act
			var dataSource = _dataProvider.GetMarketData(request);

			// Assert
			Assert.False(dataSource.Success);
			Assert.AreEqual("Requested entity does not exist.", dataSource.ErrorMessage);
		}
        public void GetMarketData_Only_Required_Fields()
        {
            // Arrange
            var request = new GetMarketData();

            request.DataSource = "GOOG";
            request.Ticker     = "NASDAQ_TSLA";

            // Act
            var dataSource = _dataProvider.GetMarketData(request);

            // Assert
            Assert.True(dataSource.Success);
            Assert.True(dataSource.Data.Any());
        }
        public void GetMarketData_Invalid_Ticker()
        {
            // Arrange
            var request = new GetMarketData();

            request.DataSource = "GOOG";
            request.Ticker     = "AAPL";

            // Act
            var dataSource = _dataProvider.GetMarketData(request);

            // Assert
            Assert.False(dataSource.Success);
            Assert.AreEqual("Requested entity does not exist.", dataSource.ErrorMessage);
        }
        public object Get(GetMarketData request)
        {
            var response = new GetMarketDataResponse();

            try
            {
                if (string.IsNullOrEmpty(request.DataSource))
                {
                    throw new ArgumentNullException("request", "[DataSource] field is empty");
                }
                if (string.IsNullOrEmpty(request.Ticker))
                {
                    throw new ArgumentNullException("request", "[Ticker] field is empty");
                }

                var cache = _cachingManager.GetMarketData(request.ToJson());
                if (cache != null)
                {
                    return(cache);
                }
                else
                {
                    var result = _dataProvider.GetMarketData(request);
                    if (result.Success)
                    {
                        response.Success = true;
                        response.Data    = result.Data;
                        _cachingManager.Save(request.ToJson(), response);
                        _cachingManager.SaveCurrentChart("CurrentChart", request);
                    }
                    else
                    {
                        response.ResponseStatus = new ResponseStatus(string.Empty, result.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                var status = new ResponseStatus();
                status.Message    = ex.Message;
                status.ErrorCode  = ex.Source;
                status.StackTrace = ex.StackTrace;

                response.ResponseStatus = status;
            }

            return(response);
        }
		public void GetMarketData_All_Fields()
		{
			// Arrange
			var client = new JsonServiceClient(_webServiceUrl);

			var request = new GetMarketData();
			request.DataSource = "GOOG";
			request.Ticker = "NASDAQ_TSLA";
			request.From = new DateTime(2000, 01, 01, 00, 00, 00);
			request.To = new DateTime(2014, 01, 01, 00, 00, 00);
			request.RowsNumber = 300;
			request.Timeframe = TimeframeType.DAILY;
			request.Transformation = TransformationType.RDIFF;
			request.SortOrder = SortOrderType.DESC;
			request.ExcludeHeaders = false;
			request.SpecificColumnNumber = null;

			// Act
			var response = client.Get(request);
			client.Dispose();

			// Assert
			Assert.True(response.Data.Any());
		}
		public void GetMarketData_Only_Required_Fields()
		{
			// Arrange
			var request = new GetMarketData();
			request.DataSource = "GOOG";
			request.Ticker = "NASDAQ_TSLA";

			// Act
			var dataSource = _dataProvider.GetMarketData(request);

			// Assert
			Assert.True(dataSource.Success);
			Assert.True(dataSource.Data.Any());
		}
		public object Get(GetMarketData request)
		{
			var response = new GetMarketDataResponse();

			try
			{
				if (string.IsNullOrEmpty(request.DataSource))
				{
					throw new ArgumentNullException("request", "[DataSource] field is empty");
				}
				if (string.IsNullOrEmpty(request.Ticker))
				{
					throw new ArgumentNullException("request", "[Ticker] field is empty");
				}

				var cache = _cachingManager.GetMarketData(request.ToJson());
				if (cache != null)
				{
					return cache;
				}
				else
				{
					var result = _dataProvider.GetMarketData(request);
					if (result.Success)
					{
						response.Success = true;
						response.Data = result.Data;
						_cachingManager.Save(request.ToJson(), response);
						_cachingManager.SaveCurrentChart("CurrentChart", request);
					}
					else
					{
						response.ResponseStatus = new ResponseStatus(string.Empty, result.ErrorMessage);
					}
				}
			}
			catch (Exception ex)
			{
				var status = new ResponseStatus();
				status.Message = ex.Message;
				status.ErrorCode = ex.Source;
				status.StackTrace = ex.StackTrace;

				response.ResponseStatus = status;
			}

			return response;
		}
		public bool SaveCurrentChart(string key, GetMarketData request)
		{
			return _client.Set(key, request);
		}
        public OperationResult <List <IMarketData> > GetMarketData(GetMarketData request)
        {
            try
            {
                var client      = new RestClient(QuandlSettings.QuandlUrl + request.DataSource + "/");
                var restRequest = new RestRequest(request.Ticker + ".json", Method.GET);

                if (request.SortOrder.HasValue)
                {
                    restRequest.AddParameter("sort_order", request.SortOrder.Value.ToString().ToLower());
                }
                else
                {
                    restRequest.AddParameter("sort_order", SortOrderType.ASC.ToString().ToLower());
                }

                if (request.ExcludeHeaders.HasValue)
                {
                    restRequest.AddParameter("exclude_headers", request.ExcludeHeaders);
                }

                if (request.RowsNumber.HasValue)
                {
                    restRequest.AddParameter("rows", request.RowsNumber.Value);
                }
                else
                {
                    if (request.From.HasValue)
                    {
                        restRequest.AddParameter("trim_start", request.From.Value.ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        restRequest.AddParameter("trim_start", DateTime.Now.AddYears(-1).Date.ToString("yyyy-MM-dd"));
                    }
                    if (request.To.HasValue)
                    {
                        restRequest.AddParameter("trim_end", request.To.Value.ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        restRequest.AddParameter("trim_end", DateTime.Now.Date.ToString("yyyy-MM-dd"));
                    }
                }

                if (request.SpecificColumnNumber.HasValue)
                {
                    restRequest.AddParameter("column", request.SpecificColumnNumber);
                }

                if (request.Timeframe.HasValue)
                {
                    restRequest.AddParameter("collapse", request.Timeframe.ToString().ToLower());
                }
                else
                {
                    restRequest.AddParameter("collapse", TimeframeType.NONE.ToString().ToLower());
                }

                if (request.Transformation.HasValue)
                {
                    restRequest.AddParameter("transformation", request.Transformation.ToString().ToLower());
                }
                else
                {
                    restRequest.AddParameter("transformation", TransformationType.NONE.ToString().ToLower());
                }

                restRequest.AddParameter("auth_token", QuandlSettings.QuandlToken);

                var response = client.Execute(restRequest);

                if (response.ResponseStatus != ResponseStatus.Completed)
                {
                    _logger.Error(response.ErrorException);
                    throw new QuandlProviderException(response.ErrorMessage, response.ErrorException);
                }
                else
                {
                    var actual = response.Content.FromJson <RootObjectMarketData>();

                    if (!string.IsNullOrEmpty(actual.error))
                    {
                        return(new OperationResult <List <IMarketData> >(false, actual.error));
                    }

                    if (actual.data != null && actual.data.Any() &&
                        actual.column_names != null && actual.column_names.Any())
                    {
                        var list = new List <IMarketData>();

                        foreach (var data in actual.data)
                        {
                            var result = _mapper.ToMarketData(request.DataSource, data);
                            list.Add(result);
                        }

                        if (list.Any())
                        {
                            return(new OperationResult <List <IMarketData> >(list));
                        }
                        else
                        {
                            return(new OperationResult <List <IMarketData> >(false, "No data found"));
                        }
                    }
                    else
                    {
                        return(new OperationResult <List <IMarketData> >(false, "No market data found for current ticker"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw new QuandlProviderException(ex.Message, ex);
            }
        }
		public OperationResult<List<IMarketData>> GetMarketData(GetMarketData request)
		{
			try
			{
				var client = new RestClient(QuandlSettings.QuandlUrl + request.DataSource + "/");
				var restRequest = new RestRequest(request.Ticker + ".json", Method.GET);

				if (request.SortOrder.HasValue)
				{
					restRequest.AddParameter("sort_order", request.SortOrder.Value.ToString().ToLower());
				}
				else
				{
					restRequest.AddParameter("sort_order", SortOrderType.ASC.ToString().ToLower());
				}

				if (request.ExcludeHeaders.HasValue)
				{
					restRequest.AddParameter("exclude_headers", request.ExcludeHeaders);
				}

				if (request.RowsNumber.HasValue)
				{
					restRequest.AddParameter("rows", request.RowsNumber.Value);
				}
				else
				{
					if (request.From.HasValue)
					{
						restRequest.AddParameter("trim_start", request.From.Value.ToString("yyyy-MM-dd"));
					}
					else
					{
						restRequest.AddParameter("trim_start", DateTime.Now.AddYears(-1).Date.ToString("yyyy-MM-dd"));
					}
					if (request.To.HasValue)
					{
						restRequest.AddParameter("trim_end", request.To.Value.ToString("yyyy-MM-dd"));
					}
					else
					{
						restRequest.AddParameter("trim_end", DateTime.Now.Date.ToString("yyyy-MM-dd"));
					}
				}

				if (request.SpecificColumnNumber.HasValue)
				{
					restRequest.AddParameter("column", request.SpecificColumnNumber);
				}

				if (request.Timeframe.HasValue)
				{
					restRequest.AddParameter("collapse", request.Timeframe.ToString().ToLower());
				}
				else
				{
					restRequest.AddParameter("collapse", TimeframeType.NONE.ToString().ToLower());
				}

				if (request.Transformation.HasValue)
				{
					restRequest.AddParameter("transformation", request.Transformation.ToString().ToLower());
				}
				else
				{
					restRequest.AddParameter("transformation", TransformationType.NONE.ToString().ToLower());
				}

				restRequest.AddParameter("auth_token", QuandlSettings.QuandlToken);

				var response = client.Execute(restRequest);

				if (response.ResponseStatus != ResponseStatus.Completed)
				{
					_logger.Error(response.ErrorException);
					throw new QuandlProviderException(response.ErrorMessage, response.ErrorException);
				}
				else
				{
					var actual = response.Content.FromJson<RootObjectMarketData>();

					if (!string.IsNullOrEmpty(actual.error))
					{
						return new OperationResult<List<IMarketData>>(false, actual.error);
					}

					if (actual.data != null && actual.data.Any() &&
						actual.column_names != null && actual.column_names.Any())
					{
						var list = new List<IMarketData>();

						foreach (var data in actual.data)
						{
							var result = _mapper.ToMarketData(request.DataSource, data);
							list.Add(result);
						}

						if (list.Any())
						{
							return new OperationResult<List<IMarketData>>(list);
						}
						else
						{
							return new OperationResult<List<IMarketData>>(false, "No data found");
						}
					}
					else
					{
						return new OperationResult<List<IMarketData>>(false, "No market data found for current ticker");
					}
				}
			}
			catch (Exception ex)
			{
				_logger.Error(ex);
				throw new QuandlProviderException(ex.Message, ex);
			}
		}
Exemplo n.º 12
0
 public bool SaveCurrentChart(string key, GetMarketData request)
 {
     return(_client.Set(key, request));
 }