Scaling out based on portfolio equity

Hello,

I have two doubts about scaling out, based on the following code:

ScaleOutProfit = 100;
SymbolProfit   = 100 * (Equity(1) - ValueWhen(Buy, Equity(1)))/ValueWhen(Buy, Equity(1));
InTrade        = Flip(Buy, Sell);
ScaleOutSignal = ExRem(InTrade AND SymbolProfit > ScaleOutProfit, Sell);
Buy = Buy + sigScaleOut * ScaleOutSignal;

SetPositionSize(12.5, spsPercentOfEquity);
SetPositionSize(50, IIf( Buy == sigScaleOut, spsPercentOfPosition, spsNoChange));

1)

What I tried with this code was selling 50% of the position when the security reaches a profit of 100%. however when I run the code I get a different result, for example:

WMB is reduced from 2 to 0 shares
Captura

MSFT is reduced from 6266 to 1 shares
Captura2

What I am doing wrong?

2)

My original intention was scaling out when the security position exceeds a percent of the total portfolio equity. In the previous example, the original position is 12.5% of the total Equity. I would like to scale out when the security position exceeds 25% of the total Equity, I would programme the condition like this:

ScaleOutPosition  = 25;
PortfolioEquity   = ¿?
EquityProportion  = 100 * (Equity(1)/PortfolioEquity);
InTrade           = Flip(Buy, Sell);
ScaleOutSignal    = ExRem(InTrade AND EquityProportion > ScaleOutPosition, Sell);
Buy = Buy + sigScaleOut * ScaleOutSignal;

SetPositionSize(12.5, spsPercentOfEquity);
SetPositionSize(50, IIf( Buy == sigScaleOut, spsPercentOfPosition, spsNoChange));

But I can't figure out how to calculate "PortfolioEquity".

I have searched it out in the forum, I found a similar doubt in the following topic How do I scale position numbers based on growing portfolio equity, where @mradtke mentioned that it could be done with CustomBacktest.

Well, at this point I have an additional doubt, which is how to get the current Equity from the CustomBacktest. I have read the following topics:

https://www.amibroker.com/guide/a_custombacktest.html
https://forum.amibroker.com/t/bo-equity-gives-equity-at-the-end-of-last-bar/5792/4
https://forum.amibroker.com/t/current-portfolio-equity-and-cash/10776/11

but there is something which it's not clear for me yet. I guess it is very easy to the experts in this forum, but I can't figure how I get the current Equity from the following code...

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest();
    bo.Equity
}

to the line that comes later.

PortfolioEquity = ¿?

I'll appreciate any help from the rest of users.

Regards.

1 Like

You can not use Equity() function like that!
You can not base trade decisions on it (so it should not be placed in Buy/Sell/.. rules). It is for calculations after the facts (so placed below of all Buy/Sell/... rules). Also it is single security equity.

bo.Equity is bar-by-bar portfolio equity.

Example in KB
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

Example(s) in users library (Note: codes in users library may contain mistakes)
http://www.amibroker.com/members/library/formula.php?id=1409

2 Likes

fxshrat,

Thanks for your answer.

I added the code from http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/ to my system, it was exactly what I needed.

Regards

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.