How to code? - page 305

 
gmailer:
^ Thanks - that might be it. Do you know how to get rid off those "green and pink points" at the bottoms and tops?

Sorry - I was actually looking at my other Stoch where such points are present. The one suggested in answer to my first post was OK. Thanks!

 

Maybe just one advice :

When you have a "brick" (a function, a snippet, a part of code) that works OK, don't alter it. Functions are good because you can change their inputs and they work according to inputs. And, once you tested functions in all kind of situations you do not have to think if it works OK. That way you can narrow the code that you have to look at when searching for errors (and that can speed up development time immensely)

crsnape@btinternet.com:
Hi mladen I agree with you entirely, the GetLots function you provided does work as it should; I tested it as part of my code and it worked.

I have made changes to it simply because I want to change my risk peramter depending on whether I am winning or losing. I have basically elaborated on the existing code (or tried to anyway) incorporating additional money management rules.

I can honestly say that my code is not secret; I am sure many others have considered and implemented in code what I am trying to do; but I suppose there is an element of privacy because I personally have spent so much time developing it.

I will keep reviewing my code, I obviously want to try and resolve any problems myself as I take pride in learning and doing things from scratch.

But I understand your final paragraph; I will use one 'Risk' variable and change this risk accordingly on the output of a function.

Anyway thanks for responding yet again, this forum honestly wouldnt be the same without you; you've helped me out countless amount of times.

Cheers :-)
 

Something that I'm not sure about is the function brackets and what goes in it.

If I write a function within a function, do I need to put this function name inside the brackets?

For instance if I have a function LastOpenTicket() and these is called within a function named GetLots, do I need to put LastOpenTicket() in the brackets, like GetLots (LastOpenTicket())?

Also do I need to put the () in there too? What about if its LastOpenTicket (int number, int digit, int anotherone), does all this get put in the function GetLots Bracket as well in this instance, e.g. GetLots (int number, int digit, int anotherone.. and any other variables in the function etc?)

 

...

1. Imagine the {} brackets pair as "start" and "end"

2. You can not write nested functions in mql (pascal allows that, but mql does not - nested functions would be declaring a function within the body of other functions. See here : Nested function - Wikipedia, the free encyclopedia). You can place a call to any function in any part of your code

3. you can use that form of a call (a direct call, like your example GetLots (LastOpenTicket())) or you can assign the return of LastOpenTicket() to some variable and then use it as an argument in a call to GetLots()

crsnape@btinternet.com:
Something that I'm not sure about is the function brackets and what goes in it.

If I write a function within a function, do I need to put this function name inside the brackets?

For instance if I have a function LastOpenTicket() and these is called within a function named GetLots, do I need to put LastOpenTicket() in the brackets, like GetLots (LastOpenTicket())?

Also do I need to put the () in there too? What about if its LastOpenTicket (int number, int digit, int anotherone), does all this get put in the function GetLots Bracket as well in this instance, e.g. GetLots (int number, int digit, int anotherone.. and any other variables in the function etc?)
 

Whats the difference between these two? Is there any?

double GetLots (int number, int call, int specialcall, int order)

{

and..

double GetLots ()

{

int number;

int call;

int specialcall;

int order;

?

 

...

First is a function accepting arguments

Second is a function with no arguments so you can not change the "number", "call", "specialcall" and "order" values outside the function body

Some more useful info on declaring functions can be found at this thread : https://www.mql5.com/en/forum/173005

crsnape@btinternet.com:
Whats the difference between these two? Is there any?

double GetLots (int number, int call, int specialcall, int order)

{

and..

double GetLots ()

{

int number;

int call;

int specialcall;

int order;

?
 

I've been playing around with my functions for the last couple of days and Im still getting blank results. I'm literally tearing my hair out.

I changed the function to calculate RiskSize as you suggested (I agree its a better system now) and used that in my GetLots function. This is it:

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

double GetLotsLong (double SLDistanceLong)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

double LotsLong = 0;

int LotDigit = 2;

int Risk = GetRiskLong (RiskLong);

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 (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);

}

Its also complaining RiskLong - variable not defined. But this is defined under the function GetRiskLong which its calling.

If its difficult to help with just the code Ive posted could I send it to you?

 

...

First of all there is no need to make separate get lots for a long and for a short. Use one function (that has already been provided to you) for both kind of orders (calculating lot size for both, short and long, must be exactly the same) Just change the risk or the stop loss distance if you wish different calculation

The error you are getting is because variables declared within functions are "visible" only within functions. It does not matter that you are calling the function that has a variable called the name you are telling : it is local to only the called function, other parts of code do not "know" about it and can not access it

crsnape@btinternet.com:
I've been playing around with my functions for the last couple of days and Im still getting blank results. I'm literally tearing my hair out.

I changed the function to calculate RiskSize as you suggested (I agree its a better system now) and used that in my GetLots function. This is it:

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

double GetLotsLong (double SLDistanceLong)

{

RefreshRates();

double MinLots, MaxLots, LotStep;

double LotsLong = 0;

int LotDigit = 2;

int Risk = GetRiskLong (RiskLong);

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 (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);

}

Its also complaining RiskLong - variable not defined. But this is defined under the function GetRiskLong which its calling.

If its difficult to help with just the code Ive posted could I send it to you?
 
mladen:
First is a function accepting arguments

Second is a function with no arguments so you can not change the "number", "call", "specialcall" and "order" values outside the function body

Some more useful info on declaring functions can be found at this thread : https://www.mql5.com/en/forum/173005

Thanks for this. So if I am calling a function called GetRisk from within a function called GetLots, I would need to put the variables of the function GetRisk in the brackets (...) of GetLots because the values are calculated outside of the function GetLots?

 

...

Let me try this way :

2 questions (assuming that we know the account current $ state) :

question 1 : I am willing to risk (don't know)%with (don't know) pips stop loss. What should be my lots size?

question 2 : I am willing to risk 1% with 100 pips stop loss. What should be my lots size?

It is obvious that the answer to 1st question is impossible since you do not know the risk percent I am willing to risk nor the stop loss I am going to use. But in the second example it is possible to calculate the lot size since you know all those. Now those are arguments : since we predict that rsik% and stop loss can vary, we provide those to lot size calculation as variable arguments that are going to be "told" to the function each time when the function is called

Hope this clarifies what arguments (parameters) in functions are for. Each and every function works on the same principle : if you predict that some part of calculation can cange, put it as an argument to the function

Here are some more examples of coding functions : https://en.wikipedia.org/wiki/Function_%28computer_science%29

crsnape@btinternet.com:
Thanks for this. So if I am calling a function called GetRisk from within a function called GetLots, I would need to put the variables of the function GetRisk in the brackets (...) of GetLots because the values are calculated outside of the function GetLots?
Reason: