I’m a bit confused with the subject, would appreciate some help very much.
I want to track the profitability of the current open position and change the amount of trailing stop offset accordingly.
The term of profitability is boolean (let’s consider the long position): if current close is higher than the position entry price then position is profitable, if current close is lower than the position entry price then position is losing.
Say, I have two predetermined trailing stop offsets: OffsetProfit and OffsetLoss. OffsetProfit must be applied on the next bar as soon as current bar results in profitable position detection, OffsetLoss must be applied on the next bar otherwise.
Not sure if this can be done via pure array math or loops are necessary, would appreciate some help very much.
From your description you have simply trailing stop plus max loss stop and nothing more. Does not require special coding just using two built-in stops.
The thing is that actual profit must be calculated since Buy that actually triggered, not all Buys.
Thus, redundant Buys must be filtered (with ExRem, for instance), but I can’t fugure out how to do that as exits depend on real time position profitability.
There are many ways to implement that depending on what you are after:
Many cases are just covered by ApplyStop() funciton or combination of two or more ApplyStop(). Really 90%+ of user cases can be handled that way. The exit based on profitability is as I wrote nothing more than trailing stop. With ability to activate it in certain levels (in 6.25) and for certain amount of time, possibilities offered by ApplyStop() are endless. Really ApplyStop is extremely powerful (see for example http://www.amibroker.com/kb/2016/01/28/how-does-risk-mode-trailing-stop-work/)