# money management and lotoptimize

27
0
22
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.
Code (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!

Last edited by a moderator: Sep 28, 2015

9,525
337
114
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.

27
0
22
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?)

9,525
337
114
You can't. You can only use some input parameter and set it manually before running your EA.

27
0
22
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!

9,525
337
114
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.

27
0
22
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: Jun 18, 2011

9,525
337
114
No, there's no such array in MQL.

27
0
22
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.

9,525
337
114
If your EA trades with OrderSend() without pending orders, then it can get AccountFreeMargin just before sending its next order.

27
0
22
Yes,without pending orders,but I am an newbie,don't know the detail about you say in #10.Show the process would more appreciation!

9,525
337
114
Just insert that line before calling OrderSend() and you'll have the amount of free margin before the last order:
Code (MQL5):
FreeMargin = AccountFreeMargin();

Last edited: Sep 28, 2015

32
1
34
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.

9,525
337
114
I don't think there is a way to override this restriction.

32
1
34
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.

9,525
337
114
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.

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.