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
}