Exrem and buy next day open and sell close current bar

I am testing an AFL that generates a buy signal at the open of next bar.
The sell is based on the close of the current bar.

Here some settings.


BuyPrice = O;
SellPrice = C;
ShortPrice = C;
CoverPrice = C;
tradedelays( 1,0,0,0); 

Numpos = 1;
SetOption( "MaxOpenPositions", Numpos );
PositionSize = -100 / Numpos;


Most trades are only 1 bar long, so it buys at open and sells at close of the same bar. There are many double buy and sell conditions that I could remove with

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

I have 2 questions and hope someone could help me a bit or show me a link what I should study.

I red this
https://www.amibroker.com/guide/afl/exrem.html
https://www.amibroker.com/guide/afl/flip.html

If I use exrem, the trades that take from open till close on the same bar do not occur anymore and those are the most usefull trades in this case. I understand that is the consequence of using exrem. Is there a way to prevent double trades and still allow the trades that buy at open and sell at close of the same bar? Maybe I should not call it double trades see second question.

Second question is this case: when not using exrem if a trades starts at bar open for example at bar 100 and takes till close bar 105 and another new trade occurs at bar open 105 till close bar 105, there are 2 open positions at the same time. I see these 2 trades occuring in this case while Numpos =1.
I also see it back in the exposure of the backtest results. How could I prevent this second trade to happen without using exrem and using 1 max openposition?

Thanks for your help.

@PWFores I'm not sure I understand your problem (and it almost always helps to see your code to understand what is going on). You can't successfully use exrem but I don't understand why without the underlying logic of the signals.

Also it may help if you read carefully the section on "RESOLVING SAME BAR, SAME SYMBOL SIGNAL CONFLICTS" when you scroll down this page,

http://www.amibroker.com/guide/h_portfolio.html

Lastly have you made sure you have set,

SetOption("AllowSameBarExit", True);

and / or in the backtester settings,
image

2 Likes

Thanks for your response
Forgot to mention I have set this already in the AFL
SetOption( "AllowSameBarExit", 1 );

Sorry I made a mistake with question 1. I now see trades with exrem that are occuring on the same bar.
Thanks for the link, I should have red that.

So can I only prevent question 2 from happening by enabling exrem and not simply by maxposition = 1?

In backtest ExRem is not required because default backtest mode is backtestRegular. And that one removes redundant signals per symbol already. And as you might have noticed I said ExRem is per symbol's array function but not function to remove signals of two or more symbols of portfolio backtest.

You may use Custom Backtester checking for open positions and signals and counting and signal skipping.

Without Custom Backtest code
214

With CBT code included -> MSFT trade skipped on July 16th
127

3 Likes

Yes that situation is exactly what I mean. See my picture.
It exits at the close of the 16:00 bar but it also opens at the open
of 16:00 bar so 2 positions are active in that bar.

Thanks I will study CBT.

image

I use this AFL with optimizing and excluding trades with the custom backtester made it slower

question 2 was fixed by adding this logic.

InTrade = Hold(Flip( Buy, Sell),2);
Buy = Buy AND NOT Intrade;

Now it allows buy at the open and close the same day without overlapping trades.

1 Like

This was not a fix, it only works for certain conditions.