Scaleout rounding

result of prev days run (appears correct)
image

next day I am scaling out 272 shares a 0.85
image

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
image

CBT code and trace ouput
image

image

I notice the trace value in the CNT is showing isScale -1m is that correct?

Thanks for any hints on what to look at,
Gary

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.

1 Like

Thanks Tomasz.

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.
image

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.

Sorry, the code snippet in the AFL



buy = IIf( scaleOutVolume > 0, sigScaleOut, buy );
sell = IIf( Buy == sigScaleOut, false, sell );
//buyPrice = IIf( Buy == sigScaleOut, close, buyPrice );

_TRACE( "buyPrice "  + buyPrice );
_TRACE( "sellPrice "  + sellPrice );
_TRACE( "scaleOutVolume " + scaleOutVolume );
PositionSize = IIf( Buy == sigScaleOut, scaleOutVolume * SellPrice, positionSize );
_TRACE( "PositionSize " + PositionSize );

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.

1 Like

Thanks,
I had read about that but what threw me was a line in
Portfolio-level back testing (amibroker.com)

Scaling size is defined by PositionSize variable which in case of scaling defines not absolute positionsize but dollar increase or decrease.

Cheers,
Gary.

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

image

I believe the problem is that sig.posSize appears empty on the following day
Code from AFl

 
_TRACE( "buy "  + buy ); 
_TRACE( "sell "  + sell ); 
SetPositionSize( scaleOutVolume, spsShares * ( Buy == sigScaleOut ) ); 
_TRACE( "PositionSize " + PositionSize );

code from CBT

for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
        {

            currentEntryDate = sig_dt[bar];

            _trace( "-----------------------" );
            _trace( StrFormat( "Sig Entry Date (%s): %s", sig.Symbol, DateTimeToStr( currentEntryDate ) ) );
            _TRACE( " isEntry " + sig.isEntry );
            _TRACE( " isExit " + sig.isExit );
            _TRACE( " isScale " + sig.isScale );
            _TRACE( " RoundlotSize " + RoundLotSize );
            _TRACE( " PosSize      " + sig.PosSize );
            _TRACE( " Price        " + sig.Price );

The first 4 lines of the trace show that the position size is set but it is lost by the time it reaches the CBT code.
image

I appreciate your help,
Gary.

You need to READ THE DOCUMENTATION. Always
http://www.amibroker.com/f?setpositionsize

The single line I provided addressed setting for scale OUTS only:

SetPositionSize( scaleOutVolume, spsShares * ( Buy == sigScaleOut ) ); 

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.

2 Likes

Thanks for the quick response.

In the trace the position size shows as -45, doesn't that mean it is already set?

PositionSize is NOT used for Sell signal. Sell signal exits entire open position always.

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?

As this document explains you have to use DETAILED LOG report mode.

Thanks for your time and assistance, I think I have a good understanding of what's happening now and fixed my problems.

Gary.