Set Position Size as Total Equity Rounded to the Nearest Thousand

Hi Amibroker Community,

I am trying to code a simple logic for position sizing using 100% of the equity, rounded to the nearest thousand. For example, if the starting balance is $10,000 and the current equity after a few trades is $11,235, I want to use $11,000 divided by the share price for the next position size instead of using $11,235. I came up with the solution below using the Equity() function, but it doesn't seem to provide the correct position size. Could someone provide me with some insights on how to improve this solution? Thank you in advance.

SetPositionSize(round(Equity()/1000)*1000,spsValue);

@datascience, maybe you should use Floor() instead of Round().

SetOption("InitialEquity", 34567); 
rounded = round(Equity()/1000)*1000;
floored = floor(Equity()/1000)*1000;
Title = "Using round: " + rounded + " - Using floor: " + floored;

Hi,

the exact dollar equity amount is only available phase 2 of the backtester.
For what you want to achieve, I would recommend using SetPositionSize( 100, spsPercentOfEquity ) in the first phase of the backtest and then do the rest of the calculations in phase 2, using CBT Mid-level approach. (this link is very helpful, especially example 2 mid-level : [http://www.amibroker.com/docs/Houston2.pdf] )
The docs in Knowledge base are also very helpful.

I didn't check the code below, but something along these lines should achieve what you want.

if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
CurrentPortfolioEquity = bo.Equity;
for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
{
   sig.PosSize = floor(CurrentPortfolioEquity/1000)*1000;
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}
1 Like

Hi beppe, thanks for the response. I used floor as well in my previous testing but doesn't seem to work. Probably it's related to the Equity() function

Thank you pmxgs! Appreciate your insights. I will check out the mid-lv BT

Please note that Equity is a SUM of all open positions value and cash. You won't be able to open position equal to portfolio equity if you have any open positions already.

You should rather use bo.Cash which is the currently available cash for purchases.

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