OK, sorry I got sidetracked for a few days there but back on task now. Here is the code with comments for my complleted IB form to connect and get data for one symbol. This is from the form1.cs tab in my VS2005. None of the code in any other files (or tabs) was altered by me. If anyone tries to copy and past this into their Visual Studio, just mind the label and button names. Some I gave descriptive names to, but some I just left with the auto generated names. So the bulk of this could be copied over, but make sure to change button and label references to the names of your buttons and labels. Anything afte the double backslashes are comments and ignored by the program.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace IBTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Next line created automatically by clicking on the menu item in form designer.
private void connectIBToolStripMenuItem_Click(object sender, EventArgs e)
//This line needs to be typed out.
{
axTws1.connect("localhost", 7496, 0);
}
//Button1 is the request mkt data button. I was lazy and didn't give my buttons or labels descriptive names.
//Clicking on the button creates this line of code.
private void button1_Click(object sender, EventArgs e)
//These next two lines had to be typed out.
{
double strike = new double();
axTws1.reqMktData(Convert.ToInt16(tbxSymID.Text),tbxSymbol.Text, tbxType.Text, tbxExpiration.Text, strike, "", "",tbxExchange.Text, "", tbxCurrency.Text, "");
}
//This line is created by clicking on the TWS control on the form, finding the events
//and clicking on the tickPrice event.
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
//These next few lines had to be typed. First, I create the variables to hold the price info.
//Then we check that the symbol ID on the tickPrice event matches the Symbol ID of the symbol.
//With only one symbol that we have requested data for, of course it will match,
//but I guess this will be more important once I add more symbols.
//Then we check the tickType. The IB API docs give the # that will be returned
//if the tick is a bid, ask, last, etc. So basically we ceck that it matches the symbol
//, check what type of tick it is, then assign it to the proper variable and label.
//the ("0.00") is just to format the return value with two decimal places. I think out of everything here
//that took me the longest to figure out.
{
double last = new double();
double bid = new double();
double ask = new double();
if ((e.id == Convert.ToInt16(tbxSymID.Text)) && (e.tickType == 4))
{
last = e.price;
lblLast.Text = last.ToString("0.00");
}
if ((e.id == Convert.ToInt16(tbxSymID.Text)) && (e.tickType == 1))
{
bid = e.price;
lblBid.Text = bid.ToString("0.00");
}
if ((e.id == Convert.ToInt16(tbxSymID.Text)) && (e.tickType == 2))
{
ask = e.price;
lblAsk.Text = ask.ToString("0.00");
}
}
//This line was auto generated by clicking on the tickSize event.
private void axTws1_tickSize(object sender, AxTWSLib._DTwsEvents_tickSizeEvent e)
//And these lines do the same as the the code above except that
//here it is for the bid and ask size instead of prices.
{
double BidSize = new double();
double AskSize = new double();
if ((e.id == Convert.ToInt16(tbxSymID.Text)) && (e.tickType == 0))
{
BidSize = e.size;
lblBsize.Text = BidSize.ToString();
}
if ((e.id == Convert.ToInt16(tbxSymID.Text)) && (e.tickType == 3))
{
AskSize = e.size;
lblAsize.Text = AskSize.ToString();
}
}
//This line auto added by clicking the stop mkt data button.
private void button2_Click(object sender, EventArgs e)
//These lines were typed. We first cancel the mkt data request and then set the labels
//all to blank.
{
axTws1.cancelMktData(Convert.ToInt16(tbxSymID.Text));
lblBid.Text = "";
lblBsize.Text = "";
lblLast.Text = "";
lblAsk.Text = "";
lblAsize.Text = "";
}
}
}
<img src="http://elitetrader.com/vb/attachment.php?s=&postid=1421842">