- you will add some textboxes, comboboxes, labels, numberupdown, to your form
- request and receive live streaming data bid, ask, and last prices
The videos are best viewed at the highest quality and full screen.
This KeyDown event requests the market data from the server.
click on the link below.
private void cbSymbol_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
// still works with fake account number
string myAccount = "U132458";
axTws1.cancelMktData(2);
axTws1.reqAccountUpdates(0, myAccount);
// Create a TagValueList object (for API version 9.71 users)
TWSLib.ITagValueList mktDataOptions = axTws1.createTagValueList();
// Create a new contract object
TWSLib.IContract ContractInfo = axTws1.createContract();
// Connection identifier
ContractInfo.conId = 0;
// Stock symbol
ContractInfo.symbol = cbSymbol.Text;
// Type Stock=STK,Option=OPT,Future=FUT, etc.
ContractInfo.secType = "STK";
// The Options or Futures expiration data in the format YYYYMMDD
ContractInfo.expiry = "";
// The Options Strike Price
ContractInfo.strike = 0;
// used for Options PUT or CALL
ContractInfo.right = "";
// The contract multiplier for Futures or Options not used for Stocks
ContractInfo.multiplier = "";
// The destination of order or request. "SMART" = IB order router
ContractInfo.exchange = "SMART";
// The primary exchange where the instrument trades.
// NYSE, NASDAQ, AMEX, BATS, ARCA, PHLX etc.
ContractInfo.primaryExchange = "NASDAQ";
// The currency of the exchange USD or GBP or CAD, etc.
ContractInfo.currency = "USD";
// Request the streaming market data for symbol in cbSymbol
// TickerID, ContractInfo, genericTicks and Snapshot (0 for streaming)
axTws1.reqMktDataEx(2, ContractInfo, "165, 233", 0);
// if you are using API version 9.71 you need a fifth parameter
// axTws1.reqMktDataEx(2, ContractInfo, "165, 233", 0, mktDataOptions);
//timer1.Start();
cbSymbol.SelectAll();
axTws1.reqAccountUpdates(1, myAccount); // requests account updates
//AddSymbol();
}
}
This event gets the bid ask and last price from the server
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if ((e.tickType) == 2) // 2 = Ask
{
// Gets the Ask price and formats it to 2 decimals
// sends the result to the Ask textbox
double theAsk = e.price;
int decimalPlaces = 2;
theAsk = theAsk * Math.Pow(100, decimalPlaces);
theAsk = Math.Truncate(theAsk);
theAsk = theAsk / Math.Pow(100, decimalPlaces);
string askPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theAsk);
tbAsk.Text = askPrice;
}
else if ((e.tickType) == 1) // 1 = Bid
{
// Gets the Bid price and formats it to 2 decimals
// sends the result to the Bid textbox
double theBid = e.price;
int decimalPlaces = 2;
theBid = theBid * Math.Pow(100, decimalPlaces);
theBid = Math.Truncate(theBid);
theBid = theBid / Math.Pow(100, decimalPlaces);
string bidPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theBid);
tbBid.Text = bidPrice;
}
if ((e.tickType) == 4) // 4 = Last
{
// Gets the Last price and formats it to 2 decimals
// sends the result to the Last text box
double theLast = e.price;
int decimalPlaces = 2;
theLast = theLast * Math.Pow(100, decimalPlaces);
theLast = Math.Truncate(theLast);
theLast = theLast / Math.Pow(100, decimalPlaces);
string lastPrice = string.Format("{0:N" + Math.Abs(decimalPlaces) + "}", theLast);
tbLast.Text = lastPrice;
}
}
This event changes the character from lowercase to uppercase
private void cbSymbol_KeyPress(object sender, KeyPressEventArgs e)
{
// changes lowercase to uppercase
if (char.IsLower(e.KeyChar))
{
e.KeyChar = char.ToUpper(e.KeyChar);
}
}
No comments:
Post a Comment