6 useful MetaQuotesLanguage mini-functions

When I look at codes of other people, I wonder why is so much coding needed in some places, when such things can be done shorter and more elegantly. I present you a few small but very useful functions, which will help to make our code shorter, more elegant and more clear.

sltpValue

This function helps to set SL/TP values in OrderSend function without the need to check if any SL was set. Firstly, I will show you the code and then show you an example of using it:

double sltpValue(double value, int sltp) {
 if(sltp>0) {
  return(value);
 }
 return(0);
}

It works in this way:

extern int sl=30;
extern int tp=90;

int start() {
 [...]
 OrderSend(Symbol(),OP_BUY,lots,Ask,slippage,
           sltpValue(Bid-sl,sl),sltpValue(Ask+tp,tp),comment,magic);
 [...]
}

All the fun with this function is that we can use tester to easily check such things as strategy operation without TP (setting tp=0) with no need to play with conditional instructions or to double OrderSend invocations.

normalize

It is used for normalizing values to the specified range. We enter input value and range borders (the order of borders doesn’t matter), the function returns the original value if the input value belongs to the specified range or one of the borders in any other case.

double normalize(double value,double min,double max) {
 value=MathMin(value,MathMax(min,max));
 value=MathMax(value,MathMin(min,max));
 return(value);
}

between

A function for quick checking if the input value belongs to the specified range (the order of borders doesn’t matter).

bool between(double value,double min, double max) {
 if(value<MathMax(min,max) && value>MathMin(min,max)) {
  return(true);
 }
 return(false);
}

betweenoe

A function similiar to the previous one, but checking “between or equal”, which means it checks if a value is inside the given range or at on of its borders.

bool betweenoe(double value,double min, double max) {
 if(value<=MathMax(min,max) && value>=MathMin(min,max)) {
  return(true);
 }
 return(false);
}

instr

An overlay for the StringFind function, which in my opinion is not intuitive and has strange syntax (PHP habits – there you always searched for “a needle in a haystack”, not for “in a haystack, a needle” – arguments order).

Because returning a position, as StringFind does, is not important in most cases, instr returns true or false depending on if the string “find” is present in the string “text”.

bool instr(string find,string text) {
 if(StringFind(text,find)>=0) {
  return(true);
 }
 return(false);
}

orderDir

It is often necessary to check the position direction (is it a sell or buy position), not the type itself. As an argument to this function we pass the result of OrderType() function for an order we want to check.

int orderDir(int oType) {
 if(oType==OP_BUY  || oType==OP_BUYLIMIT  || oType==OP_BUYSTOP) return(1);
 if(oType==OP_SELL || oType==OP_SELLLIMIT || oType==OP_SELLSTOP) return(-1);
}

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook