Calculating the Required Margin
Calculating the Required Margin
15 Dec 2020, 00:29
I am trying to calculate the Required Margin for a certain trade, this is a sample bot of what I am trying to do:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class RequiredMarginBot : Robot
{
protected override void OnBar()
{
double volume = 10000d;
double estimatedRequiredMargin = (volume/Account.PreciseLeverage) * (Symbol.Bid);
// if Account Currency matches the Base Currency
if (Account.Currency == SymbolName.Substring(0, 3))
{
Print("Account Currency Matches Base");
}
// if Account Currency matches the Quote Currency
else if (Account.Currency == SymbolName.Substring(3, 3))
{
Print("Account Currency Matches Quote");
}
else
{
throw new InvalidOperationException();
}
double actualRequiredMarginBefore = Account.FreeMargin;
ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volume);
double actualRequiredMarginAfter = Account.FreeMargin;
Print("Account Currency:\t" + Account.Currency);
Print("Symbol:\t\t\t" + SymbolName);
Print("Estimated Required Margin: " + estimatedRequiredMargin.ToString("0.00"));
Print("Actual Required Margin:\t" + (actualRequiredMarginBefore - actualRequiredMarginAfter).ToString("0.00"));
Stop();
}
}
}
I have run this on two pairs and the results are as follows:
First experiment:
Backtesting started
Account Currency Matches Quote
Account Currency: GBP
Symbol: EURGBP
Estimated Required Margin: 302.02
Actual Required Margin: 304.09
Backtesting was stopped
Second experiment:
Backtesting started
Account Currency Matches Base
Account Currency: GBP
Symbol: GBPUSD
Estimated Required Margin: 434.24
Actual Required Margin: 440.33
Backtesting was stopped
As you can see, the estimated are different from the actual. I tried using the Ask rather than the Bid, but that made a minor difference.
What am I missing to have an accurate estimate?
Replies
JeanPaul
17 Dec 2020, 13:44
RE:
PanagiotisCharalampous said:
Hi JeanPaul,
We had a look at this and it is an issue of backtesting. It seems to be using the current prices to calculate the margin instead of the historical ones. We will fix this in a future update.
Best Regards,
Panagiotis
Thank you for getting back to me, I am glad I have highlighted this.
So, while you are fixing this, is this equation right?
double estimatedRequiredMargin = (volume/Account.PreciseLeverage) * (Symbol.Bid);
And should I be using the Symbol.Bid for buying estimates and the Symbol.Ask for selling estimates?
@JeanPaul
PanagiotisCharalampous
17 Dec 2020, 16:25
Hi JeanPaul,
I am not sure why do you multiply with the bid price. The margin is calculated as follows
Margin = (Volume/Leverage) * QuoteSymbolToBalanceSymbolConversionRate
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
17 Dec 2020, 12:03
Hi JeanPaul,
We had a look at this and it is an issue of backtesting. It seems to be using the current prices to calculate the margin instead of the historical ones. We will fix this in a future update.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous