Problem with AccountMargin()

To add comments, please log in or register
Vincent
27
Vincent  

Hello,

I have developed an EA that has the potential to open new positions if the margin does not exceed a certain percentage of capital.

To do this test, I use the standard functions balanceAccount () and AccountMargin ().

This works very well most of the time, but occasionally, the EA opens new position without regard to my opening conditions (margin level).
Adding traces in this EA, I've realized that sometimes after closing a position, and while I still have opened positions, the AccountMargin() function returns zero. The problem is identified but how to solve it ?

I have to say that these tests were done with a demo account broker LMAX and the strategy allow that positions BUY and SELL coexist at the same time.
I also did the test with AccountFreeMargin() and I found the same behavior.

This problem is blocking for the implemented risk management. I thought of a workaround with MarketInfo function ("MODE_MARGININIT") to calculate margin (multiplying this by the number of lots of all open positions).

If you have an idea that would allow me to solve the bug, I am quite interested!

Thanks in advance,

Vincent.


Ubzen
5298
Ubzen  
The problem is probably in your code which we cannot see.
Vincent
27
Vincent  

Hello Ubzen,

thank you for your response.

Here's the part of the code where I use the AccountMargin() function :

if (AccountMargin()<AccountBalance()*0.2) newTradeAllowed = 1;


The problem is that the AccountMargin function sometimes returns 0, even if I have several opened positions.


Here's a portion of the trace at the moment when the problem comes :


2013.05.16 11:03:26;NBPOS;3
2013.05.16 11:04:04;BALANCE;37633.63000000
2013.05.16 11:04:04;MARGIN;5147.04000000
2013.05.16 11:04:04;TRADE_OPEN;INDICE0;16441554
2013.05.16 11:04:04;NBPOS;4
2013.05.16 11:04:04;TRADE_CLOSE;INDICE1;16430082
2013.05.16 11:04:04;NBPOS;3
2013.05.16 11:04:04;BALANCE;37622.76000000
2013.05.16 11:04:04;MARGIN;0.00000000
2013.05.16 11:04:04;TRADE_OPEN;INDICE1;16441558
2013.05.16 11:04:04;NBPOS;4
2013.05.16 11:04:05;TRADE_CLOSE;INDICE2;16430083
2013.05.16 11:04:04;NBPOS;3
2013.05.16 11:04:05;BALANCE;37611.89000000
2013.05.16 11:04:05;MARGIN;5147.00000000

NBPOS : count of opened positions

TRADE_OPEN : Event of opening a new position

TRADE_OPEN : Event of closing a position

We can see that the AccountMargin() returns 0 when we have 3 opened positions.


Thank you in advance for your help.

Ubzen
5298
Ubzen  
Are you using Print("MARGIN;", AccountMargin())
Vincent
27
Vincent  

I Have a specific Function log

void log(string s)
{
  int handle;
  handle=FileOpen(StringConcatenate(nomPgm,".txt"),FILE_CSV|FILE_READ|FILE_WRITE,';');
  if (handle>0)
  {
    FileSeek(handle,0,SEEK_END);
    FileWrite(handle,s);
    FileClose(handle);
  }
}


Wich is used like this

log(TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS) + ";MARGIN;" + AccountMargin());

Alain Verleyen
37484
Alain Verleyen  

Interesting issue, the problem is in backtest or forward test ? or both ?

Do you have an explanation for this ?

2013.05.16 11:04:04;BALANCE;37622.76000000
2013.05.16 11:04:04;MARGIN;0.00000000
2013.05.16 11:04:04;TRADE_OPEN;INDICE1;16441558
2013.05.16 11:04:04;NBPOS;4
2013.05.16 11:04:05;TRADE_CLOSE;INDICE2;16430083
2013.05.16 11:04:04;NBPOS;3
2013.05.16 11:04:05;BALANCE;37611.89000000
2013.05.16 11:04:05;MARGIN;5147.00000000
Vincent
27
Vincent  

+1, Don't know what's happened ... This logs were generated on "lived" conditions, not on backtest.

But this doesn't change the problem i have with AccountMargin().

Thank you for your help.

Alain Verleyen
37484
Alain Verleyen  
vfr92:

+1, Don't know what's happened ... This logs were generated on "lived" conditions, not on backtest.

But this doesn't change the problem i have with AccountMargin().

Thank you for your help.

What build are you using ? Did you tried with another build ?
Vincent
27
Vincent  

Version 4.00 Build 500.

No, i didn't make the test with another build.

Alain Verleyen
37484
Alain Verleyen  
That seems not a new problem. See also.
Vincent
27
Vincent  

Hello angevoyageur,

it doesn't really please me that others than me had the same problem ;-)  The post of jcc on 2009.08.11 20:30 talked precisely of the problem i actually face.

Do you know a workaround that I can use to calculate the Margin without using the AccountMargin(), which is not reliable ?

We're talking here about the position sizing and the risk of the system... wich is the most important part of a system ...

Thank you for your help

12
To add comments, please log in or register