Exemple #1
0
        /// <summary>
        /// Creates new instance of <see cref="RestClient"/> object.
        /// </summary>
        /// <param name="keyId">Application key identifier.</param>
        /// <param name="secretKey">Application secret key.</param>
        /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
        /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
        /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
        /// <param name="apiVersion">Version of Alpaca api to call.  Valid values are "1" or "2".</param>
        /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
        /// <param name="throttleParameters">Parameters for requests throttling.</param>
        public RestClient(
            String keyId,
            String secretKey,
            Uri alpacaRestApi,
            Uri polygonRestApi,
            Uri alpacaDataApi,
            Int32 apiVersion,
            Boolean isStagingEnvironment,
            ThrottleParameters throttleParameters)
        {
            if (keyId == null)
            {
                throw new ArgumentException(nameof(keyId));
            }
            if (secretKey == null)
            {
                throw new ArgumentException(nameof(secretKey));
            }
            if (!_supportedApiVersions.Contains(apiVersion))
            {
                throw new ArgumentException(nameof(apiVersion));
            }

            _alpacaRestApiThrottler = throttleParameters.GetThrottler();

            _alpacaHttpClient.DefaultRequestHeaders.Add(
                "APCA-API-KEY-ID", keyId);
            _alpacaHttpClient.DefaultRequestHeaders.Add(
                "APCA-API-SECRET-KEY", secretKey);
            _alpacaHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaHttpClient.BaseAddress = addApiVersionNumberSafe(
                alpacaRestApi ?? new Uri("https://api.alpaca.markets"), apiVersion);

            _alpacaDataClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _alpacaDataClient.BaseAddress = addApiVersionNumberSafe(
                alpacaDataApi ?? new Uri("https://data.alpaca.markets"), apiVersion);

            _polygonApiKey = keyId;
            _polygonHttpClient.DefaultRequestHeaders.Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _polygonHttpClient.BaseAddress =
                polygonRestApi ?? new Uri("https://api.polygon.io");
            _isPolygonStaging = isStagingEnvironment ||
                                _alpacaHttpClient.BaseAddress.Host.Contains("staging");

            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
        }
Exemple #2
0
        /// <summary>
        /// Creates new instance of <see cref="RateThrottler"/> object with parameters
        /// specified in <paramref name="throttleParameters"/> parameter.
        /// </summary>
        /// <param name="throttleParameters"></param>
        public RateThrottler(
            ThrottleParameters throttleParameters)
        {
            _timeUnitMilliseconds = (Int32)throttleParameters.TimeUnit.TotalMilliseconds;
            MaxRetryAttempts      = throttleParameters.MaxRetryAttempts;
            _retryHttpStatuses    = new HashSet <Int32>(
                throttleParameters.RetryHttpStatuses ?? Enumerable.Empty <Int32>());

            // Create the throttle semaphore, with the number of occurrences as the maximum count.
            _throttleSemaphore = new SemaphoreSlim(throttleParameters.Occurrences, throttleParameters.Occurrences);

            // Create a queue to hold the semaphore exit times.
            _exitTimes = new ConcurrentQueue <Int32>();

            // Create a timer to exit the semaphore. Use the time unit as the original
            // interval length because that's the earliest we will need to exit the semaphore.
            _exitTimer = new Timer(exitTimerCallback, null, _timeUnitMilliseconds, -1);
        }
Exemple #3
0
 /// <summary>
 /// Creates new instance of <see cref="RestClient"/> object.
 /// </summary>
 /// <param name="keyId">Application key identifier.</param>
 /// <param name="secretKey">Application secret key.</param>
 /// <param name="alpacaRestApi">Alpaca REST API endpoint URL.</param>
 /// <param name="polygonRestApi">Polygon REST API endpoint URL.</param>
 /// <param name="alpacaDataApi">Alpaca REST data API endpoint URL.</param>
 /// <param name="apiVersion">Version of Alpaca api to call.  Valid values are "1" or "2".</param>
 /// <param name="isStagingEnvironment">If <c>true</c> use staging.</param>
 /// <param name="throttleParameters">Parameters for requests throttling.</param>
 public RestClient(
     String keyId,
     String secretKey,
     String alpacaRestApi                  = null,
     String polygonRestApi                 = null,
     String alpacaDataApi                  = null,
     Int32?apiVersion                      = null,
     Boolean?isStagingEnvironment          = null,
     ThrottleParameters throttleParameters = null)
     : this(
         keyId,
         secretKey,
         new Uri(alpacaRestApi ?? "https://api.alpaca.markets"),
         new Uri(polygonRestApi ?? "https://api.polygon.io"),
         new Uri(alpacaDataApi ?? "https://data.alpaca.markets"),
         apiVersion ?? DEFAULT_API_VERSION_NUMBER,
         isStagingEnvironment ?? false,
         throttleParameters ?? ThrottleParameters.Default)
 {
 }