Time stop or n-bar stop with extra condition

The code below applies a time stop of 10 bars in the backtest.
ApplyStop( stopTypeNBar , stopModeBars , 10);

I want to attach an extra condition to this time stop.

Apply 10-bar stop only if the current price at the 10th bar is suffering more than 3% loss below buy price.

I don't know how this can be done because ApplyStop is a built-in function which cannot be modified.

You have to iterate (looping) then because you want to check (further) condition at particular bar after entry bar. Examples are in AmiBroker KB (knowledgebase site) and in this forum.

And no, using BarsSince() is not (proper) solution.

1 Like

This can be done without looping. ApplyStop can do it by itself. You need most recent version (6.30 or higher).

What you are asking is NOT really N-bar stop or time. It is Max-loss stop. You want to exit when LOSS EXCEEDS threshold, and only do that 10 bar after entry.

That is easily done using ApplyStop ValidFrom/ValidTo argument:

// this max loss is valid only on 10th bar
ApplyStop( stopTypeLoss, stopModePercent, 3 /* amount */, True, False, 0, 
10 /* valid from */, 10 /* valid to */ );
5 Likes

One thing to other amibroker users to note when using ApplyStop().

If you are already using ApplyStop() with stopTypeLoss to apply cut-loss, then using another ApplyStop() with stopTypeLoss to implement time stop will over-write the first ApplyStop(),

If you're trying to implement a time stop, you should be using stopTypeNBar, not stopTypeLoss. There should be no conflict between these two different types of stops.

1 Like