stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Help with MQL4 Equation
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1422
Page 1 of 2
Author:  remix919 [ Thu Jan 24, 2013 3:09 pm ]
Post subject:  Help with MQL4 Equation

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?
Author:  phil_trade [ Thu Jan 24, 2013 3:17 pm ]
Post subject:  Re: Help with MQL4 Equation

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
Author:  fxdaytrader [ Thu Jan 24, 2013 3:22 pm ]
Post subject:  Re: Help with MQL4 Equation

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
Author:  remix919 [ Thu Jan 24, 2013 3:31 pm ]
Post subject:  Re: Help with MQL4 Equation

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.
Author:  phil_trade [ Thu Jan 24, 2013 3:36 pm ]
Post subject:  Re: Help with MQL4 Equation

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
Author:  remix919 [ Thu Jan 24, 2013 3:37 pm ]
Post subject:  Re: Help with MQL4 Equation

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?
Author:  remix919 [ Thu Jan 24, 2013 3:38 pm ]
Post subject:  Re: Help with MQL4 Equation

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.
Author:  remix919 [ Thu Jan 24, 2013 9:56 pm ]
Post subject:  Re: Help with MQL4 Equation

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;
?
Author:  remix919 [ Fri Jan 25, 2013 2:43 am ]
Post subject:  Re: Help with MQL4 Equation

Problem solved, I had to put the code after the initialization, not before :\
Author:  icon [ Mon Feb 04, 2013 6:43 am ]
Post subject:  Re: Help with MQL4 Equation

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 ?
All times are UTC Page 1 of 2