Limit number of new positions for a single day


#1

Hello,

Regarding a trading system, I try to develop. I have certain criterias which have to be met to open a position.

buyCriteria = ROC(Close,100) AND ...  AND ...;

Buy=buyCriteria;

The no. of maximum open positions should be ten:

SetOption("MaxOpenPositions", 10);

But I would like also to limit the no. of positions the system can open on a single day, e.g. only two.

Is there an easy way to do that?

Thank you

Best Regards


#2

See if this might help:
http://www.amibroker.com/kb/2015/09/26/limit-number-of-trades-per-day-in-a-backtest/


#3

Hello,
thanks for the quick response.

That is the solution to limit intraday signals for a single ticker. But I want to limit the number of opened positions on a single day for a portfolio of stocks with end-of-day data. And I have to admit, I was not specific enough in my question. Sorry about that.

Further help regarding my question is highly appreciated.


#4

This will limit the number of position you can open in a single day ( bar ) if there are any available positions to open based on MaxOepnPos option and their number is greater than your imposed limit

limit_daily_entries = 5;

SetCustomBacktestProc( "" );

if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)

    for( i = 0; i < BarCount; i++ )	//  Loop through all bars
    {
        count = 0;

        for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
            if( sig.IsEntry() )
            {
                if( count < limit_daily_entries )
                    count ++;
                else
                    sig.Price = -1 ; // ignore entry signal
            }
        }

        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
    }

    bo.PostProcess();	//  Do post-processing (always required)
}


#5

Hello Aaron,

Perfect!
Many thanks for the help. Does exactly what I want.

Best Regards

Patrick.


#6

Hi Guys

Is there a way to modify code to limit entries to 20 trades over 2 years?

Regards

Nick


Limit backtest trades in a 2 year period
#7

Hi Aaron,

many thanks for posting this.

I have tried this code in a short-term system I have which can be in and out of a trade in a single day, or can hold positions for 2-4 days, depending. The problem I am having is that I may come into a session with 10 open positions, 1 may be exited on the open, then another 2 positions will be established at the open which are exited at the close and a 3rd position is established which is held, leaving me with 10 open positions at the end of the day which accords with the max open positions allowed.

However, coming into the session with 10 OPs and exiting 1 on the open means in reality I can only take one new position, but because the 2 positions I established during the day were exited on the close of the same day, according to the code I haven't breached my max OP limit of 10 OPs, even though this would not be allowed to happen in actual trading!

I have tried to apply your sample code above in the backtesting section of my AFL, but when I run it, I get exactly the same number of trades, all identical, even though I can see many days when this piece of code should be limiting the number of positions in the backtest.

Would you have any idea what I might be doing wrong? Is there a specific place in the AFL that this block of code needs to be placed? I was thinking that perhaps the max OPs needs to be handled inside the looping section of code which controls the system's logic somehow?

Many Thanks and Regards,

Matt