Build a Trading Platform in csharp Part 3 send order. I added three buttons one for disconnecting from the server, a sell button and a buy button. Added a checkbox for trading premarket and after hours. Click events for the textboxes of bid ask and last, also a timer and a text box to hold the next valid id.
# | Control | Name | Text or Value |
---|---|---|---|
1 | Textbox |
tbValid_Id | 0 |
2 | Button | btnDisconnect |
Disconnect |
3 | Button |
btnSell | SELL |
4 | Button |
btnBuy |
BUY |
5 | Checkbox |
chkOutside |
OutsideRTH |
6 | Timer |
Timer1 |
100 |
add the highlighted code to the Form1.cs file in the appropriate location
// This delegate enables asynchronous calls for setting
// the text property on a ListBox control.
delegate void SetTextCallback(string text);
delegate void SetTextCallbackTickPrice(string _tickPrice);
int order_id = 0;
int timer1_counter = 5;
// Set up the form object in the EWrapper
ibClient.myform = (Form1)Application.OpenForms[0];
// gets the next order id and puts it in the textbox
tbValid_Id.Text = ibClient.NextOrderId.ToString();
// updates the order_id value
order_id = ibClient.NextOrderId;
public void btnDisconnect_Click(object sender, EventArgs e)
{
ibClient.ClientSocket.eDisconnect();
}
public void send_order(string side)
{
// Create a new contract to specify the security we are searching for
IBApi.Contract contract = new IBApi.Contract();
// Set the underlying stock symbol from the cbSymbol combobox
contract.Symbol = cbSymbol.Text;
// Set the Security type to STK for a Stock
contract.SecType = "STK";
// Use "SMART" as the general exchange
contract.Exchange = cbMarket.Text;
// Set the primary exchange (sometimes called Listing exchange)
// Use either NYSE or ISLAND
contract.PrimaryExch = "ISLAND";
// Set the currency to USD
contract.Currency = "USD";
IBApi.Order order = new IBApi.Order();
// gets the next order id from the text box
order.OrderId = order_id;
// gets the side of the order (BUY, or SELL)
order.Action = side;
// gets order type from combobox market or limit order(MKT, or LMT)
order.OrderType = cbOrderType.Text;
// number of shares from Quantity
order.TotalQuantity = Convert.ToDouble(numQuantity.Value);
// Value from limit price
order.LmtPrice = Convert.ToDouble(numPrice.Value);
// checks for a stop order
if (cbOrderType.Text == "STP")
{
// Stop order value from the limit textbox
order.AuxPrice = Convert.ToDouble(numPrice.Value);
}
//Visible shares to the market
order.DisplaySize = Convert.ToInt32(tbVisible.Text);
//order.OutsideRth = cbOutsideRTH.Checked;
order.OutsideRth = chkOutside.Checked;
// Place the order
ibClient.ClientSocket.placeOrder(order_id, contract, order);
// increase the order id value
order_id++;
tbValid_Id.Text = Convert.ToString(order_id);
}
private void tbAsk_Click(object sender, EventArgs e)
{
numPrice.Value = Convert.ToDecimal(tbAsk.Text);
}
private void tbBid_Click(object sender, EventArgs e)
{
numPrice.Value = Convert.ToDecimal(tbBid.Text);
}
private void tbLast_Click(object sender, EventArgs e)
{
numPrice.Value = Convert.ToDecimal(tbLast.Text);
}
Add the highlighted code to the bottom of the getData() function it is used to start the timer.
ibClient.ClientSocket.reqMktData(1, contract, "", false, false, mktDataOptions);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
if (timer1_counter == 0)
{
timer1.Stop(); // stop the timer
//add the bid price to the limit box
numPrice.Value = Convert.ToDecimal(tbBid.Text);
timer1_counter = 5;// resets timer counter back to 5
}
timer1_counter--; // subtract 1 every time their is a tick
}
Hi Ken
ReplyDeleteReally enjoying watching your videos and the API examples. I have no experience of either C# or the API, however, you have helped me to understand both to a greater degree and I am really enjoying getting into coding.
One question I have with the above example, how/where is the BID and ASK prices being automatically updated, is there an update method that you are using in the ewrapper calss that updates the text boxes ?
Regards
Dinesh
Thanks Dinesh You get the prices from the EWrapperImpl.cs file in the method tickPrice from there you have to send that to the Form1.cs file. You will have to be subcribed and have an account with Interactive Brokers to get the live data, if not you can get the delayed data. Check out the second video in the series part 2 and also the blog I posted about streaming data. Sorry I missed your comment.
ReplyDeleteHere are the links to the video and the blog post
Deletehttps://sharpertradingimage.blogspot.com/2021/01/updated-trading-platform-in-c-streaming.html
https://www.youtube.com/watch?v=PG7CFl_TtWg
Hi Ken
ReplyDeleteThanks for the update. Been watching your videos and its really helped me. I now have a good understanding of the API and my C# has improved a fair amount thanks to these bogs.
Im now trying to code my own watch list and historical data graphs based on your later tutorials.
Been fantastic, keep up the great work.
Good to hear you are finding my material useful and you are learning. This will give me the motivation to do more videos. Awesome Dino!
ReplyDelete