Re entering trade

I'm going to keep looking at a few optinos but thought meanwhile I'd reach out. I'm running a backtest where one exit rule is the stock drops below the 20ema and one of the entry rules is to re-enter the trade if it pops back over the 20ema within 2 days. Right now if i just set the sell rule to be that c < ema(20,c) or crosses, this triggers regardless of if i actually have the stock, so the sell rule triggers to often. Not sure how to add a factor that says rebuy if sell in last 3 days plus i actually owned it. Any direction or suggestions i'll dig in and write the code.

One common way to get rid of "extra" signals is to use the ExRem function: http://www.amibroker.com/guide/afl/exrem.html. If you need something more than that, you should probably provide more details of what your code is doing and why ExRem is insufficient.

thanks. that does help (EXREM). but i was still not getting the re-entry signals. But I think i have that figured out. Once I clean it up I'll post it up here later in case someone needs to do the same thing or in case someone can improve the code.

Thanks

Re-entry (delay) code can be found here

2 Likes

thanks. i'll take a look!

this is how i solved it w/o a more complicated loop. in this code i have an initial buy signal. i want to enter and exit on a close < 10 ema. BUT, if it's just a 1 or 2 day shakeout i want to re-enter but only the first shakeout. if it breaks down a second time before a fresh original signal it does not take it.

//sample original buy criteria
b1 = V > MA(V,30)*2 AND C > Ref(C,-1)*1.03 AND C > EMA(C,10);
Buy = b1;
//sell first buys if close below 10ema
Sell = C < EMA(C,10);
//there are other times not related to a buy when it closes below 10ema so remove them
Sell = ExRem(Sell,Buy);
//now that there are some sell signals, redo the logic on the buys and this time
//do the original buys AND buy if the close is back above the 10ema as long as it happened w/in 2 bars of the last sell
B2 = C > EMA(C,10);
Buy = b1 OR ( b2 AND (Ref(Sell,-1) OR Ref(Sell,-2)));
//now have to do the sell signals again to capture all the buys - the originals and new ones
Sell = C < EMA(C,10);
//now remove extra sell signals
Sell = ExRem(Sell,Buy);
AddColumn(Buy,"buy",5);
AddColumn(Sell,"Sell",5);
Filter = True;

1 Like

Mike, thanks for sharing your code. It's even more useful if you enclose it with code tags:

//sample original buy criteria
b1 = V > MA(V,30)*2 AND C > Ref(C,-1)*1.03 AND C > EMA(C,10);
Buy = b1;
//sell first buys if close below 10ema
Sell = C < EMA(C,10);
//there are other times not related to a buy when it closes below 10ema so remove them
Sell = ExRem(Sell,Buy);
//now that there are some sell signals, redo the logic on the buys and this time
//do the original buys AND buy if the close is back above the 10ema as long as it happened w/in 2 bars of the last sell
B2 = C > EMA(C,10);
Buy = b1 OR ( b2 AND (Ref(Sell,-1) OR Ref(Sell,-2)));
//now have to do the sell signals again to capture all the buys - the originals and new ones
Sell = C < EMA(C,10);
//now remove extra sell signals
Sell = ExRem(Sell,Buy);
AddColumn(Buy,"buy",5);
AddColumn(Sell,"Sell",5);
Filter = True;
1 Like

Your re-entry idea is different story than that (minimum) n-bar re-entry delay of other thread. And no, it is not complicated. Latter one is similar to n-bar stop but reverse... instead of n-bar exit... n-bar entry.

You can't do latter one without loop as it works on re-entering impulse signals as well as state signals (if commenting ExRem lines). (Also for n-bar exit you have to use ApplyStop() function or loop approach.)

Contrary to that you just check for Sell signal on previous bars. So it's different story.

BTW, this one

Buy = b1 OR (b2 AND (Ref(Sell,-1) OR Ref(Sell,-2)));

can be written more flexibly.

Buy = b1 OR (b2 AND Ref(Sum(Sell,2),-1) == 1);

So then just a single number needs to be inserted as Sum period instead of writing multiple ...Ref(Sell,-1) OR Ref(Sell,-2) OR Ref(Sell, -3) OR .... OR Ref(Sell, -100) OR....