Exemplo n.º 1
0
        /// <summary>
        /// Function: Receive execution levels for a given portfolio. This function is usually used for historical simulations.
        /// </summary>
        /// <param name="executionDate">reference date for the executed orders</param>
        /// <param name="portfolio">reference portfolio</param>
        public static void ReceiveExecutionLevels(DateTime executionDate, Portfolio portfolio)
        {
            Dictionary <int, Dictionary <string, Order> > orders = portfolio.OpenOrders(executionDate, true);

            if (orders != null)
            {
                foreach (Dictionary <string, Order> os in orders.Values)
                {
                    foreach (Order order in os.Values)
                    {
                        if (order.Status == OrderStatus.Submitted)
                        {
                            Instrument  instrument = order.Instrument;
                            BusinessDay date       = Calendar.FindCalendar("WE").GetBusinessDay(executionDate);

                            if (date != null)
                            {
                                double executionLevel = instrument[executionDate, TimeSeriesType.Last, instrument.InstrumentType == InstrumentType.Strategy ? TimeSeriesRollType.Last : TimeSeriesRollType.Last];

                                if (instrument.InstrumentType == InstrumentType.Future)
                                {
                                    executionLevel *= (instrument as Future).PointSize;
                                }


                                if (!double.IsNaN(executionLevel))
                                {
                                    if (order.Unit != 0.0)
                                    {
                                        if (true)
                                        {
                                            if (instrument.InstrumentType == InstrumentType.Future)
                                            {
                                                double n_contracts = order.Unit;

                                                double exec_fee = 0.0;

                                                Dictionary <int, Dictionary <int, Instruction> > instructions = Factory.Instructions();

                                                if (instructions.ContainsKey(portfolio.ID))
                                                {
                                                    if (instructions[portfolio.ID].ContainsKey(instrument.ID))
                                                    {
                                                        exec_fee = instructions[portfolio.ID][instrument.ID].ExecutionFee;
                                                    }

                                                    else if (instructions[portfolio.ID].ContainsKey((instrument as Future).UnderlyingID))
                                                    {
                                                        exec_fee = instructions[portfolio.ID][(instrument as Future).UnderlyingID].ExecutionFee;
                                                    }

                                                    else if (instructions[portfolio.ID].ContainsKey(0))
                                                    {
                                                        exec_fee = instructions[portfolio.ID][0].ExecutionFee;
                                                    }
                                                }

                                                if (instructions.ContainsKey(0))
                                                {
                                                    if (instructions[0].ContainsKey(instrument.ID))
                                                    {
                                                        exec_fee = instructions[0][instrument.ID].ExecutionFee;
                                                    }
                                                    else if (instructions[0].ContainsKey(0))
                                                    {
                                                        exec_fee = instructions[0][0].ExecutionFee;
                                                    }
                                                }

                                                if (order.Unit > 0)
                                                {
                                                    executionLevel += exec_fee;
                                                }
                                                else if (order.Unit < 0)
                                                {
                                                    executionLevel -= exec_fee;
                                                }

                                                Instrument underlying = (instrument as Future).Underlying;
                                                double     value      = underlying.ExecutionCost;
                                                if (value < 0)
                                                {
                                                    value = -value;
                                                    if (order.Unit > 0)
                                                    {
                                                        executionLevel += value * executionLevel;
                                                    }
                                                    else
                                                    {
                                                        executionLevel -= value * executionLevel;
                                                    }
                                                }
                                                else
                                                {
                                                    value *= (instrument as Future).PointSize;
                                                    if (order.Unit > 0)
                                                    {
                                                        executionLevel += value;
                                                    }
                                                    else
                                                    {
                                                        executionLevel -= value;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    portfolio.UpdateOrderTree(order, OrderStatus.Executed, double.NaN, executionLevel, executionDate);
                                }
                                else
                                {
                                    portfolio.UpdateOrderTree(order, OrderStatus.NotExecuted, double.NaN, executionLevel, executionDate);
                                }
                            }
                        }
                    }
                }
            }
        }