Esempio n. 1
0
 static void client_ExecDetails(object sender, ExecDetailsEventArgs e)
 {
     Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",
         e.Contract.Symbol, e.Execution.AcctNumber, e.Execution.ClientId, e.Execution.Exchange, e.Execution.ExecId,
         e.Execution.Liquidation, e.Execution.OrderId, e.Execution.PermId, e.Execution.Price, e.Execution.Shares, e.Execution.Side, e.Execution.Time);
 }
Esempio n. 2
0
 static void ClientExecDetails(object sender, ExecDetailsEventArgs e)
 {
     execCounter++;
     Logger.WriteToLog(DateTime.Now, string.Format("ClientManager.client_ExecDetails(): Execution details for market {0} : Time: {1,-4}, Symbol: {2,-4}, Side: {3,-4}, Quantity: {4,-4} ", Mode, e.Execution.Time, e.Contract.Symbol, e.Execution.Side, e.Execution.CumQuantity), " ExecDetails");
 }
        public void fillReceived(Object sender, ExecDetailsEventArgs e)
        {
            if(openOrderContracts.ContainsKey(e.OrderId))
            {
                FlexTrade.Fill fill = new FlexTrade.Fill();
               
                //Get the open orders based on the order ID
                Krs.Ats.IBNet.Order krsOrder = openOrders[e.OrderId];
                FlexTrade.Order ftOrder = ordersOrgFormat[e.OrderId];

                ftOrder.status = Order.OrderStatus.FILL;
                ftOrder.fillPrices.Add(e.Execution.Price);
                ftOrder.fillQuantities.Add(e.Execution.Shares);

                //if completely filled, remove the orders from the list of open orders
                if (ftOrder.fillQuantities.Sum() == ftOrder.orderQuantity)
                {
                    ordersOrgFormat[e.OrderId].status = Order.OrderStatus.FILL;
                    openOrders.Remove(e.OrderId);
                    ordersOrgFormat.Remove(e.OrderId);
                    openOrderContracts.Remove(e.OrderId);
                }
                else
                    ordersOrgFormat[e.OrderId].status = Order.OrderStatus.PARTIAL_FILL;

                //Set the execution values in the order
                fill.originalOrder = ftOrder;
                fill.price = e.Execution.Price;
                fill.qty = e.Execution.Shares;
                fill.time = DateTime.UtcNow;

                if (FillUpdate != null && fill != null)
                    FillUpdate(fill);
            }
        }
Esempio n. 4
0
        void client_ExecDetails(object sender, ExecDetailsEventArgs e)
        {
            lock (_lockObject)
            {
                Symbol symbol = TWSAssetArgs.SymbolFromContract(e.Contract);

                string msg = "IB ExecDetails: " + e.Execution.Time + " " + e.Execution.Side + " " + symbol + " Order ID: " + e.Execution.OrderId +
                    " Size: " + e.Execution.Shares + " Price: " + e.Execution.Price;
                Trace.WriteLine(msg);
                //Console.WriteLine(msg);

                bool bIgnore = false;

                if (!string.IsNullOrEmpty(accountCode) && e.Execution.AccountNumber != accountCode)
                {
                    Trace.WriteLine("### Execution ignored - Wrong Account");
                    bIgnore = true;
                }
                else if (e.Execution.Shares < 0)
                {
                    Trace.WriteLine("### Execution Ignored - Negative Fill");
                    bIgnore = true;
                }

                if (!bIgnore)
                {

                    string datePattern = "yyyyMMdd hh:mm:ss";
                    DateTimeFormatInfo dateFormat = new DateTimeFormatInfo();
                    dateFormat.SetAllDateTimePatterns(new[] { "yyyyMMdd" }, 'd');
                    dateFormat.SetAllDateTimePatterns(new[] { "HH:mm:ss" }, 't');

                    string[] dateSplit = e.Execution.Time.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    DateTime execDateTime = DateTime.ParseExact(dateSplit[0], "d", dateFormat).Date;
                    TimeSpan execTime = DateTime.ParseExact(dateSplit[1], "t", dateFormat).TimeOfDay;
                    execDateTime += execTime;

                    BrokerOrder openOrder;

                    if (openOrders.TryGetValue(e.OrderId.ToString(), out openOrder))
                    {
                        //	If we are reconnecting after a disconnect, we may get execDetails for partial fills that
                        //	we already know about.  So loop through the fills through this order and see if
                        //	we already know about this execution.
                        bool alreadyReported = false;
                        foreach (Fill fill in openOrder.Fills)
                        {
                            string fillTimeString = fill.FillDateTime.ToString(datePattern);
                            if (fillTimeString == e.Execution.Time && fill.Price.SymbolPrice == e.Execution.Price && fill.Quantity == e.Execution.Shares)
                            {
                                alreadyReported = true;
                            }
                        }

                        if (!alreadyReported)
                        {
                            string information = "";

                            Fill fill = new Fill();
                            fill.FillDateTime = execDateTime;
                            fill.Price = new Price(e.Execution.Price, e.Execution.Price);
                            fill.Quantity = e.Execution.Shares;

                            //	Apparently IB doesn't send commissions
                            fill.Commission = 0;

                            openOrder.Fills.Add(fill);

                            int totalFilled = 0;
                            foreach (Fill f in openOrder.Fills)
                            {
                                totalFilled += f.Quantity;
                            }
                            if (totalFilled < openOrder.Shares)
                            {
                                openOrder.OrderState = BrokerOrderState.PartiallyFilled;
                                information = "Partial fill";
                            }
                            else
                            {
                                openOrder.OrderState = BrokerOrderState.Filled;
                                openOrders.Remove(openOrder.OrderId);

                                //	Only remove from the potentially cancelled orders if it was completely filled
                                //	It may have been partially filled while disconnected and then cancelled
                                if (_gettingReconnectData)
                                {
                                    if (_potentiallyCancelledOrders.ContainsKey(openOrder.OrderId))
                                    {
                                        _potentiallyCancelledOrders.Remove(openOrder.OrderId);
                                    }
                                }
                            }

                            _filledOrders.RecordFill(openOrder.OrderId, fill.FillDateTime);

                            //var callback = OutOfBandCallback;
                            //if (callback != null)
                            //{
                            //    callback(this, EventArgs.Empty);
                            //}

                            OrderUpdatedDelegate tmp = OrderUpdated;
                            if (tmp != null)
                            {
                                tmp(openOrder, fill, information);
                            }
                        }
                    }
                }
            }
        }