private Tuple <ContractDetails_ViewModel, List <Bar> > DownloadPreviousCloses(ContractDetails_ViewModel cd_vm) { //Quote_ViewModel symVM = null; List <Bar> closePricesList = null; ContractDetails_ViewModel retCd_vm = cd_vm; //symVM = QuotesList.FirstOrDefault(s => s.Symbol == cd_vm.Symbol); //if (symVM != null) //{ // // Little prob. to find a symbol previously added to the list: // // First cancel the old: // TwsData.DataFeeder.CancelRealTime(symVM.ReqId); // QuoteRemoved?.Invoke(this, new Quote_EventArgs(symVM)); //} //else //{ Tuple <List <Bar>, TwsError> tuple = TwsData.DataFeeder.GetPreviousCloses(cd_vm.ContractDetails.Contract, 2); closePricesList = tuple.Item1; if (closePricesList == null || (closePricesList.Count < 1 || closePricesList.Count > 2)) { // Not the right count, give up retCd_vm = null; } else { closePricesList = tuple.Item1; } //} // finally, if everything was ok, retCd_vm is valid, otherwise is null return(new Tuple <ContractDetails_ViewModel, List <Bar> >(cd_vm, closePricesList)); }
public void AddTime(ContractDetails_ViewModel cd_vm) { string uniqueSymbol = cd_vm.UniqueName; lock (dictionary_Lock) { openingHours[uniqueSymbol] = cd_vm; StartTimer(); } }
private async Task <Quote_ViewModel> PushQuoteToList(ContractDetails_ViewModel cd_vm, List <Bar> closePricesList) { Quote_ViewModel retQuote_vm; retQuote_vm = new Quote_ViewModel(0, cd_vm); retQuote_vm.SetClosedValues(closePricesList); QuoteAdded?.Invoke(this, new Quote_EventArgs(retQuote_vm)); await Task.CompletedTask; return(retQuote_vm); }
// // From IB, returns a ContractDetails for the specified contract. // Normally only one contract should be received since at this point // the presented contract data specifies all the fields // private Tuple <string, ContractDetails_ViewModel> DownloadContractDetails(ContractData contractData) { ContractDetails_ViewModel contractDetails_vm = null; ContractDetails contractDetails = null; contractDetails = TwsData.DataFeeder.GetContractDetailsList(contractData).FirstOrDefault(); if (contractDetails != null) { contractDetails_vm = new ContractDetails_ViewModel(contractDetails); } return(new Tuple <string, ContractDetails_ViewModel>(contractData.Symbol, contractDetails_vm)); }
// // Called from ContractDetails_ViewModel when data about one contract has been received. // When all ContractDetails are received, IsSearchingInProgress is set to false // private void ContractVM_EndPriceUpdate_Event(object sender, EventArgs e) { ContractDetails_ViewModel contractVM = sender as ContractDetails_ViewModel; if (contractVM == null) { return; } contractVM.EndPriceUpdate_Event -= ContractVM_EndPriceUpdate_Event; lock (countContractsUpdating_lock) { --countContractsUpdating; } if (countContractsUpdating == 0) { IsSearchingInProgress = false; } }
private async Task StartSearch(string smb) { IsSearchingInProgress = true; Contracts_List.Clear(); List <ContractDetails> contractDetailsList; if (string.IsNullOrEmpty(smb)) { contractDetailsList = TwsData.DataFeeder.GetContractDetailsList(new ContractData() { Symbol = this.Symbol, SecType = "STK" }); } else { contractDetailsList = TwsData.DataFeeder.GetContractDetailsList(new ContractData() { Symbol = smb, SecType = "STK" }); } foreach (ContractDetails cd in contractDetailsList) { ContractDetails_ViewModel contractVM = new ContractDetails_ViewModel(cd); await dispatcher.InvokeAsync(() => Contracts_List.Add(contractVM)); contractVM.EndPriceUpdate_Event += ContractVM_EndPriceUpdate_Event; Thread t = new Thread(new ThreadStart(contractVM.StartDownloadData)); t.Start(); lock (countContractsUpdating_lock) { ++countContractsUpdating; } } }
// // Double click on contract will fire up ContractSelected_Event (to AddSymbol_ViewModel) // private async Task leftDoubleClicked() { if (SelectedReqId <= 0) { return; } ContractDetails_ViewModel contractVM = Contracts_List.FirstOrDefault(c => c.ReqId == SelectedReqId); if (contractVM.IsEnabled == false) { return; } // First cancel others ContractDetails requests foreach (ContractDetails_ViewModel cdVm in Contracts_List) { cdVm.CancelPriceUpdate(); } ContractSelected_Event?.Invoke(this, new ContractDetailsRecv_EventArgs(SelectedReqId, contractVM.ContractDetails)); await Close(); }
public async Task Run(List <ContractData> cDataList) { var taskSchedulerPair = new ConcurrentExclusiveSchedulerPair(); var exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler; var linkOptions = new DataflowLinkOptions { PropagateCompletion = true }; cancellationSource = new CancellationTokenSource(); if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); } CurrentTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); downloadContractDetails = new TransformBlock <ContractData, Tuple <string, ContractDetails_ViewModel> >(contractData => { return(DownloadContractDetails(contractData)); }, new ExecutionDataflowBlockOptions { CancellationToken = cancellationSource.Token, MaxDegreeOfParallelism = -1, TaskScheduler = taskSchedulerPair.ConcurrentScheduler }); downloadPreviousCloses = new TransformBlock <Tuple <string, ContractDetails_ViewModel>, Tuple <ContractDetails_ViewModel, List <Bar> > >(inputTuple => { return(DownloadPreviousCloses(inputTuple.Item2)); }, new ExecutionDataflowBlockOptions { CancellationToken = cancellationSource.Token, MaxDegreeOfParallelism = -1, TaskScheduler = taskSchedulerPair.ConcurrentScheduler }); pushQuoteToList = new TransformBlock <Tuple <ContractDetails_ViewModel, List <Bar> >, Quote_ViewModel>(async inputTuple => { ContractDetails_ViewModel cd_vm = inputTuple.Item1; List <Bar> bars_List = inputTuple.Item2; return(await PushQuoteToList(cd_vm, bars_List)); }, new ExecutionDataflowBlockOptions { CancellationToken = cancellationSource.Token, MaxDegreeOfParallelism = -1, TaskScheduler = CurrentTaskScheduler }); startRealTime = new ActionBlock <Quote_ViewModel>(quote_vm => { StartRealTime(quote_vm); }, new ExecutionDataflowBlockOptions { CancellationToken = cancellationSource.Token, MaxDegreeOfParallelism = -1, TaskScheduler = CurrentTaskScheduler }); Predicate <Tuple <string, ContractDetails_ViewModel> > checkNullContractDetails = cd_vm => cd_vm.Item2 != null; downloadContractDetails.LinkTo(downloadPreviousCloses, linkOptions, checkNullContractDetails); // Discard if downloaded ContractDetails is null downloadContractDetails.LinkTo(DataflowBlock.NullTarget <Tuple <string, ContractDetails_ViewModel> >(), linkOptions, cd_vm => !checkNullContractDetails(cd_vm)); downloadPreviousCloses.LinkTo(pushQuoteToList, linkOptions, output => (output.Item2 != null && output.Item2.Count > 0)); downloadPreviousCloses.LinkTo(DataflowBlock.NullTarget <Tuple <ContractDetails_ViewModel, List <Bar> > >(), linkOptions, output => (output.Item2 == null || output.Item2.Count == 0)); pushQuoteToList.LinkTo(startRealTime, linkOptions, quoteVM => quoteVM != null); pushQuoteToList.LinkTo(DataflowBlock.NullTarget <Quote_ViewModel>(), linkOptions, quoteVM => quoteVM == null); cDataList.ForEach(cData => { downloadContractDetails.Post(cData); }); downloadContractDetails.Complete(); try { // Asynchronously wait for the pipeline to complete processing and for // the progress bars to update. await Task.WhenAll( downloadContractDetails.Completion, downloadPreviousCloses.Completion, startRealTime.Completion, pushQuoteToList.Completion); } catch (OperationCanceledException) { } }
public Quote_ViewModel(int reqId, ContractDetails_ViewModel cDetails) { ReqId = reqId; ContractDetails = cDetails; IsTabSelected = true; }