Click or drag to resize

Getting started

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

OECTrader Main Window
  1. First, you need to create an instance of the OpenECryTrader:

    C#
    Trader = new OpenECryTrader {
        Login = Login.Text, 
        Password = Password.Password,
        OECConnectionAddress = address,
        OECConnectionPort = port,
        EnableOECLogging = true
    };
  2. Next, you should 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 portofolios (=accounts) received
    Trader.NewPortfolios += portfolios =>
    {
        // subscribe on portfolio updates
        portfolios.ForEach(Trader.RegisterPortfolio);
    
        _portfoliosWindow.Portfolios.AddRange(portfolios);
    };
    
    // 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. Finally, you need to connect to OEC server:

    C#
    Trader.Connect();

    Once connected, you can expect to trigger events:

  4. Instruments search (in more detail):

    OECTrader Security
    C#
    private void SearchText_PreviewKeyUp(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 { Code = text });
    }
  5. For updates on the selected instrument and ticks you need to use the RegisterSecurity(Security) and RegisterTrades(Security) methods, respectively:

    C#
    private void BidAsk_Click(object sender, RoutedEventArgs e)
    {
        var security = SelectedSecurity;
        var trader = MainWindow.Instance.Trader;
    
        if (_bidAskSecurities.Contains(security))
        {
            // unsubscribe from level1 flow
            trader.UnRegisterSecurity(security);
            trader.UnRegisterTrades(security);
    
            _bidAskSecurities.Remove(security);
        }
        else
        {
            // subscribe on level1 flow
            trader.RegisterSecurity(security);
            trader.RegisterTrades(security);
    
            _bidAskSecurities.Add(security);
        }
    }
  6. To request updates on the order books you need to use the RegisterMarketDepth(Security) method:

    C#
    private void Quotes_Click(object sender, RoutedEventArgs e)
    {
        var window = _quotesWindows.SafeAdd(SelectedSecurity, security =>
        {
            // subscribe on order book flow
            MainWindow.Instance.Trader.RegisterMarketDepth(security);
    
            // create order book window
            var wnd = new QuotesWindow { Title = security.Code + " quotes" };
            wnd.MakeHideable();
            return wnd;
        });
    
        if (window.Visibility == Visibility.Visible)
            window.Hide();
        else
            window.Show();
    }
  7. For portfolios updates the RegisterPortfolio(Portfolio) method is used:

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

    C#
    private void Send_Click(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;
    }