Example #1
0
        private static Trading.Contract ConvertContract(IBApi.Contract contractIB)
        {
            if (contractIB != null)
            {
                Trading.Contract contract = new Trading.Contract();
                contract.ComboLegsDescription = contractIB.ComboLegsDescription;
                contract.ContractId           = contractIB.ConId;
                contract.Symbol  = contractIB.Symbol;
                contract.SecType = contractIB.SecType;
                contract.LastTradeDateOrContractMonth = contractIB.LastTradeDateOrContractMonth;
                contract.Strike          = contractIB.Strike;
                contract.Right           = contractIB.Right;
                contract.Multiplier      = contractIB.Multiplier;
                contract.Exchange        = contractIB.Exchange;
                contract.Currency        = contractIB.Currency;
                contract.LocalSymbol     = contractIB.LocalSymbol;
                contract.PrimaryExchange = contractIB.PrimaryExch;
                contract.TradingClass    = contractIB.TradingClass;
                contract.SecIdType       = contractIB.SecIdType;
                contract.SecId           = contractIB.SecId;
                contract.SID             = GetSIDFromName(contract.Symbol);
                return(contract);
            }

            return(null);
        }
Example #2
0
        public int PlaceSimpleOrder(SimpleOrder simpleOrder)
        {
            try
            {
                IBUser user = _userManager.GetUser(simpleOrder.UserName);
                if (user != null)
                {
                    IBApi.Contract contract = IBContractSamples.GetContract(simpleOrder);
                    IBApi.Order    order    = IBOrderSamples.GetOrder(simpleOrder);
                    return(user.PlaceOrder(contract, order, simpleOrder.Fund, simpleOrder.Strategy, simpleOrder.Folder));
                }

                return(-1);
            }
            catch (Exception ex)
            {
                _Logger.Error(ex);
                throw ex;
            }
        }
Example #3
0
        protected async System.Threading.Tasks.Task ProcessFileAsync(IBDTaskInstruction Instruction, string CurrentFile, Action <TaskResultData> OnTakResult)
        {
            IBApi.Contract underlyingContract = null;
            Dictionary <string, List <CboeCsvRecord> > fileOptionQuotes = new Dictionary <string, List <CboeCsvRecord> >();

            ZipArchive zipFile = null;
            Stream     stream;

            if (CurrentFile.EndsWith("zip", StringComparison.InvariantCulture))
            {
                //TODO: support more than 1 file per archive
                zipFile = ZipFile.OpenRead(CurrentFile);
                stream  = zipFile.Entries[0].Open();
            }
            else
            {
                stream = File.OpenRead(CurrentFile);
            }

            this.Log("Processing {0}...", CurrentFile);

            using (stream)
            {
                StreamReader fileStream = new StreamReader(stream);
                CsvReader    reader     = new CsvReader(fileStream);
                foreach (var record in reader.GetRecords <CboeCsvRecord>())
                {
                    if (underlyingContract == null)
                    {
                        underlyingContract = record.AsUnderlyingContract();
                    }
                    else if (underlyingContract.Symbol != record.AsUnderlyingContract().Symbol)
                    {
                        throw new Exception("Task does not support data with multiple underlying contracts!");
                    }

                    string optionLocalSymbol = record.AsOptionContract().LocalSymbol;
                    if (!fileOptionQuotes.ContainsKey(optionLocalSymbol))
                    {
                        fileOptionQuotes[optionLocalSymbol] = new List <CboeCsvRecord>();
                    }

                    fileOptionQuotes[optionLocalSymbol].Add(record);
                }
            }
            if (zipFile != null)
            {
                zipFile.Dispose();
            }

            //grouped by option symbol/contract
            foreach (var optionQuoteSet in fileOptionQuotes)
            {
                var underlyingQuotes = new Dictionary <DateTime, HistoricalDataMessage>();
                var optionQuotes     = new List <HistoricalDataMessage>();
                foreach (CboeCsvRecord quote in optionQuoteSet.Value)
                {
                    optionQuotes.Add(quote.AsOptionQuote());

                    var underlyingQuote = quote.AsUnderlyingQuote();
                    underlyingQuotes[underlyingQuote.Date.ParseElse <DateTime>(DateTime.Now)] = underlyingQuote;
                }

                var ins = new IBDTaskInstruction()
                {
                    contract = optionQuoteSet.Value[0].AsOptionContract(), parameters = Instruction.parameters.Clone()
                };                                                                                                                                                         //clone parameters so we get same settings (barSize etc)
                ins.metadata[IBDMetadataType.underlying]     = underlyingContract;
                ins.metadata[IBDMetadataType.underlyingData] = underlyingQuotes;

                var taskResult = new TaskResultData(ins, true, optionQuotes);

                //fire event with this task result
                OnTakResult(taskResult);

                //give storage a second to keep up
                await System.Threading.Tasks.Task.Delay(10);
            }
        }