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. Here's the code:

double int Lots = (Risk * NormalizeDouble(AccountBalance() * 50) / (MarketInfo(Symbol(),MODE_BID) * 10000),2));
 
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. Here's the code:

Perhaps you should explain your reasoning why you think this should work ?  use some figures to explain why.
 

Sorry, I had typed the code wrong, I actually got it working, but there's an error code 4051 (Invalid function parameter value.) which basically means invalid lot amount when I try to run it. Here is what it's supposed to look like:

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:

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?
 
remix919:

Any idea what I'm doing wrong?

Well first of all, a percentage is a number out of 100,  so 10% == 10/100   so 3% == 3/100 == 0.03 not 0.3

What is the relevance of leverage ?  if you want to risk 10% of your Account Balance  and your Account Balance is $1000 then you are risking $100  if you have 50:1 or 500:1 Leverage. 

 

Answer this question,  assuming your figures are correct,  and you place a Sell order at 1.33150 ,  what will price have to move up to for you to lose your 3% that you wanted to risk ?  

 
RaptorUK:

Well first of all, a percentage is a number out of 100,  so 10% == 10/100   so 3% == 3/100 == 0.03 not 0.3

What is the relevance of leverage ?  if you want to risk 10% of your Account Balance  and your Account Balance is $1000 then you are risking $100  if you have 50:1 or 500:1 Leverage. 

 

Answer this question,  assuming your figures are correct,  and you place a Sell order at 1.33150 ,  what will price have to move up to for you to lose your 3% that you wanted to risk ?  

Then that is a calculation with a fixed lotsize

It can also be if I loose 100 pips how big can the lotsize be to risk 3% of my account balance 

 
Sorry, I probably shouldn't have used the word risk, a better description would be percentage of account balance, that's what I'm referring to. As for the risk percentage, once again bad use of language, I'll be just using a 0.00 figure to determine that, not a solid percentage number. I'm not quite sure I understand how leverage works in this case, I was assuming that if you have an account with $1000 in it, and leverage is 50:1, that means you can trade up to 50000 worth of lots?
 
Problem solved, didn't realize I had to initialize the expert before processing the equation
Reason: