Account.Margin not matching formula
Account.Margin not matching formula
02 Jul 2022, 00:53
Hello,
I am still a bit puzzled why Account.Margin is not matching VolumeInUnits * obj.Position.EntryPrice / Leverage, with Leverage = min(Account.PreciseLeverage, Symbol.DynamicLeverage).
I've checked this using an account (1:10) with the same currency (USD) as for the symbol so that no currency conversion is applicable.
- Symbol: NAS100
- Volume: 0.4 units
- Entry price: 6907.2
By running a backtest I got the following:
- Calculated margin (formula above): 276.29
- Account.Margin: 293.92
This is indeed a huge (more than 6%) difference. Am I missing something here?
Thank you.
Replies
ncel01
05 Jul 2022, 19:58
RE:
PanagiotisCharalampous said:
Hi ncel01,
Can you provide us with a cBot that will allow us to reproduce what you are looking at?
Best Regards,
Panagiotis
Hi Panagiotis,
Thanks for your reply.
Below is an example that can be used to check this. Margin is calculated once the position opens and then compared to Account.Margin.
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Margincalc : Robot
{
[Parameter(DefaultValue = 0.0, Step = 0.1)]
public double VolumeInUnits { get; set; }
protected override void OnStart()
{
Positions.Opened += Positions_Opened;
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, VolumeInUnits);
}
private void Positions_Opened(PositionOpenedEventArgs obj)
{
var leverageMin = Math.Min(Account.PreciseLeverage, Symbol.DynamicLeverage[0].Leverage);
var accountMargin = obj.Position.VolumeInUnits * obj.Position.EntryPrice / leverageMin;
Print("Volume in units : ", obj.Position.VolumeInUnits);
Print("Entry price : ", obj.Position.EntryPrice);
Print("Min. applicable leverage : ", leverageMin);
Print("Account margin (calculated) : {0:0.00}", accountMargin);
Print("Account.Margin (API): ", Account.Margin);
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@ncel01
PanagiotisCharalampous
06 Jul 2022, 14:14
Hi ncel01,
Just tried on live execution this and looks fine
On backtesting there is an issue indeed because margin is calculated based on the current rate and not the historical one, hence the discrepancy. It will be fixed in a future version of the application.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
ncel01
06 Jul 2022, 21:39
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi ncel01,
Just tried on live execution this and looks fine
On backtesting there is an issue indeed because margin is calculated based on the current rate and not the historical one, hence the discrepancy. It will be fixed in a future version of the application.
Best Regards,
Panagiotis
Hello Panagiotis,
Thank you for your reply!
Yes, indeed. In real time it looks okay.
However, to make it more simple, and so that no currency conversion is applicable, I have only tested this using an account of the same currency as the instrument. This way, I was really expecting these values to match.
I see you're using a USD account while buying in EUR. If you check this using (for instance) a US indice, you notice that this mismatch still remain when backtesting, although there's no currency conversion involved.
Thank you once again!
@ncel01
ncel01
15 Jul 2022, 08:54
( Updated at: 21 Dec 2023, 09:22 )
RE: RE:
ncel01 said:
PanagiotisCharalampous said:
Hi ncel01,
Just tried on live execution this and looks fine
On backtesting there is an issue indeed because margin is calculated based on the current rate and not the historical one, hence the discrepancy. It will be fixed in a future version of the application.
Best Regards,
Panagiotis
Hello Panagiotis,
Thank you for your reply!
Yes, indeed. In real time it looks okay.
However, to make it more simple, and so that no currency conversion is applicable, I have only tested this using an account of the same currency as the instrument. This way, I was really expecting these values to match.
I see you're using a USD account while buying in EUR. If you check this using (for instance) a US indice, you notice that this mismatch still remain when backtesting, although there's no currency conversion involved.
Thank you once again!
Hello,
Is there any update on this?
Apparently there is no issue when backtesting currency pairs.
@ncel01
PanagiotisCharalampous
15 Jul 2022, 09:26
Hi ncel01,
The explanation is the same. The conversion logic applies even if the base asset and the account currency are the same.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
ncel01
15 Jul 2022, 14:15
RE:
PanagiotisCharalampous said:
Hi ncel01,
The explanation is the same. The conversion logic applies even if the base asset and the account currency are the same.
Best Regards,
Panagiotis
Hi Panagiotis,
Considering backtest (same currency for both account and instrument):
I understand it applies since it is considered in the margin calculation formula. However, what I don't really understand is how this can affect margin calculation considering that, in such case, currency conversion ratio is a constant (equal to 1).
It is also not clear to me why the this looks okay for currency pairs but not for indices.
Did you have the opportunity to check this?
Thank you!
@ncel01
PanagiotisCharalampous
15 Jul 2022, 15:55
Hi ncel01,
in such case, currency conversion ratio is a constant (equal to 1).
That's not the case and as I explained it is an issue that needs to be fixed :) What you describe is how it should work, but it doesn't work like that at the moment.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
ncel01
18 Jul 2022, 13:56
RE:
PanagiotisCharalampous said:
Hi ncel01,
in such case, currency conversion ratio is a constant (equal to 1).
That's not the case and as I explained it is an issue that needs to be fixed :) What you describe is how it should work, but it doesn't work like that at the moment.
Best Regards,
Panagiotis
Hi Panagiotis,
I see. Now it's clear that the issue it is not only about the current ratio and the historical one but also the above.
Still, this looks to be an issue for indices but not for currency pairs. However, when currencies match, Symbol.TickValue/Symbol.TickSize always equals to 1 for both backtest and real time, regardless of the instrument.
@ncel01
PanagiotisCharalampous
05 Jul 2022, 12:05
Hi ncel01,
Can you provide us with a cBot that will allow us to reproduce what you are looking at?
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous