/// <summary>
        /// Get time converted to MBT format.
        /// </summary>
        /// <returns></returns>
        int?GetConvertedTime(DataHistoryOperation operation)
        {
            // Rules:
            //Day bars:
            //-3 = Calendar year
            //-2 = Calendar month
            //-1 = Calendar week
            //0 or 1 = One day
            //2 or more = days in bar with current day as last day

            //Min bars:
            //0 or 60 = One minute
            //Otherwise, the number of seconds desired, E.g.:
            //20 = 20 second bars
            //90 = one and-a-half minute bars
            //600 = ten minute bars

            if (operation.Request.IsDayBased)
            {
                if (operation.Request.Period == TimeSpan.FromDays(365))
                {// Year.
                    return(-3);
                }

                if (operation.Request.Period == TimeSpan.FromDays(31))
                {// Month.
                    return(-2);
                }

                if (operation.Request.Period == TimeSpan.FromDays(7) || operation.Request.Period == TimeSpan.FromDays(5))
                {// Week.
                    return(-1);
                }

                if (operation.Request.Period >= TimeSpan.FromDays(1) && operation.Request.Period <= TimeSpan.FromDays(4))
                {// Day (1,2,3 or 4)
                    return(1);
                }
            }

            if (operation.Request.IsMinuteBased)
            {
                if (operation.Request.Period >= TimeSpan.FromMinutes(1) && operation.Request.Period < TimeSpan.FromHours(24))
                {
                    return((int)operation.Request.Period.TotalSeconds);
                }
            }

            SystemMonitor.InvalidCall("Do not call time for tick based.");
            return(null);
        }
        /// <summary>
        ///
        /// </summary>
        void _historyClient_OnDataEvent(int lRequestId, object pHist, enumHistEventType evt)
        {
            List <DataBar> extractedBars = new List <DataBar>();

            if (pHist is MbtHistDayBar)
            {
                MbtHistDayBar bars = (MbtHistDayBar)pHist;
                bars.First();

                while (bars.Eof == false)
                {
                    decimal open  = (decimal)bars.Open;
                    decimal close = (decimal)bars.Close;
                    decimal high  = (decimal)bars.High;
                    decimal low   = (decimal)bars.Low;

                    extractedBars.Insert(0, new DataBar(bars.CloseDate, open, high, low, close, bars.TotalVolume));
                    bars.Next();
                }
            }
            else if (pHist is MbtHistMinBar)
            {
                MbtHistMinBar bars = (MbtHistMinBar)pHist;
                bars.First();

                while (bars.Eof == false)
                {
                    decimal open  = (decimal)bars.Open;
                    decimal close = (decimal)bars.Close;
                    decimal high  = (decimal)bars.High;
                    decimal low   = (decimal)bars.Low;

                    extractedBars.Insert(0, new DataBar(bars.UTCDateTime, open, high, low, close, bars.TotalVolume));
                    bars.Next();
                }
            }

            DataHistoryOperation operation = (DataHistoryOperation)_operationStub.GetOperationById(lRequestId.ToString());

            if (operation != null)
            {
                _operationStub.CompleteOperation(lRequestId.ToString(), new DataHistoryUpdate(operation.Request.Period, extractedBars));
            }
        }
        public bool StartOperation(OperationInformation operationInformation)
        {
            DataHistoryOperation operation = (DataHistoryOperation)operationInformation;

            bool result = true;

            _messageLoopOperator.Invoke(delegate()
            {// Placing the request on the stolen main application thread, since we need the requestMessage pump for this to work properly.
                MbtHistMgr historyClient = _historyClient;
                if (historyClient == null)
                {
                    result = false;
                    return;
                }

                if (operation.Request.IsMinuteBased == false && operation.Request.IsDayBased == false)
                {
                    result = false;
                    return;
                }

                int?time = GetConvertedTime(operation);

                if (operation.Request.IsDayBased)
                {
                    MbtHistDayBar histBar;
                    histBar = historyClient.CreateHistDayBar();
                    histBar.Clear();
                    histBar.SendRequest(operation.Symbol, Int32.Parse(operation.Id), time.Value,
                                        new DateTime(0), new DateTime(0), operation.Request.MaxValuesRetrieved.HasValue ? operation.Request.MaxValuesRetrieved.Value : int.MaxValue, true);
                }
                else if (operation.Request.IsMinuteBased)
                {
                    MbtHistMinBar histBar = historyClient.CreateHistMinBar();
                    histBar.Clear();
                    histBar.SendRequest(operation.Symbol, Int32.Parse(operation.Id), time.Value,
                                        new DateTime(0), new DateTime(0), operation.Request.MaxValuesRetrieved.HasValue ? operation.Request.MaxValuesRetrieved.Value : int.MaxValue, true);
                }
            });

            return(result);
        }
        public DataHistoryUpdate GetDataHistoryUpdate(DataSessionInfo session, DataHistoryRequest request)
        {
            MBTradingConnectionManager manager = _manager;

            if (manager != null)
            {
                DataHistoryOperation operation = new DataHistoryOperation(session.Symbol.Name, request);
                if (manager.History.Place(operation) == false)
                {
                    SystemMonitor.OperationError("Failed to place data history operation.");
                    return(null);
                }

                if (operation.CompletionEvent.WaitOne(TimeSpan.FromSeconds(120)) == false)
                {
                    SystemMonitor.OperationError("Data history operation timed out.");
                    return(null);
                }

                return(operation.Responce);
            }

            return(null);
        }
 /// <summary>
 /// Submit a dataDelivery retrieval request.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public bool Place(DataHistoryOperation operation)
 {
     return(_operationStub.PlaceOperation(operation, true));
 }
 /// <summary>
 /// Submit a dataDelivery retrieval request.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public bool Place(DataHistoryOperation operation)
 {
     return _operationStub.PlaceOperation(operation, true);
 }
        /// <summary>
        /// Get time converted to MBT format.
        /// </summary>
        /// <returns></returns>
        int? GetConvertedTime(DataHistoryOperation operation)
        {
            // Rules:
            //Day bars:
            //-3 = Calendar year
            //-2 = Calendar month
            //-1 = Calendar week
            //0 or 1 = One day
            //2 or more = days in bar with current day as last day

            //Min bars:
            //0 or 60 = One minute
            //Otherwise, the number of seconds desired, E.g.:
            //20 = 20 second bars
            //90 = one and-a-half minute bars
            //600 = ten minute bars

            if (operation.Request.IsDayBased)
            {
                if (operation.Request.Period == TimeSpan.FromDays(365))
                {// Year.
                    return -3;
                }

                if (operation.Request.Period == TimeSpan.FromDays(31))
                {// Month.
                    return -2;
                }

                if (operation.Request.Period == TimeSpan.FromDays(7) || operation.Request.Period == TimeSpan.FromDays(5))
                {// Week.
                    return -1;
                }

                if (operation.Request.Period >= TimeSpan.FromDays(1) && operation.Request.Period <= TimeSpan.FromDays(4))
                {// Day (1,2,3 or 4)
                    return 1;
                }
            }

            if (operation.Request.IsMinuteBased)
            {
                if (operation.Request.Period >= TimeSpan.FromMinutes(1) && operation.Request.Period < TimeSpan.FromHours(24))
                {
                    return (int)operation.Request.Period.TotalSeconds;
                }
            }

            SystemMonitor.InvalidCall("Do not call time for tick based.");
            return null;
        }
        public DataHistoryUpdate GetDataHistoryUpdate(DataSessionInfo session, DataHistoryRequest request)
        {
            MBTradingConnectionManager manager = _manager;
            if (manager != null)
            {
                DataHistoryOperation operation = new DataHistoryOperation(session.Symbol.Name, request);
                if (manager.History.Place(operation) == false)
                {
                    SystemMonitor.OperationError("Failed to place data history operation.");
                    return null;
                }

                if (operation.CompletionEvent.WaitOne(TimeSpan.FromSeconds(120)) == false)
                {
                    SystemMonitor.OperationError("Data history operation timed out.");
                    return null;
                }

                return operation.Response;
            }

            return null;
        }