Fixed Exit Period and BarsSince(Buy)

Hi guys,

I've been away from Amibroker for quite a while, and I’ve decided to get back into it. I see that I’ve forgotten much more than I initially thought because I’m getting stuck on something that seems trivial.

I’m trying to apply a sample of code from a book by Howard Bandy, focusing mainly on closing an open trade after a predetermined number of bars. An example of this was already discussed in the following post: https://forum.amibroker.com/t/fixed-holding-period/11560

I have the following question: Is it really appropriate to use 'BarsSince(Buy)' for the trade exit?

I’m testing with the following code, and I see that if I set the exit at 10 bars, it seems to work fine.

SetTradeDelays(0,0,0,0);
BuyPrice = C;
SellPrice = C;

MA1 = MA(C,3);
MA2 = MA(C, 16);

Buy = Cross(MA1, MA2) AND Status("BarInRange");
Sell = BarsSince(Buy) >= 10;

//Buy = ExRem(Buy, Sell);
//Sell = ExRem(Sell, Buy);

Filter=1;

AddColumn(C,"C",1.4);
AddColumn(Buy,"Buy",1);
AddColumn(Sell,"Sell",1);
AddColumn(BarsSince(Buy),"BarsSince(Buy)",1);

However, if I set it up to 15 bars, SELL remains at 0, and the exit doesn’t occur.

I’m aware that I have a lot to review and that it’s possible to set up the exit using a stop-loss defined with a fixed number of bars, but I would want to know how to do it directly with SELL. I’d really appreciate it if someone could give me a hint as to what I might be approaching incorrectly.

Thank you very much!

See these threads, what is happening with ValueWhen() is what you get with other Array functions, BarsSince() in your case

Problem with ValueWhen and Buy - AFL Programming - AmiBroker Community Forum

Valuewhen exit problem - AFL Programming - AmiBroker Community Forum

Basically BackTester applies logic and excess signals are handled correctly which normal Array functions are not meant to deal with. Array function would just "see" the most recent condition.

So, ApplyStop(), Equity(1) etc are much more advanced.
As an old user, if you searched for this, there are no less than 20 forums topics :slight_smile: and probably in the hundreds indirectly on it

1 Like

unable to edit above, see this

and another explanation in same topic by Tomasz

How to get the price of entry into the position? - AFL Programming - AmiBroker Community Forum

1 Like

What are the odds that the same type of question has been asked around 30 min ago and replied with same explanation around 15min ago and 7min :sweat_smile:

Yes @nsm51, I see it’s the "eternal" question. :smiling_face_with_tear:

Not using Amibroker for so long has taken its toll on me. With the hint you gave me, I dusted off my notes and found the solution. In fact, I based it on a post from the Knowledge Base.

However, I must admit that I was overly confident because it was an instruction in a book by Howard Bandy, assuming it should work in all cases and that I was making a mistake elsewhere.

Thank you very much for shedding a bit of light so quickly.

2 Likes