Click or drag to resize

Getting started

The application SampleSterling shows an example of working with SterlingTrader, as shown in the figure below. The example source code is in the Samples/Sterling folder of the installation package.

Sterling Sample
Note Note

To work with an example of connecting to the Sterling Trader Pro the Visual Studio must be run from an administrator account!

  1. First, you need to create an instance of the SterlingTrader:

    C#
    Trader = new SterlingTrader { LogLevel = LogLevels.Debug };
  2. Next, you should subscribe on required IConnector events:

    C#
    Trader.Connected += () =>
    {
        this.GuiAsync(() => OnConnectionChanged(true));
        AddSecurities();
        Trader.RegisterNews();
    };
    
    // subscribe on connection error event
    Trader.ConnectionError += error => this.GuiAsync(() =>
    {
        OnConnectionChanged(Trader.ConnectionState == ConnectionStates.Connected);
        MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2959);
    });
    
    Trader.Disconnected += () => this.GuiAsync(() => OnConnectionChanged(false));
    
    // subscribe on error event
    Trader.Error += error =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2955));
    
    // subscribe on error of market data subscription event
    Trader.MarketDataSubscriptionFailed += (security, msg, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2956Params.Put(msg.DataType, security)));
    
    Trader.NewSecurity += security => _securitiesWindow.SecurityPicker.Securities.Add(security);
    Trader.NewMyTrade += trade => _myTradesWindow.TradeGrid.Trades.Add(trade);
    Trader.NewOrder += order => _ordersWindow.OrderGrid.Orders.Add(order);
    Trader.NewStopOrder += order => _stopOrdersWindow.OrderGrid.Orders.Add(order);
    Trader.NewPortfolio += portfolio =>
    {
        _portfoliosWindow.PortfolioGrid.Portfolios.Add(portfolio);
    
        // subscribe on portfolio updates
        Trader.RegisterPortfolio(portfolio);
    };
    Trader.NewPosition += position => _portfoliosWindow.PortfolioGrid.Positions.Add(position);
    
    // subscribe on error of order registration event
    Trader.OrderRegisterFailed += _ordersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of order cancelling event
    Trader.OrderCancelFailed += OrderFailed;
    
    // subscribe on error of stop-order registration event
    Trader.StopOrderRegisterFailed += _stopOrdersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of stop-order cancelling event
    Trader.StopOrderCancelFailed += OrderFailed;
    
    Trader.MassOrderCancelFailed += (transId, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str716));
    
    Trader.NewNews += news =>  _newsWindow.NewsPanel.NewsGrid.News.Add(news);
  3. Then you need to connect to the Sterling server:

    C#
    Trader.Connect();
  4. When the connection is successful, you can expect to trigger the IPortfolioProviderNewPortfolio event. In this event handler you should subscribe to changes in the IConnectorPortfoliosChanged portfolios using the RegisterPortfolio(Portfolio) method.

  5. Instruments search (in more detail):

    Sterling securities
    C#
    private void FindClick(object sender, RoutedEventArgs e)
    {
        var wnd = new SecurityLookupWindow { Criteria = new Security { Code = "AAPL" } };
    
        if (!wnd.ShowModal(this))
            return;
    
        MainWindow.Instance.Trader.LookupSecurities(wnd.Criteria);
    }
  6. To get trades by the selected instrument you should call the RegisterTrades(Security) method:

    C#
    private void TradesClick(object sender, RoutedEventArgs e)
    {
        TryInitialize();
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _tradesWindows.SafeAdd(security, s =>
            {
                // create tick trades window
                var wnd = new TradesWindow
                {
                    Title = security.Code + " " + LocalizedStrings.Ticks
                };
    
                // subscribe on tick trades flow
                MainWindow.Instance.Trader.RegisterTrades(security);
    
                wnd.MakeHideable();
                return wnd;
            });
    
            if (window.Visibility == Visibility.Visible)
                window.Hide();
            else
                window.Show();
        }
    }

    Trades will come in the IConnectorNewTrade event.

  7. To request updates on the order books you need to use the RegisterMarketDepth(Security) method:

    C#
    private void DepthClick(object sender, RoutedEventArgs e)
    {
        TryInitialize();
    
        var trader = MainWindow.Instance.Trader;
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _quotesWindows.SafeAdd(security, s =>
            {
                // subscribe on order book flow
                trader.RegisterMarketDepth(security);
    
                // create order book window
                var wnd = new QuotesWindow
                {
                    Title = security.Id + " " + LocalizedStrings.MarketDepth
                };
                wnd.MakeHideable();
                return wnd;
            });
    
            if (window.Visibility == Visibility.Visible)
                window.Hide();
            else
                window.Show();
    
            TraderOnMarketDepthChanged(trader.GetMarketDepth(security));
        }
    }

    The order books appears in the IConnectorMarketDepthsChanged event.

  8. For orders registration the New Order button used. The RegisterOrder(Order) method sends an order to the server:

    C#
    private void NewOrderClick(object sender, RoutedEventArgs e)
    {
        var newOrder = new OrderWindow
        {
            Order = new Order { Security = SecurityPicker.SelectedSecurity },
            SecurityProvider = MainWindow.Instance.Trader,
            MarketDataProvider = MainWindow.Instance.Trader,
            Portfolios = new PortfolioDataSource(MainWindow.Instance.Trader),
        };
    
        if (newOrder.ShowModal(this))
            MainWindow.Instance.Trader.RegisterOrder(newOrder.Order);
    }