コード例 #1
0
        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);
        }
コード例 #2
0
        public EditInstrumentViewModel(Instrument model, IDataClient client, IClosableView view, IDialogCoordinator dialogCoordinator) : base(model, new InstrumentValidator())
        {
            _view             = view;
            DialogCoordinator = dialogCoordinator;
            if (model.Sessions == null)
            {
                model.Sessions = new List <InstrumentSession>();
            }
            if (model.Tags == null)
            {
                model.Tags = new List <Tag>();
            }

            foreach (var session in model.Sessions)
            {
                Sessions.Add(new SessionViewModel(session));
            }

            ContractMonths = new ObservableCollection <KeyValuePair <int, string> >();
            //fill the continuous futures contrat month combobox
            for (int i = 1; i < 10; i++)
            {
                ContractMonths.Add(new KeyValuePair <int, string>(i, MyUtils.Ordinal(i) + " Contract"));
            }

            Load = ReactiveCommand.CreateFromTask(async _ =>
            {
                var tags              = client.GetTags();
                var sessionTemplates  = client.GetSessionTemplates();
                var exchanges         = client.GetExchanges();
                var underlyingSymbols = client.GetUnderlyingSymbols();
                var datasources       = client.GetDatasources();

                await Task.WhenAll(tags, sessionTemplates, exchanges, underlyingSymbols, datasources).ConfigureAwait(true);

                var responses = new ApiResponse[] { tags.Result, sessionTemplates.Result, exchanges.Result, underlyingSymbols.Result, datasources.Result };
                if (await responses.DisplayErrors(this, DialogCoordinator).ConfigureAwait(true))
                {
                    return;
                }

                foreach (var tag in tags.Result.Result.Select(x => new CheckBoxTag(x, model.Tags.Contains(x))))
                {
                    AllTags.Add(tag);
                    tag.PropertyChanged += Tag_PropertyChanged;
                }

                Exchanges.AddRange(exchanges.Result.Result);

                foreach (var template in sessionTemplates.Result.Result)
                {
                    SessionTemplates.Add(template);
                }
                foreach (var us in underlyingSymbols.Result.Result)
                {
                    UnderlyingSymbols.Add(us);
                }
                foreach (var ds in datasources.Result.Result)
                {
                    Datasources.Add(ds);
                }
            });

            //Sessions
            AddNewSession = ReactiveCommand.Create(() => AddSession());
            RemoveSession = ReactiveCommand.Create <SessionViewModel>(ExecRemoveSession);

            //Save
            var saveCanExecute = this
                                 .WhenAnyValue(x => x.HasErrors)
                                 .Select(x => !x);

            Save = ReactiveCommand.CreateFromTask(async _ =>
            {
                if (model.ID == null || model.ID <= 0)
                {
                    //adding a new instrument
                    return(await client.AddInstrument(model).ConfigureAwait(true));
                }
                else
                {
                    //updating an existing one
                    return(await client.UpdateInstrument(model).ConfigureAwait(true));
                }
            }
                                                  , saveCanExecute);
            Save.Subscribe(async result =>
            {
                var errors = await result.DisplayErrors(this, DialogCoordinator).ConfigureAwait(true);
                if (!errors)
                {
                    AddedInstrument = result.Result;
                    _view.Close();
                }
            });

            this.Validate();
        }
コード例 #3
0
        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--));
        }