Beispiel #1
0
        public MainPage()
        {
            this.plotView   = createChartPanel();
            this.buyButton  = new TradingButton("Buy");
            this.sellButton = new TradingButton("Sell");

            this.Content = new StackLayout {
                Orientation       = StackOrientation.Vertical,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions   = LayoutOptions.FillAndExpand,
                Children          =
                {
                    createTopPanel(),
                    this.plotView,
                    createBottomPanel()
                }
            };
            // Using messaging center to ensure that content only gets loaded once authentication is successful.
            // Once Xamarin.Forms adds more support for view life cycle events, this kind of thing won't be as necessary.
            // The OnAppearing() and OnDisappearing() overrides just don't quite cut the mustard yet, nor do the Appearing and Disappearing delegates.
            MessagingCenter.Subscribe <App> (this, "Authenticated", (sender) => {
                accountsAPI = new AccountsAPI(App.ACCOUNTS_API_HOST_URL, App.Instance.Token);
                tradingAPI  = new TradingAPI(App.TRADING_API_HOST, App.TRADING_API_PORT, App.Instance.Token, App.CLIENT_ID, App.CLIENT_SECRET);

                fillAccounts();
                fillSymbols();
                refreshPlotView();

                tradingAPI.Start();
                tradingAPI.ExecutionEvent += (executionEvent) => {
                    Device.BeginInvokeOnMainThread(() => {
                        String filledTitle   = "Order Filled at {0}";
                        String filledMessage = "Your request to {0} {1} of {2} was filled at VWAP {3}";
                        if (executionEvent.executionType == ProtoOAExecutionType.OA_ORDER_FILLED)
                        {
                            ProtoOAOrder order = executionEvent.order;
                            string title       = String.Format(filledTitle, order.executionPrice);
                            string message     = String.Format(filledMessage, order.tradeSide, order.requestedVolume / 100, order.symbolName, order.executionPrice);
                            DisplayAlert(title, message, "Close");
                        }
                    });
                };
                tradingAPI.SpotEvent += (spotEvent) => {
                    if (spotEvent.symbolName.Equals(currentSymbol.SymbolName))
                    {
                        if (spotEvent.askPriceSpecified)
                        {
                            LineAnnotation annotation = (LineAnnotation)plotView.Model.Annotations[0];
                            annotation.Y    = spotEvent.askPrice;
                            annotation.Text = spotEvent.askPrice.ToString();
                        }
                        Device.BeginInvokeOnMainThread(() => {
                            if (spotEvent.askPriceSpecified)
                            {
                                buyButton.setPrice(spotEvent.askPrice);
                                plotView.Model.InvalidatePlot(true);
                            }
                            if (spotEvent.bidPriceSpecified)
                            {
                                sellButton.setPrice(spotEvent.bidPrice);
                            }
                        });
                    }
                };
                tradingAPI.SendSubscribeForTradingEventsRequest(currentTradingAccount.AccountId);
                tradingAPI.SendSubscribeForSpotsRequest(currentTradingAccount.AccountId, currentSymbol.SymbolName);
            });
        }
		public MainPage ()
		{
			this.plotView = createChartPanel();
			this.buyButton = new TradingButton ("Buy");
			this.sellButton = new TradingButton("Sell");

			this.Content = new StackLayout {
				Orientation = StackOrientation.Vertical,
				HorizontalOptions = LayoutOptions.FillAndExpand,
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children = {
					createTopPanel (),
					this.plotView,
					createBottomPanel ()
				}
			};
			// Using messaging center to ensure that content only gets loaded once authentication is successful.
			// Once Xamarin.Forms adds more support for view life cycle events, this kind of thing won't be as necessary.
			// The OnAppearing() and OnDisappearing() overrides just don't quite cut the mustard yet, nor do the Appearing and Disappearing delegates.
			MessagingCenter.Subscribe<App> (this, "Authenticated", (sender) => {
				accountsAPI = new AccountsAPI (App.ACCOUNTS_API_HOST_URL, App.Instance.Token);
				tradingAPI = new TradingAPI (App.TRADING_API_HOST, App.TRADING_API_PORT, App.Instance.Token, App.CLIENT_ID, App.CLIENT_SECRET);

				fillAccounts();
				fillSymbols();
				refreshPlotView ();

				tradingAPI.Start ();
				tradingAPI.ExecutionEvent += (executionEvent) => {
					Device.BeginInvokeOnMainThread (() => {
						String filledTitle = "Order Filled at {0}";
						String filledMessage = "Your request to {0} {1} of {2} was filled at VWAP {3}";
						if (executionEvent.executionType == ProtoOAExecutionType.OA_ORDER_FILLED) {
							ProtoOAOrder order = executionEvent.order;
							string title = String.Format (filledTitle, order.executionPrice);
							string message = String.Format (filledMessage, order.tradeSide, order.requestedVolume / 100, order.symbolName, order.executionPrice);
							DisplayAlert (title, message, "Close");
						}
					});
				};
				tradingAPI.SpotEvent += (spotEvent) => {
					if (spotEvent.symbolName.Equals(currentSymbol.SymbolName)) {
						if (spotEvent.askPriceSpecified) {
							LineAnnotation annotation = (LineAnnotation)plotView.Model.Annotations[0];
							annotation.Y = spotEvent.askPrice;
							annotation.Text = spotEvent.askPrice.ToString();
						}
						Device.BeginInvokeOnMainThread (() => {
							if (spotEvent.askPriceSpecified) {
								buyButton.setPrice (spotEvent.askPrice);
								plotView.Model.InvalidatePlot(true);
							}
							if (spotEvent.bidPriceSpecified) {
								sellButton.setPrice (spotEvent.bidPrice);
							}
						});
					}
				};
				tradingAPI.SendSubscribeForTradingEventsRequest (currentTradingAccount.AccountId);
				tradingAPI.SendSubscribeForSpotsRequest (currentTradingAccount.AccountId, currentSymbol.SymbolName);
			});
		}