public TaskResultData(IBDTaskInstruction Instruction, bool HasData = false, object DataObject = null) { this.Instruction = Instruction; this.HasData = HasData; this.Data = DataObject; this.Metadata = new Dictionary <string, object>(); }
public async System.Threading.Tasks.Task RetrieveEquityData(IBDTaskInstruction optionInstruction) { if (_equityDataCache.Count == 0) { if (!await this.LookupDerivative(optionInstruction)) { //right now, this is fatal throw new Exception("Failed to find derivative!"); } var underlying = (Contract)optionInstruction.metadata[IBDMetadataType.underlying]; var downloadTask = new DownloadHistoricalData(_TaskHandler); var ins = new IBDTaskInstruction() { contract = underlying, parameters = optionInstruction.parameters.Clone() }; //clone parameters so we get same settings (barSize etc) ins.parameters["StartDate"] = optionInstruction.GetParameter("StartDate").ParseElse(DateTime.Now.AddMonths(-6)).ToString(); var result = await downloadTask.ExecuteAsync(ins); if (!result.HasData) //right now this is fatal { throw new Exception("No data found for underlying!"); } foreach (var quote in (List <HistoricalDataMessage>)result.Data) { var quoteDate = (DateTime)Framework.ParseDateTz(quote.Date); _equityDataCache[quoteDate] = quote; } } optionInstruction.metadata[IBDMetadataType.underlyingData] = _equityDataCache; }
public static TaskResultData Failure(IBDTaskInstruction Instruction, string Message = null) { if (!string.IsNullOrEmpty(Message)) { Framework.LogError("[{0}] Task failed: {1}", Instruction.taskType, Message); } return(new TaskResultData(Instruction, false)); }
public async System.Threading.Tasks.Task <DateTime> GetStartDate(IBDTaskInstruction Instruction) { DateTime startDate = Instruction.GetParameter("StartDate").ParseElse(DateTime.MinValue); if (startDate == DateTime.MinValue) { startDate = await _Controller.HistoricalDataManager.GetEarliestDataTime(Instruction.contract); } startDate = startDate.StartOfDay(); return(startDate); }
public async void ImportCsvData() { var controller = new IBController(); Assert.True(controller.Connect(), "Connection setup failed!"); var taskHandler = new IBDTaskHandler(controller); var instruction = new IBDTaskInstruction("ImportCsv"); instruction.parameters["FilePathName"] = "/Users/jason/Downloads/cboe"; taskHandler.AddTask(instruction); ElasticsearchStorage es = new ElasticsearchStorage(new DataStorage.Processors.StockOptionQuoteProcessor()); taskHandler.OnTaskResult += es.ProcessTaskResult; await taskHandler.BeginAsync(); await es.FlushAsync(); }
public override async System.Threading.Tasks.Task ExecuteMultiAsync(IBDTaskInstruction Instruction, Action <TaskResultData> OnTaskResult) { List <string> files = new List <string>(); var filePathName = Instruction.GetParameter("FilePathName"); if (Directory.Exists(filePathName)) { //is dir var tmpFiles = Directory.GetFiles(filePathName); foreach (string fileName in tmpFiles) { if (fileName.EndsWith("csv", StringComparison.InvariantCulture) || fileName.EndsWith("zip", StringComparison.InvariantCulture)) { files.Add(fileName); } } files.Sort(); } else if (File.Exists(filePathName)) { //is file files.Add(filePathName); } else { this.LogWarn("No files found in specified location!"); return; } foreach (string file in files) { await ProcessFileAsync(Instruction, file, OnTaskResult); } }
public async System.Threading.Tasks.Task <bool> LookupDerivative(IBDTaskInstruction Instruction) { var selectedContracts = await _Controller.ContractManager.GetContractDetails(Instruction.contract); if (selectedContracts.Count < 1) { return(false); } //reset to full derivative contract definition Instruction.contract = selectedContracts[0].Summary; int underlyingConId = selectedContracts[0].UnderConId; //lookup underlying var underlying = await _Controller.ContractManager.GetContract(underlyingConId, string.Empty); //TODO: empty exchange may not always work if (underlying == null) { return(false); } Instruction.metadata[IBDMetadataType.underlying] = underlying; return(true); }
public async void GenerateOptionHistoricalDataTasks() { var controller = new IBController(); Assert.True(controller.Connect(), "Connection setup failed!"); var taskHandler = new IBDTaskHandler(controller); var instruction = new IBDTaskInstruction("BuildOptionDownloadTasks") { Symbol = "VIX", SecType = "OPT" }; instruction.parameters["filter.expirytype"] = OptionChain.Expiration.Type.any.ToString(); taskHandler.AddTask(instruction); var storage = new JSONFile(); taskHandler.OnTaskResult += storage.ProcessTaskResult; await taskHandler.BeginAsync(); await storage.FlushAsync(); }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction instruction) { Managers.BarSize barSize = instruction.GetParameter("BarSize").ParseElse(Managers.BarSize.M15); var selectedContracts = await _Controller.ContractManager.GetContractDetails(instruction.contract); if (selectedContracts.Count < 1) { return(TaskResultData.Failure(instruction, "No matching contracts found!")); } DateTime earliestDate = await GetStartDate(instruction); TimeSpan duration = DateTime.Now.EndOfDay().AddDays(1) - earliestDate; this.Log("Fetching data for {0} starting from {1}", instruction.ConId, earliestDate); List <HistoricalDataMessage> bars = new List <HistoricalDataMessage>(); if (duration.TotalDays > 0) { int days = (int)duration.TotalDays; for (var i = 1; i <= days; i++) { //we can safely download up to 30 days at a time int pageSize = (days - i) + 1; if (pageSize > 30) { pageSize = 30; } i += pageSize - 1; var data = await _Controller.HistoricalDataManager.GetHistoricalData( instruction.contract, earliestDate.AddDays(i), barSize, pageSize, Managers.HistoricalDataType.TRADES, true); this.Log("Downloaded {0} bars.", data.Count); bars.AddRange(data); } } return(new TaskResultData(instruction, bars.Count > 0, bars)); }
public virtual System.Threading.Tasks.Task ExecuteMultiAsync(IBDTaskInstruction Instruction, Action <TaskResultData> OnTaskResult) { throw new NotImplementedException("Check if IsMulti flag is set according to implemented Execute method!"); }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction instruction) { // var accSummary = await _Controller.AccountManager.GetAccountSummary(); accSummary.All((i) => { Framework.Log("\n{0}=", i.Tag); Framework.DebugPrint(i); return(true); }); // List <Contract> contracts = await _Controller.ContractManager.GetContracts(Managers.SecurityType.STK, "SPY"); var contract = contracts.First(); // List <ContractDetails> details = await _Controller.ContractManager.GetContractDetails(contract); Framework.Log("CONTRACT INFO"); details.All((i) => { Framework.Log(i.LongName); return(true); }); // var data = await _Controller.HistoricalDataManager.GetHistoricalData(contract, DateTime.Now, Managers.BarSize.M15); data.All((bar) => { Framework.Log("{0} close={1}", bar.Date, bar.Close); return(true); }); // var optionChain = await _Controller.OptionManager.GetOptionChain(contract); optionChain.Expirations.All((optionExpiry) => { Framework.Log("{0}\t PUTS={1}\t CALLS={2}", optionExpiry.Key.ToShortDateString(), optionExpiry.Value.Puts.Count, optionExpiry.Value.Calls.Count); return(true); }); return(new TaskResultData(instruction, true, accSummary)); }
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); } }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction instruction) { //First, make sure we have the underlying symbol's data await RetrieveEquityData(instruction); //then, follow instruction to download option return(await base.ExecuteAsync(instruction)); }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction instruction) { var optionList = await base.ExecuteAsync(instruction); if (!optionList.HasData) { return(TaskResultData.Failure(instruction, "No option contracts!")); } List <IBDTaskInstruction> instructionSet = new List <IBDTaskInstruction>(); foreach (var optionContractId in (IEnumerable <int>)optionList.Data) { instructionSet.Add(new IBDTaskInstruction("DownloadStockAndOptionHistoricalData") { ConId = optionContractId }); } return(new TaskResultData(instruction, true, instructionSet)); }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction Instruction) { List <string> files = new List <string>(); string idField = Instruction.GetParameter("IDField"); string dateField = Instruction.GetParameter("DateField"); var filePathName = Instruction.GetParameter("FilePathName"); if (Directory.Exists(filePathName)) { //is dir var tmpFiles = Directory.GetFiles(filePathName); foreach (string fileName in tmpFiles) { if (fileName.EndsWith("json", StringComparison.InvariantCulture)) { files.Add(fileName); } } files.Sort(); } else if (File.Exists(filePathName)) { //is file files.Add(filePathName); } else { return(TaskResultData.Failure(Instruction, "No files found in specified location!")); } this.Log("Reading {0} using id={1} and date={2}...", filePathName, idField, dateField); List <IDataRow> allRows = new List <IDataRow>(); //var results = System.Threading.Tasks.Task.TaskResultData(); foreach (string file in files) { allRows.AddRange(ProcessFile(file, idField, dateField)); await System.Threading.Tasks.Task.Delay(1); } return(new TaskResultData(Instruction, true, allRows)); }
public override async System.Threading.Tasks.Task <TaskResultData> ExecuteAsync(IBDTaskInstruction instruction) { double limitStrikeHigh = double.MaxValue; double limitStrikeLow = double.MinValue; if (instruction.GetParameter("filter.range").ParseElse <double>(0) > 0) { //get the high-low during the past 6mos so we can filter down the option chain var highLow = await _Controller.HistoricalDataManager.GetPriceRange(instruction.contract, 6); double rangeDev = (highLow.Item1 - highLow.Item2) * instruction.GetParameter("filter.range").ParseElse <double>(0.3); // +/- 30% limitStrikeHigh = highLow.Item1 + rangeDev; limitStrikeLow = highLow.Item2 - rangeDev; this.Log("Filtering to strikes between {0} and {1}", limitStrikeLow, limitStrikeHigh); } OptionChain.Expiration.Type expType = instruction.GetParameter("filter.expirytype").ParseElse(OptionChain.Expiration.Type.monthly); if (expType != OptionChain.Expiration.Type.any) { this.Log("Filtering to {0}s", expType); } int limitExpiries = instruction.GetParameter("filter.expiries").ParseElse(3); if (limitExpiries > 0) { this.Log("Filtering to {0} expiries ahead", limitExpiries); } var optionChain = await _Controller.OptionManager.GetOptionChain(instruction.contract); int expiryCounter = 0; List <int> flatContractIdList = new List <int>(); optionChain.Expirations.All((expiration) => { if (limitExpiries > 0 && expiryCounter > limitExpiries) { return(true); } if (expType != OptionChain.Expiration.Type.any && expiration.Value.ExpType != expType) { return(true); } expiryCounter++; expiration.Value.Puts.All((contract) => { if (contract.Value.Strike >= limitStrikeLow && contract.Value.Strike <= limitStrikeHigh) { flatContractIdList.Add(contract.Value.ConId); } return(true); }); expiration.Value.Calls.All((contract) => { if (contract.Value.Strike >= limitStrikeLow && contract.Value.Strike <= limitStrikeHigh) { flatContractIdList.Add(contract.Value.ConId); } return(true); }); return(true); }); this.Log("Returning {0} option contracts.", flatContractIdList.Count); return(new TaskResultData(instruction, flatContractIdList.Count > 0, flatContractIdList)); }