Request realtimebars IB API

UniversalGoldmine

Junior member
Messages
26
Likes
0
I am trying to request realtime data with IB API.

What I want to request is the Real-Time values of the Daily Bar:
* Close
* Net Change
* Bid
* Ask
* High
* Low
* Todays Volume

I have begun some code but are not sure how to complete it for MSFT in this example. I know that I will need to call an event.
I have tried to set it up like in the code below, but the close value does not appear in the ListBox. I wonder what I can be missing out ?

Thank you...
Code:
	private AxTWSLib.AxTws Tws1;            

	Tws1 = new AxTWSLib.AxTws();
            Tws1.BeginInit();
            Tws1.Enabled = true;
            Tws1.Location = new System.Drawing.Point(32, 664);
            Tws1.Name = "Tws1";
            Controls.Add(Tws1);
            Tws1.EndInit();

            Tws1.connect("127.0.0.1", 7496, 0);


            Tws1.reqRealTimeBars(1, "MSFT", "STK", "", 0, "", "", "SMART", "", "USD", 0, 5, "TRADES", 1);
            Tws1.realtimeBar += new AxTWSLib._DTwsEvents_realtimeBarEventHandler(this.realtimeBar);

        private void realtimeBar(object sender, AxTWSLib._DTwsEvents_realtimeBarEvent e)
        {
            String msg = e.close.ToString();
            listBox1.Items.Add(msg);
        }
 
Last edited:
If you want real time 'tick' data, you need to call something like reqMktData (Java API). The real time bars stuff was an added in kludge to get the high and low of previous bar in case the high and/or low was missed due to the aggregation of "tick" data. You don't meed it to start with - get the streaming tick data working first. You may never need it.
 
Thanks dcraig1, I am happy for your response.

I understand, so I should use the reqMktData. I have tried to set it up like below but when running the code after I have connected to TWS, nothing happens. Perheps I have the arguments wrong in the reqMktData.

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            [B]//Connected to TWS[/B]            
            Tws1.reqMktData(1, "MSFT", "STK", "", 0, "", "", "smart", "", "USD", "5", 0);
            Tws1.tickPrice += new AxTWSLib._DTwsEvents_tickPriceEventHandler(this.tickData);
        }
        private void tickData(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
        {
            String msg = "Price Update: Id=" + e.id + " Type: " + e.tickType + " Price: " + e.price; 
            listBox1.Items.Add(msg);
        }
 
Last edited:
Try using an empty string ("") for your genericTicklist parameter to the reqMktData() function.
 
Are you connected to TWS?

Make sure you implement the equivalent of:

Code:
public void error(int id, int errorCode, String errorMsg)
{
    System.out.println (id + " " + errorCode + " " + errorMsg);
}

It should tell you what is going on.
 
Yes, I am connected to TWS, I have the ability to request historical data so the connection do works anyway.
label2 in the below code shows the string that connection was succesful.
I tried to implement the Error function but no messagebox is shown. The complete code that I have is this code:

Code:
        private AxTWSLib.AxTws Tws1;
        private void button1_Click_1(object sender, EventArgs e)
        {
            Tws1 = new AxTWSLib.AxTws();
            Tws1.BeginInit();
            Tws1.Enabled = true;
            Tws1.Location = new System.Drawing.Point(32, 664);
            Tws1.Name = "Tws1";
            Controls.Add(Tws1);
            Tws1.EndInit();
            Tws1.connect("127.0.0.1", 7496, 0);
            String msg = "Connected to TWS server version " + Tws1.serverVersion + "at " + Tws1.TwsConnectionTime;
            label2.Text = msg; //Connected to TWS


            Tws1.reqMktData(1, "MSFT", "STK", "", 0, "", "", "smart", "", "USD", "", 0);
            Tws1.tickPrice += new AxTWSLib._DTwsEvents_tickPriceEventHandler(this.tickData);
        }
        private void tickData(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
        {
            String msg = "Price Update: Id=" + e.id + " Type: " + e.tickType + " Price: " + e.price; 
            listBox1.Items.Add(msg);
        } 
        public void error(int id, int errorCode, String errorMsg)
        {
            MessageBox.Show(id + " " + errorCode + " " + errorMsg);
        }
 
Top