- Place order to buy or sell.
- Add a buy, and sell button.
- Get the nextValidId, and increase the next valid id by 1 every time an order to buy or sell is placed.
Name = btnBuy - Text = BUY
Name = btnSell - Text = SELL
Private Sub btnSell_Click(sender As Object, e As EventArgs) Handles btnSell.Click
Dim strAction = "SELL"
'calls the order method to send the order to SELL
Call DoOrder(strAction)
End Sub
Private Sub btnSell_Click(sender As Object, e As EventArgs) Handles btnBuy.Click
Dim strAction = "BUY"
'calls the order method to send the order to SELL
Call DoOrder(strAction)
End Sub
Private Sub DoOrder(strAction As String)
' variable to hold the contract information
Dim ContractInfo As TWSLib.IContract
' variable to hold the order information
Dim OrderInfo As TWSLib.IOrder
' Create a contract to represent the data we request
ContractInfo = AxTws1.createContract()
' Create new order Object
OrderInfo = AxTws1.createOrder()
' Now fill the ContractInfo object with the necessary information
ContractInfo.conId = 0 ' identifier
' Stock symbol
ContractInfo.symbol = cbSymbol.Text
' Security Type: Stock=STK, Option=OPT, Future=FUT, etc.
ContractInfo.secType = "STK"
' The destination of order or request. "SMART" =IB order router
ContractInfo.exchange = "SMART"
' The primary exchange where the instrument trades.
ContractInfo.primaryExchange = "NASDAQ"
' The currency USD or GBP or CAD or EUR, etc.
ContractInfo.currency = "USD"
' orderId is a *Unique* order identifier
OrderInfo.orderId = orderId
' action will be BUY, SELL or SSHORT
OrderInfo.action = strAction
'totalQuantity is the number of shares (or contracts)
OrderInfo.totalQuantity = Integer.Parse(numQuantity.Value)
'orderType is one of the following MKT, LMT, STP, etc.
OrderInfo.orderType = cbOrderType.Text
'lmtPrice is the limit price for a limit order
OrderInfo.lmtPrice = numPrice.Value
'timeInForce is either DAY, GTC, IOC etc.
OrderInfo.timeInForce = cbTIF.Text
' There are many other properties for different order types...
' We don't need to specify them in this case
' Submit the order. Parameters are
' OrderId - A different 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
AxTws1.placeOrderEx(orderId, ContractInfo, OrderInfo)
'increases the order id by 1
orderId += 1
End Sub
Make sure you add a public variable just under Public Class Form1 near the top of the class
Public orderId As Integer = 0
nextValidId Event Private Sub AxTws1_nextValidId(sender As Object, e As AxTWSLib._DTwsEvents_nextValidIdEvent) Handles AxTws1.nextValidId
' sets the next order id number
orderId = e.id
End Sub
Click Event Private Sub tbBid_Click(sender As Object, e As EventArgs) Handles tbBid.Click
'set the Bid price in the limit price text box
numPrice.Value = Double.Parse(tbBid.Text)
End Sub
Click Event Private Sub tbAsk_Click(sender As Object, e As EventArgs) Handles tbAsk.Click
'set the Ask price in the limit price text box
numPrice.Value = Double.Parse(tbAsk.Text)
End Sub
ib api ib api ib api placeorder ib api ib api ib abi ib api ib api ib api ib api ib api ib api placeorder ib api ib api ib api ib api ib api
ib api ib api send order ib api ib api ib api ib abi ib api ib api ib api ib api ib api ib api ib api ib api ib api ib api ib api ib api
No comments:
Post a Comment