EarnForex / PositionSizer

Calculate your position size based on the risk and account size and execute your trades with this free MetaTrader expert advisor.
https://www.earnforex.com/metatrader-expert-advisors/Position-Sizer/
Apache License 2.0
379 stars 151 forks source link

Error in margin calculation for XAUUSD #74

Closed maccieddu closed 8 months ago

maccieddu commented 9 months ago

Hi, i have noticed that in the margin tab the calculation for the specific contract is incorrect... On the specific contract the margin is calculated like this: (trade size contract size price / leverage) margine rate On this particulare broker the leverage is 4 and the margin rate is 3 so the calculation is: (0.07 100 1900.00 / 4) 3 = 9975$ But the position sizer give me the value of (0.07 100 1900.00 / 4) * 2.5 = 8312.5$ that is the value calculated with margin rate maintenace 2.5

Is it possible to add a textbox were insert a custom margin rate alongside the custom leverege that is in place now or let the user chose the margin rate initial or maintenace?

margin image

EarnForex commented 9 months ago

Thank you for letting me know. I will consider adding both calculations.

EarnForex commented 9 months ago

Hello! Could you please let me know which broker is that, so I could test?

maccieddu commented 9 months ago

Hello, it's a prop firm not a broker...

EarnForex commented 9 months ago

Could you please let me know which one? I would ask them for a demo account to test this?

maccieddu commented 9 months ago

Yes sure, the prop is the5ers.

maccieddu commented 8 months ago

Hope you got the demo for testing this fix...

EarnForex commented 8 months ago

Sorry, they refused to provide a demo account. I couldn't find any other broker that have different but non-zero initial and maintenance margin. Besides, I've been busy with another project.

I will soon prepare a fix for this and will send you a beta, so you could see if it works in your account.

maccieddu commented 8 months ago

Ok, i'm very sad that they not provide you a demo to improve a strument like this... i'm looking for the beta to test your proposed fix and i will tell you if its work. I know its very hard to fix thing without testing in first person

EarnForex commented 8 months ago

Hello @maccieddu ! Was this on a netting account or on a hedging one?

maccieddu commented 8 months ago

Hello! Is a hedging one

EarnForex commented 8 months ago

Position Sizer.zip Please try the attached version when you have time and let me know how it works in your account.

maccieddu commented 8 months ago

I'm trying it right now, but i dont see changes in the calculation. I have to enable something? For your info i have downloaded the indicator of max lotage from the prop: https://the5ers.com/downloads/ and the max lot limit is different from what is calculated from position sizer. image

maccieddu commented 8 months ago

In theory the formula would be this (correct me if i'm wrong): (Volume in lots Contract size Open market price / Leverage) Margin Rate? for example: (0.42 100 1923.00$ / 10) 3 = 24229.8 <---- (3 is the initial margin) the account is of 25k

with position size calculator that propose me 0.49 lot max the calculation is this: (0.49 100 1923.00$ / 10) * 3 = 28268.1 <---- (3 is the initial margin)

i think the problem is the margin rate used in the formula

maccieddu commented 8 months ago

Maybe this lines of code are where the margin is calculated and they use maintenace_margin_rate instead of initial_margin_rate image

EarnForex commented 8 months ago

Attached a wrong file in my previous (deleted message). Please use the one attached here. Position Sizer.zip

maccieddu commented 8 months ago

It seems to be going much better now. image

maccieddu commented 8 months ago

However, I continue to notice very small differences, in the other pairs, between the values calculated with the tool provided by the prop and with the values calculated by the position sizer, at this point I believe that each instrument (forex, indices, metals) has its own formula for the calculation of the required margin. But the situation has improved now image

EarnForex commented 8 months ago

Make sure you are attaching the version that I uploaded in this thread. It should say "ver. 3.062" in the header. The last four screenshots show "3.06".

EarnForex commented 8 months ago

As for the remaining difference (0.41 vs 0.42), it might be attributed to rounding down vs rounding up. But if you notice a bigger difference, please let me know.

maccieddu commented 8 months ago

image

EarnForex commented 8 months ago

Is the initial margin on SP500 and EURUSD also larger than the maintenance margin?

maccieddu commented 8 months ago

image image image

EarnForex commented 8 months ago

Thank you! Is your account in USD?

maccieddu commented 8 months ago

Yes sir.

EarnForex commented 8 months ago

The Position Sizer uses Free Margin for max position size calculation. Perhaps, you have open trades and the firm's calculator is using balance or equity for its calculation? Could you please check?

maccieddu commented 8 months ago

I send you screenshot with no trade open, so equity is equal to balance

maccieddu commented 8 months ago

Perhaps the problem is that every asset have is own margin calculation? https://www.metatrader5.com/en/terminal/help/trading_advanced/margin_forex If you read in the last screen shot on the row: calculation, one have type forex and one CFD leverage p.s. im sorry for my bad english, hope is understable

EarnForex commented 8 months ago

Do you know your account's stop-out level? It's another factor considered by the Position Sizer when calculating the Max Position Size by Margin.

maccieddu commented 8 months ago

You are refering to this? https://www.babypips.com/learn/forex/what-is-a-stop-out-level

EarnForex commented 8 months ago

Yes, it's different for each broker. The attached script prints your account's stop-out mode and level when you run it. Could you please let me know what it says? PrintStopOut.zip

maccieddu commented 8 months ago

image

EarnForex commented 8 months ago

And your current free margin is?

maccieddu commented 8 months ago

The account start size is 25000$ now is 24387.90$

EarnForex commented 8 months ago

I think that explains the difference between the two values for PS and the firm's indicator: 24387.9 / 23750 = 2.30 / 2.24 for example

maccieddu commented 8 months ago

Ah ok now sound good, so the position sizer is more reliable of firm's indicator if i understand good?

EarnForex commented 8 months ago

I don't know. The Max Position Size feature in the Position Sizer is designed so that if you take that trade, you will be as close to the stop-out as possible without actually getting stopped out (OK, you'll probably get stopped out anyway due to the spread, but still...).

It looks like their calculator, doesn't consider the stop-out level at all. So, taking a trade size that it offers will likely result in an immediate stop-out regardless the current spread or anything like that.

maccieddu commented 8 months ago

I think you are right, therefore i continue to use position sizer as only reference to risk. So you think that now implement the double visualization of maintenace and initial margin is worth, maybe the initial margin only is the best option for the broker that provide two different margin rate?

EarnForex commented 8 months ago

For now, I think, that when both are given, the best option is to use the maintenance margin only for the Future margin calculation, and the initial margin for everything else. I don't think it's necessary to display the margin rates on the panel for now.

EarnForex commented 8 months ago

Fixed in 3.07.