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 CqgContinuumTrader, CqgComTrader:

    C#
    Connector = new.CqgContinuumTrader { LogLevel = LogLevels.Debug };
    // Or
    Connector = new.CqgComTrader { LogLevel = LogLevels.Debug };
  2. Next, you should subscribe on required IConnector events:

    C#
    // subscribe on connection successfully event
    Connector.Connected += () =>
    {
        this.GuiAsync(() => OnConnectionChanged(true));
    };
    
    // subscribe on connection error event
    Connector.ConnectionError += error => this.GuiAsync(() =>
    {
        OnConnectionChanged(Connector.ConnectionState == ConnectionStates.Connected);
        MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2959);
    });
    
    Connector.Disconnected += () => this.GuiAsync(() => OnConnectionChanged(false));
    
    // subscribe on error event
    Connector.Error += error =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2955));
    
    // subscribe on error of market data subscription event
    Connector.MarketDataSubscriptionFailed += (security, msg, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2956Params.Put(msg.DataType, security)));
    
    Connector.NewSecurity += security => _securitiesWindow.SecurityPicker.Securities.Add(security);
    Connector.NewMyTrade += trade => _myTradesWindow.TradeGrid.Trades.Add(trade);
    Connector.NewTrade += trade => _tradesWindow.TradeGrid.Trades.Add(trade);
    Connector.NewOrder += order => _ordersWindow.OrderGrid.Orders.Add(order);
    Connector.NewStopOrder += order => _stopOrdersWindow.OrderGrid.Orders.Add(order);
    Connector.NewPortfolio += portfolio =>
    {
        _portfoliosWindow.PortfolioGrid.Portfolios.Add(portfolio);
    
        // subscribe on portfolio updates
        Connector.RegisterPortfolio(portfolio);
    };
    Connector.NewPosition += position => _portfoliosWindow.PortfolioGrid.Positions.Add(position);
    
    // subscribe on error of order registration event
    Connector.OrderRegisterFailed += _ordersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of order cancelling event
    Connector.OrderCancelFailed += OrderFailed;
    
    // subscribe on error of stop-order registration event
    Connector.StopOrderRegisterFailed += _stopOrdersWindow.OrderGrid.AddRegistrationFail;
    // subscribe on error of stop-order cancelling event
    Connector.StopOrderCancelFailed += OrderFailed;
    
    Connector.MassOrderCancelFailed += (transId, error) =>
        this.GuiAsync(() => MessageBox.Show(this, error.ToString(), LocalizedStrings.Str716));
  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 IConnector.NewPortfolio 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 FindClick(object sender, RoutedEventArgs e)
    {
        var wnd = new SecurityLookupWindow { Criteria = new Security { Code = "ES" } };
    
        if (!wnd.ShowModal(this))
            return;
    
        Connector.LookupSecurities(wnd.Criteria);
    }
  6. To get trades by the selected instrument you should call the RegisterTrades(Security) method:

    C#
    private void QuotesClick(object sender, RoutedEventArgs e)
    {
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            if (Connector.RegisteredSecurities.Contains(security))
            {
                Connector.UnRegisterSecurity(security);
                Connector.UnRegisterTrades(security);
            }
            else
            {
                Connector.RegisterSecurity(security);
                Connector.RegisterTrades(security);
            }
        }
    }

    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)
    {
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _quotesWindows.SafeAdd(security, key =>
            {
                // create order book window
                var wnd = new QuotesWindow
                {
                    Title = security.Id + " " + LocalizedStrings.MarketDepth
                };
                wnd.MakeHideable();
                return wnd;
            });
    
            if (window.Visibility == Visibility.Visible)
            {
                // unsubscribe from order book flow
                Connector.UnRegisterMarketDepth(security);
    
                window.Hide();
            }
            else
            {
                // subscribe on order book flow
                Connector.RegisterMarketDepth(security);
    
                window.Show();
            }
    
            if (!_initialized)
            {
                OnMarketDepthChanged(Connector.GetMarketDepth(security));
                Connector.MarketDepthChanged += OnMarketDepthChanged;
                _initialized = true;
            }
        }
    }

    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 = Connector,
            MarketDataProvider = Connector,
            Portfolios = new PortfolioDataSource(Connector)
        };
    
        if (newOrder.ShowModal(this))
            Connector.RegisterOrder(newOrder.Order);
    }