Help with MQL4 Equation

User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Help with MQL4 Equation

Post by remix919 »

Hi there, I'm new to MQL4 coming from a PHP/mySQL background. My question is probably quite simple, but I'm not quite understanding why it won't work. I'm trying to basically get the account's balance, the price of the current currency being traded, and then calculate the lot size that should be traded based on the Risk variable. There's an error code 4051 (Invalid function parameter value.) which basically means invalid lot amount when I try to run it. Here's the code:

Code: Select all

double Lots;
Lots = NormalizeDouble(Risk * ((AccountBalance() * 50) / (MarketInfo(Symbol(),MODE_BID) * 10000)),2);
Let's assume, Risk is 3%, AccountBalance is 10000, and the MarketInfo Symbol is EUR/USD with a current BiD of 1.33150.

So first I want to get the useable balance given my account's leverage of 50:1, so 10000 * 50 = 50000.

Then I want to divide this number by the current bid price of the currency chart I'm looking at * 10000 for 1 lot (Oanda). This should be the maximum lot size I can trade based on my account balance.

Next, I want to factor in my Risk % * Value in order to get the actual lot size I should trade based on my Risk %.

Lastly, I want to round the final value to just 2 decimal places.

So in this example it would be:

Code: Select all

NormalizeDouble(0.3 * ((10000 * 50) / (1.33150 * 10000)),2);

NormalizeDouble(0.3 * (50000 / 13315),2);

NormalizeDouble(0.3 * 3.75***),2);

END VALUE = 1.13;
Any idea what I'm doing wrong?
EA's are the best way to trade. Why? Because they take the emotion out of trading.
phil_trade

Re: Help with MQL4 Equation

Post by phil_trade »

Code: Select all

double Lots,Risk;
Risk = 0.3;
Lots = NormalizeDouble(Risk * ((AccountBalance() * 50) / (MarketInfo(Symbol(),MODE_BID) * 10000)),2);
Print(DoubleToStr(Lots,2))   ;
Hi

No problem at home with this code. It should be another line returning your error code
User avatar
fxdaytrader
Trader
Posts: 190
Joined: Sun Jun 10, 2012 7:03 am
Location: Poon-Town (germany, se-asia, se-europe) ...

Re: Help with MQL4 Equation

Post by fxdaytrader »

by the way:
NormalizeDouble is the wrong way to round the lotsize. You have to use lotstep

Further instead of using the balance you should use the free margin ;)
if you like have a look to the script here: http://www.stevehopwoodforex.com/phpBB3 ... =23&t=1332
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Re: Help with MQL4 Equation

Post by remix919 »

phil_trade wrote:

Code: Select all

double Lots,Risk;
Risk = 0.3;
Lots = NormalizeDouble(Risk * ((AccountBalance() * 50) / (MarketInfo(Symbol(),MODE_BID) * 10000)),2);
Print(DoubleToStr(Lots,2))   ;
Hi

No problem at home with this code. It should be another line returning your error code
I'm using this code with the strategy tester, could it be a problem with the strategy tester?

Here's the exact errors I'm getting from the journal:

Code: Select all

2013.01.24 10:30:40	2012.01.02 23:30  GeniusFxv4 EURUSD,M30: OrderSend error 4051
2013.01.24 10:30:40	2012.01.02 23:30  GeniusFxv4 EURUSD,M30: invalid lots amount for OrderSend function
If I use extern double Lots = x; then that works fine, but if I try to set the Lots dynamically with the above code, that's when it gives me those errors.
EA's are the best way to trade. Why? Because they take the emotion out of trading.
phil_trade

Re: Help with MQL4 Equation

Post by phil_trade »

remix919 wrote:
phil_trade wrote:

Code: Select all

double Lots,Risk;
Risk = 0.3;
Lots = NormalizeDouble(Risk * ((AccountBalance() * 50) / (MarketInfo(Symbol(),MODE_BID) * 10000)),2);
Print(DoubleToStr(Lots,2))   ;
Hi

No problem at home with this code. It should be another line returning your error code
I'm using this code with the strategy tester, could it be a problem with the strategy tester?

Here's the exact errors I'm getting from the journal:

Code: Select all

2013.01.24 10:30:40	2012.01.02 23:30  GeniusFxv4 EURUSD,M30: OrderSend error 4051
2013.01.24 10:30:40	2012.01.02 23:30  GeniusFxv4 EURUSD,M30: invalid lots amount for OrderSend function
If I use extern double Lots = x; then that works fine, but if I try to set the Lots dynamically with the above code, that's when it gives me those errors.
May be the result is under MinLot authorized for this pairs with this broker. try manually to trade this lot to check
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Re: Help with MQL4 Equation

Post by remix919 »

fxdaytrader wrote:by the way:
NormalizeDouble is the wrong way to round the lotsize. You have to use lotstep

Further instead of using the balance you should use the free margin ;)
if you like have a look to the script here: http://www.stevehopwoodforex.com/phpBB3 ... =23&t=1332
Thanks for the tips and I've downloaded and am looking over your buy script, but it's quite a handful for a beginner in MQL4 :roll: I've already changed the AccountBalance to AccountFreeMargin, but I did some searching for Lotstep in the MQL4 docs and parts of your code and I'm not really understanding how to use Lotstep to integrate into my equation? Lotstep seems to be used for part of the call for MarketInfo?
EA's are the best way to trade. Why? Because they take the emotion out of trading.
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Re: Help with MQL4 Equation

Post by remix919 »

phil_trade wrote:
May be the result is under MinLot authorized for this pairs with this broker. try manually to trade this lot to check
I use Oanda and they allow pretty much any lot size.
EA's are the best way to trade. Why? Because they take the emotion out of trading.
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Re: Help with MQL4 Equation

Post by remix919 »

Just noticed something interesting, why is it that the following works:

Code: Select all

double Lots = 3.75;
but this doesn't work:

Code: Select all

double Lots;
Lots = 3.75;
?
EA's are the best way to trade. Why? Because they take the emotion out of trading.
User avatar
remix919
Trader
Posts: 91
Joined: Wed Jan 23, 2013 2:42 am
Location: Some Road in the USA

Re: Help with MQL4 Equation

Post by remix919 »

Problem solved, I had to put the code after the initialization, not before :\
EA's are the best way to trade. Why? Because they take the emotion out of trading.
icon
Trader
Posts: 18
Joined: Thu Apr 12, 2012 4:49 pm

Re: Help with MQL4 Equation

Post by icon »

I saw some functions with & in the input what does that mean ?
for example void DoSOmething( double& a[])
What is the job of the & before the a ?
I saw some examples on FF http://www.forexfactory.com/showthread.php?t=374836 and some others in Empty4 codebase and I am curious what they use the & for ?
Post Reply

Return to “Coders Hangout”