--- In twsapi@y..., ruffstuff123 <no_reply@y...> wrote:
The only thing that I'm really missing is a trailing stop.
Anyone managed to code something like that?
If so please mail me the code or point me to an example.
It would save me huge amounts of time.
Finally away from the screen for a few hours :-)
I can code a bit in VB6. Level... erm... advanced 'Hello world' ;)
But if anyone would be so kind to share a few lines of code that
modifies an order I have enough to build on.
To modify an order, just reenter the order with the new price and
contracts via the .placeorder method using the same TWS order id #
(argument 1 in the "placeorder" method).
Trailing stops take a bit of proper organization to work out
correctly. You need to keep track of entry price, whether it is a buy
or sell, MaxPnL for the open position and the Trail Stop amount. You
use live data to figure the MaxPnL. For a long position the Trail
stop price = Entry Price - MaxPnL.
Those are the basics of setting up a trailing stop. Whether you
choose to hold that stop on your machine or to transmit each new stop
price to IB is a personal choice. You can cancel untransmitted stops
easier than transmitted ones and know that you aren't going to be
filled. Globex held stops will probably fill at a better price than
local system held stops.
Some of the complexities involved with trailing stops include:
Multiple entries at different times with multiple entry prices.
Partial exits and reentries.
Formating your stop orders so that they are within the minimum tick
range expected by the exchange you trade on. This is particularly
important when you consider partial fills at different prices that
may create an average price that is outside the minimum tick of your
exchange.
Doing a trailing stop for multiple symbols pretty much requires you
to code so that multiple instances of the code can be created for
each symbol you trade (class module).
I'm not ambitious enough to create sample code for this, but
hopefully this short discussion of the issues will help some who are
trying to figure it out.