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 successfully connect event
    Trader.Connected += () =>
    {
        // set flag (connection is established)
        _isConnected = true;
    
        // update gui labes
        this.GuiAsync(() => ChangeConnectStatus(true));
    };
    
    // subscribe on connection error event
    Trader.ConnectionError += error => this.GuiAsync(() =>
    {
        // update gui labes
        ChangeConnectStatus(false);
    
        MessageBox.Show(this, error.ToString(), "Connection was dropped.");    
    });
    
    // subscribe on successfully disconnect event
    Trader.Disconnected += () => this.GuiAsync(() => ChangeConnectStatus(false));
    
    // subscribe on error event
    Trader.Error += error => this.GuiAsync(() => MessageBox.Show(this, error.ToString(), "Data processing error"));
    
    // new securities received
    Trader.NewSecurities += securities => this.GuiAsync(() => _securitiesWindow.AddSecurities(securities));
    
    // new own trades received
    Trader.NewMyTrades += trades => this.GuiAsync(() => _myTradesWindow.Trades.AddRange(trades));
    
    // new anonymous trades received
    Trader.NewTrades += trades => this.GuiAsync(() => _tradesWindow.Trades.AddRange(trades));
    
    // new orders received
    Trader.NewOrders += orders => this.GuiAsync(() => _ordersWindow.Orders.AddRange(orders));
    
    // new stop-orders received
    Trader.NewStopOrders += orders => this.GuiAsync(() => _stopOrdersWindow.Orders.AddRange(orders));
    
    // new positions received
    Trader.NewPositions += positions => this.GuiAsync(() => _positionsWindow.Positions.AddRange(positions));
    
    // subscribe on error of order registration event
    Trader.OrdersRegisterFailed += OrdersFailed;
    
    // subscribe on error of order cancelling event
    Trader.OrdersCancelFailed += OrdersFailed;
    
    // subscribe on error of stop-order registration event
    Trader.StopOrdersRegisterFailed += OrdersFailed;
    
    // subscribe on error of stop-order cancelling event
    Trader.StopOrdersCancelFailed += OrdersFailed;
  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#
    void SearchTextPreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
            return;
    
        var text = SearchText.Text.Trim();
    
        if (text.IsEmpty())
            return;
        MainWindow.Instance.Trader.LookupSecurities(new Security { Name = text });
    }
  6. To request updates on the portfolios you need to use the RegisterPortfolio method:

    C#
    // new portofolios (=accounts) received
    Trader.NewPortfolios += portfolios =>
    {
        // subscribe on portfolio updates
        portfolios.ForEach(Trader.RegisterPortfolio);
    
        _portfoliosWindow.Portfolios.AddRange(portfolios);
    };
  7. For orders registration the New Order button used. The RegisterOrder(Order) method sends an order to the server:

    C#
    private void SendClick(object sender, RoutedEventArgs e)
    {
        var order = new Order
        {
            Type = IsMarket.IsChecked == true ? OrderTypes.Market :OrderTypes.Limit,
            Portfolio = Portfolio.SelectedPortfolio,
            Volume = Volume.Text.To<decimal>(),
            Price = Price.Text.To<decimal>(),
            Security = Security,
            Direction = IsBuy.IsChecked == true ? OrderDirections.Buy :OrderDirections.Sell,
        };
    
        MainWindow.Instance.Trader.RegisterOrder(order);
        DialogResult = true;
    }