Money management - Dynamic lotsize based on risk compared to margin utilising a martingalesque technique

 

Hi

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. 

 
simoncs:

Hi

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.

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. 

 
RaptorUK:

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? 

 
simoncs:


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 understand what you are trying to do . . .  I don't understand why you can't simply use AccountFreeMarginCheck() to see if your proposed trade size will work based on available margin,  if it will work you place the trade,  if it won't you don't place the trade.  If you want to maximise your position size based on available margin and AccountFreeMarginCheck() tells you that you don't have enough margin you can adjust your position size to 90% of what your were going to use (LOTSTEP and MINLOT permitting)  and retry AccountFreeMarginCheck(),  rinse and repeat . . .
 

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 ?


 
   ae=AccountEquity();
   am=AccountMargin();
   if(am==0) am=Point;
   aaa=ae/am*100;
   if(AccountMargin()==0) aaa=0;

 

aaa is MARGIN LEVEL

:) 


 
Most traders trade too much, and that is just a fact. Over-trading usually stems from traders trading without a solid forex trading plan, or without one at all, because when you don’t have a plan or at least a general guide of what you will do in the markets, you end up ‘shooting first’ and asking questions later…which is definitely not the best way to trade. If you have fully mastered your trading edge and developed it into a comprehensive trading plan, you have no reason to trade when your edge is not present, unless you deviate from your plan. To preserve your trading capital and get the most ‘mileage’ out of it, the best thing you can do is to simply not trade when your edge isn’t present. This seems like an obvious thing to say, but many, if not most traders, end up trading when they know their edge is not present. When you do this you voluntarily take frivolous risks with your hard-earned money, and obviously this is something you don’t want to do.
 
No offence here but usually any successfully trades done in demo account gone bad when trades done in real account. Broker also have many advantages over their client and they can do what they want with it. When we start trades, it is always in red first. No such thing as start at zero. It always start in negative. So, don't say people don't have the edge. They just want to make money real fast which is why they failed miserably.
 
simoncs: 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.
Suppose you open your ONE TRADE (no EAs or manual trades running on any other pair) and at that moment free margin is $1.
No problem the trade is open.
Now the market ticks one point south. Margin call. Order closed at a loss.
It is not free margin at the open, it is positive free margin at the SL.