A bot has to implement the 'nextValidId' method.
One of the first things that TWS does when something
connects via the API is send a next-valid-order-number,
and when that is received EReader will call 'nextValidId',
supplying the number as an argument. Without that number,
the bot couldn't reliably place an order. After that number
is used, the bot increments it for use with the next order.
It appears that TWS only sends a next-valid-order-number when
a bot initially connects. (I have not tried submitting orders
to TWS in any other way than by incrementing the order id number
as each order is submitted; however, with the 'Sample' app I did
try using a number less than the one supplied by TWS and got some
sort of invalid order message back.)
Early in RandomBotMgr, currently about line 126, the thread sleeps
for one second to allow time for the number to be received.
That should use a timeout instead of arbitrarily waiting for one second.
There is timeout functionality in RandomBot, which is used to wait for
order confirmation. It occurred to me that, in order to implement a timeout
while waiting for next-valid-order-number, I could add a 'timeout' method to
the 'TimeOut' class; the 'timeout' method could be called from either
RandomBot or RandomBotMgr. It wouldn't be a difficult thing to do if a
method could be called with a parameter that is a class -- it would look
something like:
Code:
boolean timeout(Class c) {
int tries = MAX_TRIES;
int ms = MS_TO_SLEEP;
while ((tries-- > 0) && (c.eventHappened() == false))
Thread.sleep(ms);
if (tries < 0)
return true; // timed-out
else
return false;
}
Another argument that could be supplied to 'timeout' might be a
maximum-length-of-time-to-wait value.
Ha, ha! The compiler doesn't like that.
Eventually, I stumbled upon the
"Reflection API Trail" -- a long and winding road -- where this appears,
fortunately near the beginning:
"First, a note of caution. Don't use the reflection API when other tools more
natural to the Java programming language would suffice. For example, if you are
in the habit of using function pointers in another language, you might be
tempted to use the Method objects of the reflection API in the same way.
Resist the temptation! Your program will be easier to debug and maintain if you
don't use Method objects. Instead, you should define an interface, and then
implement it in the classes that perform the needed action."
I am in the habit of using function pointers in another language, so I
hope that an "interface" will let me do what is needed. What I hope to do is
something like:
Code:
class TimeOut {
...
boolean timeout(...) {
...
}
}
class RandomBot ... {
...
boolean eventHappened() {
...
}
boolean timedOut = TimeOut.timeout(this);
}
class RandomBotMgr ... {
...
boolean eventHappened() {
...
}
boolean timedOut = TimeOut.timeout(this);
}
Is this possible?
I may try it, later. Right now I am a little frustrated with Java.