开云体育

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

VB Sample Project: How To Place An Order


marinindextrader
 

Andrew,

Go here and download this: 1_Project_PlaceOrder.zip



To All:

I havnt spent much time at all on the placeOrder routine...that is
coming this week for me.

However here is my take on the whole affair after some quick study of
the methods.

The next valid Id call is made right after startup. If it is the
first time you have started IB for the day and have had no open
orders, the next valid id will be 0. If you have made orders the last
order id you placed will be incramented by 1...

That having been said..If you are making your first trade you can
assign anything you please as an order id...the key is to incrament
it each time a trade is made. If and when you logged off to go to
lunch for instance, then upon relogging, that is when the next valid
id comes into play. Of course your system could save the information
in the registry or whatnot and you more than likely could recall your
own last id and go from their...the bottom line is that the order
id's must ascend in relative value to one another...that is really
the crux of it...relogging in will fetch the next valid id..

All that having been said, let me share some code with you on how I
would handle incramenting the id and storing relevant order
information....

Assume your first chosen id is 3030. More on choosing id#'s later;
read footnote at bottom. Here is what happens. You place the order
and it is sent upstream to IB and they record your chosen order id.
If you relogged and captured the next valid id it would read
3031..but your not going to relog each time to fetch an id so you
have to handle the incramenting your self.

Now...you can incrament by whatever you choose...1, 5 10 etc...it is
a Long type value so you will be limited to a ceiling of
2,147,483,647...LOL shouldn't be a problem with whatever creative
method you choose to craft id's

So on to te crux of the matter....

First off I would create a module that tracks your order id and all
order information.

Second I would create a Form level sub to handle the Tws1.place order
call...


I would post the code but its easier to just download the project:



Scott
Owner TWS API Yahoo Discussion Forum


marinindextrader
 

Here is the footnote I left off concerning order id's, thier creation
and value.

Why choose an arbitrary value for an order id?

I might suggest that your order id system be based on time.
Using various functions one can extract a string from the system time
and date, convert it to a long value and then and use this as an id.

Why would one want to do this?

If they were (in the future) to decide to capture all trades and
store in a database, the id can be reconverted into its date value
and reports can be printed on a time basis.

Here is a crude example of creating an id based on date and time:

Example Date: 6/20/02 10:24:17 AM

Private Sub Command2_Click()
Dim newString As String
Dim newId As Long
newString = newString & Month(Date)
newString = newString & Day(Date)
newString = newString & DatePart("h", Now)
newString = newString & DatePart("m", Now)
newString = newString & DatePart("s", Now)

Text1 = newString 'displays 620102417; a unique number
newId = CLng(newString) 'convert to long value

'this value is within the confines of Long type data: 2,147,483,647


End Sub


There are other reasons to manipulate the order id...

What if you want your id to be based on a conditional check of the
symbol...for instance all trades with cisco will be between X and Y...

Or...if you trade options you might want an order prefix that
describes where in the money you are...or something like that..
I dont know much about options specifications

Or...you may want use an ordering system that relates to the type of
trade it was...Long or Short...

The choices are endless, and to dismiss everything but an arbitrary
system is ridiculous

If your storing the number...put it to work

Scott

Owner TWS API Yahoo Discussion Forum
"The other Yahoo Board"

heheheheh