Same Bar Exit and Limit Orders allowing too many open positions

Hi there! Im so sorry if this is simple, i have tried sooo many combinations and searched google and these forums but cant seem to get it to work on a Portfolio Level - using same bar exits allows too many open positions.
I have used Tomaz's example of how to enter at a limit here:
Using that code for a Portfolio of say 6 max open postions, I came up with the AFL below:

SetOption("AllowSameBarExit", True );  
SetOption("SettlementDelay", 1 ); //--Not Working!
SetOption( "initialequity",30000); 
MaxPos = 6; 
SetOption( "maxopenpositions", MaxPos );


BuySignal = C < O;

// buy on the next bar
Buy = Ref( BuySignal, -1);
BuyLimitPrice = Ref( Close, -1) * 0.99;

// now we check if limit was hit
Buy = Buy AND L < BuyLimitPrice;

// if Open price is below the limit, then we use Open for entry
BuyPrice = Min( Open, BuyLimitPrice );

//Sell on close of first up bar.
Sell = C > O;
SellPrice = C;

Running this on a list of Stocks in the USA Results in the following Backtest:

Recall above in the code that we only allow a max of 6 open positions.
So we start trading on Day 2. It opens 6 positions, and exits 3.
So day 3 should have room for only 3 open Positions. However notice it has opened 5 positions!

So if anyone has any idea on how to solve this, I would really appreciate this.
Also note the setting:

SetOption("SettlementDelay", 1 );

is not working in the above scenario either.
Thanks sooo much in advance!

No, your observations are incorrect. It opened 6 positions on 2/10, it closed 3 of them the same day, then on 3/10 it closed 2 more positions FIRST (so had 5 available free slots) and THEN opened 5 new positions. Look at the order of trades. Positions are closed on 3/10 BEFORE entering new ones. (You asked for single bar trades).

Use “DETAILED LOG” for better illustration.

Hi Tomaz, thank you for your reply.
So how do we tell Amibroker that we can sell on the SAME bar as entry at close, but we CANT OPEN a new postion with that money on the same day (as we are exiting at Close)?
Also - why does settlementDelay not work in this case?
Thank you!

Also, please note that if I remove the Setting AllowSameBarExit (and ensure its unticked in the settings window) I get the same result where the Exit happens before the entry, allowing an entry on the same Bar:

Open the Users’ Guide at , scroll down to “resolving same bar conflicts” - it is all described in great detail there (with examples).

1 Like

Hi Tomaz, thank you for your reply. I have read that article may times over, and I feel that there is a bug here, or that perhaps it doesn't work as expected in a portfolio situation. Please note that as per that article, I would like scenario 2, which simply shows:

So according to that documentation, if i have "AllowSameBarExit" = true; the entry should PRECEDE the exit.
Now please refer to the code im my first post. It HAS the setting as per scenario2. So why then in the detailed log are the Exits PRECEDING the Entry?
The only thing I can reason here is that its only PRECEDING the entry in the same stock.
However i would like my portfolio to only enter new positions the day AFTER the exits.
Im really gratefull for any help here, and thanks in advance!

1 Like

Apparently you did not read carefully enough. There is no bug. You are supposed to get the same results! That is precisely how it should work.

All supported scenarios are described precisely in the manual. And I am not going to re-copy entire portions of the manual over and over again.

Any open positions existing from PREVIOUS DAYS are ALWAYS handled BEFORE next bar entries (because you need to free your cash to enter any new trades). Then entries for the bar are handled. THEN if and only if you turned ON “Allow same bar exit” EXITS from from these NEW current bar entries are handled. That is the only difference made by “same day exit”. It handles exits after entries IN ADDITION to what it normally does.

As to Settlement Delay - IT WORKS, if you enable it. Use DETAILED LOG, it should show this:

Exit signals:MSFT=Sell,
0 Open Positions: , Market Value: 0.00, Equity: 2505394.97, Cash: 2505394.97, Margin: 0.00, Net Cash Balance: 2505394.97, (Available: 25.83, Unsettled: T+1 = 0.00, T+2 = 2505369.14, T+3 = 0.00)

Those T+1, T+2, T+3 describe DELAYED settlement cash that is unavailable for current bar purchases but will be only available AFTER +1, +2, +3 bars.

Tomaz, thank you very much.
If anyone is interested, I finally found why it was still taking trades the same day when there should have been no cash. I had accidentally left margin account set to on from a previous backtesting session, so it was able to purchase more trades on margin than there was cash!! So simple in the end! Also that SettlementDelay option is also neccessary to emulate a real portfolio account that may enter and exit on the same day. I hope that helps someone!
Word out.