Topics
Replies
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
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
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
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
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
TonNcie
11 Nov 2019, 10:22
RE:
cTrader team,
Although not every broker supports autochartists it still would be a great addon to cAlgo.
It also might encourage some brokers to support this function.
So please provide access to the autochartist function from within cAlgo,
It would be a great plus for the platform.
So is there alread a date set for this ;-)
please let us now.
@TonNcie
TonNcie
04 Nov 2019, 12:27
RE: Optimization settings
And keep the results sort & position when switching between optimization results; and when you ar at it enable a set-default for sort,columns,position too.
And add a Params column (params in order of paraminput screen separated by space or ;)
@TonNcie
TonNcie
21 Nov 2019, 10:54
it's calculated in Backtesting and Optimization so it;s there. Just make it public. I'd say this is a quick win for CTDN
@TonNcie