AFL code for Sell after N days since Buy signal

0 votes
Hi, I'm new to Amibroker, I've looked around the site, FAQ and AFL coding section and can't see an answer to my question below. If my question has already been answered previously, my apologies and a link to the previous answer would be great.

I am trying to write a simple trading system to buy a stock if the close price is greater than the close price in the last 200 days. "Buy = C>=HHV(C,200);" and sell the stock 50 trading days later.

I've tried the formula "Sell = BarsSince(Buy)==50;" but this formula is affected by any subsequent buy signals and will actually sell 50 days after the last time the buy signal was triggered not when I may have physically entered the trade. This becomes an issue if there are multiple buy signals before the sell trigger.

I've tried to add Buy=EXRem(Buy,Sell); and Sell = ExRem(Sell, Buy); but this doesn't help.

Ideally I would like it to pick up all the buy signals and then attach a corresponding sell trigger to each buy signal.

Any help would be greatly appreciated.


asked Jun 10, 2017 in AFL Code Wizard by Kieran

2 Answers

0 votes
Best answer

The solution is very simple (one line);

// sell 50 bars later
ApplyStop( stopTypeNBar, stopModeBars, 50 );

answered Jun 13, 2017 by anonymous
Thanks, this has helped me out a lot.
0 votes
Proper regular N-bar stop is achieved either by using ApplyStop(...) function or by writing looping code.
answered Jun 10, 2017 by fxshrat (1,200 points)