# AccountFreeMarginCheck()

327

Hi,

Using the function AccountFreeMarginCheck() before placing an order and controling the result after the position is open I can see a slight difference. Where does it comes from?

Here is an exemple from a real account I use for testing:

FreeMargin before the opening =986.3265   MarginCheck= 933.2923
open #11169786 sell 0.04 GBPUSD at 1.50210 sl: 1.51028 tp: 1.49358 ok
slippage = 0.0 pip, commission = 0.32 for that position

FreeMargin after the opening =932.7305   MarginCheckfor next order = 879.6945

Moderator
10566

Possibly AccountFreeMarginCheck() doesn't take into account the spread.
15574

Jacques366: I can see a slight difference. Where does it comes from?
The moment you open a buy, you are now at a loss (the spread) and free margin has been reduced If you opened a sell, they would match until you closed the order where you pay the spread.
327

I have taken into account the spread (even x 2) , the slippage (even though it was already part of the Lotsize calculation), I use the pip value of the StopLoss level but I still being stoped out.

I still do not understand, there must be something I do not get or something else to be considered in this calculation.

Here is the code I use for the tests:

```   double OperationSize                 = NormalizeDouble(NewOSize*Ocount,2);
double FreeMarginCheck               = AccountFreeMarginCheck(Instrument,localOType,OperationSize);
double pipvsl = 10/(PV1SL+(CurSPREAD*IntPOINT));  // calculation of the pip value with ask price at the stop loss level
FreeMarginCheck = FreeMarginCheck - (slippage*Ocount*NewOSize*pipvsl) - (CurSPREAD*Ocount*NewOSize*pipvsl*2);
double Cushion = 10*Ocount*NewOSize*pipv;   // needed cushion of 10 pips
Print("OSizeOK>>>    FreeMarginCheck)=",FreeMarginCheck,"   pipvsl=",pipvsl);```
```   Print("OSizeOK>>>    FreeMarginCheck)=",FreeMarginCheck,"  AccountStopoutMode()=",AccountStopoutMode(),"  AccountEquity()=",AccountEquity()
,"  ",AccountStopoutLevel(),"  >  ",((FreeMarginCheck/AccountEquity()) * 100),"   MNT: ",AccountEquity()*AccountStopoutLevel()*(0.01),"  >  ",FreeMarginCheck*AccountStopoutLevel()*(0.01)
,"  OperationRisk= ",OperationRisk,"  AccountEquity()-FreeMarginCheck= ",AccountEquity()-FreeMarginCheck
);
double MarginRequired = CurMarginRequired*NewOSize;
Print("OSizeOK>>>    MarginRequired=",MarginRequired);```

The print in the journal I get in return:

```>>> NewOSize=0.98   Ocount= 1   SO=0 100  MC=100
>>>    BrokerStopOutPercentage=100  OperationRisk=586.1006  CurrentRiskSize=0  AccountBalance()=10000  ResultantPercentage=5.861
>>>    FreeMarginCheck)=94.5613   pipvsl=10.7591
>>>    FreeMarginCheck)=94.5613  AccountStopoutMode()=0  AccountEquity()=10000  100  >  0.9456   MNT: 10000  >  94.5613  OperationRisk= 586.1006  AccountEquity()-FreeMarginCheck= 9905.4387
>>>    MarginRequired=0```

Here is the test I do to avoid the Broker stopout: (with this Broker AccountStopoutLevel() = 100 so it's not active but on another account it's = 30)

`double AccountStopoutLevelDouble = AccountStopoutLevel();   `
```if (AccountStopoutMode() == 0)    // it's the case
{if ((AccountStopoutLevel() < 100  &&  NormalizeDouble((AccountStopoutLevelDouble-1)-((FreeMarginCheck/AccountEquity()) * 100),2) >= 0)     // 1% margin on AccountStopoutLevel (I tried to give a cushion of x%)
{
Print("OSizeOK>>>  rejected 3    NewOSize=",NewOSize,"  AccountStopoutLevel()=  ",AccountStopoutLevelDouble,"  >  (FreeMarginCheck/AccountEquity()) * 100=",(FreeMarginCheck/AccountEquity()) * 100);
return(false);
}
}

else it's considered to be OK for opening```

At the opening: (one print before and one after to show the free marfin / marginCheck:

```                                          FreeMargin before the opening =10000   MarginCheckfor next order = 200   pipv=10.1366
2       10:30:20        2015.02.04 10:30  EA USDCHF,M1: open #1 sell 0.98 USDCHF at 0.92352 sl: 0.92945 tp: 0.91365 ok
0       10:30:20        2015.02.04 10:30  EA USDCHF,M1: slippage = -0.2
0       10:30:20        2015.02.04 10:30  EA USDCHF,M1:   FreeMargin after the opening =162.87   MarginCheckfor next order = -9637.13   pipv=10.1366

... and  stopped because of Stop Out```
Moderator
10566

Please limit the length of the lines in your code. Long lines make the post and code difficult to read as need to keep scrolling left and right.
327

GumRai:
Please limit the length of the lines in your code. Long lines make the post and code difficult to read as need to keep scrolling left and right.
Sorry, I will, I do not have the problem here.
327

Hi,

The solution was to get the FreeMarginCheck with the function, then actually substract the spread value and finally substract the operation risk.

If the result is greater than zero, there won't be a stopout.

Thanks

256

.