I'm trying to code the position sizing in futures using current equity not margin.

My understanding is that I need to use the custom backtester as the SetPositionSize(10,spsPercentOfEquity) will use the Margin in futures mode. Although it is a clever way to do it, it will give unrealistic results in a long backtest as margins will changes a lot.

I want use a ATR dynamic position sizing based in the current equity. In the custom backtest I tried:

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 ) )
{
dollarVol=ATR(20)PointValue;
PositionRisk=1;
lotSize = int( CurrentPortfolioEquity(PositionRisk/100) / dollarVol );

sig.PosSize=lotSize;

}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}

However, the lotsize is the number of contracts but sig.PosSize is the position size (positive numbers mean dollar value, negative values mean percent of portfolio equity).

So, how can I set the PosSize to the number of contracts/shares?

Using the position sizing based on risk above, how can I retrieve the number of contracts in the exploration for a multi-currency portfolio?

For instance:
AddColumn(PositionSize,"PosSize",1.0); returns the % of equity not the number of contracts

while
Contracts=int((PositionRiskEquity() / ( 100RiskPerContract * PointValue )));
AddColumn(Contracts,"PosSize",1.0);
returns the number of contract of the USD denominated symbols using the final equity value. This is not exactly what I want.

I just need the same number of contracts that is shown in the detailed log (Shares:). I'm sure there is a easy way to retrieve i but couldn't find it.

Moreover, is it possible to customize the backtest detailed log for, say, include the stop-loss level?

Risk-based position sizing requires the current equity to determine the number of contracts. Portfolio equity is not available from an Exploration. So no, you can't report number of contracts from the Exploration when using risk-based position sizing.

Also, please use code tags around your AFL snippets for better readability.

If your futures contracts specify the currency and your database contains currency symbols that can be used to determine the exchange rate, you could make it work.

The margin per lot keeps on changing. For eg in year 1 the margin per lot was $1000. The margin per lot in year 2 was $2000. If the Initial equity is $100000, the position size using spspercentequity might not stay the same.The margin per lot is calculated using the underlying value of Nifty

There is something not right about the statement you have made.

Equity is the amount of Cash you have whereas as "Margin" or "Margin per Lot" is a value fixed by your Broker.

You have to calculate the Margin based on the Contract value or something on those lines and check if cash is sufficient to buy a lot or how many max. lots can be bought.

Ok. So the formula to calculate margin Per Lot
MarginPerLot = ( Current price of the Underlying * Lotsize ) / 15 . The max Number of Lots I can trade will be:
MaxLotsTraded = Current Equity Value / MarginPerLot
My question is How do I get the Current Equity Value?

Hi All, I read through this post, provided links and some other related topics, still have a bit of difficulty to understand how to do position sizing based on risk.
assume I have an SL distance (variable based on trade conditions) and I want to risk 1% of the total current equity in each trade.

Looking at the numbers in the example given in the article, if we denote MarginDeposit by M, we get:

PctSize = 1% * $M / (10 * $50) = M/500 %
M/500 % of $1,000,000 equity is $20 * M.
So with margin deposit of $M, we can buy $20 * M / $M = 20 contracts.

This is the same number in the example. So the number of contracts is independent of MarginDeposit (M). So it seems using any value of M will suffice for backtest purposes? Is my understanding correct?

There is one caveat: the backtest report has a column PositionValue = MarginDeposit * Qty. This is not independent of M. However, this seems to be an issue that can be ignored for most practical backtest purposes?