IB Java API: Extracting ticker data (real time bars) for multiple contracts
<p>I'm doing some self-learning and experimentation with algorithmic trading and the IB API. I decided to use Java but I'm open to switching to C++. I went through an online tutorial that walks you through the code shown below but was wondering about extending it past just one stock. I want to go through all SP500 stocks and check ticker data to make decisions based on that.</p>

<p>The code below will create a contract for and get data for Microsoft but I'd like to get data for all 500 stocks. All of the other methods defined in the EWrapper interface were left out of the post for more ease of readability. </p>

<p>I'm thinking that I need to store the ticker symbols in a file, parse this, and add each contract one by one to a vector. However, I'm not sure about how to monitor the data after that. It would be nice if I could just sequentially loop through each ticker and make a request for data but I believe the stream is processed on an asynchronous thread (correct me if wrong.) </p>

<p><strong>So how do I go through all 500 stocks and check their ticker data?</strong></p>

<p>Code snippets and explanations would be appreciated. Thanks!</p>

<pre><code>// Import Java utilities and Interactive Brokers API                                            
import java.util.Vector;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TagValue;
import com.ib.client.CommissionReport;
import com.ib.client.UnderComp;

// RealTimeBars Class is an implementation of the                                               
// IB API EWrapper class                                                                        
public class RealTimeBars implements EWrapper
{
    // Keep track of the next ID                                                                
    private int nextOrderID = 0;
    // The IB API Client Socket object                                                          
    private EClientSocket client = null;

    public RealTimeBars ()
    {
        // Create a new EClientSocket object                                                    
        client = new EClientSocket (this);
        // Connect to the TWS or IB Gateway application                                         
        // Leave null for localhost                                                             
    // Port Number (should match TWS/IB Gateway configuration                               
        client.eConnect (null, 7496, 0);

        // Pause here for connection to complete                                                
    try
            {
                // Thread.sleep (1000);                                                         
                while (! (client.isConnected()));
            } catch (Exception e) {
            e.printStackTrace ();

        };
        // Create a new contract                                                                
        Contract contract = new Contract ();
        contract.m_symbol = "MSFT";
        contract.m_exchange = "SMART";
        contract.m_secType = "STK";
    contract.m_primaryExch = "NASDAQ";
        contract.m_currency = "USD";
        // Create a TagValue list                                                               
        Vector&lt;TagValue&gt; realTimeBarsOptions = new Vector&lt;TagValue&gt;();
        // Make a call to start off data retrieval                                              
        client.reqRealTimeBars(0, contract,
                               5,            // Bar Size 5 seconds                              
                               "TRADES",     // whatToShow                                      
                               false,         // useRTH                                         
                               realTimeBarsOptions);
        // At this point our call is done and any market data events                            
        // will be returned via the realtimeBar method                                          

    } 

public static void main (String args[])
{
    try
        {
            // Create an instance                                                           
            // At this time a connection will be made                                       
    // and the request for market data will happen                                  
            RealTimeBars myData = new RealTimeBars();
        }
    catch (Exception e)
        {
            e.printStackTrace ();
        }
}    
</code></pre>

<p>}</p>