free margin check issue
- Use a mutex to compute lot size through open.
Prevent EA from opening trades at the same time across two or more pairs? (Steve) - MQL4 and MetaTrader 4 - MQL4 programming forum - If you are worried about margin and leverage, you are not controlling your risk.
Never risk more than a small percentage of your account, certainly less than 2%
per trade, 6%
total.
In code (MT4):
Risk depends on your initial stop loss, lot size, and the value of the pair.
- You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that
MODE_TICKVALUE is returning a value in your deposit currency, as promised
by the documentation, or whether it is returning a value in the instrument's
base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers. - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
- Use a mutex to compute lot size through open.
Prevent EA from opening trades at the same time across two or more pairs? (Steve) - MQL4 and MetaTrader 4 - MQL4 programming forum - If you are worried about margin and leverage, you are not controlling your risk.
Never risk more than a small percentage of your account, certainly less than 2%
per trade, 6%
total.
In code (MT4):
Risk depends on your initial stop loss, lot size, and the value of the pair.
- You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that
MODE_TICKVALUE is returning a value in your deposit currency, as promised
by the documentation, or whether it is returning a value in the instrument's
base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers. - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
1. I didnt ask you how to prevent my EA from opening trades on different charts at the same time
2. I didnt ask you what my risk should be
I asked whether there could be an issue with margin check when opening positions at the same time on different charts
1. I didnt ask you how to prevent my EA from opening trades on different charts at the same time
2. I didnt ask you what my risk should be
I asked whether there could be an issue with margin check when opening positions at the same time on different charts
@William Roeder has given you the answer - you need a way to ensure that only one EA gets to perform the following chunk of code (highlighted) at any one time, and the use of GlobalVariableSetOnCondition() is the way:
while (!GlobalVariableSetOnCondition("Lock",1,0)) Sleep (100); // Check Free Margin // Open new position GlobalVariableSet("Lock",0);
so if you have two or more charts attempting to perform these actions at the same time, the use of GlobalVariableSetOnCondition() will make sure they line up properly.

- docs.mql4.com
@William Roeder has given you the answer - you need a way to ensure that only one EA gets to perform the following chunk of code (highlighted) at any one time, and the use of GlobalVariableSetOnCondition() is the way:
so if you have two or more charts attempting to perform these actions at the same time, the use of GlobalVariableSetOnCondition() will make sure they line up properly.

- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I want to check my free margin before I open a position. However my EA opens positions at the same time (when markets open).
Now I am afraid, that if my EA on different charts checks free margin at the same time, they will all get returned the same free margin for each chart (because all positions will probably open at the same time).
Has anyone dealt with something like this before?