money management and lotoptimize

univetsity

Trader
Mar 14, 2011
27
0
22
china
I try to use these code to manage my money,but an problem is I don't know how to descirbe my initital money(priniciple) .my method is cotrol lot :use priniciple 's maximumrisk+3 times about of profit(or loss)'s maximunrisk as lot,it profit>0,it will enlarge,otherwise,the vise.
MQL5:
double LotsOptimized()
  {
   double b,lot;//=Lots;
   int    orders=HistoryTotal();    
   int    losses=0,wins=0;  
   int    c,e,f,d;
   b=iATR(NULL,0,60,0); 
   c=AccountLeverage(); 
 
   d=AccountFreeMargin();//I wanna use it describe account's principal,but couldn't how to
   if(orders>=1)
   {e=AccountFreeMargin(); }            
   Print(MarketInfo(Symbol(), MODE_LOTSIZE));
   Print(MarketInfo(Symbol(), MODE_LOTSTEP));
   Print(MarketInfo(Symbol(), MODE_MAXLOT));
   Print("this accountleverage is",c);
   if(d!=0)  
   lot=NormalizeDouble((e*MaximumRisk/2000)*(1+3*(e/d-1)),2);//if profitiable,increase position,otherwise,the vice
   if(lot<0.01) lot=0.01;
   return(lot);
  }
but the first order's position still be 0.01(the mini lot of my MM terminal), these follow will be normal,don't know why. And I know the d==e in my code,so e/d always=1, but I wanna use d stands principal,but how do?TKS!
And have another method of lotoptimize,if solve this,I will list that one here!:p
 
Last edited by a moderator:

univetsity

Trader
Mar 14, 2011
27
0
22
china
If by principal you mean the initial balance on an account, then you can't code it with some MQL function. You should create an input parameter and the user will have to fill it with the principal value.
yes,I mean the initial balance,but problem now is I don't know how to describe the initial balace(also means:how to get this value?)
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
Fixed fractional risk method is my target,but if couldn't get the initial balance value,this is impossible to achieve.Are you sure only" set it manually "this way?
TKS!
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
As far as I know, yes. There is no MQL4 variable for initial balance.
But wait. If by initial balance you mean the balance at the start of the EA running (immediately before attaching it to a chart), then it can be done by reading the current account balance only once in the init() function.
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
TKS! Initial balance I mean is the deposit money of account.Whether array could be used for it or not?e.g. array[0] stands the lastest balance,array[n-1] stands the initial balance, or array[1] stands balance before the lastest trade happend.This is only a hypothesis,actually,I hardly know array clearly.
Any suggestion would be appreciation!
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
TKS! Initial balance I mean is the deposit money of account.Whether array could be used for it or not?e.g. array[0] stands the lastest balance,array[n-1] stands the initial balance, or array[1] stands balance before the lastest trade happend.This is only a hypothesis,actually,I hardly know array clearly.
Any suggestion would be appreciation!
No, there's no such array in MQL.
 

univetsity

Trader
Mar 14, 2011
27
0
22
china
How to get the balance or accountfreemargin before the lastest trade happend.I wanna compare the lastest accountfreemargin with the previous one's value,that could help me confirm lot.
 

DCCS

Active Trader
May 22, 2017
34
2
34
48
Kuala Lumpur, Malaysia.
Hi,

I believed there is a control of lot size for any particular broker.

Print("Maximum permitted amount of a lot=",MarketInfo(Symbol(),MODE_MAXLOT));

May I know how to overcome this code so that the order lot size is not restricted by a particular broker?

Thanks.
 

DCCS

Active Trader
May 22, 2017
34
2
34
48
Kuala Lumpur, Malaysia.
Hi Enivid,

Ok, I understand.

So when I place manual trade in demo account the lot can be larger than MODE_MAXLOT. How come like that?

Is there anyway to convert MT4 mql4 file format to MT5 mql5 file format?

Thanks.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
So when I place manual trade in demo account the lot can be larger than MODE_MAXLOT. How come like that?
I guess you need to contact your broker's support about that. Perhaps, they will also be able to increase the maximum allowed position size for expert advisors. After all, they are interested in higher trading volume by the traders.

Is there anyway to convert MT4 mql4 file format to MT5 mql5 file format?
Yes, but it the difficulty of the process varies greatly depending on the code. It can be as easy as copy/paste and fix some minor issues to a completely rewrite hundreds of new lines of code. This article, even being quite old and somewhat outdated, can help you with such a conversion.