- send a LMT = limit order
- change the up down arrow in the limit box to move up 10 cents at a time
- get the next valid id
- create Buy and Sell buttons
Copy and paste the highlighted text and the text.
public void DoOrder(ref int myShares, ref string orderType, ref string myAction)
{
// myAction represent BUY or SELL
// orderType represents MKT, LMT etc...
// myShares is number of shares to buy or sell
int shares = Convert.ToInt32(this.numQuantity.Value);
double iShares = System.Math.Abs(shares);
// Create a new contract object
TWSLib.IContract ContractInfo = axTws1.createContract();
// Create a new Order object
TWSLib.IOrder OrderInfo = axTws1.createOrder();
// Fill the ContractInfo object with the necessary information
// Connection identifier
ContractInfo.conId = 0;
// Ticker symbol for the order
ContractInfo.symbol = this.cbSymbol.Text;
ContractInfo.secType = "STK";
ContractInfo.exchange = this.cbMarket.Text;
ContractInfo.primaryExchange = this.tbPrimaryExchange.Text;
ContractInfo.currency = "USD";
// Fill in the Order information:
// orderId is a *Unique* order identifier
OrderInfo.orderId = order_id;
// action will be BUY, SELL or SSHORT but I never used SSHORT
OrderInfo.action = myAction;
// totalQuantity is the total number of shares (or contracts)
OrderInfo.totalQuantity = myShares;
// orderType is one of MKT, LMT, STP, etc.
OrderInfo.orderType = orderType;
// auxPrice used for STP orders
// This is the STOP price for stop-limit orders, and the offset amount for relative orders.
// In all other cases, specify zero.
// OrderInfo.auxPrice = 0
// lmtPrice is the limit price for a limit order
// This is the LIMIT price, used for limit, stop-limit and relative orders.
// In all other cases specify zero. For relative orders with no limit price, also specify zero.
OrderInfo.lmtPrice = double.Parse(this.numLimitPrice.Text);
// timeInForce is either DAY, GTC, IOC, GTD etc.
OrderInfo.timeInForce = cbTIF.Text;
// triggerMethod
// Specifies how Simulated Stop, Stop-Limit and Trailing Stop orders are triggered. Valid values are:
//0 - The default value. The "double bid/ask" function will be used for orders for OTC stocks and US options.
// All other orders will used the "last" function.
//1 - use "double bid/ask" function, where stop orders are triggered based on two consecutive bid or ask prices.
//2 - "last" function, where stop orders are triggered based on the last price.
//3 double last function.
//4 bid/ask function.
//7 last or bid/ask function.
//8 mid-point function.
// displaySize
//OrderInfo.displaySize = int.Parse(this.tbVisible.Text);
// OrderInfo.outsideRth = myOutsideValue; // 1 = true and 0 = false
// There are about 40 other properties for different order types...
// We do not need to specify them in this case
// Submit the order. Parameters are
// OrderId - A Unique identifier for each order
// Contract - An object specifying the security to be ordered
// OrderInfo - An object specifying the type, quantity, side and other order parameters
this.axTws1.placeOrderEx(order_id, ContractInfo, OrderInfo);
// Changes the order type back to limit
cbOrderType.Text = "LMT";
order_id++;
}
private void numLimitPrice_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Control)
numLimitPrice.DecimalPlaces = 2;
numLimitPrice.Increment = 0.10M;
}
private void axTws1_nextValidId(object sender, AxTWSLib._DTwsEvents_nextValidIdEvent e)
{
// The next id will be held in e.id
order_id = e.id;
}
private void numLimitPrice_KeyUp(object sender, KeyEventArgs e)
{
numLimitPrice.DecimalPlaces = 2;
numLimitPrice.Increment = 0.01M;
}
private void btnSell_Click(object sender, EventArgs e)
{
string myAction = "SELL";
int myShares = Convert.ToInt32(numQuantity.Text);
string orderType = cbOrderType.Text;
if (cbOrderType.Text == "LMT")
{
// Calls the methond DoOrder and passes on the variables
// myShares, orderType, and myAction values
DoOrder(ref myShares, ref orderType, ref myAction);
}
}