TWS Trailing Stop Loss Orders and How To Modify them?


When I send an Lmt Order with a Trailing Stp Loss Order to TWS for the trailing order the Stop Price I set is how many Points (@ES#) the order should be trailing the market price.

But when I modify the Trailing Stp Loss Order it's not always working properly! Sometimes I get the new Trailing Points as I set it, other times I get +/- a Tick or so. And sometimes TWS just cancel the Trailing Stp Order instead of modifying it.

Anyone here that has some experience with modifying Trailing Stop Orders and can give some advice?

Thanks and Regards,





(You wont get a good response until you post your code. )

I am very sure that 2000 lines of code will not be of help. So here we go:

Using the ModifyOrder function- where I set string Type = "TRAIL", number StopPrice = abs( TDashValue - LastValue( Close ) ), where TDashValue is the Price my cursor is at when I click the Order Button.

I don't see anything wrong with this and still not working 100% of the time.




How have you tried to debug this?

Do you create a CSV file with the data?
Do you output a msgbox somewhere to show the values you are using?
Do you output the values to the Interpretation window?

Can you confirm or see when it uses a value that you don't think is correct?

As Pinecone said, without the code it is hard for us to see what the problem is.

If you don't want to reveal your "secret sauce" then REDUCE the code to the minimum required to replicate the error, and just post that.

I would assume that if you are dealing with 2000 lines of code you already know how to debug, but show us the code, and how you are debugging, and then we might have more that a "Wild Guess".

HI there snoopy.pa30,

Yes to all- more or less. Every value is correct and I have used AmiBroker for 14 years so I usually find problems and fix them very fast.

I can put together a simple code using the the same order code. So please stay tuned.

Thanks and Regards,


The thing is that on the forum, nobody knows what you do in "real life", i.e. nobody knows what you did, what your code looks like, what is your experience, how many years you use AmiBroker, how good your coding is, UNLESS you provide that information directly. If you don't provide any information people will make assumptions. Assumptions are very rarely correct.
On experience scale 0..100 people on forum are in ALL levels. And people can assume you are anywhere on that scale.

As to your problem check the TickSize argument to ModifyOrder and prices you pass. Prices that you pass are subject to rounding to TickSize argument that you pass.

As IBController code is open source, you can easily see what exactly is done: src/BrokerIBDlg.cpp · master · AmiBroker / IBController · GitLab

	order.lmtPrice = dTickSize * floor( 0.5 + dLimitPrice / dTickSize );
	order.auxPrice = dTickSize * floor( 0.5 + dStopPrice / dTickSize );
1 Like


Sure - I understand. I am now writing a very compressed code showing the problem. Once it's done I will be back with the code. And it's unfortunately not a problem with the TickSize.

Thanks and Regards,



Suddenly, without changing anything in my code, it started to work. My guess is that the TWS paper trading account was messing it up....?

So all good. :slight_smile:

Thanks and Regards,


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.