Click or drag to resize

Getting started

The application SampleETrade shows an example of working with ETradeTrader as shown in the figure below. The example source code is in the Samples/ETrade folder of the installation package.

etrade sample main window
  1. First, you need to create an instance of the ETradeTrader:

    C#
    Trader = new ETradeTrader(IsSandbox, IsSandbox ? GetSandboxSecurities() : null)
    {
        ConsumerKey = ConsumerKey,
        ConsumerSecret = secret,
        LogLevel = LogLevels.Debug,
        DumpRestData = true
    };
  2. Then you should to subscribe on required IConnector events:

    C#
    // subscribe on connection successfully event
    Trader.Connected += () => this.GuiAsync(() =>
    {
        Properties.Settings.Default.AccessToken = Trader.AccessToken.Serialize();
        OnConnectionChanged(true);
    });
    
    // 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));
  3. Next, you should to sign in the E*TRADE server:

    C#
    Trader.Connect();

    If the authorization was already carried out on the current day, the single call of Connect is enough. Otherwise ETradeTrader automatically launches the full authorization procedure, where the user will be prompted to enter a username/password to the E*TRADE site, after which the user will receive a verification code. The verification code must be written in the ETradeTraderVerificationCode property, and then the authorization procedure should be re-started:

    C#
    Trader.VerificationCode = Verifier;
    Trader.Connect();
  4. Once connected, you can expect to trigger events:

  5. Instruments search (in more detail):

    etrade sample 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 request updates on the portfolios you need to use the RegisterPortfolio method:

    C#
    Trader.NewPortfolio += portfolio =>
    {
        _portfoliosWindow.PortfolioGrid.Portfolios.Add(portfolio);
    
        // subscribe on portfolio updates
        Trader.RegisterPortfolio(portfolio);
    };
  7. 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);
    }