I am expecting 988 shares to be left, not 989 as shown. I am guessing it is a rounding problem in the scaleout processing which I haven't touched (well not that I know of).
values in AFL procedure
CBT code and trace ouput
I notice the trace value in the CNT is showing isScale -1m is that correct?
FORMULA is missing from your post. Unfortunately your question isn't clear enough and does not provide all necessary details to give you an answer. Please follow this advice: How to ask a good question
To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?
you need to use DETAILED LOG.
I didn't include the code as I thought the _trace outputs would show the problem, which is a variation on the number of shares to be scaled out and the actual number used.
From the detail log, I can see it is changed to 271 instead of 272.
In the AFL code, the posSize is calculated as scalOutVolume X sellPrice which is correct. I am guessing the positionSIze is then used later to re-calculate back to the number to be scaled out using the sellPrice..
Of course I can include the entires code but at the moment I just need some clarity on what happens in the CBT to derive the num of remaining shares from the price and posSize.
What AmiBroker is doing is correct. You are getting 217 shares because in your calcuation of PosSize you forgot about COMMISSION. That one share less is due to COMMISSION that must be included in posSize when expressed in dollars. If you want fixed number of Shares you should use spsShares in SetPositionSize.
scaleOutVolume * SellPrice; // wrong does not include commission
SetPositionSize( scaleOutVolume, spsPercentOfPosition * ( Buy == sigScaleOut ) );// that is proper way to use shares
That is why posting formula is crucial. It is easy to spot mistakes if formula is given.
That problem appears correct now, but when I do a sell for all remaining shares the next day the report is showing the shares as though they are not sold
I believe the problem is that sig.posSize appears empty on the following day
Code from AFl
If you read the docs, you would notice that passing zero to SetPositionSize in second argument means spsNoChange. So the line above will NOT CHANGE values of array for bars that are not scale-outs.
You have to set the values for position size for ALL OTHER BARS as instructed in the manual:
// the default for all bars
SetPositionSize( 10, spsPercentOfEquity );
// special case for scale outs only
SetPositionSize( scaleOutVolume, spsShares * ( Buy == sigScaleOut ) );
If you fail to do that, non scale-out bars will be "unchanged" and if they were not set before they will be Null.