Exemplo n.º 1
0
        /// <summary>
        /// Creates an IB contract from the order.
        /// </summary>
        /// <param name="order">The order to create a contract from</param>
        /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
        /// <returns>A new IB contract for the order</returns>
        private IB.Contract CreateContract(Order order, string exchange = null)
        {
            var securityType = ConvertSecurityType(order.SecurityType);
            var contract     = new IB.Contract(order.Symbol, exchange ?? "Smart", securityType, "USD");

            return(contract);
        }
Exemplo n.º 2
0
        private IB.ContractDetails GetContractDetails(IB.Contract contract)
        {
            IB.ContractDetails details = null;
            var requestID = GetNextRequestID();

            var manualResetEvent = new ManualResetEvent(false);

            // define our event handlers
            EventHandler <IB.ContractDetailsEventArgs> clientOnContractDetails = (sender, args) =>
            {
                // ignore other requests
                if (args.RequestId != requestID)
                {
                    return;
                }
                details = args.ContractDetails;
                _contractDetails.TryAdd(contract.Symbol, details);
                manualResetEvent.Set();
            };

            _client.ContractDetails += clientOnContractDetails;

            // make the request for data
            _client.RequestContractDetails(requestID, contract);

            // we'll wait a second, but it may not exist so just pass through
            manualResetEvent.WaitOne(1000);

            // be sure to remove our event handlers
            _client.ContractDetails -= clientOnContractDetails;

            return(details);
        }
Exemplo n.º 3
0
        private Order ConvertOrder(IB.Order ibOrder, IB.Contract contract)
        {
            decimal price = 0;

            if (ibOrder.LimitPrice != 0)
            {
                price = ibOrder.LimitPrice;
            }
            else if (ibOrder.AuxPrice != 0)
            {
                price = ibOrder.AuxPrice;
            }

            var order = new Order(contract.Symbol,
                                  ConvertSecurityType(contract.SecurityType),
                                  ibOrder.TotalQuantity,
                                  ConvertOrderType(ibOrder.OrderType),
                                  new DateTime(), // not sure how to get this data
                                  price
                                  );

            order.BrokerId.Add(ibOrder.OrderId);

            return(order);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Maps the IB Contract's symbol to a QC symbol
 /// </summary>
 private static string MapSymbol(IB.Contract contract)
 {
     if (contract.SecurityType == IB.SecurityType.Cash)
     {
         // reformat for QC
         return(contract.Symbol + contract.Currency);
     }
     return(contract.Symbol);
 }
Exemplo n.º 5
0
        private Order ConvertOrder(IB.Order ibOrder, IB.Contract contract)
        {
            // this function is called by GetOpenOrders which is mainly used by the setup handler to
            // initialize algorithm state.  So the only time we'll be executing this code is when the account
            // has orders sitting and waiting from before algo initialization...
            // because of this we can't get the time accurately

            Order order;
            var   mappedSymbol = MapSymbol(contract);
            var   orderType    = ConvertOrderType(ibOrder.OrderType);

            switch (orderType)
            {
            case OrderType.Market:
                order = new MarketOrder(mappedSymbol,
                                        ibOrder.TotalQuantity,
                                        new DateTime() // not sure how to get this data
                                        );
                break;

            case OrderType.Limit:
                order = new LimitOrder(mappedSymbol,
                                       ibOrder.TotalQuantity,
                                       ibOrder.LimitPrice,
                                       new DateTime()
                                       );
                break;

            case OrderType.StopMarket:
                order = new StopMarketOrder(mappedSymbol,
                                            ibOrder.TotalQuantity,
                                            ibOrder.AuxPrice,
                                            new DateTime()
                                            );
                break;

            case OrderType.StopLimit:
                order = new StopLimitOrder(mappedSymbol,
                                           ibOrder.TotalQuantity,
                                           ibOrder.AuxPrice,
                                           ibOrder.LimitPrice,
                                           new DateTime()
                                           );
                break;

            default:
                throw new InvalidEnumArgumentException("orderType", (int)orderType, typeof(OrderType));
            }

            order.SecurityType = ConvertSecurityType(contract.SecurityType);
            order.BrokerId.Add(ibOrder.OrderId);

            return(order);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates an IB contract from the order.
        /// </summary>
        /// <param name="order">The order to create a contract from</param>
        /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
        /// <returns>A new IB contract for the order</returns>
        private IB.Contract CreateContract(Order order, string exchange = null)
        {
            var securityType = ConvertSecurityType(order.SecurityType);
            var contract     = new IB.Contract(order.Symbol, exchange ?? "Smart", securityType, "USD");

            if (order.SecurityType == SecurityType.Forex)
            {
                // forex is special, so rewrite some of the properties to make it work
                contract.Exchange = "IDEALPRO";
                contract.Symbol   = order.Symbol.Substring(0, 3);
                contract.Currency = order.Symbol.Substring(3);
            }
            return(contract);
        }
Exemplo n.º 7
0
        private string GetPrimaryExchange(IB.Contract contract)
        {
            IB.ContractDetails details;
            if (_contractDetails.TryGetValue(contract.Symbol, out details))
            {
                return(details.Summary.PrimaryExchange);
            }

            details = GetContractDetails(contract);
            if (details == null)
            {
                // we were unable to find the contract details
                return(null);
            }

            return(details.Summary.PrimaryExchange);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates an IB contract from the order.
        /// </summary>
        /// <param name="symbol">The symbol whose contract we need to create</param>
        /// <param name="type">The security type of the symbol</param>
        /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
        /// <returns>A new IB contract for the order</returns>
        private IB.Contract CreateContract(string symbol, SecurityType type, string exchange = null)
        {
            var securityType = ConvertSecurityType(type);
            var contract     = new IB.Contract(symbol, exchange ?? "Smart", securityType, "USD");

            if (type == SecurityType.Forex)
            {
                // forex is special, so rewrite some of the properties to make it work
                contract.Exchange = "IDEALPRO";
                contract.Symbol   = symbol.Substring(0, 3);
                contract.Currency = symbol.Substring(3);
            }

            // some contracts require this, such as MSFT
            contract.PrimaryExchange = GetPrimaryExchange(contract);

            return(contract);
        }
Exemplo n.º 9
0
        private Order ConvertOrder(IB.Order ibOrder, IB.Contract contract)
        {
            decimal price = 0;

            if (ibOrder.LimitPrice != 0)
            {
                price = ibOrder.LimitPrice;
            }
            else if (ibOrder.AuxPrice != 0)
            {
                price = ibOrder.AuxPrice;
            }

            Order order;
            var   orderType    = ConvertOrderType(ibOrder.OrderType);
            var   securityType = ConvertSecurityType(contract.SecurityType);

            switch (orderType)
            {
            default:
            case OrderType.Market:
                order = new MarketOrder(contract.Symbol, ibOrder.TotalQuantity, new DateTime(), "", securityType);
                break;

            case OrderType.Limit:
                order = new LimitOrder(contract.Symbol, ibOrder.TotalQuantity, price, new DateTime(), "", securityType);
                break;

            case OrderType.StopMarket:
                order = new StopMarketOrder(contract.Symbol, ibOrder.TotalQuantity, price, new DateTime(), "", securityType);
                break;
            }

            order.BrokerId.Add(ibOrder.OrderId);

            return(order);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates an IB contract from the order.
        /// </summary>
        /// <param name="symbol">The symbol whose contract we need to create</param>
        /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
        /// <returns>A new IB contract for the order</returns>
        private IB.Contract CreateContract(Symbol symbol, string exchange = null)
        {
            var securityType = ConvertSecurityType(symbol.ID.SecurityType);
            var ibSymbol = _symbolMapper.GetBrokerageSymbol(symbol);
            var contract = new IB.Contract(ibSymbol, exchange ?? "Smart", securityType, "USD");
            if (symbol.ID.SecurityType == SecurityType.Forex)
            {
                // forex is special, so rewrite some of the properties to make it work
                contract.Exchange = "IDEALPRO";
                contract.Symbol = ibSymbol.Substring(0, 3);
                contract.Currency = ibSymbol.Substring(3);
            }

            if (symbol.ID.SecurityType == SecurityType.Option)
            {
                contract.Expiry = symbol.ID.Date.ToString(DateFormat.EightCharacter);
                contract.Right = symbol.ID.OptionRight == OptionRight.Call ? IB.RightType.Call : IB.RightType.Put;
                contract.Strike = Convert.ToDouble(symbol.ID.StrikePrice);
                contract.Symbol = symbol.ID.Symbol;
            }

            // some contracts require this, such as MSFT
            contract.PrimaryExchange = GetPrimaryExchange(contract);

            return contract;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Creates an IB contract from the order.
        /// </summary>
        /// <param name="symbol">The symbol whose contract we need to create</param>
        /// <param name="type">The security type of the symbol</param>
        /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
        /// <returns>A new IB contract for the order</returns>
        private IB.Contract CreateContract(string symbol, SecurityType type, string exchange = null)
        {
            var securityType = ConvertSecurityType(type);
            var contract = new IB.Contract(symbol, exchange ?? "Smart", securityType, "USD");
            if (type == SecurityType.Forex)
            {
                // forex is special, so rewrite some of the properties to make it work
                contract.Exchange = "IDEALPRO";
                contract.Symbol = symbol.Substring(0, 3);
                contract.Currency = symbol.Substring(3);
            }

            // some contracts require this, such as MSFT
            contract.PrimaryExchange = GetPrimaryExchange(contract);

            return contract;
        }
 /// <summary>
 /// Creates an IB contract from the order.
 /// </summary>
 /// <param name="order">The order to create a contract from</param>
 /// <param name="exchange">The exchange where the order will be placed, defaults to 'Smart'</param>
 /// <returns>A new IB contract for the order</returns>
 private IB.Contract CreateContract(Order order, string exchange = null)
 {
     var securityType = ConvertSecurityType(order.SecurityType);
     var contract = new IB.Contract(order.Symbol, exchange ?? "Smart", securityType, "USD");
     return contract;
 }