EA not working under Build 610 - page 2

 
crsnape@btinternet.com:
Hi mladen,

I've ironed out all the errors in my code and I am only left with 4 warnings as detailed below:

1. declaration of 'Risk' hides global declaration at line 20

2. not all control paths return a value

3. declaration of 'Risk' hides global declaration at line 20

4. not all control paths return a value

The first error picks up 'Risk' at line 20 which is shown below:

extern double Risk = 5; // Risk allocated per trade, in percents

The warning highlights the row highlighted in red below:

//--- Function for calculating lots for long positions

double GetLotsLong (double SLDistanceLong, double DonchianLowM5, double Risk)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

double LotsLong = 0;

int LotDigit = 2;

SLDistanceLong = (Ask - DonchianLowM5) / 0.0001;

MinLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MINLOT), 2); // Lots need to be normalized to accommodate LotStep.

MaxLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2); // Normalization does not round, but cuts any portion of lot that is larger than LotStep.

LotStep = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2); // That way the risk is a bit smaller.

if (MarketInfo(Symbol(), MODE_DIGITS) == 3 || MarketInfo(Symbol(), MODE_DIGITS) == 5) SLDistanceLong *= 10.0;

if (LotStep == 1.00) LotDigit = 0;

if (LotStep == 0.10) LotDigit = 1;

if (LotStep == 0.01) LotDigit = 2;

if (Risk > 0)

{

Print("Risk percentage ready for use in lots calculation");

if (AccountBalance() > AccountFreeMargin())

LotsLong = NormalizeDouble(AccountFreeMargin() * (Risk / 100) / (SLDistanceLong * MarketInfo (Symbol(), MODE_TICKVALUE)), LotDigit);

else LotsLong = NormalizeDouble(AccountBalance() * (Risk / 100) / (SLDistanceLong * MarketInfo (Symbol(), MODE_TICKVALUE)), LotDigit);

LotsLong = NormalizeDouble(NormalizeDouble(LotsLong / LotStep, 0) * LotStep, LotDigit);

LotsLong = MathMax(LotsLong, MinLots);

Print("Position size calculated successfully using derived risk percentage");

return (LotsLong);

}

else Print("Error calculating position size for long position", GetLastError());

}

Just to clarify this function is used to calculate a position size and is called from within Start {}. This code is positioned outside of Start{}.

The second warning 'not all control paths return a value' is also related to this section of code, and I have highlighted the row in blue (just the small bracket at the end of the code above).

Do you know why this is happening?

Just to confirm, I do have:

Start

{

code in here

}

return(0);

The last two warnings are an exact copy of the first two but relate to my position size calculation for short positions as opposed to long positions which is a seperate section of code.

Any help here would be very much appreciated!!

PS- sorry I do not know how to paste code so it retains its indentation.

Thanks,

Chris.

The "declaration of 'Risk' hides global declaration at line 20" should be a benign warning. Just in case, rename one of the instances (so that the names do not conflict any more) and use the appropriate instance name where you renamed it

 

I'm not sure I know what you mean mladen.

Can I delete the 'double Risk' in the GetLotsLong function 'GetLotsLong(double SLDistanceLong, double DonchianLowM5, double Risk)' because Risk is already defined in the external variables at the start; 'double Risk = 5; //percentage of risk on trade. If I remove it, will it still be able to pull the 5 value into the function even though its not specified in the brackets of the GetLotsLong function? The same applies for the other two, SLDistanceLong and DonchianLowM5.

What is the purpose of specifying the variables within the brackets?

Just to clarify, the GetLotsLong function is called from my OrderSend function to derive how many units to purchase.

EDIT - I've deleted the double Risk in the 'GetLotsLong' function (so its just GetLotsLong(double SLDistanceLong, double DonchianLowM5) and changed its call point from OrderSend to GetLotsLong(SLDistanceLong, DonchianLowM5) and it no longer comes up with the error. However if I delete the double SLDistanceLong and double DonchianLowM5 in the same way it comes up with an error saying their undeclared identifiers.

Is this because double Risk is declared in the external variables of the EA, where as the double SLDistanceLong and double DonchianLowM5 are not (they are declared in Start {} as double SLDistanceLong, DonchianLowM5).

Chris.

 
crsnape@btinternet.com:
I'm not sure I know what you mean mladen.

Can I delete the 'double Risk' in the GetLotsLong function 'GetLotsLong(double SLDistanceLong, double DonchianLowM5, double Risk)' because Risk is already defined in the external variables at the start; 'double Risk = 5; //percentage of risk on trade. If I remove it, will it still be able to pull the 5 value into the function even though its not specified in the brackets of the GetLotsLong function? The same applies for the other two, SLDistanceLong and DonchianLowM5.

What is the purpose of specifying the variables within the brackets?

Just to clarify, the GetLotsLong function is called from my OrderSend function to derive how many units to purchase.

EDIT - I've deleted the double Risk in the 'GetLotsLong' function (so its just GetLotsLong(double SLDistanceLong, double DonchianLowM5) and changed its call point from OrderSend to GetLotsLong(SLDistanceLong, DonchianLowM5) and it no longer comes up with the error. However if I delete the double SLDistanceLong and double DonchianLowM5 it comes up with an error saying their undeclared identifiers.

Is this because double Risk is declared in the external variables, where as the SLDistanceLong and DonchianLowM5 are not (they are declared in Start {} as double SLDistanceLong, DonchianLowM5).

Chris.

Chris

Replace the GetLotsLong() function with this one :

double GetLotsLong (double SLDistanceLong, double DonchianLowM5, double tRisk)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

double LotsLong = 0;

int LotDigit = 2;

SLDistanceLong = (Ask - DonchianLowM5) / 0.0001;

MinLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MINLOT), 2); // Lots need to be normalized to accommodate LotStep.

MaxLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2); // Normalization does not round, but cuts any portion of lot that is larger than LotStep.

LotStep = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2); // That way the risk is a bit smaller.

if (MarketInfo(Symbol(), MODE_DIGITS) == 3 || MarketInfo(Symbol(), MODE_DIGITS) == 5) SLDistanceLong *= 10.0;

if (LotStep == 1.00) LotDigit = 0;

if (LotStep == 0.10) LotDigit = 1;

if (LotStep == 0.01) LotDigit = 2;

if (tRisk > 0)

{

if (AccountBalance() > AccountFreeMargin())

LotsLong = NormalizeDouble(AccountFreeMargin() * (tRisk / 100) / (SLDistanceLong * MarketInfo (Symbol(), MODE_TICKVALUE)), LotDigit);

else LotsLong = NormalizeDouble(AccountBalance() * (tRisk / 100) / (SLDistanceLong * MarketInfo (Symbol(), MODE_TICKVALUE)), LotDigit);

LotsLong = NormalizeDouble(NormalizeDouble(LotsLong / LotStep, 0) * LotStep, LotDigit);

LotsLong = MathMax(LotsLong, MinLots);

}

return (LotsLong);

}
 

Thanks mladen,

I've changed it and there are no errors or warnings.

Just wondering though, the GetLotsLong function needs to pull in the 5 for risk as specified in the external variables extern int Risk = 5; This was the reason that Risk is named in the function.

Correct me if I am mistaken, but I don't think it will obtain this figure as Risk is no longer specified in the GetLotsLong function, tRisk is, but this has no value attributed to it.

 

Five (5) is simple a risk (in %) for a single order in case if it turns out to be a losing trade.

You can change that risk by simply entering different value in that input field

 

Sorry mladen, this isn't what I meant (I'm likely not explaining myself clearly enough!) What you say is right, but my concern is that the GetLotsLong function now uses 'tRisk', however tRisk doesnt hold the value 5, it is 'Risk' that holds the value 5 in the external variables (extern int Risk = 5).

Wouldnt I now need to change the external variable to 'extern int tRisk = 5;' since my GetLotsLong function now uses tRisk to obtain the risk percentage?

 
crsnape@btinternet.com:
Sorry mladen, this isn't what I meant (I'm likely not explaining myself clearly enough!) What you say is right, but my concern is that the GetLotsLong function now uses 'tRisk', however tRisk doesnt hold the value 5, it is 'Risk' that holds the value 5 in the external variables (extern int Risk = 5). Wouldnt I now need to change the external variable to 'extern int tRisk = 5;' since my GetLotsLong function now uses tRisk to obtain the risk percentage?

tRisk is a function parameter that receives a value when you call a function

For example :

GetLotsLong(xxx,yyy, 10);

would pass 10 as a a value to the tRisk parameter. That is not a global variable, but a function parameter. Some more information on how functions are written and how they should be used you can find here : https://www.mql5.com/en/forum/173005

 

Thank you very much for your help mladen.

Reason: