Click or drag to resize

Getting started

The application SampleIQFeed shows an example of working with IQFeedTrader as shown in the figure below. The example source code is in the Samples/IQFeed folder of the installation package.

Iq Feed Sample
  1. First, you need to create an instance of the IQFeedTrader:

    C#
    Trader = new IQFeedTrader();
  2. Next, you should subscribe on required IQFeedTrader events:

    C#
    // subscribe on connection successfully event
    Trader.Connected += () =>
    {
        Trader.RegisterNews();
    
        // update gui labes
        this.GuiAsync(() => ChangeConnectStatus(true));
    };
    Trader.Disconnected += () => this.GuiAsync(() => ChangeConnectStatus(false));
    
    // subscribe on connection error event
    Trader.ConnectionError += error => this.GuiAsync(() =>
    {
        // update gui labes
        this.GuiAsync(() => ChangeConnectStatus(false));
    
        MessageBox.Show(this, error.ToString(), LocalizedStrings.Str2959);
    });
    
    // 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.NewNews += news => _newsWindow.NewsGrid.News.Add(news);
  3. Then you need to establish the connection:

    C#
    Trader.Connect();
  4. The subscription to the news in the event handler will be done after a successful connection:

    C#
    Trader.RegisterNews();

    After this operation the following events should be expected:

    1. SecuritiesChanged – instrument change (last price, best bid/ask price, etc.).
    2. MarketDepthsChanged – order book change, requested through RegisterMarketDepth(Security) method.
    3. NewTrade – new ticks on instruments subscribed by using RegisterTrades(Security) method.
    4. NewNews – news.
  5. Instruments search (in more detail):

    IQFeed Find Security
    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. For updates on the selected instrument and ticks you need to use the RegisterSecurity(Security) and RegisterTrades(Security) methods, respectively:

    C#
    private void Level1Click(object sender, RoutedEventArgs e)
    {
        TryInitialize();
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _level1Windows.SafeAdd(security, s =>
            {
                // create level1 window
                var wnd = new Level1Window
                {
                    Title = security.Code + " level1"
                };
    
                // subscribe on level1
                MainWindow.Instance.Trader.RegisterSecurity(security);
    
                wnd.MakeHideable();
                return wnd;
            });
    
            if (window.Visibility == Visibility.Visible)
                window.Hide();
            else
                window.Show();
        }
    }
  7. To request updates on the order books you need to use the RegisterMarketDepth(Security) method:

    C#
    private void DepthClick(object sender, RoutedEventArgs e)
    {
        var trader = MainWindow.Instance.Trader;
    
        foreach (var security in SecurityPicker.SelectedSecurities)
        {
            var window = _quotesWindows.SafeAdd(security, s =>
            {
                // 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();
    
            TryInitialize();
    
            TraderOnMarketDepthChanged(trader.GetMarketDepth(security));
        }
    }
  8. For the trade history getting on the specified date the IQFeedTraderGetHistoricalLevel1(SecurityId, DateTimeOffset, DateTimeOffset, Boolean) method should be used:

    IQFeed History Trades
    C#
    var ticks = MainWindow.Instance.Trader.GetHistoricalLevel1(_security.ToSecurityId(), date, date.AddDays(1), out isSuccess);
    
    this.GuiAsync(() =>
    {
        BusyIndicator.IsBusy = false;
    
        L1Grid.Messages.Clear();
        L1Grid.Messages.AddRange(ticks);
    });
  9. For the candles history on instrument getting on the specified period the IQFeedTraderGetHistoricalCandles(Security, Type, Object, Int64, Boolean) method should be used:

    IQFeed History Candles
    C#
    var messages = MainWindow.Instance.Trader.GetHistoricalCandles(_security, typeof(TimeFrameCandle), (TimeSpan)TimeFramePicker.SelectedValue, (DateTime)DateFromPicker.Value, (DateTime)DateToPicker.Value, out isSuccess);
    Chart.Reset(new[] { _candlesElem });
    foreach (var message in messages)
        Chart.Draw(_candlesElem, message.ToCandle(_security));