Esempio n. 1
0
        /// <summary>
        ///     Called ona reply to a historical data request
        /// </summary>
        private void HandleHistoricalDataRequestReply()
        {
            using (var ms = new MemoryStream())
            {
                // 2nd message part: the HistoricalDataRequest object that was used to make the request
                bool hasMore;
                var  requestBuffer = _historicalDataSocket.ReceiveFrameBytes(out hasMore);
                if (!hasMore)
                {
                    return;
                }

                var request = MyUtils.ProtoBufDeserialize <HistoricalDataRequest>(requestBuffer, ms);
                // 3rd message part: the size of the uncompressed, serialized data. Necessary for decompression.
                var sizeBuffer = _historicalDataSocket.ReceiveFrameBytes(out hasMore);
                if (!hasMore)
                {
                    return;
                }

                var outputSize = BitConverter.ToInt32(sizeBuffer, 0);
                // 4th message part: the compressed serialized data.
                var dataBuffer   = _historicalDataSocket.ReceiveFrameBytes();
                var decompressed = LZ4Codec.Decode(dataBuffer, 0, dataBuffer.Length, outputSize);
                var data         = MyUtils.ProtoBufDeserialize <List <OHLCBar> >(decompressed, ms);
                // Remove from pending requests
                lock (_pendingHistoricalRequestsLock)
                {
                    PendingHistoricalRequests.RemoveAll(x => x.RequestID == request.RequestID);
                }

                RaiseEvent(HistoricalDataReceived, this, new HistoricalDataEventArgs(request, data));
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Request historical data. Data will be delivered through the HistoricalDataReceived event.
        /// </summary>
        /// <returns>An ID uniquely identifying this historical data request. -1 if there was an error.</returns>
        public int RequestHistoricalData(HistoricalDataRequest request)
        {
            if (request == null)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Historical Data Request Failed: Request cannot be null."));

                return(-1);
            }

            if (request.EndingDate < request.StartingDate)
            {
                RaiseEvent(Error, this,
                           new ErrorArgs(-1, "Historical Data Request Failed: Starting date must be after ending date."));

                return(-1);
            }

            if (request.Instrument == null)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Historical Data Request Failed: Instrument cannot be null."));

                return(-1);
            }

            if (!ClientRunningAndIsConnected)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not request historical data - not connected."));

                return(-1);
            }

            if (!request.RTHOnly && request.Frequency >= BarSize.OneDay &&
                request.DataLocation != DataLocation.ExternalOnly)
            {
                RaiseEvent(
                    Error,
                    this,
                    new ErrorArgs(
                        -1,
                        "Warning: Requesting low-frequency data outside RTH should be done with DataLocation = ExternalOnly, data from local storage will be incorrect."));
            }

            request.RequestID = Interlocked.Increment(ref requestCount);

            lock (pendingHistoricalRequestsLock)
            {
                PendingHistoricalRequests.Add(request);
            }

            historicalDataRequests.Enqueue(request);

            return(request.RequestID);
        }
Esempio n. 3
0
        /// <summary>
        ///     Called when we get some sort of error reply
        /// </summary>
        private void HandleErrorReply()
        {
            // The request ID
            byte[] buffer = historicalDataSocket.ReceiveFrameBytes(out bool hasMore);
            if (!hasMore)
            {
                return;
            }
            int requestId = BitConverter.ToInt32(buffer, 0);

            // Remove from pending requests
            lock (pendingHistoricalRequestsLock)
            {
                PendingHistoricalRequests.RemoveAll(x => x.RequestID == requestId);
            }
            // Finally the error message
            string message = historicalDataSocket.ReceiveFrameString();

            // Raise the error event
            RaiseEvent(Error, this, new ErrorArgs(-1, message, requestId));
        }
Esempio n. 4
0
        /// <summary>
        /// Called when we get some sort of error reply
        /// </summary>
        private void HandleErrorReply()
        {
            //the request ID
            bool hasMore;

            byte[] buffer = _dealerSocket.Receive(out hasMore);
            if (!hasMore)
            {
                return;
            }
            int requestID = BitConverter.ToInt32(buffer, 0);

            //remove from pending requests
            lock (_pendingHistoricalRequestsLock)
            {
                PendingHistoricalRequests.RemoveAll(x => x.RequestID == requestID);
            }

            //finally the error message
            string message = _dealerSocket.ReceiveString();

            //raise the error event
            RaiseEvent(Error, this, new ErrorArgs(-1, message, requestID));
        }
Esempio n. 5
0
        /// <summary>
        /// Request historical data. Data will be delivered through the HistoricalDataReceived event.
        /// </summary>
        /// <returns>An ID uniquely identifying this historical data request. -1 if there was an error.</returns>
        public int RequestHistoricalData(HistoricalDataRequest request)
        {
            //make sure the request is valid
            if (request.EndingDate < request.StartingDate)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Historical Data Request Failed: Starting date must be after ending date."));
                return(-1);
            }

            if (request.Instrument == null)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Historical Data Request Failed: null Instrument."));
                return(-1);
            }

            if (!Connected)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not request historical data - not connected."));
                return(-1);
            }

            if (!request.RTHOnly && request.Frequency >= BarSize.OneDay && request.DataLocation != DataLocation.ExternalOnly)
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Warning: Requesting low-frequency data outside RTH should be done with DataLocation = ExternalOnly, data from local storage will be incorrect."));
            }

            request.RequestID = _requestCount++;

            lock (_pendingHistoricalRequestsLock)
            {
                PendingHistoricalRequests.Add(request);
            }

            _historicalDataRequests.Enqueue(request);
            return(request.RequestID);
        }