Exemplo n.º 1
0
        public void RequestsAreGivenAUniqueAssignedIDGreaterThanZero()
        {
            var request = new HistoricalDataRequest(_instrument, BarSize.OneDay, new DateTime(2012, 1, 1), new DateTime(2013, 1, 1),
                                                    dataLocation: DataLocation.ExternalOnly,
                                                    saveToLocalStorage: false,
                                                    rthOnly: true);
            var req2 = (HistoricalDataRequest)request.Clone();
            var req3 = (HistoricalDataRequest)request.Clone();

            var assignedIDs = new List <int>();

            _dataSourceMock
            .Setup(x => x.RequestHistoricalData(It.IsAny <HistoricalDataRequest>()))
            .Callback <HistoricalDataRequest>(req => assignedIDs.Add(req.AssignedID));


            _broker.RequestHistoricalData(request);
            _broker.RequestHistoricalData(req2);
            _broker.RequestHistoricalData(req3);

            Thread.Sleep(50);

            Assert.AreEqual(3, assignedIDs.Count);
            Assert.AreEqual(3, assignedIDs.Distinct().Count());
            Assert.AreEqual(0, assignedIDs.Count(x => x < 0));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Splits a historical data request into multiple pieces so that they obey the request limits
        /// </summary>
        private List <HistoricalDataRequest> SplitRequest(HistoricalDataRequest request)
        {
            var requests = new List <HistoricalDataRequest>();

            //start at the end, and work backward in increments
            int      step         = 495; //max bars returned per request
            var      freqInterval = request.Frequency.ToTimeSpan().TotalSeconds *step;
            DateTime currentDate  = request.EndingDate;

            while (currentDate > request.StartingDate)
            {
                var newReq = (HistoricalDataRequest)request.Clone();
                newReq.EndingDate   = currentDate;
                newReq.StartingDate = newReq.EndingDate.AddSeconds(-freqInterval);
                if (newReq.StartingDate < request.StartingDate)
                {
                    newReq.StartingDate = request.StartingDate;
                }

                currentDate = newReq.StartingDate;
                requests.Add(newReq);
            }

            requests.Reverse();
            return(requests);
        }
Exemplo n.º 3
0
        /// <summary>
        /// When a data request can be partly filled by the local db,
        /// we need to split it into one or two sub-requests for the parts that
        /// are not locally available. This method does that and forwards the sub-requests.
        /// </summary>
        private void GenerateSubRequests(HistoricalDataRequest request, StoredDataInfo localDataInfo)
        {
            _subRequests.TryAdd(request.AssignedID, new List <HistoricalDataRequest>());

            //earlier data that may be needed
            HistoricalDataRequest newBackRequest = null;

            if (localDataInfo.EarliestDate > request.StartingDate)
            {
                newBackRequest                 = (HistoricalDataRequest)request.Clone();
                newBackRequest.EndingDate      = localDataInfo.EarliestDate.AddMilliseconds(-request.Frequency.ToTimeSpan().TotalMilliseconds / 2);
                newBackRequest.IsSubrequestFor = request.AssignedID;
                newBackRequest.AssignedID      = GetUniqueRequestID();
                _subRequests[newBackRequest.IsSubrequestFor.Value].Add(newBackRequest);
            }

            //later data that may be needed
            HistoricalDataRequest newForwardRequest = null;

            if (localDataInfo.LatestDate < request.EndingDate)
            {
                //the local storage is insufficient, so we save the original request, make a copy,
                //modify it, and pass it to the external data source
                newForwardRequest = (HistoricalDataRequest)request.Clone();
                newForwardRequest.StartingDate    = localDataInfo.LatestDate.AddMilliseconds(request.Frequency.ToTimeSpan().TotalMilliseconds / 2);
                newForwardRequest.IsSubrequestFor = request.AssignedID;
                newForwardRequest.AssignedID      = GetUniqueRequestID();
                _subRequests[newForwardRequest.IsSubrequestFor.Value].Add(newForwardRequest);
            }

            //we send these together, because too large of a delay between the two requests can cause problems
            if (newBackRequest != null)
            {
                ForwardHistoricalRequest(newBackRequest);
            }
            if (newForwardRequest != null)
            {
                ForwardHistoricalRequest(newForwardRequest);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Splits a historical data request into multiple pieces so that they obey the request limits
        /// </summary>
        private List <HistoricalDataRequest> SplitRequest(HistoricalDataRequest request)
        {
            var requests = new List <HistoricalDataRequest>();

            //start at the end, and work backward in increments slightly lower than the max allowed time
            int      step        = (int)(TWSUtils.MaxRequestLength(request.Frequency) * .95);
            DateTime currentDate = request.EndingDate;

            while (currentDate > request.StartingDate)
            {
                var newReq = (HistoricalDataRequest)request.Clone();
                newReq.EndingDate   = currentDate;
                newReq.StartingDate = newReq.EndingDate.AddSeconds(-step);
                if (newReq.StartingDate < request.StartingDate)
                {
                    newReq.StartingDate = request.StartingDate;
                }

                currentDate = currentDate.AddSeconds(-step);
                requests.Add(newReq);
            }

            return(requests);
        }