Implement exit according to current position profitability

Hi All,

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.

Thanks to everybody in advance.

This might get you started.

profit = C - ValueWhen( Buy, BuyPrice );
stop = IIf( profit <= 0, offsetLoss, offsetProfit );

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:

  1. 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/)
  2. Users wanting to implement their own stops for some reason (i.e. including exits based on profitability) there is an example how to do this using loops without ApplyStop, the example is in KnowledgeBase and covers remaining 9.9% of cases http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
  3. Users who have esoteric needs (such as implement exits based on correlation of profits of multiple open positions on different symbols) need to use custom backtester which reports each position profit bar by bar on the fly, docs: http://www.amibroker.com/guide/a_custombacktest.html and practical example: http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/ that covers remaining 0.1% of most esoteric needs.
3 Likes

I like the word “esoteric” in this context :slight_smile:

OK, got it, thank you!