Error 130 - invalid stop. Why? - page 5

 

...

Try using something like this function :

double getLots(string symbol, double Risk, double stopLossDistance)

{

RefreshRates();

double lots = 0;

double MinLots = NormalizeDouble(MarketInfo(symbol,MODE_MINLOT) ,2);

double MaxLots = NormalizeDouble(MarketInfo(symbol,MODE_MAXLOT) ,2);

double LotStep = NormalizeDouble(MarketInfo(symbol,MODE_LOTSTEP),2);

int LotDigit = 2;

if(MarketInfo(symbol,MODE_DIGITS)==3 || MarketInfo(symbol,MODE_DIGITS)==5) stopLossDistance *= 10.0;

//

//

//

//

//

if (LotStep==1) LotDigit=0;

if (LotStep==0.1) LotDigit=1;

if (LotStep==0.01) LotDigit=2;

if (Risk>0)

{

if (AccountBalance()>AccountFreeMargin())

lots = NormalizeDouble(AccountFreeMargin()*(Risk/100.0)/(stopLossDistance*MarketInfo(symbol,MODE_TICKVALUE)),LotDigit);

else lots = NormalizeDouble(AccountBalance() *(Risk/100.0)/(stopLossDistance*MarketInfo(symbol,MODE_TICKVALUE)),LotDigit);

}

//

//

//

// lots need to be normalized to accomodate lot step

// normalization does not rounds but cuts any portion of lot

// that is larger than lot step (that way the risk is a bit smaller)

//

//

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

lots = MathMax(MathMin(lots,MaxLots),MinLots);

return(lots);

}

Probably the error you are getting is due to lack of lots rounding (the last part of the function does that)

_________________________________

PS: stop loss distance should be passed as pips to this function

crsnape@btinternet.com:
Hello all,

I am getting the OrderSend Error 131. I am trying to re-code so the lots amount is permissable by my broker. I have added this but I am still getting an error 131. Please could you tell me whether I have written this correctly and if I am going about it the right way? Thanks.

int LotsLong, Remainder;

LotsLong = MathFloor(AccountBalance() * (PositionSize * 0.01)) / ((Ask - SLPriceLong) * PipValue); //Calculate Lots

{

Remainder = LotsLong%10;

{

LotsLong = LotsLong - Remainder;

I did MarketInfo() and it gave me:

2008.07.10 15:13:37 MACD Sample EURUSD, H1: 1000

2008.07.10 15:13:37 MACD Sample EURUSD, H1: 0.01

2008.07.10 15:13:37 MACD Sample EURUSD, H1: 0.01

2008.07.10 15:13:37 MACD Sample EURUSD, H1: 100000
 

Thanks mladen,

Il give that a try.

 

Hi mladen,

I'm new to coding, and just wanted to check whether I have understood the code you pasted correctly.

double getLots(string symbol,double Risk,double stopLossDistance)

Is this declaring variables? I've never seen it constructed this way. Is it the same as;

double getLots

string symbol

double Risk

double stopLossDistance?

double MinLots=NormalizeDouble(MarketInfo(symbol,MODE_MINLOT) ,2);

What does the 2 mean at the end of this line?

Instead of symbol, could I use Symbol()?

 

...

1. It is not declaring variables but is declaring a function with those expected variables. Making it a function is practical in cases when you plan to reuse it in some other code because that way you just copy the function and then place a call to it where you need it. An example of a call (let say from within start()) would be something like this :

double lotsToTrade = getLots(Symbol(),1,100);

which would then mean the following : get the lot size that I can trade for current chart symbol using 1% risk and having a 100 pips stop loss on the order

2. the "2" at the end of line means that the NormalizeDouble() function should round the result to 2 decimals. In that particular line you could omit the normalization, but better to be safe with metatrader

3. I think that the example call from the above answers if you can use Symbol() instead. The function is more flexible if you dont, since that way it will be able to calculate lot size for any symbol. That will allow you, for example, to have a multi symbol trading EA

crsnape@btinternet.com:
Hi mladen,

I'm new to coding, and just wanted to check whether I have understood the code you pasted correctly.

double getLots(string symbol,double Risk,double stopLossDistance)

Is this declaring variables? I've never seen it constructed this way. Is it the same as;

double getLots

string symbol

double Risk

double stopLossDistance?

double MinLots=NormalizeDouble(MarketInfo(symbol,MODE_MINLOT) ,2);

What does the 2 mean at the end of this line?

Instead of symbol, could I use Symbol()?
 

I think I understand. So the value of GetLots is all the coding underneath it between the { and }? So whenever you call GetLots it runs through all this coding?

So if I typed GetLots under the lots part of OrderSend() it would insert the quantity of lots to be opened based on this coding?

 

...

Yes ...

crsnape@btinternet.com:
I think I understand. So the value of GetLots is all the coding underneath it between the { and }? So whenever you call GetLots it runs through all this coding? So if I typed GetLots under the lots part of OrderSend() it would insert the quantity of lots to be opened based on this coding?
 

You know when you get an ice cream for getting something right when your younger... thats what I feel like right now.

So the string symbol, double lots and double StopLossDistance - can these only be recognised from within the GetLots function? Are they declared in brackets because the function uses these variables within its GetLots coding?

There is another area that I dont get:

stopLossDistance*=10.0;

I dont know why StopLossDistance is being multiplied by 10.0? And it only does this if the symbol (say EURUSD) has 3 or 5 decimal places- in the instance of EURUSD (which with my broker has fractional pips; so 5) it would be multiplying my StopLossDistance by 10, so say 50 pips (I cant see the preceeding calculation for StopLossDistance in the code to determine if StopLossDistance is in pips or 0.0050 for instance) but this would be 500 in this instance.

Anyway within my coding I have differentiated lots by using LotsLong and LotsShort because as part of my calculation I use StopLossDistance (in pips, say 50) multiplied by MODE_TICKVALUE) and divide this by my (risk % of account balance). But this code uses GetLots only and doesnt differentiate by using GetLots for long and then GetLots for short positions - will have to work out how this has been simplied!

Hopefully by the time I have understood this code I can amend it to insert within my coding!

Thankful for any insight you can provide.

Chris.

 

...

stopLossDistanceis multiplied in this case to adjust for 5 digit brokers. It needs to be done since tick value is used in further calculation and if it hasn't been done, you would get a lot size 10 time larger then it should bee

As of lot size for longs and shorts : as long as the stop loss and the risk are the same you need only one calculation for a lot size. If, and only if, the stop loss size or risk for longs and shorts differ, then you need 2 calculations

crsnape@btinternet.com:
You know when you get an ice cream for getting something right when your younger... thats what I feel like right now.

So the string symbol, double lots and double StopLossDistance - can these only be recognised from within the GetLots function? Are they declared in brackets because the function uses these variables within its GetLots coding?

There is another area that I dont get:

stopLossDistance*=10.0;

I dont know why StopLossDistance is being multiplied by 10.0? And it only does this if the symbol (say EURUSD) has 3 or 5 decimal places- in the instance of EURUSD (which with my broker has fractional pips; so 5) it would be multiplying my StopLossDistance by 10, so say 50 pips (I cant see the preceeding calculation for StopLossDistance in the code to determine if StopLossDistance is in pips or 0.0050 for instance) but this would be 500 in this instance.

Anyway within my coding I have differentiated lots by using LotsLong and LotsShort because as part of my calculation I use StopLossDistance (in pips, say 50) multiplied by MODE_TICKVALUE) and divide this by my (risk % of account balance). But this code uses GetLots only and doesnt differentiate by using GetLots for long and then GetLots for short positions - will have to work out how this has been simplied!

Hopefully by the time I have understood this code I can amend it to insert within my coding!

Thankful for any insight you can provide.

Chris.
 

I've changed the code to suit my EA. I've got LotsLong and LotsShort because I have a different SLDistance calculation for each, one uses Ask the other Bid. This is my lots calculation:

double GetLotsLong (double Risk, double SLDistanceLong)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

int LotDigit = 2;

double LotsLong = 0;

MinLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MINLOT), 2);

MaxLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2);

LotStep = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2);

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

if (LotStep == 1) LotDigit = 0;

if (LotStep == 0.1) LotDigit = 1;

if (LotStep == 0.01) LotDigit = 2;

if (Risk > 0)

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(MathMin(LotsLong, MaxLots), MinLots);

return (LotsLong);

}

Ive duplicated the code for LotsShort. This code doesnt spit out any errors, but now its in there there are about a million errors in the code afterwards that werent there previously like variables not being defined and 'expression on global scope not allowed.' The code after this is where it searches for long or short positions, including ordersend where I include getLotsShort or Long for lots.

Im guessing ive structured it all wrong?

 

Copy the function code to the very end of your EA and those errors will disappear

As of the rest : it might be good to look for additional information at this thread :https://www.mql5.com/en/forum/173005

PS: attached a very simple indicator (not an EA) that is using that function as an example (it will simply write out what is the calculated lot size depending on parameters : risk and stop loss). The principle of using it is the same as for any EA (the way how it should be called) so it might help a bit in coding what you need

crsnape@btinternet.com:
I've changed the code to suit my EA. I've got LotsLong and LotsShort because I have a different SLDistance calculation for each, one uses Ask the other Bid. This is my lots calculation:

double GetLotsLong (double Risk, double SLDistanceLong)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

int LotDigit = 2;

double LotsLong = 0;

MinLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MINLOT), 2);

MaxLots = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2);

LotStep = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2);

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

if (LotStep == 1) LotDigit = 0;

if (LotStep == 0.1) LotDigit = 1;

if (LotStep == 0.01) LotDigit = 2;

if (Risk > 0)

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(MathMin(LotsLong, MaxLots), MinLots);

return (LotsLong);

}

Ive duplicated the code for LotsShort. This code doesnt spit out any errors, but now its in there there are about a million errors in the code afterwards that werent there previously like variables not being defined and 'expression on global scope not allowed.' The code after this is where it searches for long or short positions, including ordersend where I include getLotsShort or Long for lots.

Im guessing ive structured it all wrong?
Files:
test.mq4  2 kb
Reason: