The Martingale part:
I basically want to be able to increase the percentage of equity/account balance risked if the last trade was profitable. Keep increasing the percentage by 1% to a max of 10%.
I have managed to code this.
In testing the first issue came as I then ran out of margin as soon as the percent increased past 3 - Oanda 50:1
(The risk amount is based on the stoploss, so the lotsize is dynamic)
The Margin Questions:
So i read a bunch of threads about checking against margin to make sure this is not exceeded.
I think I then just need to create a MaxMarginLotsize and then compare this back to the Lotsize that gets calculated as part of my Martingale technique, so that I always stay within boundaries of the FreeMarginCheck.
So my first question -MODE_MARGINREQUIRED - I presume this is in the same currency as the account balance? It seems to be but then I got confused when i added a quick script to check the outputs.
My account balance is GBP.
Here's the script:
str= str + "AccountBalance=" + AccountBalance() +"\n";
str= str + "AccountEquity=" + AccountEquity() +"\n";
str= str + "AccountFreeMargin=" + AccountFreeMargin() +"\n";
str= str + "AccountFreeMarginCheck=" + AccountFreeMarginCheck(Symbol(), OP_BUY, 1) +"\n";
str= str + "MODE_MARGINREQUIRED=" + MarketInfo(Symbol(),MODE_MARGINREQUIRED) +"\n";
str= str + "calculated margin=" + (((MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_BID))/AccountLeverage())/ MarketInfo("GBPAUD",MODE_BID)) +"\n";
The output for AUDUSD
AccountBalance = 4536.89
AccountEquity = 4536.89
AccountFreeMgn = 4536.89
AccountFreeMarginCheck = 3308.41
MODE_MARGINREQUIRED = 1228.47
calculated margin = 1165.40
So if to calculate the margin required for 1 lot is (baseccy/homeccy*100000)/leverage (which gives the margin in the baseccy) and then convert that to the Account currency (GBP).
So my next question is why would the MODE_MARGINREQUIRED differ from my calculated amount?
When I dropped it on a GBPAUD or GBPUSD chart the two amounts match.
Once I know that using MODE_MARGINREQUIRED fulfills what I expect it too.
Final formula for MaxMgnLots
I am working on the principal
Account Balance - (AccountBalance * RiskPercent/100) / (baseccy/homeccy*100000)/leverage
so I am thinking that could be
double RiskAmount = (AccountBalance() * (MaxPctLoss / 100));
MaxMarginLotSize = (AccountBalance() - RiskAmount) / MarketInfo(Symbol(),MODE_MARGINREQUIRED);
Does that seem right?
I then just compare that to the Lotsize I get from the martingale part.
Why not simply use AccountFreeMarginCheck() and if it is greater than 0 you can place your trade ? if you get an error 134 then don't place the trade.
WHRoeder has posted some code to check if your proposed trade and any other trades you have open will result in a Margin Call given the worst case scenario.
I went through WHR's code base. There was a lot of stuff in there that wasn't relative to me, (Don't get me wrong it seems very complete) and started getting a little confusing for my level of coding, so I am trying to understand this from my perspective rather than trying to adapt other code.
My EA should only have one trade open at a time. AccountFreeMarginCheck() therefore, I think, will always be greater than 0, provided i have enough money in the account.
If the Martingale technique is turned on - then if there are consecutive wins I need to increase the risk each time, hence why I need to check the lots calculated based on a risk method (Riskamount / StopLoss / Tickvalue) to the number of lots calculated on a margin method. I can only increase the number of lots within the bounds dictated by the margin method, otherwise no trade would be placed at all which is not what I want.
That's why I need to try to understand the MODE_MARGINREQUIRED.
Does that make sense?
I have this same problem here and I'm out.
MarginLevel = 300% and I want incrase risk, position to get 150% after entry. Maybe is simple, but have bad time today. :D :D
what is pratice for MARGINMAINTENANCE ?
aaa is MARGIN LEVEL
To add comments, please log in or register