Beispiel #1
0
        /// <summary>
        /// Opens connection to Alpaca streaming API.
        /// </summary>
        public void Connect()
        {
            var          connectedEvent = new ManualResetEvent(false);
            EventHandler onOpenAction   = (s, e) =>
            {
                connectedEvent.Set();
            };

            _webSocket.Open += onOpenAction;

            try
            {
                _webSocket.Connect();

                if (!connectedEvent.WaitOne(ConnectionTimeout))
                {
                    throw new Exception("SockClient.Connect(): WebSocket connection timeout.");
                }
            }
            finally
            {
                _webSocket.Open -= onOpenAction;

                connectedEvent.DisposeSafely();
            }
        }
        /// <summary>
        /// Subscribe to the specified configuration
        /// </summary>
        /// <param name="dataConfig">defines the parameters to subscribe to a data feed</param>
        /// <param name="newDataAvailableHandler">handler to be fired on new data available</param>
        /// <returns>The new enumerator for this subscription request</returns>
        public IEnumerator <BaseData> Subscribe(SubscriptionDataConfig dataConfig, EventHandler newDataAvailableHandler)
        {
            // streaming is not supported by sandbox
            if (_useSandbox)
            {
                throw new NotSupportedException(
                          "TradierBrokerage.DataQueueHandler.Subscribe(): The sandbox does not support data streaming.");
            }

            // initialize data queue handler on-demand
            if (!_isDataQueueHandlerInitialized)
            {
                _isDataQueueHandlerInitialized = true;

                _streamSession = CreateStreamSession();

                using (var resetEvent = new ManualResetEvent(false))
                {
                    EventHandler triggerEvent = (o, args) => resetEvent.Set();
                    _webSocketClient.Open += triggerEvent;

                    _webSocketClient.Connect();

                    if (!resetEvent.WaitOne(ConnectionTimeout))
                    {
                        throw new TimeoutException("Websockets connection timeout.");
                    }

                    _webSocketClient.Open -= triggerEvent;
                }
            }

            if (!CanSubscribe(dataConfig.Symbol))
            {
                return(Enumerable.Empty <BaseData>().GetEnumerator());
            }

            var enumerator = _aggregator.Add(dataConfig, newDataAvailableHandler);

            _subscriptionManager.Subscribe(dataConfig);

            return(enumerator);
        }
Beispiel #3
0
 /// <summary>
 /// Opens connection to Alpaca streaming API.
 /// </summary>
 /// <returns>Waitable task object for handling action completion in asyncronious mode.</returns>
 public Task ConnectAsync()
 {
     return(Task.Run(() => _webSocket.Connect()));
 }