Requested size is less than MinShares

Hello! First post so go easy.
I'm not sure if this post should go here or in the '.afl programming' section but here goes.

In my backtest report I am getting the the following error:
'xxx not entered because the requested size is less than MinShares/MinPosValue'.

I have set MinShares to 1 and MinPosValue to 500. I don't know what the requested size is but I've setup some backtesting parameters as follows:

InitEquity = Param("Initial Capital", 25000, 10000, 1000000, 5000);
MaxPos = 20;
Tradesize = InitEquity/MaxPos;
SetOption("InitialEquity", InitEquity);
SetOption("MaxOpenPositions", MaxPos);
SetOption("MinShares", 1);
SetOption("MinPosValue", 500);
PosType = -1;
PosType = ParamToggle("Compounded?", "Not Compounded|Compounded", 1);
if( PosType == 0)
	SetPositionSize(Tradesize, spsValue);
if (PosType == 1)

This happens several times throughout the detailed backtest report. In fact, often the system does not open any more positions. I would understand if the cash available was low (less than $500 if most positions are open). However, I've seen times when, while the cash amount is less than the initial equity, it is still large enough to handle the requested entry. For example, I've seen this error happen when there is 22K in cash/equity (init equity = 25K). Position sizing is based on 5% of equity. Allowing Position size shrinking doesn't make a difference.

The only pattern I can see is that it happens when the account is much less than the initial equity. How does this effect position sizing?

I feel like I'm missing something simple. I'll have to give myself a smack on the back of the head when I find out.

The program clearly tells you already everything. You have requested position size that is smaller than MinShares/MinPosValue. It is not about cash. It is about constraints that you have activated yourself. Remove / comment-out constraints that you have put

//SetOption("MinShares", 1); // remove
//SetOption("MinPosValue", 500); // remove

and it will enter this trade.

Keep in mind that you might have also round lot size set in the Settings and that might play the role too. For example, if you have set round lot size = 100, then if you wanted to buy 5% of equity of 20K equity that would be $1000, but if stock price is $15 what would be 66 shares and that rounded to 100 (round lot size) gives 0 (zero) shares (round lot sizing always rounds DOWN).

The other factor is described in the Knowledge Base article here:
The program by default prevents from taking trade that is larger than 10% of market volume for given stock at given bar (see the article). Check your data. Your data may be limiting the trade size below limit you have defined.

It goes like this:

  1. PositionSize defined in formula is taken, then
  2. Per-bar volume limit is applied (as defined in the Settings)
  3. Round Lot Sizing applied (as defined in the Settings)

Resulting value is “requested position size” and it is checked against constraints (MinPosValue/MinShares).

If you don’t understand your own code you need to add _TRACE and/or AddColumn to get insight into what is happening in your code:

1 Like

@Tomasz How to set the per-bar volume limit programmatically without using the GUI?

It is GUI-only option. But when you save project into APX file all settings are stored.

1 Like

Thanks Tomasz. Thanks for the explanation. I’ll try your suggestions.

Hi Tomasz,
Can I suggest some users would find this useful to control via code? AB seems to default this value to non-zero, and it's one of those things that if it gets set wrong and the user forgets - it can go unnoticed for a long time (or it did with me!) . I like to make sure everything is set in the code to avoid surprises. Cheers,

Sorry, to be clear I was responding to this:

  • How to set the per-bar volume limit programmatically without using the GUI?
  • It is GUI-only option. But when you save project into APX file all settings are stored.

I just realized the real danger is that every time I open a new analysis window it goes back to default setting? Other things, like commissions remain the same once set, shouldn't this param be the same?

Thanks, that would be my suggestion.

Sorry, although I saw the value reset in the past, now it does seem to stay at "0" if I set it there. Still, would be great to control from code. Cheers,

No it doesn't go to default. The Analysis Project (APX) stores ALL settings (from "Settings" dialog) and when you load analysis project ALL settings are loaded as they were set when project was saved. So there is NO problem and NOTHING to worry about. Just set the settings and save the Analysis project the way you want.

1 Like

Can i confirm quite the opposite?

If i use analysis window without saving it, then on the next run, whatever settings i used last seem to be retained.
It doesn't go to any sort of default. so i think all these settings are also stored in the registry maybe ?

@ezekiel I don't think the Analysis goes to default as in like fresh installation.

You are not saving project file. So you are doing exact opposite from what I wrote.

If you don't save project file then obviously what you set in the settings is held and used for new Analysis windows because otherwise you would need to re-do all changes all the time whenever you create new Analysis.

And I wrote about that already: