开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

twsapi: Re: VB Sample Project: BEER TIME


marinindextrader
 

I am going to have a beer now.

Thanks to whoever brought up the whole order id question....

I needed to start working on this and his question forced my day to
focus on and understand the order process...

Scott <----------tossing back a Boddingtion Ale



--- In twsapi@y..., "Bruce Hawkins" <hawkinsk001@h...> wrote:
Thanks once again Scott,

Bruce



----- Original Message -----
From: marinindextrader
To: twsapi@y...
Sent: Sunday, June 23, 2002 10:37 AM
Subject: twsapi: Re: VB Sample Project: How To Place An Order
[Code]


Put a Text1 textbox on your form.
Put a Command1 command button on your form
Add a Simple BAS Module

Stick this in the Form: (and the rest in the Module)

Private Sub Command1_Click()

'here I have just plugged in literal values for the example
'of course you could pull these in as variables or control
properties
'such as a combo box text or what not...
Call PlaceNewOrder("BUY", 1, "ES", "FUT", "200209",
0, "", "GLOBEX", "", "MKT", 0, 0)
End Sub

Private Sub Form_Load()

'connection call
Tws1.Connect "", 7496

'initialize your module variables
Call InitVarsNewOrderMod
Text1 = "PLACE ORDER DEMO:" & vbCrLf & vbCrLf & _
"When you click Command1, a market order will fire
off
for 1 Contract of September " & _
"ES. Keep clicking it and it will keep buying one at
market. Each time you click, " & _
"This box will fill with the order information stored
in
your array"

End Sub


'and use this for when you log out and relog in
'you can conditionally test next valid id in the module

Private Sub Tws1_nextValidId(ByVal id As Long)

'call a module level Sub that captures the IB id on log in
Call CaptureID(id)


End Sub

'now create a private sub that will handle the order routines
'the array captures the order info and stores it for you
'good idea IMHO to internally track this info and not rely
'on IB soley

Private Sub PlaceNewOrder(ByVal action As String, ByVal quantity
As
Long, _
ByVal symbol As String, ByVal secType As String, ByVal expiry
As
String, _
ByVal strike As Double, ByVal right As String, ByVal exchange
As
String, _
ByVal curency As String, ByVal orderType As String, ByVal
lmtPrice
As Double, _
ByVal auxPrice As Double)

Dim mirror(12) As Variant 'left this as zero based for clarity

Dim newID As Long

'assign all of the arguements to the array
'not the conspicuous absence of id in the arguements

mirror(1) = action: mirror(2) = quantity: mirror(3) = symbol
mirror(4) = secType: mirror(5) = expiry: mirror(6) = strike:
mirror(7) = right: mirror(8) = exchange: mirror(9) = curency:
mirror(10) = orderType: mirror(11) = lmtPrice: mirror(12) =
auxPrice

'pass the array out to the module
newID = Get_Set_NewOrderID(mirror)


'place the order
Call Tws1.placeOrder(newID, action, quantity, symbol, secType,
expiry, strike, right, exchange, _
curency, orderType, price, auxPrice)


End Sub

'MODULE CODE STARTS HERE

Option Explicit

Private m_arrOrders() As Variant 'master array
Private m_myID As Long 'your starting point
Private m_theirID As Long 'thier starting point
Private m_nextID 'next id your going to use
Private m_incrament As Integer 'a variable to incrament by any
value;
1, 5, 10 etc
'


'call on form load
Sub InitVarsNewOrderMod()

m_myID = 3030 'set your starting level here

ReDim m_arrOrders(12, 0)

m_incrament = 10 'optional to incrament by other than 1

End Sub


Sub CaptureID(ByVal theirID As Long)

'capture theirs at log on
m_theirID = theirID


End Sub

'store your order info and give an id back to the calling sub
'this is the workhorse

Function Get_Set_NewOrderID(ByVal mirror As Variant) As Long

Dim iTemp As Integer

'find the proper id; this looks a little convaluted but stare
at it
'you will see that if the next available IB id is greater than
your starting
'point, it will back up one then add your incrament
'otherwise it just keeps ripping out new id's per your
incrament
If m_theirID < m_myID Then
m_nextID = m_myID
m_myID = m_myID + m_incrament
Else
m_myID = m_theirID - 1 + m_incrament
m_nextID = m_myID
m_myID = m_myID + m_incrament
End If

'assign the id to the first element of the array
m_arrOrders(0, UBound(m_arrOrders, 2)) = m_nextID

'assign the mirror to the rest of the array
For iTemp = 1 To UBound(m_arrOrders)
m_arrOrders(iTemp, UBound(m_arrOrders, 2)) = mirror(iTemp)
Next iTemp

'redim and preserve your array in preperation for the next
order
ReDim Preserve m_arrOrders(12, UBound(m_arrOrders, 2) + 1)

'display your array in the text box
Call DisplayArray

'finally send the new id back to the calling sub to actually
make
the order
Get_Set_NewOrderID = m_nextID


End Function

'and a sub to display your array information
Private Sub DisplayArray()

Dim iTemp As Integer, iTemp2 As Integer
Dim OrderString As String

'loop through the array and build a string to display
For iTemp = 0 To UBound(m_arrOrders, 2) - 1
For iTemp2 = 0 To UBound(m_arrOrders)
Select Case iTemp2
Case LBound(m_arrOrders)
'place the number of orders at the begining of each
line
OrderString = OrderString & "Trade: " & iTemp + 1
& ", "
& _
"ID# " & m_arrOrders(iTemp2, iTemp) & ", "
Case UBound(m_arrOrders)
'insert a line return character when you reach the
end of
the
'first trade
OrderString = OrderString & m_arrOrders(iTemp2,
iTemp) &
vbCrLf
Case Else
OrderString = OrderString & m_arrOrders(iTemp2,
iTemp)
& ", "
End Select
Next iTemp2
Next iTemp

'display the string
Form1.Text1 = OrderString

End Sub







To unsubscribe from this group, send an email to:
twsapi-unsubscribe@y...



Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.