# Positionsize help

Hello,
You could help me with this code ?
I want to establish positionsize buying 50 contracts every € 100,000.
In other words, when the portfolio is € 50,000, buy 25 and when the portfolio is € 100,000, you buy 100 contracts.
In this way, compound interest will increase.
Thank you.

I think something similar to this untested snippet should work. If you're using Futures Mode, then "Price" should be your margin deposit, otherwise it should be your entry price.

``````pctPosSize = Price * .05;     // Simplified equation for pctPosSize = 100 * Price * 50/100000;
SetPositionSize(pctPosSize, spsPercentOfEquity);
``````

Hi, thanks for answering.
I tried with the code that you give me but I can not make it work.
I really need to know how to say to use equity to apply the formula.
It would be something like that, but of course, it is not correct because it gives me error.

I need to know how to say the word "equity" (money from the account at that time to calculate contracts)

``````contracts = (equity*50)/100000;
SetPositionSize (contracts, spsShares);
``````

You need to use CBT (custom backtester)

if below 100k Eq. then pos. size 25
if >= 100k Eq. then pos. size 100
if >= 200k Eq. then add 50 every 100k further increase, 200k -> 150, 300k -> 200, 400k -> 250,... etc..

``````SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) {
/// by fxshrat@gmail.com
// retrieve the interface to portfolio backtester
bo = GetBacktesterObject();
bo.PreProcess();

size = 25;
for ( bar = 0; bar < BarCount; bar++ ) {
//retrieving current portfolio equity
curr_equity = bo.Equity;

// iterating through all trade signals
for ( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) {
// when equity < 100000 then set possize to 25
if ( curr_equity < 100000 )
size = 25;
// when equity >= 100000 then set possize to 100
if ( curr_equity >= 100000 )
size = 100;
// when equity >= 200000 then add 50 every 100k increase
if ( curr_equity >= 200000 )
size = (int(curr_equity/100000) - 1) * 50 + 100;

sig.PosSize = -2000 - size;// shares/contracts pos. size
}

}

bo.PostProcess();
}

//Dummy system
period = 20;
m = MA( Close, period );
Buy = Cross( Close, m );
Sell = Cross( m, Close );

Short = Cover = 0;
``````
3 Likes

Hello,
Sorry it´s my fault, I did not have to explain myself well, it really was something simpler.
It is to make a rule of three, to calculate, for example:

if cash before a trade 72531 € then = (50 * 72531) /100,000 ( 36,23 should be the shares for the trade)
if cash before a trade 168325 € then = (50 * 168325) /100,000 ( 84,16 should be the shares for the trade)

My problem is that I dont know how to say to the formula the portfolio equity and that it adds and subtracts gains and losses of trades.

Thank you so much.

The code that I provided in my first post should work fine if you replace the `Price` variable with your entry price or the margin deposit, as appropriate.

thanks mradtke and fxshrat
I'm very clumsy, could you give me an example for the SP500?
I only manage to buy for € 50000 value and the contracts do not increase, in fact, in the first purchase I should buy 50 fair and start with 39.13.
Of course it's my fault, but I can not make it work, thanks for your patience.

i´ve tried this, and obviously its no correct.

``````SetOption("InitialEquity", 100000 );
pctPosSize = 50 * .05;     // Simplified equation for pctPosSize = 100 * Price * 50/100000;
SetPositionSize(pctPosSize, spsPercentOfEquity);;
``````
``````SetOption("InitialEquity", 100000 );
pctPosSize = BuyPrice * .05;     // Simplified equation for pctPosSize = 100 * Price * 50/100000;
SetPositionSize(pctPosSize, spsPercentOfEquity);;
``````

Thank you very much to all, you are very kind and patient with my clumsiness, since the first answer was well explained, without a doubt, it was my fault for lack of knowledge. Thanks again.
You already buy the contracts correctly, however, now comes a time that cuts the backtest when it takes several years.
Should I set something up so this does not happen? It is a backtest from 1998 to 2018 and in 2014 it now stops operating. However, if you only buy a contract this does not happen.
Thank you.

If your back test stops before the end of the date range, it's often because your equity has gone to zero.

Works perfectly, thanks so much.