Dynamic Margin Depsoit

Hi Everyone,

I was trying to calculate MarginDeposit dynamically, using the Previous Day's Close Price. Code is below:

Max_lots = 1;
prev_day_close = TimeFrameGetPrice("C", inDaily, -1);

RoundLotSize = 1;
PointValue = 25;
MarginDeposit = prev_day_close * PointValue;
TickSize = 0.05;

SetOption( "FuturesMode", True );  
SetOption("InitialEquity", 500000);
SetOption("MaxOpenPositions", 1 ); 
SetOption("AccountMargin", 100);

PositionSize = Max_lots * MarginDeposit;

However, the Position Value is not giving correct value, when comparing to the Calculated Margin Deposit and Position Value that I put as a Custom Metric and also doesn't seem to be changing as can be seen in the Screenshot of the Analysis below:

Upon further investigation, it seems that the Position Value being taken in the Analysis is being calculated from the Margin Deposit value from the last bar in the data:
Margin Deposit

Can MarginDeposit be dynamic? If yes, how do I set that up and where am I going wrong? Please help.


Recommended reading:

You definitely should NOT have pointvalue included in calculation of margin deposit. That is wrong.


MarginDeposit = prev_day_close * PointValue;

MarginDeposit of course can be dynamic (variable on bar by bar basis). MarginDeposit is a deposit (fraction of actual price) so it is LESS than price, not 25 * price.

1 Like


this issue was also discussed here: Using an array for futures MarginDeposit - #2 by pmxgs

From my experience I also found that when margin deposit is array, apparent it uses LastValue of that array.

If you want to use dynamic margin based on price, simply specify NEGATIVE amount:

MarginDeposit = -1; // =-1 means 1% of price

Hi @Tomasz,

Now I understand that I have been using it incorrectly due to misunderstanding on my part. This is basically what I was trying to emulate:

For buying 1 contract/lot (e.g. lot size is 25), you require the equivalent funds available of (entry price * lot size) / leverage.

I was testing for the leverage of 1, therefore I had put

MarginDeposit = prev_day_close * PointValue;

where PointValue was the lot size. Since each price point moved at 1 contract would give a MTM profit of 1 lot size, I thought this was correct. Obviously, this is not how it works.

So, the question becomes how do I set the lot size in MarginDeposit calculation? And since the Margin requirement set by the exchange changes over time, how do we account for it?

PointValue is NOT Lot size. PointValue is dollar value of profit / loss of single contract position when price of underlying instrument change by 1 (one).

I wrote you re-read the manual carefully

You must NOT "reinterpret" or "assume" things. Variables have strict meaning defined in the guide and ONLY that meaning defined in the guide is correct.

Doing otherwise (out of the blue assumptions and not following the manual) leads to errors.

I understand the mistake I made.

However, lot sizes are not standardized to 100 shares in India as it is in American Exchanges for example. Instead it is full contract value that is standardized. (For example, the Nifty 50 Index has a current lot size of 75. Another index BankNifty, has a current lot size of 25. Lot sizes are usually re-calculated every few months because of course the prices keeps changing. This was initially why I put PointValue equal to the lot size, because if you buy 1 lot of BankNifty, each 1 point movement gives ₹25 profit/loss.

If PointValue cannot be used for lot size in this use case, can you give me an idea of how to set up lot size while calculating let's say 10% MarginDeposit of full contract value as mentioned in the guide for futures back-testing? I would be really grateful for the help.

No, you don't seem to understand. LotSize has absolutely NOTHING to do with PointValue. Lot size is controlled by RoundLotSize variable, which is completely different than PointValue.

You need to carefully read the manual

Thank you Tomasz. I have figured out a way to get what I want done without using PointValue incorrectly. I'll be marking this as solved.

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