Unusual stop based partially on purchase price

I’m trying to test a system that is supposedly profitable but the author of the system has the following exit signal.

At the time of entry the stop is purchase price - 2ATR, which should be below the 200 day moving average.
As time goes on the stop goes below the 200 day moving average.
So the rule would be something like:

  • stop is initially purchase - 2ATR… if/when the moving average crosses that price, switch it permanantly to the average.

I could probably simulate results if needed by saying for 10 or 15 trading days use the ATR rule then switch to the moving average rule.

Not sure best way to do this, especially basing the exit on the purchase price initiailly.

I can see two exit rules here. One is classic stop-loss and the other is moving average crossover.

// stop loss @-2ATR since entry price
ApplyStop( stopTypeLoss, stopModePoint, -2 * ATR( 14 ), True ); 
// exit at MA crossover
Sell = Cross( MA( Close, 200 ), Close ); 

If you want to limit activity of ApplyStop just to first 15 bars you can use recently added ValidFrom/ValidTo parameters

Version( 6.0 ); // requires at least v6.0
// stop loss @-2ATR since entry price
ApplyStop( stopTypeLoss, stopModePoint, -2 * ATR( 14 ), True, False, 0, 0, 15 ); 
// exit at MA crossover
Sell = Cross( MA( Close, 200 ), Close ); 

Tomasz I may have interpreted the OP differently or may not understand AmiBroker’s precedence rules. I believe there is the intention to have an Initial maximum stop loss, and then if the trade moves in his favor a Trailing stop loss.

If the trade immediately moves against him, and drops below the 200 day m.a. will not your Sell order be triggered even if the -2 ATR level has not been reached? Does the ApplyStop take precedence over the Sell order?

ApplyStop( stopTypeLoss, stopModePoint, -2 * ATR( 14 ), True ); Sell = Cross( MA( Close, 200 ), Close );

Mike wrote, “At the time of entry the stop is purchase price - 2ATR, which should be below the 200 day moving average.
As time goes on the stop goes below the 200 day moving average.”

So the initial Exit should be -2 ATR, not the cross below the 200 day m.a.

For Mike and anyone trying to learn about afl writing and stops, I posted the below links on the other forums and hope it may be of use for you to review.

AB user guide
http://www.amibroker.com/guide/afl/applystop.html

AB Knowledge Base
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/
https://www.amibroker.com/kb/2016/01/28/how-does-risk-mode-trailing-stop-work/
http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

afl code examples from a nice series of 3 articles in TASC on stops.
http://www.amibroker.com/members/traders/05-2009.html
http://www.amibroker.com/members/traders/06-2009.html
http://www.amibroker.com/members/traders/07-2009.html

3 Likes

That's correct. It is question of interpretation. If natural language was strict, we would not need to invent programming languages.

Thank you both for sharing your thoughts. Yes, Portfoliobuilder, I think your interpretation was correct. If I buy once it breaks over the 200MA, the 200MA is going to be pretty close; so I initially want to set a stop at 2ATR. But assuming the stock moves up, the 200MA will rise above the 2ATR and I want the 200MA to become the stop. So essentially until the 200MA crosses above the 2ATR I want it to be the 2ATR, but then become the 200MA once it crosses above it for the first time.