Code not working, looking for suggestion.

To add comments, please log in or register
Callum
34
Callum  

Hey guys, so I'm trying to make a code that adjusts the lot based on the acceptable risk, amount of money in the account, and the stop loss.


double Lots;

//---- select lot size

Lots=NormalizeDouble((AccountFreeMargin()*riskPerc)/(Stop*10),1);

if(Lots<minLots) Lots=minLots;

if(Lots>maxLots) Lots=maxLots;

return(Lots);



Simple enough. I think my math is correct... It made my head hurt thinking about it, I guess I'm out of practice... Anyway, even if it is not, this code should still compile and execute fine! But it does not, when it tries to trade (this code is executed right within the ordersend() under the volume field) it returns an OrderSend error 131. But if I replace return(Lots); with return(1); or any other number, it trades just fine... Any idea what it is that is breaking the ordersend?


Any help is much appreciated. Sometimes when it is late, all you need is a fresh pair of eyes.

Al W
163
Al W  
It would be better if you showed us the result of Lots via Print(). The answer may be simple.
Callum
34
Callum  
The results of Lots is 0.3 when I print it. ($1000 initial deposit).
Ronaldo
281
Ronaldo  
So if it accepts Lots=1.0 but not Lots=0.3, it means your demo account does not allow lotsize less than 1; try a different broker or account type that allows lots sizes like 0.1,0.2,0.3; similarly if you wish to go for lot sizes of 0.01,0.02, for example, make sure you have a broker accnt that allows that; use Marketinfo with MODE_LOTSIZE to check
cloudbreaker
1574
cloudbreaker  
ronaldosim:
So if it accepts Lots=1.0 but not Lots=0.3, it means your demo account does not allow lotsize less than 1; try a different broker or account type that allows lots sizes like 0.1,0.2,0.3; similarly if you wish to go for lot sizes of 0.01,0.02, for example, make sure you have a broker accnt that allows that; use Marketinfo with MODE_LOTSIZE to check

MODE_MINLOT, MODE_MAXLOT and MODE_LOTSTEP are the ones fxCal needs to check.


CB

Callum
34
Callum  

MODE_MINLOT is 23

MODE_MAXLOT is 25

MODE_LOTSTEP is 24


Is that right? Doesn't seem right... It is a demo account which can trade as little as 0.01 lots and as much as 1000 lots (which I know is weird, but I requested it for testing some things in the EA) so I assumed it would work the same in the strategy testing feature... Let me know what this means if you don't mind! Thanks.

cloudbreaker
1574
cloudbreaker  

Aaargh!


fxCal - I didn't mean you to go look up the docs and tell us what the index number of these marketinfo parameters are - we all know these, they are constant, and they don't tell us anything about YOUR scenario.

What I meant you to do was to insert a call such as the following in your code and tell us what the actual value is. Exactly as the documentation tells you how to use the MarketInfo() function.


Print(DoubleToStr(MarketInfo(Symbol(),MODE_MINLOT),2);


which is the same as:


Print(DoubleToStr(MarketInfo(Symbol(),23),2);


CB

whocares
84
whocares  
fxCal wrote >>

Hey guys, so I'm trying to make a code that adjusts the lot based on the acceptable risk, amount of money in the account, and the stop loss.

double Lots;

//---- select lot size

Lots=NormalizeDouble((AccountFreeMargin()*riskPerc)/(Stop*10),1);

if(Lots<minLots) Lots=minLots;

if(Lots>maxLots) Lots=maxLots;

return(Lots);

Simple enough. I think my math is correct... It made my head hurt thinking about it, I guess I'm out of practice... Anyway, even if it is not, this code should still compile and execute fine! But it does not, when it tries to trade (this code is executed right within the ordersend() under the volume field) it returns an OrderSend error 131. But if I replace return(Lots); with return(1); or any other number, it trades just fine... Any idea what it is that is breaking the ordersend?

Any help is much appreciated. Sometimes when it is late, all you need is a fresh pair of eyes.

fxCal

May I also suggest you review this acticle that talks about calculating the lot size.

https://www.mql5.com/en/articles/1571

Callum
34
Callum  
Ah, right. That post was made at the end of a long day, I should have realized that those were just constants to be plugged into another function. Dumb of me. I'll get the information as you said, and also I will read the article posted by whocares and come back later.
cloudbreaker
1574
cloudbreaker  

No worries - we'll get it sorted.


CB

To add comments, please log in or register