Esempio n. 1
0
        public PnLWidgetViewModel(DemoWorkspaceViewModel host, String cacheID = null) : base(host, cacheID)
        {
            Strategies = new ReactiveList <StrategyWidgetViewModel>();

            this.WhenAnyObservable(vm => vm.Strategies.CountChanged).Subscribe(change => StrategiesCount = change);

            var strategyObservable = StrategyService.All.Connect()
                                     .Transform(strategy => new StrategyWidgetViewModel(Workspace)
            {
                Strategy = strategy
            })
                                     .Bind(_strategies)
                                     .DisposeMany()
                                     .Subscribe();

            var pricesObservable = PriceService.All
                                   .Connect()
                                   .ObserveOn(AppScheduler.MainThread)
                                   .Do(changes =>
            {
                PnLGlobal = Strategies.Sum(strategy => strategy.Trades.Sum(trade => trade.PnL));
            })
                                   .DisposeMany()
                                   .Subscribe();

            OpenStrategyWorkspace = ReactiveCommand.Create <StrategyWidgetViewModel>((vm) =>
            {
                var state = this.Workspace.Dashboard.GetState();
                this.Workspace.Dashboard.CreateWorkspace(new DemoWorkspaceState(vm.Name, state.Date, DemoWorkspaceType.Strategy)
                {
                    Strategy = vm.Strategy
                }, true);
            });

            this.WhenAny(vm => vm.TradesCount, vm => vm.StrategiesCount, vm => vm.PnLGlobal, (trd, str, pnl) => String.Format("PnL viewer - {0} Strategies - {1} Trades(s) - PnL [{2}] ", StrategiesCount, TradesCount, PnLGlobal))
            .Subscribe(header =>
            {
                Header = header;
            });

            var tradesCountObservable = Observable
                                        .Interval(TimeSpan.FromMilliseconds(500))
                                        .ObserveOnDispatcher()
                                        .Subscribe(observer =>
            {
                TradesCount = Strategies.Sum(strategy => strategy.TradesCount);
            });

            Cleanup(tradesCountObservable, strategyObservable, pricesObservable);
        }
        public StrategyChartWidgetViewModel(DemoWorkspaceViewModel host, String cacheID) : base(host, cacheID)
        {
            StrategyChartItems = new ReactiveList <StrategyChartItemViewModel>();

            this.WhenAnyValue(vm => vm.Workspace)
            .Where(obj => null != obj)
            .Subscribe((obs) =>
            {
                Strategy = obs.State.Strategy;
            });


            this.WhenAnyValue(vm => vm.Strategy)
            .Where(chg => chg != null)
            .Subscribe(chg =>
            {
                var tradeObservable = Strategy.Trades
                                      .Connect()
                                      .Group(trade => trade.Counterparty)
                                      .Transform(group => new StrategyChartItemViewModel(group))
                                      .Bind(StrategyChartItems)
                                      .DisposeMany()
                                      .Subscribe();

                Cleanup(tradeObservable);
            });

            this.WhenAnyObservable(vm => vm.Strategy.Trades.CountChanged)
            .Subscribe((obs) =>
            {
                Header = String.Format("{0} Counterparties", Strategy.Trades.Items.Select(trade => trade.Counterparty).Distinct().Count());

                //force chart to redraw
                this.RaisePropertyChanged("StrategyChartItems");
            });
        }
Esempio n. 3
0
 public DemoWidgetViewModelBase(DemoWorkspaceViewModel host, String cacheID = null) : base(host, cacheID)
 {
 }
        public StrategyWidgetViewModel(DemoWorkspaceViewModel host, String cacheId = null) : base(host, cacheId)
        {
            var pricesObservable = PriceService.All
                                   .Connect()
                                   .ObserveOn(AppScheduler.MainThread)
                                   .Do(changes =>
            {
                changes.ForEach(change =>
                {
                    var pnl = 0.0;

                    for (var i = 0; i < Trades.Count; i++)
                    {
                        if (Trades[i].Trade.Asset == change.Current.Asset.Name)
                        {
                            Trades[i].PnL = Trades[i].Trade.Price - change.Current.Value;
                        }

                        pnl += Trades[i].PnL;
                    }

                    StrategyPnL = pnl;
                });
            })
                                   .DisposeMany()
                                   .Subscribe();

            Cleanup(pricesObservable);


            this.WhenAnyValue(vm => vm.Workspace)
            .Where(obj => null != obj)
            .Subscribe((obs) =>
            {
                Strategy = obs.State.Strategy;
            });

            this.WhenAnyValue(vm => vm.Strategy)
            .Where(obj => null != obj)
            .Subscribe((obs) =>
            {
                Trades = new ReactiveList <TradeViewModel>();

                var tradeObservable = _strategy.Trades
                                      .Connect()
                                      .Transform(trade => new TradeViewModel(trade))
                                      .Bind(_trades)
                                      .DisposeMany()
                                      .Subscribe();

                Cleanup(tradeObservable);
            });

            _tradesCount = this.WhenAnyObservable(vm => vm.Trades.CountChanged)
                           .ToProperty(this, vm => vm.TradesCount);

            this.WhenAnyObservable(vm => vm.Trades.CountChanged)
            .Select(change => Trades.Sum(trade => trade.Price + trade.PnL))
            .Subscribe(chg =>
            {
                MarketValue = chg;
            });


            this.WhenAny(vm => vm.TradesCount, vm => vm.StrategyPnL, (trd, pnl) => String.Format("Strategy viewer - {0} Trades - PnL [{1}] ", TradesCount, StrategyPnL))
            .Subscribe(header =>
            {
                Header = header;
            });
        }