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.
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 */ );
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.
I guess the question @thankyou18 was asking is - how do you implement both a stopTypeNBar and a stopTypeLoss, if you want to exit Either one of the below conditions occur:
- If still in position and reached Nth bar, then exit
- If still in position and reached Z loss, then exit
Can these both conditions be put in place (with applystop or not)?
Those stops (N-bar, max loss, or any other ApplyStop type) are independent and can co-exists without any problems. One stop does not overwrite another. If they occur on the same bar, the user can decide which one is taken first using SetStopPrecedence function.
There is a must read for every AmiBroker user and it is called "Users' Guide". All answers are there.