Little confused by "Min. shares" Analysis setting

Hi guys!

I am trying to understand how "min. shares" (Settings/General Tab) works and I am a little confused. Probably, I am sure I am doing something wrong.

I run a backtest and I get these results. All seems ok. By the way, I am using PositionScore.

test1

setting%2010

My problem arrives when I set "min. shares" to 11. I would expect "VAL" symbol would be excluded but I find all symbols (ranked by PositionScores) after "VAL" (including it) are out from my buys.

settings11

test2

I see in the "Detailed Log" VAL has been correctly excluded, by I don't know what happened with the rest of buys (because I don't find any messages for them).

detailed%20log

Does Amibroker stop to process buy signals when it finds some symbol for which requested size is less than MinShares? What I am doing wrong?

I usually solve my questions searching in this forum or Google. This time I have not found any response to it, but I apologized if it really was.

Thanks,

Hi guys,

Definitely, I am doing something wrong... but I don't find out what.

I have realized that, if I set "Round lot size" to 15 (greater than 10 shares for VAL), I have a similar problem running my backtest.

roundlot15

roundlot%20after

It seems all rest of my buys stop when AB is valuating the VAL buy.

rl%20val

Here is the AFL code I am using (not a system but just to learn):

SetOption("AllowSameBarExit", True);
SetOption("UsePrevBarEquityForPosSizing", True ); 
SetOption("MaxOpenPositions",15);
PositionSize = (-50/15);
PositionScore = Ref(MA(Volume*Avg,125),-1);

liquido=Ref(MA(Volume,10),-1)>100000;
 
Buy= DateNum() == 1100104 AND liquido;
BuyPrice=O;

Sell= DateNum() == 1100106;
SellPrice=O;

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

I would really appreciate some help.

Thanks,

@BernieTGN,

See SetPositionSize (use instead of 'PositionSize').

Set your Initial Equity via 'SetOption'.

Hi @TrendSurfer,

Thank you for your advice. I have added this two statements to my code instead
"PositionSize".

SetOption("InitialEquity",50000);
SetPositionSize(50/15,spsPercentOfEquity);

Unfortunately, I still have the same problem.

Thanks anyway.

Try setting 'Min. shares:' to your x value and 'Min. pos. value:' to zero and 'Round lot size:' to zero.

See Back Testing Ideas and Portfolio Back Testing.

Your code requested position size of 3.33% of equity which would be $1666.66. Since price of instrument is $162.80 that would be $1666.66/$162.80 = 10.23 shares which is less than Round lot size (15) so restrictions are not met and position can't be open. With your current settings you can only open integer multiplies of round lot size, like 15 shares, 30 shares, 45 shares, and so on.

Constraints work TOGETHER. They ALL must be met for trade to be open.

Hi guys,

@TrendSurfer
I haven' done it because I wouldn't get the result I expect. I mean, if I set "min. shares" to 15 (for example) and RoundLotSize to zero, I can get positions of 16 shares, when I want blocks of 15 shares. Thanks any way.

@tomasz
Thanks for your explanation. I understand what happens with a trade when some constraints don't meet. I expected position in VAL was excluded.
What I am trying to understand is what happens with the rest of the symbols (NBR, RDC, PDE, PTEN, HP, etc...). Constraints for these symbols are met, but "adjusted" positions are not opened. That is what I don't understand.

Maybe, I haven't understood well the below statement:
"Constraints work TOGETHER. They ALL must be met for trade to be open."
I guess you are referring to each individual trade, not to a whole of trades for different symbols in a specific bar. Am I right?

Beware on backtesting with lot sizes.

The current lot size may not be the historical lot size. Stocks that have undergone a stock split and have adjusted data will have a different lot size prior to the current lot size took effect.

e.g. Current lot size = 100. Stock has a 5:1 stock split with ex-date 1 July 2019. Prices prior to the split are adjusted by a factor of 0.2. The effective lot size prior to the split for the purposes of backtesting is actually 500. A way around this is to have the original close value and calculate your position sizes accordingly per-trade according to your own lot size rules.

2 Likes

Positions are opened in the RANKED order (according to position score). If higher ranked position is NOT open due to lack of funds (or constraints like min shares which accounts for "lack of funds" too) then lower ranked positions are NOT opened. This prevents opening positions for symbols that you did not really want (because they scored lower).
If you want to open them, you have to change your PositionScore so they are ranked higher.

2 Likes

@NorgateData

Thanks for your advice, Richard. I am just trying to deeply understand AmiBroker behaviour, but I didn't think about you are pointing out.

@Tomasz

I appreciate your explanation. I didn't expect that behaviour at all, so I didn't understand those results. It's ok. Knowing that I will modify my afl code to ask AmiBroker what I want... simply not allowing positions under a certain number of shares or an amount of money, but without sacrificing the rest of entries from PositionScore.

Thanks a lot!