Exemplo n.º 1
0
        /// <summary>
        /// Generates stats based on the difference of the order executions from the reference price and time
        /// </summary>
        /// <param name="benchmarkPrice"></param>
        /// <param name="referenceTime">Null to use QDMS-provided time, the reference time otherwise.</param>
        public void GenerateExecutionStats(ExecutionBenchmark benchmarkPrice, TimeSpan? referenceTime)
        {
            if(Orders == null || Orders.Count == 0)
            {
                throw new Exception("No orders selected.");
            }

            _referenceTime = referenceTime;
            Stats = new List<ExecutionStats>();

            if (referenceTime == null && benchmarkPrice != ExecutionBenchmark.Reference)
            {
                _instrumentSessions = GetSessionTimes(Orders.Select(x => x.Instrument).Distinct());
            }

            //if it's at the open we have to grab external data
            if(benchmarkPrice == ExecutionBenchmark.Open)
            {
                //make sure we're connected to external data source
                if(_datasourcer.ExternalDataSource == null || !_datasourcer.ExternalDataSource.Connected)
                {
                    throw new Exception("Must be connected to external data source.");
                }

                //grab the data
                RequestRequiredData();
            }

            //generate the stats
            Benchmark(Orders, benchmarkPrice);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates stats based on the difference of the order executions from the reference price and time
        /// </summary>
        /// <param name="benchmarkPrice"></param>
        /// <param name="referenceTime">Null to use QDMS-provided time, the reference time otherwise.</param>
        public void GenerateExecutionStats(ExecutionBenchmark benchmarkPrice, TimeSpan?referenceTime)
        {
            if (Orders == null || Orders.Count == 0)
            {
                throw new Exception("No orders selected.");
            }

            _referenceTime = referenceTime;
            Stats          = new List <ExecutionStats>();

            if (referenceTime == null && benchmarkPrice != ExecutionBenchmark.Reference)
            {
                _instrumentSessions = GetSessionTimes(Orders.Select(x => x.Instrument).Distinct());
            }

            //if it's at the open we have to grab external data
            if (benchmarkPrice == ExecutionBenchmark.Open)
            {
                //make sure we're connected to external data source
                if (_datasourcer.ExternalDataSource == null || !_datasourcer.ExternalDataSource.Connected)
                {
                    throw new Exception("Must be connected to external data source.");
                }

                //grab the data
                RequestRequiredData();
            }

            //generate the stats
            Benchmark(Orders, benchmarkPrice);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the reference price for a particular order, given an execution benchmark.
        /// </summary>
        private decimal?GetReferencePrice(Order order, ExecutionBenchmark benchmark)
        {
            if (benchmark == ExecutionBenchmark.Close)
            {
                return(order.ClosePrice);
            }
            else if (benchmark == ExecutionBenchmark.Open)
            {
                //For the open we need to look at the external data
                if (!_data.ContainsKey(order.InstrumentID))
                {
                    return(null);
                }
                var bar = _data[order.InstrumentID].FirstOrDefault(x => x.DT.Date == order.TradeDate.Date);
                if (bar == null)
                {
                    return(null);
                }
                return(bar.Open);
            }
            else if (benchmark == ExecutionBenchmark.VWAP)
            {
                throw new NotImplementedException();
            }
            else if (benchmark == ExecutionBenchmark.Reference)
            {
                return(order.ReferencePrice);
            }

            return(0);
        }
Exemplo n.º 4
0
        private DateTime?GetReferenceTime(Order order, ExecutionBenchmark benchmark)
        {
            //If the user sets a fixed reference time, we use that one in all cases
            if (_referenceTime != null)
            {
                return(order.TradeDate.Date + _referenceTime.Value);
            }

            //Otherwise use QDMS or the reference time set at the order
            if (benchmark == ExecutionBenchmark.Close)
            {
                var session = _instrumentSessions[order.InstrumentID]
                              .FirstOrDefault(x => x.IsSessionEnd && (int)x.ClosingDay == order.TradeDate.DayOfWeek.ToInt());
                if (session == null)
                {
                    return(null);
                }

                return(order.TradeDate.Date + session.ClosingTime);
            }
            else if (benchmark == ExecutionBenchmark.Open)
            {
                var session = _instrumentSessions[order.InstrumentID]
                              .Where(x => (int)x.ClosingDay == order.TradeDate.DayOfWeek.ToInt())
                              .OrderBy(x => x.OpeningTime)
                              .FirstOrDefault();
                if (session == null)
                {
                    return(null);
                }

                return(order.TradeDate.Date + session.OpeningTime);
            }
            else if (benchmark == ExecutionBenchmark.VWAP)
            {
                throw new NotImplementedException();
            }
            else if (benchmark == ExecutionBenchmark.Reference)
            {
                return(order.ReferenceTime);
            }

            return(new DateTime(1, 1, 1));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loop through the orders and benchmark their executions.
        /// </summary>
        private void Benchmark(IEnumerable <Order> orders, ExecutionBenchmark benchmark)
        {
            foreach (Order order in orders)
            {
                decimal?referencePrice = GetReferencePrice(order, benchmark);
                if (!referencePrice.HasValue)
                {
                    continue;
                }

                DateTime?referenceTime = GetReferenceTime(order, benchmark);
                if (!referenceTime.HasValue)
                {
                    continue;
                }

                GenerateStats(order, referenceTime.Value, referencePrice.Value);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Loop through the orders and benchmark their executions.
        /// </summary>
        private void Benchmark(IEnumerable<Order> orders, ExecutionBenchmark benchmark)
        {
            foreach (Order order in orders)
            {
                decimal? referencePrice = GetReferencePrice(order, benchmark);
                if (!referencePrice.HasValue) continue;

                DateTime? referenceTime = GetReferenceTime(order, benchmark);
                if (!referenceTime.HasValue) continue;

                GenerateStats(order, referenceTime.Value, referencePrice.Value);
            }
        }
Exemplo n.º 7
0
        private DateTime? GetReferenceTime(Order order, ExecutionBenchmark benchmark)
        {
            //If the user sets a fixed reference time, we use that one in all cases
            if(_referenceTime != null)
            {
                return order.TradeDate.Date + _referenceTime.Value;
            }

            //Otherwise use QDMS or the reference time set at the order
            if (benchmark == ExecutionBenchmark.Close)
            {
                var session = _instrumentSessions[order.InstrumentID]
                    .FirstOrDefault(x => x.IsSessionEnd && (int)x.ClosingDay == order.TradeDate.DayOfWeek.ToInt());
                if (session == null) return null;

                return order.TradeDate.Date + session.ClosingTime;
            }
            else if (benchmark == ExecutionBenchmark.Open)
            {
                var session = _instrumentSessions[order.InstrumentID]
                    .Where(x => (int)x.ClosingDay == order.TradeDate.DayOfWeek.ToInt())
                    .OrderBy(x => x.OpeningTime)
                    .FirstOrDefault();
                if (session == null) return null;

                return order.TradeDate.Date + session.OpeningTime;
            }
            else if (benchmark == ExecutionBenchmark.VWAP)
            {
                throw new NotImplementedException();
            }
            else if (benchmark == ExecutionBenchmark.Reference)
            {
                return order.ReferenceTime;
            }

            return new DateTime(1, 1, 1);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the reference price for a particular order, given an execution benchmark.
        /// </summary>
        private decimal? GetReferencePrice(Order order, ExecutionBenchmark benchmark)
        {
            if (benchmark == ExecutionBenchmark.Close)
            {
                return order.ClosePrice;
            }
            else if (benchmark == ExecutionBenchmark.Open)
            {
                //For the open we need to look at the external data
                if (!_data.ContainsKey(order.InstrumentID)) return null;
                var bar = _data[order.InstrumentID].FirstOrDefault(x => x.DT.Date == order.TradeDate.Date);
                if (bar == null) return null;
                return bar.Open;
            }
            else if (benchmark == ExecutionBenchmark.VWAP)
            {
                throw new NotImplementedException();
            }
            else if (benchmark == ExecutionBenchmark.Reference)
            {
                return order.ReferencePrice;
            }

            return 0;
        }