free margin check issue

Demosfen
104

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?

William Roeder
27833
William Roeder  
  1. 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

  2. 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.
    1. 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.
    2. 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.)
    3. 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.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP SL is $5/$10/5=0.1 Lots maximum.

Demosfen
104
Demosfen  
William Roeder:
  1. 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

  2. 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.
    1. 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.
    2. 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.)
    3. 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.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP SL is $5/$10/5=0.1 Lots maximum.

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

Seng Joo Thio
1887
Seng Joo Thio  
Demosfen:

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.

GlobalVariableSetOnCondition - Global Variables of the Terminal - MQL4 Reference
GlobalVariableSetOnCondition - Global Variables of the Terminal - MQL4 Reference
  • docs.mql4.com
Sets the new value of the existing global variable if the current value equals to the third parameter check_value. If there is no global variable, the function will generate an error ERR_GLOBALVARIABLE_NOT_FOUND (4501) and return false. If successful, the function returns true, otherwise it returns false. For details about the error call...
Demosfen
104
Demosfen  
Seng Joo Thio:

@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.

Now I get it, thank you