# Position sizing based on equity value

I am trying to programm positionsize of my system based on equity value. The idea is I inwest 1 future contract for every 10000 pln. So I start with my initial equity of 10000 and 1 contract. When my equity grows to 20000 I invest 2 contract, when it grows to 30000 I invest 3 contracts itc.I also want to decrease the number of contracts when my equity decreases. I used this logic in the code below...

``````SetBarsRequired( sbrAll, sbrAll );
SetOption("MaxOpenPositions",1 );
SetOption("AllowPositionShrinking",True);
SetOption("InitialEquity",10000);
SetOption("AllowSameBarExit",True);
SetOption("FuturesMode",True);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",9);
SetOption("AccountMargin",100);
SetOption("ReverseSignalForcesExit",True);
SetOption("UsePrevBarEquityForPosSizing", True);
PositionSize=-32;

MarginDeposit=3200;
PointValue=20;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;
``````

This is a simple logic I used. It is as simple as possible, maybe to simple. This code at the first glance works, but the problem is according to this code my eguity can not go below 10000, if it does, I will go bankrupct. This is what Monte Carlo shows me. I want to correct this code in the way, that I will go bankrupct only if my equity goes below my margin deposit which in this case is 3200. I wonder if it can be done in the regular bactesting . Any guidance from you how I should proceed further with this code will be appreciated.
Thank you.

Hi,
Why you don't use IIF and setpositionsize?
Example:
If equity is between 3200 and 10000 then use 1 lot,
else positionsize = equity/10000 round it to int

I am refreshing this topic. I am trying to put into egsistance the idea which likhazar gave me about the code. I came up with the following code...

e=Equity();
numbofcontracts = e/10000;
numbofcontracts = int(numbofcontracts);
numbofcontracts= IIf(e>3200 AND e<10000, 1, numbofcontracts);
SetPositionSize(numbofcontracts,spsShares);

But still this code not working. When I do the backtest the report shows me I invest one contract only. Where is my mistake? What did I do wrong with the above code? Could you guide me please?