Esempio n. 1
0
        public static Contract SmartFutureComboContract()
        {
            //! [smartfuturespread]
            Contract contract = new Contract();

            contract.Symbol   = "WTI"; // WTI,COIL spread. Symbol can be defined as first leg symbol ("WTI") or currency ("USD").
            contract.SecType  = "BAG";
            contract.Currency = "USD";
            contract.Exchange = "SMART";

            ComboLeg leg1 = new ComboLeg();

            leg1.ConId    = 55928698;//WTI future June 2017
            leg1.Ratio    = 1;
            leg1.Action   = "BUY";
            leg1.Exchange = "IPE";

            ComboLeg leg2 = new ComboLeg();

            leg2.ConId    = 55850663;//COIL future June 2017
            leg2.Ratio    = 1;
            leg2.Action   = "SELL";
            leg2.Exchange = "IPE";

            contract.ComboLegs = new List <ComboLeg>();
            contract.ComboLegs.Add(leg1);
            contract.ComboLegs.Add(leg2);
            //! [smartfuturespread]
            return(contract);
        }
Esempio n. 2
0
        /*
         * CBOE Volatility Index Future combo contract
         * Leg 1: 195538625 - FUT expiring 2016/02/17
         * Leg 2: 197436571 - FUT expiring 2016/03/16
         */
        public static Contract FutureComboContract()
        {
            //! [bagfutcontract]
            Contract contract = new Contract();

            contract.Symbol   = "VIX";
            contract.SecType  = "BAG";
            contract.Currency = "USD";
            contract.Exchange = "CFE";

            ComboLeg leg1 = new ComboLeg();

            leg1.ConId    = 195538625;//VIX FUT 20160217
            leg1.Ratio    = 1;
            leg1.Action   = "BUY";
            leg1.Exchange = "CFE";

            ComboLeg leg2 = new ComboLeg();

            leg2.ConId    = 197436571;//VIX FUT 20160316
            leg2.Ratio    = 1;
            leg2.Action   = "SELL";
            leg2.Exchange = "CFE";

            contract.ComboLegs = new List <ComboLeg>();
            contract.ComboLegs.Add(leg1);
            contract.ComboLegs.Add(leg2);
            //! [bagfutcontract]
            return(contract);
        }
Esempio n. 3
0
        /*
         * STK Combo contract
         * Leg 1: 43645865 - IBKR's STK
         * Leg 2: 9408 - McDonald's STK
         */
        public static Contract StockComboContract()
        {
            //! [bagstkcontract]
            Contract contract = new Contract();

            contract.Symbol   = "IBKR,MCD";
            contract.SecType  = "BAG";
            contract.Currency = "USD";
            contract.Exchange = "SMART";

            ComboLeg leg1 = new ComboLeg();

            leg1.ConId    = 43645865;//IBKR STK
            leg1.Ratio    = 1;
            leg1.Action   = "BUY";
            leg1.Exchange = "SMART";

            ComboLeg leg2 = new ComboLeg();

            leg2.ConId    = 9408;//MCD STK
            leg2.Ratio    = 1;
            leg2.Action   = "SELL";
            leg2.Exchange = "SMART";

            contract.ComboLegs = new List <ComboLeg>();
            contract.ComboLegs.Add(leg1);
            contract.ComboLegs.Add(leg2);
            //! [bagstkcontract]
            return(contract);
        }
Esempio n. 4
0
        public static Contract OptionComboContract()
        {
            //! [bagoptcontract]
            Contract contract = new Contract();

            contract.Symbol   = "DBK";
            contract.SecType  = "BAG";
            contract.Currency = "EUR";
            contract.Exchange = "DTB";

            ComboLeg leg1 = new ComboLeg();

            leg1.ConId    = 197397509;//DBK JUN 15 '18 C
            leg1.Ratio    = 1;
            leg1.Action   = "BUY";
            leg1.Exchange = "DTB";

            ComboLeg leg2 = new ComboLeg();

            leg2.ConId    = 197397584;//DBK JUN 15 '18 P
            leg2.Ratio    = 1;
            leg2.Action   = "SELL";
            leg2.Exchange = "DTB";

            contract.ComboLegs = new List <ComboLeg>();
            contract.ComboLegs.Add(leg1);
            contract.ComboLegs.Add(leg2);
            //! [bagoptcontract]
            return(contract);
        }
        public static Contract InterCmdtyFuturesContract()
        {
            //! [intcmdfutcontract]
            Contract contract = new Contract();

            contract.Symbol   = "CL.BZ";
            contract.SecType  = "BAG";
            contract.Currency = "USD";
            contract.Exchange = "NYMEX";

            ComboLeg leg1 = new ComboLeg();

            leg1.ConId    = 47207310; //CL Dec'16 @NYMEX
            leg1.Ratio    = 1;
            leg1.Action   = "BUY";
            leg1.Exchange = "NYMEX";

            ComboLeg leg2 = new ComboLeg();

            leg2.ConId    = 47195961; //BZ Dec'16 @NYMEX
            leg2.Ratio    = 1;
            leg2.Action   = "SELL";
            leg2.Exchange = "NYMEX";

            contract.ComboLegs = new List <ComboLeg>();
            contract.ComboLegs.Add(leg1);
            contract.ComboLegs.Add(leg2);
            //! [intcmdfutcontract]
            return(contract);
        }
Esempio n. 6
0
        public Contract ExtractContract(string mktDataRequest)
        {
            Contract contract = new Contract();

            string[] strings = mktDataRequest.Split(TwsRtdServerData.CHAR_UNDERSCORE);
            if (strings.Length != 16)
            {
                SetError(TwsRtdServerErrors.CANNOT_EXTRACT_CONTRACT, "Cannot extract contract from " + mktDataRequest);
                return(contract);
            }

            try
            {
                if (strings[0] != null && strings[0].Length > 0)
                {
                    contract.ConId = StrToInt(strings[0]);
                }
                contract.Symbol  = strings[1];
                contract.SecType = strings[2];
                contract.LastTradeDateOrContractMonth = strings[3];
                if (strings[4] != null && strings[4].Length > 0)
                {
                    contract.Strike = StrToDouble(strings[4]);
                }
                contract.Right        = strings[5];
                contract.Multiplier   = strings[6];
                contract.Exchange     = strings[7];
                contract.PrimaryExch  = strings[8];
                contract.Currency     = strings[9];
                contract.LocalSymbol  = strings[10];
                contract.TradingClass = strings[11];
            }
            catch
            {
                SetError(TwsRtdServerErrors.CANNOT_EXTRACT_CONTRACT, "Exception during contract extraction from " + mktDataRequest);
                return(contract);
            }

            // parse combo legs
            string comboStr = strings[12];

            try
            {
                if (comboStr != null && comboStr.Length > 0)
                {
                    contract.ComboLegs = new List <ComboLeg>();
                    string[] comboLegsStr = comboStr.Split(TwsRtdServerData.CHAR_SEMICOLON);
                    foreach (string comboLegStr in comboLegsStr)
                    {
                        if (comboLegStr != null && comboLegStr.Length > 0)
                        {
                            ComboLeg comboLeg    = new ComboLeg();
                            string[] comboParams = comboLegStr.Split(TwsRtdServerData.CHAR_HASH);
                            if (comboParams.Length == 4)
                            {
                                comboLeg.ConId = StrToInt(comboParams[0]);
                                comboLeg.Ratio = StrToInt(comboParams[1]);
                                string action = comboParams[2];
                                if (action != null && action.Length > 0)
                                {
                                    if (action == TwsRtdServerData.ACTION_SELL_1 || (action == TwsRtdServerData.ACTION_SELL_2))
                                    {
                                        comboLeg.Action = TwsRtdServerData.ACTION_SELL_1;
                                    }
                                    else if (action == TwsRtdServerData.ACTION_SELL_SHORT_1 || (action == TwsRtdServerData.ACTION_SELL_SHORT_2))
                                    {
                                        comboLeg.Action = TwsRtdServerData.ACTION_SELL_SHORT_1;
                                    }
                                    else // default
                                    {
                                        comboLeg.Action = TwsRtdServerData.ACTION_BUY_1;
                                    }
                                }
                                comboLeg.Exchange = comboParams[3];
                                contract.ComboLegs.Add(comboLeg);
                            }
                            else
                            {
                                SetError(TwsRtdServerErrors.CANNOT_EXTRACT_COMBO_LEGS, "Cannot extract combo legs from " + comboStr);
                                return(contract);
                            }
                        }
                    }
                }
            }
            catch
            {
                SetError(TwsRtdServerErrors.CANNOT_EXTRACT_COMBO_LEGS, "Exception during combo legs extraction from " + comboStr);
                return(contract);
            }

            // parse delta-neutral contract
            string deltaNeutralContractStr = strings[13];

            try
            {
                if (deltaNeutralContractStr != null && deltaNeutralContractStr.Length > 0)
                {
                    contract.DeltaNeutralContract = new DeltaNeutralContract();

                    string[] deltaNeutralContractParams = deltaNeutralContractStr.Split(TwsRtdServerData.CHAR_HASH);
                    if (deltaNeutralContractParams != null && deltaNeutralContractParams.Length >= 3)
                    {
                        try
                        {
                            contract.DeltaNeutralContract.ConId = StrToInt(deltaNeutralContractParams[0]);
                        }
                        catch
                        {
                            SetError(TwsRtdServerErrors.CANNOT_EXTRACT_DELTA_NEUTRAL_CONTRACT, "Cannot convert conid from " + deltaNeutralContractParams[0] + " : " + StrToInt(deltaNeutralContractParams[0]));
                            return(contract);
                        }
                        try
                        {
                            contract.DeltaNeutralContract.Delta = StrToDouble(deltaNeutralContractParams[1]);
                        }
                        catch
                        {
                            SetError(TwsRtdServerErrors.CANNOT_EXTRACT_DELTA_NEUTRAL_CONTRACT, "Cannot convert delta from " + deltaNeutralContractParams[1] + " : " + StrToDouble(deltaNeutralContractParams[1]));
                            return(contract);
                        }
                        try
                        {
                            contract.DeltaNeutralContract.Price = StrToDouble(deltaNeutralContractParams[2]);
                        }
                        catch
                        {
                            SetError(TwsRtdServerErrors.CANNOT_EXTRACT_DELTA_NEUTRAL_CONTRACT, "Cannot convert price from " + deltaNeutralContractParams[2] + " : " + StrToDouble(deltaNeutralContractParams[2]));
                            return(contract);
                        }
                    }
                    else
                    {
                        SetError(TwsRtdServerErrors.CANNOT_EXTRACT_DELTA_NEUTRAL_CONTRACT, "Cannot extract delta-neutral contract from " + deltaNeutralContractStr);
                        return(contract);
                    }
                }
            }
            catch
            {
                SetError(TwsRtdServerErrors.CANNOT_EXTRACT_DELTA_NEUTRAL_CONTRACT, "Exception during delta-neutral contract extraction from " + deltaNeutralContractStr);
            }

            return(contract);
        }
Esempio n. 7
0
        protected Contract generateContractFromComboAssetID(string comboAssetID)
        {
            Contract con = new Contract();

            con.Currency  = "USD";
            con.Exchange  = "SMART";
            con.SecType   = "BAG";
            con.Symbol    = "";
            con.ComboLegs = new List <ComboLeg>();

            List <Contract> sub_contracts = new List <Contract>();
            List <string>   asset_ids     = new List <string>();

            var combo_IDs = comboAssetID.Split('|');

            foreach (var combo_id in combo_IDs)
            {
                var combo_parts = combo_id.Split('*');
                if (combo_parts.Length != 2)
                {
                    throw new FormatException(String.Format("Format Error: {0}", comboAssetID));
                }
                Contract sub_con = generateContractFromAssetID(combo_parts[1]);
                sub_contracts.Add(sub_con);
                asset_ids.Add(combo_parts[1]);

                bool bFindCon = false;
                for (int i = 0; i < sub_contracts.Count - 1; ++i)
                {
                    if (sub_contracts[i].Symbol == sub_con.Symbol && sub_contracts[i].SecType == sub_con.SecType)
                    {
                        bFindCon = true;
                        break;
                    }
                }
                if (!bFindCon)
                {
                    if (con.Symbol == "")
                    {
                        con.Symbol = sub_con.Symbol;
                    }
                    else
                    {
                        con.Symbol += ", " + sub_con.Symbol;
                    }
                }

                ComboLeg leg = new ComboLeg();
                leg.ConId    = -1;
                leg.Exchange = "SMART";

                int ratio = Convert.ToInt32(combo_parts[0]);
                if (ratio >= 0)
                {
                    leg.Ratio  = ratio;
                    leg.Action = "BUY";
                }
                else
                {
                    leg.Ratio  = -ratio;
                    leg.Action = "SELL";
                }
                con.ComboLegs.Add(leg);
            }

            // 处理ConId逻辑
            // 先不触发reqContractDetails,来获取ConId
            bool bConIdLeft = false;

            rwLock.AcquireReaderLock(1000);
            try
            {
                for (int i = 0; i < sub_contracts.Count; ++i)
                {
                    if (con.ComboLegs[i].ConId == -1)
                    {
                        if (dicContract.ContainsKey(asset_ids[i]))
                        {
                            var find_con = dicContract[asset_ids[i]];
                            con.ComboLegs[i].ConId = find_con.ConId;
                        }
                        else
                        {
                            if (!bConIdLeft)
                            {
                                bConIdLeft = true;
                                ibClient.ContractDetails += ibClient_HandleContractDataMessageSignal;
                                ibClient.ClientSocket.reqContractDetails(CONTRACT_ID++, sub_contracts[i]);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                rwLock.ReleaseReaderLock();
            }

            if (!bConIdLeft)
            {
                return(con);
            }

            // 触发reqContractDetails来获取ConId
            while (bConIdLeft)
            {
                autoEvent.WaitOne(5000);
                if (isDebug)
                {
                    Console.WriteLine("Got an auto event.");
                }

                bConIdLeft = false;
                rwLock.AcquireReaderLock(1000);
                try
                {
                    for (int i = 0; i < sub_contracts.Count; ++i)
                    {
                        if (con.ComboLegs[i].ConId == -1)
                        {
                            if (dicContract.ContainsKey(asset_ids[i]))
                            {
                                var find_con = dicContract[asset_ids[i]];
                                con.ComboLegs[i].ConId = find_con.ConId;
                            }
                            else
                            {
                                bConIdLeft = true;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    rwLock.ReleaseReaderLock();
                }
            }

            ibClient.ContractDetails -= ibClient_HandleContractDataMessageSignal;
            return(con);
        }