Click or drag to resize

Getting started

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

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

    C#
    Trader = new CQGTrader { LogLevel = LogLevels.Debug };
  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. Then you need to connect to the CQG server:

    C#
    Trader.Connect();
  4. When the connection is successful, you can expect to trigger the IConnectorNewPortfolios 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):

    C#
    private void Ok_Click(object sender, RoutedEventArgs e)
    {
        var criteria = new Security
        {
            Code = SecCode.Text,
            Type = SecType.SelectedType
        };
    
        MainWindow.Instance.Trader.LookupSecurities(criteria);
        DialogResult = true;
    }
  6. To get trades by the selected instrument you should call the RegisterTrades(Security) method:

    C#
    private void TradesClick(object sender, RoutedEventArgs e)
    {
       TryInitialize();
    
       var window = _tradesWindows.SafeAdd(SelectedSecurity, security =>
       {
         // create tick trades window
         var wnd = new TradesWindow { Title = security.Code + " 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 IConnectorNewTrades 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;
    
        var window = _quotesWindows.SafeAdd(SelectedSecurity, security =>
        {
            // 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();
    }

    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#
    void NewOrderClick(object sender, RoutedEventArgs e)
    {
        var newOrder = new OrderWindow
        {
            Order = new Order { Security = SecurityPicker.SelectedSecurity },
            Connector = MainWindow.Instance.Trader,
        };
    
        if (newOrder.ShowModal(this))
            MainWindow.Instance.Trader.RegisterOrder(newOrder.Order);
    }