Hey I am working an an automated strategy and hoping to become an Elite member on this site should the strategy work.
I am wondering if it is possible to write a piece of code that is a profit target like order that turns into a trailing stop should the price of the instrument meet the profit target. What I hope to do with this is maximize the profitability of each position. I have done some back tests and found that sometimes the price continues to fall after the profit target is met and the position is closed.
In other words instead of having the activated profit target close the position, it turns into a trailing stop where the initial profit target number becomes the top of the trailing stop.
Currently I have this code and I hope that its possible to simply switch the profit target line with the described action.
I'm not sure if I remember the correct combination but I believe that the SetStopLoss and the SetTrailingStop can't be used at the same time and I think that's what you're really wanting to do.
What you can do is to use code to inspect the price, if the target is reached, then change your trailing stop variable value to the target - x ticks (for a long) so it doesn't close immediately when hitting the target. Then on each tick update, check if the price hits the stop and exit your position. You will not be able to do this in the strategy back tester since it only triggers the on bar update at the close of the bar not on each tick, but you'll get a close approximation if your time frame or tick count is low.
The following user says Thank You to dynoweb for this post: