Required margin, taken margin and reported margin all differ

Created at 06 Nov 2019, 18:22
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TO

TonNcie

Joined 23.01.2019

Required margin, taken margin and reported margin all differ
06 Nov 2019, 18:22



I have a usd 25/mil 1 way commission 1:400 account

trade CADJPY

I calculate Required Margin with

double RM = VolumeinUnits / PreciseLeverage * Bid * Symbol.TickValue / Symbol.TickSize;

Result:
RM is 59.34 for 31000 units

I place the order

and the margin drops with 63.10

the reported margin in the Position Info screen is $ 58.83

- Why these differences? (except for filling differences)

- What do i miss in the RM calculation?

 


@TonNcie
Replies

PanagiotisCharalampous
07 Nov 2019, 08:15

Hi El Antonio,

Can you please explain why do you use the Bid price in the calculation? It seems redundant to me.

Best Regards,

Panagiotis


@PanagiotisCharalampous

srubtsov
07 Nov 2019, 10:04

Usually, margin calculates for USD volume. Also usually symbol has dynamic leverage.


@srubtsov

TonNcie
07 Nov 2019, 10:56

RE:

Hi Panagiotis.
What is the definition for monetaryvalue as stated in the docs?

eg. When trading EURJPY on a USD account, What is the monetaryvalue(tickVlaue) in  USD or JPY or EUR. The docs are not clear about that.


@TonNcie

TonNcie
07 Nov 2019, 10:57

RE:

Hi srubtsov

I can't match your reply to my question. What do you mean to say?

 


@TonNcie

PanagiotisCharalampous
07 Nov 2019, 10:58

Hi El Antonio,

The tick value represents the value in the account's currency. So in this case it is in USD.

Best Regards,

Panagiotis


@PanagiotisCharalampous

TonNcie
07 Nov 2019, 11:01

Ok and is that the price using the bid or the ask?


@TonNcie

PanagiotisCharalampous
07 Nov 2019, 11:04

Hi El Antonio,

The value is calculated using an internal conversion engine based on the bid prices of the symbols.

Best Regards,

Panagiotis


@PanagiotisCharalampous

TonNcie
11 Nov 2019, 10:36

Hi Panagiotis, 
Thanks for your answer. It helps but still the results differ.
Could you please reveal the way the taken margin is calculated when taking a position in a base currency that is not equal to the account currency?
So we can adjust our required margin calculator to closely match the taken margin.

Best Rgds
El A

 


@TonNcie

PanagiotisCharalampous
11 Nov 2019, 10:57

Hi El Antonio,

This post might be helpful for you.

Best Regards,

Panagiotis 


@PanagiotisCharalampous

TonNcie
11 Nov 2019, 12:52

RE:

Hi Panagiotis,

I'd seen that post and studied it.
Even with adaptions regarding leverage (taking into account the symbol.dynamicleverage and using tickvalue instead of calculating the SymbolRate ) also these results differ!
So still a formula on how you guys exactly calculate the taken margin would be verry helpfull.

Best Rgds,
El A

 


 

 


@TonNcie

PanagiotisCharalampous
11 Nov 2019, 15:28

Hi El Antonio,

The formula to get the margin required is simple and it is the following

Required Margin =  (Volume / Leverage) * Conversion Rate 

where the conversion rate is the rate between the base symbol and the account currency. There is no simple general way to get this conversion rate. Internally in cTrader we have a engine that gets the rate between any two symbols using a conversion chain. The conversion chain is available in Open API using the ProtoOASymbolsForConversionReq message. This conversion chain is not available in cTrader Automate API at the moment therefore I do not have an easy solution for you.

Best Regards,

Panagiotis


@PanagiotisCharalampous

TonNcie
11 Nov 2019, 21:16 ( Updated at: 21 Dec 2023, 09:21 )

Example of Difference Calculated margin and taken margin

Hi Panagiotis,

Just to show you the outcome of the required margin and the taken margin and the unexplainable difference (meaning i can't explain it but certainly you folks can)
I'd like to be able to calculate the real TAKEN margin in my money management module.
So teh question is
What constitutes the difference !
Best rgds,
El A.

 

        protected override void OnStart()
        {
            double CrossBid = 1;
            double Volume = 30000;
            string Cross = Symbol.Name.Left(3) + Account.Currency;
            if (Symbol.Name.Left(3) != Account.Currency)
            {
                Symbol CrossSym = Symbols.GetSymbol(Cross);
                if (CrossSym != null)
                    CrossBid = CrossSym.Bid;
                else
                {
                    Cross = Account.Currency + Symbol.Name.Left(3);
                    CrossSym = Symbols.GetSymbol(Cross);
                    CrossBid = 1 / CrossSym.Bid;
                }
                Cross = CrossSym.ToString();
            }
            double StartMargin = Account.FreeMargin;
            Print("Account Currency ", Account.Currency);
            Print("Cross Symbol \t", Cross.ToString());
            Print("CrossBid2Use  " + CrossBid.ToString("F5"));
            Print("Current Symbol \t", Symbol.ToString());
            Print("Account Leverage \t", Account.PreciseLeverage);
            Print("Symbol Leverage \t", Symbol.DynamicLeverage[0].Leverage);
            double UsedLeverage = Math.Min(Account.PreciseLeverage, Symbol.DynamicLeverage[0].Leverage);
            Print("Used Leverage \t", UsedLeverage);
            Print("Free Margin \t", StartMargin.ToString("N2"));
            Print("Volume in Units \t", Volume);
            double rm = Volume / UsedLeverage * CrossBid;
            Print("Required Margin in " + Account.Currency + "\t", rm.ToString("N2"));
            TradeResult tradeResult = ExecuteMarketOrder(TradeType.Buy, Symbol.Name, Volume);
            Print("Free Margin \t", Account.FreeMargin.ToString("N2"));
            Print("Taken Margin \t", (StartMargin - Account.FreeMargin).ToString("N2"));
            Print("Difference \t", (100 * (StartMargin - (rm * CrossBid)) / Account.FreeMargin).ToString("N2"), "%");
            Print("Position Commissions \t", tradeResult.Position.Commissions.ToString("N2"));
            Print("Position EntryPrice\t", tradeResult.Position.EntryPrice.ToString("F5"));
            Print("Position GrossProfit \t", tradeResult.Position.GrossProfit.ToString("N2"));

            Stop();

        }

Output:

Prder info


@TonNcie

PanagiotisCharalampous
12 Nov 2019, 08:36

Hi El Antonio,

The problem is here

 Print("Taken Margin \t", (StartMargin - Account.FreeMargin).ToString("N2"));

To calculate the taken margin you need consider the net PnL as well. So the equation should be Taken Margin = Start Margin - Free Margin + PnL. If you check the margin used by the position in MarketWatch you will see that your calculation is correct. 

Best Regards. 

Panagiotis


@PanagiotisCharalampous

TonNcie
12 Nov 2019, 10:16

RE:

Hi Panagiotis,

Lets wrap it up

The correct formula to calculate the required marging is NOT

Required Margin =  (Volume / Leverage) * Conversion Rate

but more like  :

(volumeInUnits / UsedLeverage * (CrossBid + MySymbol.Spread)) + (CrossBid * (Commission1WayPerM / 1000000) * volumeInUnits) + (volumeInUnits * MySymbol.Spread);

So an extention method like the one here below [with your own cross ofcourse ;-) ]  in the robot or Symbol or Account class would be very nice in a next release.

  public static class extention
    {
        public static double Required_Margin(this Robot MyRobot, Symbol MySymbol, double volumeInUnits, TradeType tradeType, double Commission1WayPerM)
        {
            string Cross = MySymbol.Name.Left(3) + MyRobot.Account.Currency;
            double UsedLeverage = Math.Min(MyRobot.Account.PreciseLeverage, MySymbol.DynamicLeverage[0].Leverage);
            double CrossBid = 1;
            if (MySymbol.Name.Left(3) != MyRobot.Account.Currency)
            {
                Symbol CrossSym = MyRobot.Symbols.GetSymbol(Cross);
                if (CrossSym != null)
                    CrossBid = tradeType == TradeType.Buy ? CrossSym.Bid : CrossSym.Bid;
                else
                {
                    Cross = MyRobot.Account.Currency + MySymbol.Name.Left(3);
                    CrossSym = MyRobot.Symbols.GetSymbol(Cross);
                    CrossBid = 1 / (tradeType == TradeType.Buy ? CrossSym.Bid : CrossSym.Bid);
                }
                Cross = CrossSym.ToString();
            }
            return (volumeInUnits / UsedLeverage * (CrossBid + MySymbol.Spread)) + (CrossBid * (Commission1WayPerM / 1000000) * volumeInUnits) + (volumeInUnits * MySymbol.Spread);
        }
    }

If this is not correct please correct me, it it is please state so !
At the end we'll have a concensused answer on how cTrader calculates the required margin and we can all update our MoneyManagement routines.

Best regards
El Antonio


@TonNcie

PanagiotisCharalampous
12 Nov 2019, 10:28 ( Updated at: 21 Dec 2023, 09:21 )

Hi El Antonio,

The formula I provided is correct. See below using your own algo

I am not sure why you do all the rest things you do.

Best Regards. 

Panagiotis


@PanagiotisCharalampous

TonNcie
12 Nov 2019, 10:44

RE:

Hi Panagiotis
I need to know the 'taken margin' because for me it is important to know how much my margin drops when i place an order. That is the real margin an order will cost you.
As you can see in my example the margin does not drop with the amount you circled but with 68.59 which is about 3.6% more.
if you have a freemargin of 67.00 and you use your calculation which says that you only need 66.21 for your position your order will get cancelled on lack of free margin.
That's why i.m.h.o. you need to use the 'taken margin' calculation. 
And as all traders know sometimes a few percent can make the difference!
Best rgds,

El A

 


@TonNcie

PanagiotisCharalampous
12 Nov 2019, 10:55

Hi El Antonio,

if you have a freemargin of 67.00 and you use your calculation which says that you only need 66.21 for your position your order will get cancelled on lack of free margin.

I am not sure this is true. As far as I know in this case the order will be placed and the margin level will fall a bit under 100%.

Best Regards. 

Panagiotis


@PanagiotisCharalampous

TonNcie
12 Nov 2019, 11:17 ( Updated at: 21 Dec 2023, 09:21 )

RE:

Hi Panagiotis,
Well That is where my question started. I needed to know what the difference (Look at the drop in free margin) was

 


@TonNcie

PanagiotisCharalampous
12 Nov 2019, 11:20

Hi El Antonio,

I have explained here why this happens.

Best Regards,

Panagiotis


@PanagiotisCharalampous

TonNcie
12 Nov 2019, 11:41

RE:

Yep and that's why I now have a requiredmargin function that is closer to the real taken margin than the requiredmargin function i used before :-)
Thanks.


@TonNcie