示例#1
0
        /// <summary>
        /// Function: Update and a fill value and time for a specific order. This is usually called by the market connection when the brokers reverts with a fill confirmation.
        /// </summary>
        public static void RecordFillValue(Order order, DateTime fillTime, double fillValue)
        {
            order.Portfolio.MasterPortfolio.UpdateOrderTree(order, OrderStatus.Executed, double.NaN, fillValue, fillTime);
            OrderRecord record = Market.GetOrderRecord(order.ID);

            record.Price  = (decimal)order.ExecutionLevel;
            record.Status = "Executed";
            UpdateRecord(record);
            Market.RemoveOrderRecord(order.ID);
        }
示例#2
0
 /// <summary>
 /// Function: update all the update events added to the market with this record
 /// </summary>
 public static void UpdateRecord(OrderRecord record)
 {
     try
     {
         if (_updateDB != null)
         {
             _updateDB(record);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
示例#3
0
        /// <summary>
        /// Function: Execute simulated orders during live paper trading.
        /// </summary>
        public static void ExecuteSimulatedOrders()
        {
            foreach (Portfolio portfolio in portfolios.ToList())
            {
                if (!(portfolio.MasterPortfolio != null && portfolio.MasterPortfolio == portfolio))
                {
                    RemovePortfolio(portfolio);
                }

                if (portfolio.Strategy != null && !portfolio.Strategy.Simulating)
                {
                    try
                    {
                        if (portfolio != null)
                        {
                            bool     book = false;
                            DateTime t    = DateTime.Now;
                            Dictionary <int, Dictionary <string, Order> > orders = portfolio.OpenOrders(t, true);
                            if (orders != null)
                            {
                                foreach (Dictionary <string, Order> os in orders.Values.ToList())
                                {
                                    foreach (Order order in os.Values.ToList())
                                    {
                                        if (order.Status == OrderStatus.Submitted && order.Client == "Simulator")
                                        {
                                            if (order.OrderDate.Date == DateTime.Today)
                                            {
                                                try
                                                {
                                                    double last    = order.Instrument[t, TimeSeriesType.Last, TimeSeriesRollType.Last];
                                                    double last_5h = order.Instrument[t.AddHours(-5), TimeSeriesType.Last, TimeSeriesRollType.Last];

                                                    //if (last != last_5h)
                                                    {
                                                        double bid = order.Instrument[t, TimeSeriesType.Bid, TimeSeriesRollType.Last];
                                                        double ask = order.Instrument[t, TimeSeriesType.Ask, TimeSeriesRollType.Last];

                                                        if (double.IsNaN(bid))
                                                        {
                                                            bid = last;
                                                        }

                                                        if (double.IsNaN(ask))
                                                        {
                                                            ask = last;
                                                        }

                                                        if (order.Type == OrderType.Market || (order.Limit >= ask && order.Unit > 0) || (order.Limit <= bid && order.Unit < 0))
                                                        {
                                                            OrderRecord ord = Market.GetOrderRecord(order.ID);
                                                            if (ord != null)
                                                            {
                                                                ord.Price = (decimal)(order.Unit < 0 ? bid : ask) * (order.Instrument.InstrumentType == InstrumentType.Future ? (decimal)(order.Instrument as Future).PointSize : 1);
                                                                portfolio.UpdateOrderTree(order, OrderStatus.Executed, double.NaN, (double)ord.Price, t);
                                                                ord.Status = "Executed";

                                                                book = true;
                                                                Market.RemoveOrderRecord(order.ID);

                                                                UpdateRecord(ord);
                                                            }
                                                            else
                                                            {
                                                                book = true;
                                                                portfolio.UpdateOrderTree(order, OrderStatus.Executed, double.NaN, (order.Unit < 0 ? bid : ask), t);
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Console.WriteLine(e);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            int num = portfolio.MasterPortfolio.Strategy.Tree.BookOrders(t);

                            if (book || num != 0)
                            {
                                UpdateRecord(null);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Function: Submit a specific order
        /// </summary>
        /// <param name="order">reference order</param>
        public static object Submit(Order order)
        {
            if (order.Unit != 0 && order.Status == OrderStatus.New)
            {
                Instrument instrument = order.Instrument;

                Dictionary <int, Dictionary <int, Instruction> > list = Instructions();
                Instruction defaultInstruction = list.ContainsKey(0) && list[0].ContainsKey(0) ? list[0][0] : null;
                Instruction portfolioDefault   = list.ContainsKey(order.Portfolio.MasterPortfolio.ID) && list[order.Portfolio.MasterPortfolio.ID].ContainsKey(0) ? list[order.Portfolio.MasterPortfolio.ID][0] : null;

                Instruction instruction = GetInstruction(order);

                if (instruction != null && (order.Client == null || string.IsNullOrWhiteSpace(order.Client)))
                {
                    if (instruction.Client == "Inherit")
                    {
                        if (portfolioDefault == null || portfolioDefault.Client == "Inherit")
                        {
                            order.Client      = defaultInstruction.Client;
                            order.Destination = defaultInstruction.Destination;
                            order.Account     = defaultInstruction.Account;
                        }
                        else
                        {
                            order.Client      = portfolioDefault.Client;
                            order.Destination = portfolioDefault.Destination;
                            order.Account     = portfolioDefault.Account;
                        }
                    }
                    else
                    {
                        order.Client      = instruction.Client;
                        order.Destination = instruction.Destination;
                        order.Account     = instruction.Account;
                    }
                    order.Portfolio.MasterPortfolio.UpdateOrderTree(order, OrderStatus.Submitted, double.NaN, double.NaN, DateTime.MaxValue, order.Client, order.Destination, order.Account);
                }
                else
                {
                    order.Portfolio.MasterPortfolio.UpdateOrderTree(order, OrderStatus.Submitted, double.NaN, double.NaN, DateTime.MaxValue);
                }


                if (order.Portfolio.MasterPortfolio.Strategy.Simulating)
                {
                    return(null);
                }

                OrderRecord orderRecord = new OrderRecord()
                {
                    Date              = order.OrderDate.TimeOfDay.ToString(),
                    OrderID           = order.ID,
                    Name              = order.Instrument.Name,
                    Side              = order.Unit > 0 ? "Long" : "Short",
                    Type              = order.Type.ToString(),
                    Unit              = Math.Abs(order.Unit).ToString(),
                    Price             = (decimal)order.Limit,
                    Status            = "New",
                    RootPortfolioID   = order.Portfolio.MasterPortfolio.ID,
                    ParentPortfolioID = order.Portfolio.ID
                };

                DateTime t1 = DateTime.Now;

                Dictionary <string, Market.ClientConnection> clientConnections = _connectionDB;// Factory.GetClientConnetions();

                if (clientConnections.ContainsKey(order.Client))
                {
                    if (clientConnections[order.Client].SubmitFunction != null)
                    {
                        clientConnections[order.Client].SubmitFunction(order);
                    }
                }

                if (order.OrderDate.Date == DateTime.Today)
                {
                    if (orderDict.ContainsKey(orderRecord.OrderID))
                    {
                        orderDict[orderRecord.OrderID] = orderRecord;
                    }
                    else
                    {
                        orderDict.Add(orderRecord.OrderID, orderRecord);
                    }

                    UpdateRecord(orderRecord);
                }
                return(orderRecord);
            }
            return(null);
        }