/// <summary> /// /// </summary> /// <param name="instrument"></param> /// <returns>null if addition failed</returns> private async Task <Instrument> TryAddInstrument(Instrument instrument) { var result = await _qdmsClient.AddInstrument(instrument).ConfigureAwait(true); if (await result.DisplayErrors(this, _dialogService).ConfigureAwait(true)) { //request failed _logger.Error("IB add instrument failure: " + string.Join(",", result.Errors)); return(null); } var addedInstrument = result.Result; AddedInstruments.Add(addedInstrument); return(addedInstrument); }
private bool TryAddInstrument(InstrumentRepository instrumentSource, Instrument newInstrument, bool showDialogs = true) { try { if (instrumentSource.AddInstrument(newInstrument) != null) { AddedInstruments.Add(newInstrument); return(true); } } catch (Exception ex) { if (showDialogs) { _dialogService.ShowMessageAsync(this, "Error", ex.Message); } _logger.Log(NLog.LogLevel.Warn, ex, "Error adding instrument"); } return(false); }
private bool TryAddInstrument(InstrumentManager instrumentSource, Instrument newInstrument, bool showDialogs = true) { try { if (instrumentSource.AddInstrument(newInstrument) != null) { AddedInstruments.Add(newInstrument); return(true); } } catch (Exception ex) { if (showDialogs) { _dialogService.ShowMessageAsync(this, "Error", ex.Message); } Application.Current.Dispatcher.Invoke(() => _logger.Log(NLog.LogLevel.Warn, ex, "Error adding instrument")); } return(false); }
private bool TryAddInstrument(InstrumentRepository instrumentSource, Instrument newInstrument, bool showDialogs = true) { try { if (instrumentSource.AddInstrument(newInstrument).Result.ID != 0) { //if (AddedInstruments.Contains(newInstrument)) return false; AddedInstruments.Add(newInstrument); return(true); } } catch (Exception ex) { if (showDialogs) { dialogService.ShowMessageAsync(this, "Error", ex.Message); } Logger.Log(LogLevel.Warn, ex, "Error adding instrument"); } return(false); }
public AddInstrumentBinanceViewModel(IDataClient client, IDialogCoordinator dialogCoordinator) { _client = client; _dialogCoordinator = dialogCoordinator; //Create commands Load = ReactiveCommand.CreateFromTask(async _ => { //load datasource var dsResult = await _client.GetDatasources().ConfigureAwait(true); if (dsResult.WasSuccessful) { _thisDS = dsResult.Result.FirstOrDefault(x => x.Name == "Binance"); } else { _logger.Error("Could not find Binance datasource"); return; } //load instruments try { var instruments = await BinanceUtils.GetInstruments(_thisDS); foreach (var inst in instruments) { Instruments.Add(inst); _allInstruments.Add(inst); } } catch (Exception ex) { _logger.Error(ex, "Could not load symbols from binance"); } }); Search = ReactiveCommand.Create(() => { Instruments.Clear(); foreach (var i in _allInstruments.Where(x => string.IsNullOrEmpty(Symbol) || //case-insensitive Contains() x.Symbol.IndexOf(Symbol, StringComparison.OrdinalIgnoreCase) >= 0)) { Instruments.Add(i); } }); Add = ReactiveCommand.CreateFromTask <IList>(async selectedInstruments => { int count = 0; foreach (Instrument instrument in selectedInstruments) { instrument.Datasource = _thisDS; instrument.DatasourceID = _thisDS.ID; var result = await _client.AddInstrument(instrument).ConfigureAwait(true); if (await result.DisplayErrors(this, _dialogCoordinator).ConfigureAwait(true)) { continue; } count++; AddedInstruments.Add(result.Result); } Status = string.Format("{0}/{1} instruments added.", count, selectedInstruments.Count); }); this.WhenAny(x => x.Symbol, x => x) .Select(x => new Unit()) //hack .InvokeCommand(Search); }
public AddInstrumentQuandlViewModel(IDataClient client, IDialogCoordinator dialogCoordinator, string authToken) { _client = client; _dialogCoordinator = dialogCoordinator; //Create commands Load = ReactiveCommand.CreateFromTask(async _ => { var dsResult = await _client.GetDatasources().ConfigureAwait(true); if (dsResult.WasSuccessful) { _thisDS = dsResult.Result.FirstOrDefault(x => x.Name == "Quandl"); } else { _logger.Error("Could not find FRED datasource"); } }); var canSearch = this.WhenAnyValue(x => x.Symbol).Select(x => !string.IsNullOrEmpty(x)); Search = ReactiveCommand.CreateFromTask(async _ => { Status = "Searching..."; Instruments.Clear(); QuandlUtils.QuandlInstrumentSearchResult foundInstruments; try { foundInstruments = await QuandlUtils.FindInstruments(Symbol, authToken, CurrentPage ?? 1).ConfigureAwait(true); } catch (Exception ex) { await _dialogCoordinator.ShowMessageAsync(this, "Error", ex.Message).ConfigureAwait(true); return; } foreach (var i in foundInstruments.Instruments) { i.Datasource = _thisDS; i.DatasourceID = _thisDS.ID; i.Multiplier = 1; Instruments.Add(i); } Status = foundInstruments.Count + " contracts found"; CurrentPage = CurrentPage ?? 1; }, canSearch); Add = ReactiveCommand.CreateFromTask <IList>(async selectedInstruments => { int count = 0; foreach (Instrument instrument in selectedInstruments) { instrument.Datasource = _thisDS; instrument.DatasourceID = _thisDS.ID; var result = await _client.AddInstrument(instrument).ConfigureAwait(true); if (await result.DisplayErrors(this, _dialogCoordinator).ConfigureAwait(true)) { continue; } count++; AddedInstruments.Add(result.Result); } Status = string.Format("{0}/{1} instruments added.", count, selectedInstruments.Count); }); this.WhenAny(x => x.CurrentPage, x => x) .Select(x => new Unit()) //hack .InvokeCommand(Search); IncrementPage = ReactiveCommand.Create(() => CurrentPage == null ? 1 : CurrentPage++); DecrementPage = ReactiveCommand.Create(() => Math.Max(1, CurrentPage == null ? 1 : (int)CurrentPage--)); }