Using cash only for position size

I have read many posts about this subject, and I'm still confused about the solution. Please note that I have been trading for 22 years, and although my programming skills are not good, I know a few things about trading, although I am not an expert.

I cannot, in general, use open equity to buy shares, only cash. I do not use margin. Yet, the standard Amibroker position size method uses open equity to size positions. This is a simple example below with 7 ETFs and a simple entry and exit method that is irrelevant. If I use the following position size method:

SetPositionSize( 100/7, spsPercentOfEquity);

for a backtest that starts on January 3, 2007, I first get four positions triggered, as shown below.

The fifth position is triggered on January 2, 2008, while all the previous four positions are open with a gain of $7,615 at the close of December 31, 2007. The initial capital was $100,000.

The position value of the fifth position on January 2, 2008, is shown as $15,370.53. This means Amibroker is using the open equity to size the position as follows: $107,615/7 = $15,373

But this is not correct. You cannot use open equity to size positions because that is unrealized profit and the cash is not available. The available cash in the account is $57,175; there are up to three more potential positions to take after the first four. In this case, only one was triggered on January 2, 2008, but it could have been all three.

I am asking what the solution is to this and why using open equity is the default mode when cash should have been the default mode because open equity cannot be used to buy shares unless you are an institution or in a position to post collateral or borrow funds through margin.

I know there are solutions based on CBT. I have tried a few, but they do not work in the form I tried. In this particular example, the proper position value for the DBC trade on January 2, 2008, should be around $14,280 based on the initial equity of $100,000.

I found the following mid-level CBT to be a promising solution, as per this post: https://forum.amibroker.com/t/position-sizing-on-available-cash/21152

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) 
{
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)

    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {	
              // do your custom signal processing
              sig.PosSize=bo.Cash/7;
        }	
        
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
    }	  
      
    bo.PostProcess();	//  Do post-processing (always required)
}

The problem is the remaining cash is divided by the number of positions and I need to account for already existing open positions. Any ideas? Thanks in advance.

Please read the manual AFL Function Reference - SETPOSITIONSIZE

spsPercentOfEquity uses equity (value of cash plus value of open positions) because it makes sense, while your approach doesn't.

Say you want 4 positions, then you use spsPercentOfEquity to allocated 25% of equity in each position. This way you get pretty much equal allocation of funds (and that is desired outcome with same percentage specified).

If that would use percent of cash instead, you would get UNEQUAL allocation even if your percent is the same and cash would never be used

Say you have $10000 in Cash at start.

1st position = 25% of $10000= $2500, left $7500 in cash
2nd position = 25% of $7500 = $1875, left $5625 in cash
3rd position = 25% of $5625 = $1406, left $4193 in cash
4th position = 25% of $4193 = $1048, left $3144 in cash

You see the problem - unequal allocation and cash is never allocated fully.

As to "you cannot open position" statement - it is WRONG. If you are using the DEFAULT SETTINGS (i.e. "AllowPositionShrinking" turned ON), the position WILL BE OPEN upto maximum available cash even if desired position size was larger.

2 Likes

I think your CBT solution would work if you just changed this:

sig.PosSize=bo.Cash/7;

to this:

sig.PosSize=bo.Cash / (7 - bo.GetOpenPosQty());

In other words, divide your cash by the number of potential remaining entries.

2 Likes

Say you have $10000 in Cash at start.

1st position = 25% of $10000= $2500, left $7500 in cash
2nd position = 25% of $7500 = $1875, left $5625 in cash
3rd position = 25% of $5625 = $1406, left $4193 in cash
4th position = 25% of $4193 = $1048, left $3144 in cash

You see the problem - unequal allocation and cash is never allocated fully

Be assured, I have spent hundreds of hours on this problem. I have read literally everything. It depends on how the program calculates positions. As you can see from my example, where I want up to 7 open positions, the allocation is correct for the first four signals, and not done according to your example. But for the fifth signal, it uses open equity.

Your example involves sequential allocation when it should maybe be parallelized in portfolio trading.

But the point remains, you cannot use the open, unrealized equity to size positions. Is there a solution?

spsPercentOfEquity uses equity (value of cash plus value of open positions) because it makes sense, while your approach doesn't.

I know that, and this is the problem. You cannot use the value of open positions to buy shares. My approach? I still do not have any, but using the value of open positions is wrong, unless you liquidate everything and start all over again with every new position. Maybe this could be done in low level CBT.

I am sure it can be done in Amibroker if we accept there is an issue and look for a solution.

@mradtke

What you are suggesting does not work. You have to consider the cash before opening the previous positions and then allocate it to the new signals based on the available cash left, i.e., the difference. If there is a profit, then the allocation for my example would still be 100,000/7, but not what Amibroker does when using open equity.

You have to realize that using open equity to size positions is akin to pyramiding. It exacerbates the drawdown when there are corrections in equity. Plus, and more importantly, retail cannot use open equity to size positions.

It is totally UP TO YOU how you calculate the value of position to be entered.

You do NOT need to use percent of equity. Do your own calculations (whatever you wish) and instead of spsPercentOfEquity, use spsValue or directly assign sig.PosSize in DOLLARS, not percent.

You can also express position size in shares/contracts.

No-one is going to do your homework. Do the calculations whatever you want and assign dollar value. The program itself has nothing to do with your calculations. @mradtke has shown you some approach. If you don't like it, do your own calculation.

1 Like

The solution I suggested does not use open equity, it only uses cash. Changes in the value of an open position do not affect your cash balance until the position is closed.

That is incorrect. The bo.cash value is the "available cash left". There is no reason to track the cash before opening the previous positions. Plus, using that logic would mean that you could only evaluate your total equity when your account was flat, i.e .no open positions at all.

Pyramiding usually applies to purchasing more of the same trading instrument, which is not what we're talking about here. Sizing your new positions while taking open equity into account is more like "preemptive compounding", and it can absolutely be done in a retail account with standard 2:1 leverage assuming that I don't typically use all of the available margin. If you are trying to model trading in a cash account like an IRA (US) or Superfund (Australia), then what you said is true.

2 Likes

If you use margin to compound open equity via position size increase, you pay interest. It has to be accounted for in the backtest. Things get complicated because loan rates vary with time. Performance may not be realistic without the interest charged on margin use.

I will follow Tomasz's suggestions to use values in position size. Still, I believe the percent case could be solvable, but the CBT could get a little complicated. If you have open trades with profits in excess of the initial cash, even using margin may not make any sense for spsPercentOfEquity.

Thanks for the effort to solve this.

Not sure why you think the CBT could "get a little complicated". You already posted the entire CBT needed, and I provided the one minor modification required to make it work exactly as you described. At the very least, you should try it out and see if you can identify any issues with the position sizing. This approach would not require margin in live trading, so historical fluctuations in margin interest are not an issue.

3 Likes