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.
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.
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).
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.
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.
It seems all rest of my buys stop when AB is valuating the VAL buy.
Here is the AFL code I am using (not a system but just to learn):
SetOption("UsePrevBarEquityForPosSizing", True );
PositionSize = (-50/15);
PositionScore = Ref(MA(Volume*Avg,125),-1);
Buy= DateNum() == 1100104 AND liquido;
Sell= DateNum() == 1100106;
Buy = ExRem (Buy,Sell);
Sell = ExRem (Sell, Buy);
I would really appreciate some help.
See SetPositionSize (use instead of 'PositionSize').
Set your Initial Equity via 'SetOption'.
Thank you for your advice. I have added this two statements to my code instead
Unfortunately, I still have the same problem.
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.
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.
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.
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.
Thanks for your advice, Richard. I am just trying to deeply understand AmiBroker behaviour, but I didn't think about you are pointing out.
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!