Futures Backtesting - how to employ a constant leverage

Hello,

I spent some time on figuring this out, but I could not. I would like to test a futures system. ( I was always developing futures system without really using the futures backtesting feature of Amibroker since I did not know how to vary margin deposit ). I would like to test the system going back many years, but I am not able to find a way to vary the margin deposit depending on the index value (margin deposit when index is at 2000 should be totally different compared to the case when index is at 15000 ). If I am able vary the margin deposit to say,20% of contract value at any given time/bar, I would be able to keep the leverage employed at a constant value, no matter where the index is at.

something like below is what am trying to achieve.

PositionSize=-100;
PointValue=20;
MarginDeposit=C*Pointvalue/5;  // This obviously is not correct, but this what I am trying to achieve fundamentally 

Appreciate any input how you guys would do this. ( Workaround I always used is to develop a future system as if trading on say, @NQ# or QQQ (like any stock) and use Account margin box to see the approx. leveraged return. But this obviously is not the accurate way )

Thanks in advance!

As always, please read the manual:
http://www.amibroker.com/guide/h_futbacktest.html

As documented, you can express margin as percentage of contract price

MarginDeposit = -20; // minus means percent of contract price
2 Likes

Thanks, yup I overlooked that line in the document and was looking only at examples. Will use this feature

Hi Tomasz,

I just sat down to try your suggestion. I realized after making the changes and backtesting, the proposed solution does not make sure to employ constant leverage as the margin deposit is expressed as a percentage of contract price and not contract value (i.e. pointvalue is not taken into consideration) when I use the below code.

MarginDeposit = -20; // minus means percent of contract price

Could you please provide additional insight how exactly a constant leverage can be employed?

Thanks in advance!

Can't you just multiply by the point value, like this?

MarginDeposit = -20 * PointValue;
1 Like

thanks for the reply and your suggestion did help. I guess the construct should be the below, right?

leverageToBeEmployed = 20;
MarginDeposit = -(100/leverageToBeEmployed) * PointValue;

No. That is incorrect.
If PointValue is 20 and if you multiply by 20 you would not pay 20% of contract price but you would pay 20*20% of contract price (400% of contract price, so today say 400%*$14000 in case of NQ contract i.e. $56000 to enter single contract)
If you multiply that by (-100/20=-5) then you will effectively use 5% of contract price (5%*14000 = 700) that is way less than required margin for NQ ($16500).

BY ALL MEANS: Instead of asking on forum use DETAILED LOG - it shows all the numbers you need.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

thanks, yeah you are right about the fact that my formulae is wrong and does not achieve the requirement to employ a constant leverage. I believe the below solution from @mradtke

MarginDeposit = -leverageToBeEmployed * PointValue;

is also not correct because here margindeposit is directly proportional to leverage. Intuitively, margindeposit should decrease when leverageToBeEmployed is increased.(this was actually the reason why I tried to bring leverageToBeEmployed to the denominator).

So my little brain can't really get around how to employ a constant leverage in an AFL at all!

I think there is some confusion here because in your original post you said you wanted the margin deposit to be 20% of contract value, and then later you said you wanted to use 20x leverage, i.e. the margin deposit should be 5% of the notional value of the contract. Assuming the latter is what you want, then I think your previous example is correct:

In the case of NQ, which has a point value of 20, this means that the margin deposit will be 100% of the contract price, or currently about 14000. In the case of ES, which has a point value of 50, the margin deposit will be 250% of the contract price, or currently about 4400 * 2.5 = 11000.

As @Tomasz said, using the Detailed Log will allow you to verify whether things are working as you want.

1 Like

sorry for the confusion. That 20% etc was just an example. You are right, my requirement is to just make sure a configurable leverage can be set with the variable leverageToBeEmployed, and use that to make sure a constant leverage is used in the afl throughout the bars.You seem to agree that my solution is correct, but I am also convinced by Tomasz's reply that I am wrong. Let me think it through again, this topic surely gets me confused!

If anyone who is using constant leverage in their AFL to backtest Futures, please do share how you do it.

I guess you are right that my solution is probably correct. I was probably biased already that I am wrong when I saw Tomasz's reply :wink: Neverthelss, I will update after trying out

Precisely, @amiuser doesn't know what he wants or he changes his mind every now and then.

But this "example" was different than what you asked afterwards.
First you are asking for deposit of 20% of contract value, then you are asking for 20x leverage. These are two totally different things.
Don't do that. If you want correct responses you have to be PRECISE in your questions because you are fooling people trying to give you precise response.
You are just wasting people time if you can't make up your mind in first place what you really are asking for.

And really by all means, can't you just do basic Math 101 yourself.

The manual is dead straight about the margin deposit. Let me quote it again:
http://www.amibroker.com/guide/h_futbacktest.html

. You can specify per-symbol margin in the Symbol-Information page (picture above). Positive values describe margin value in dollars, while negative express margin value as percentage of contract price. Margin can be also specified in the formula by using MarginDeposit reserved variable

Is it really difficult to understand this single sentence?

Whatever NEGATIVE number you assign to MarginDeposit is margin value as percentage of contract price, so

if( MarginDepost > 0 )
{
   you pay just MarginDeposit in DOLLARS per single contract;
}
else
{
   you pay CurrentPrice * (-MarginDeposit/100) result is in DOLLARS per single contract;
}
1 Like

please just read the topic headline - "Futures Backtesting - how to emply constant leverage". cant be more clear than that.
Anyway thanks to all who responded and sorry for any confusion. The below does solves the requirement.

leverageToBeEmployed = 20; // type in whatever leverage you want to employ here
MarginDeposit = -(100/leverageToBeEmployed) * PointValue;

I actually tried to add the example to make the topic clearer. Apparently it did not serve the purpose! My intention was always about constant leverage

And my first reply provided actual solution. Expressing margin as percentage of contract price provides everything you need for constant leverage (as the rest is just a matter of scaling). Only afterwards the thread entered muddy waters.