Using an array for futures MarginDeposit

Hi,

I currently use a formula for futures position sizing similar to the following, provided by Tomasz (original post: https://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/)

Buy = Cross( C, MA( C, 20 ) ); // some trading rules
Sell = Cross( MA( C, 20 ), C );
//
RiskPerContract = 2 * ATR( 20 );
ApplyStop( stopTypeLoss, stopModePoint, RiskPerContract, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 1;
PctSize =  PositionRisk * MarginDeposit  / ( RiskPerContract * PointValue );
SetPositionSize( PctSize, spsPercentOfEquity )

MarginDeposit is a constant and updated by my data provider as needed. This works well for live trading, however using current MarginDeposit amount for historical backtest seems to give incorrect results. As such, I would like to use a custom array such as, for example, ATR() * PointValue to approximate historical MarginDeposit. I can calculate the array but don't seem to be able to correctly apply it to the above code. Below is one attempt, though I've tried various similar versions.

Any help would be greatly appreciated.
Dan

///////Historical Margin adjustment///////////
periods = param( "Periods", 21, 1, 200, 5 ); 
MyATR = ATR(periods);
Multiplier = param( "Multiplier", 2, 0.5, 5, 0.5 ); 

for( i = 0; i < (BarCount); i++ )
{
MarginDeposit[i] = MyATR[i] * PointValue * Multiplier;
PctSize[i] =  PositionRisk [i] * MarginDeposit[i] / ( RiskPerContract [i] * PointValue]);
}

SetPositionSize( Ref(PctSize, -1), spsPercentOfEquity );

Hello,

technically your code is correct.
If your calculation of margin deposit is not exactly what you expect, you can for example, add your variables to the watch window, or use exploration to understand how it is being calculated.

The loop is also correct but you can replace it with array.

MarginDeposit = MyATR * PointValue * Multiplier;
PctSize =  PositionRisk  * MarginDeposit / ( RiskPerContract * PointValue);
1 Like

Thanks for the reply pmxgs,

I have run an exploration that suggests the custom MarginDeposit and PctSize are being calculated correctly, however the pos size in backtest is still not correct. Without going into too much detail, my investigations suggest the issue is that

SetPositionSize( PctSize, spsPercentOfEquity ); 

is using the LastValue of the MarginDeposit array rather than the relevant array value. (I can provide my reasoning, but thought I would omit to keep this as brief as possible). Assuming this is indeed the source of the issue is there any fix or workaround that you think might work, preferably without using the CBT?

Hi,

I've tested this code and now I see what you question was.
All the calculations are correct, but when margindeposit is an array something in backtest process is not working as expected.
If margindeposit is a scalar (including using lastvalue function) everything works well.

In the example below, at 30/08/2019 MaginDeposit is 4142,72.
In the backtest report, with a Position Value of 46,193.07 in 5 contracts, margindeposit is 9,838.6, which doesn't match with the variable value.
I thought margindeposit could be array, but maybe there are some restrictions...
Maybe someone else can help with this...

forum

Hi pmxgs,

Thanks for looking into this further. Without wasting too much time on it, can you identify where that 9,838.6 figure is coming from? That might help narrow down what's happening.

If anyone else has any insights on using MarginDeposit as an array that would be great.

Thanks again
Dan

I believe you can vary MarginDeposit on array basis only via CBT.

//.... some incomplete mid-level CBT code snippet
		if (sig.IsEntry()) {				
				your_array = StaticVarGet("Your_Array"+sig.Symbol);
				deposit = your_array[i] /*....*/;
				sig.MarginDeposit = deposit;
		}
//....

But also read @Tomasz post from old Yahoo Groups forum:

Source:
https://www.mail-archive.com/amibroker@yahoogroups.com/msg35441.html

2 Likes

Hi @danm ,

In my last reply I typed the amount incorrectly, 9,238.60 is correct value (46,193.07/5) used by backtester.
Thanks to the information provided by @fxshrat I searched for that amount in the margin deposit variable and it's the last value of the array.

forum

So, apparently, when we use array for margin deposit, only the last value of the array is used (in the yahoo groups topic, @Tomasz mentions that the value used is the one when the buy signal occured, but I also don't find that )

In order to achive this, you can follow cbt approach suggested by @fxshrat.

Hi fxshrat,

Thanks for the info and your suggested CBT code. Tomasz is correct about using the exchange's MarginDeposit for actual trading, but I still think a custom array might be useful for backtesting in this case, so I'll have a play around with the mid-level CBT.

Thanks again for your input
Dan

2 Likes