Useful features from KimIV

 

In this thread I will post the codes of my functions in MQL4, give examples of their use, and answer questions related to their use. Each function will be published in two posts. The first post will contain the function code, the second - examples of use and short explanations. Sometimes I will attach the code of a script to the second post to actually test the function and display the results.

I will start with functions to handle orders as per Lukyanov's request in the topic "How to Run Two EAs At the Same Time" in the end.

Let the first be the order setting function (MT4 tester version):

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия  : 13.06.2007                                                      |
//|  Описание : Установка ордера. Версия функции для тестов на истории.        |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (NULL или "" - текущий символ)          |
//|    op - операция                                                           |
//|    ll - лот                                                                |
//|    pp - цена                                                               |
//|    sl - уровень стоп                                                       |
//|    tp - уровень тейк                                                       |
//|    mn - Magic Number                                                       |
//|    ex - Срок истечения                                                     |
//+----------------------------------------------------------------------------+
void SetOrder(string sy, int op, double ll, double pp,
              double sl=0, double tp=0, int mn=0, datetime ex=0) {
  color clOpen;
  int   err, ticket;
 
  if (sy=="" || sy=="0") sy=Symbol();
  if (op==OP_BUYLIMIT || op==OP_BUYSTOP) clOpen=clOpenBuy; else clOpen=clOpenSell;
  ticket=OrderSend(sy, op, ll, pp, Slippage, sl, tp, "", mn, ex, clOpen);
  if (ticket<0) {
    err=GetLastError();
    Print("Error(",err,") set ",GetNameOP(op),": ",ErrorDescription(err));
    Print("Ask=",Ask," Bid=",Bid," sy=",sy," ll=",ll,
          " pp=",pp," sl=",sl," tp=",tp," mn=",mn);
  }
}
//+----------------------------------------------------------------------------+
 
Will there be any published features for real trading? P.S.: Thanks for the topic. I think it will be very popular and in demand.
 
Hello, Igor!
I support the idea. Right decision........!!!
Your code is huge, questions arise often, and it's not good to address you in other people's posts and on other topics.
 

Great.

Suggestion to the developers: start forming a library of functions. In my opinion, for this, it is desirable to present to the community design requirements (e.g., mandatory description of variables, stipulation of usage limits, number of characters per string, etc.). An MQ article on the subject would be a good idea.

Over time, not only simple but also complex functions will appear in the library.

 
Lukyanov:
Will there be published features for real trading?

Yes, they will... Next will be the SetOrder() function for online trading...


Examples of how to use the SetOrder() function:

1. Setting a BuyLimit order with 0.1 lot, 50 pips below the current price:

SetOrder(NULL, OP_BUYLIMIT, 0.1, Ask-50*Point);

2. Setting a BuyStop order with lot 0.3 36 pips above the current price with a 32 pips stop:

SetOrder(NULL, OP_BUYSTOP, 0.3, Ask+36*Point, Ask+(36-32)*Point);

3. Placing a SellLimit order by lot 0.2 at 42 pips above the current price with a 50 pips takeaway:

SetOrder(NULL, OP_SELLLIMIT, 0.2, Bid+42*Point, 0, Bid+(42-50)*Point);

4. Placing a SellStop order by lot 0.5 by 100 pips below the current price with a stop of 30 pips and a takeaway of 70 pips:

SetOrder(NULL, OP_SELLSTOP, 0.5, Bid-100*Point, Bid-(100-30)*Point, Bid-(100+70)*Point);

5. A BuyLimit order with lot 0.1 placed 50 pips below the current price with magic number 123456 and expiry in two hours:

SetOrder(NULL, OP_BUYLIMIT, 0.1, Ask-50*Point, 0, 0, 123456, TimeCurrent()+2*60*60);
The trailer is a real-life script with the above examples. The first four examples are commented out.
Files:
 
VBAG:
Hello, Igor!
I support the idea. Right decision........!!!
Your code is huge, questions arise often, and it's not good to address you in other people's posts and on other topics.
Well, here we can talk. Formulate your questions, I'll be glad to answer them :-)
 
Show your version of the delays between trade transactions
Thank you.
 
Thanks for the useful topic, although I'm about burnt pies, but still maybe someone has faced the problem of stockpiling and sampling from large data sets in µl. µl and databases? Has anyone thought in that direction?
 
zhuki писал (а):
Show your version of delays between trades

I do not make any delays between trades. That is, if I need to place two or more orders, I perform these trading operations without a pause between them. But between trade attempts, which have to be repeated if there is an error when contacting the server, I make a pause according to recommendations of MT4 developers. As an example you can see how such pauses are implemented in my SetOrder() function for online trading. Different pauses are made for different errors returned by the trading server.

This function SetOrder() is used for setting pending orders. It is recommended for use in online trading on Demo and Live accounts.

Files:
 
Very original, but explain why the delays of 7.7, 17, 11 seconds. Also, what if there is more than one Expert Advisor, then the analysis is required.
 
zhuki:
...explain why there is a delay of 7.7,17,11 seconds . Is this a joke or is it justified.

What kind of jokes can there be with real money?

Errors 129 (Incorrect bid or ask price), 130 (Incorrect stops), 134 (Not enough money), 136 (No price) will pause for 7.7 seconds. MT4 developers recommend pauses of more than 5 seconds in these cases. I put 7.7 seconds. Might as well have put 6.1 seconds. I do not like round values because of their attractiveness, so I set non-circular ones.

The 17 second pause was supposed to be for error 145 (Modification prohibited). But since the SetOrder function does not handle modification, I removed the handling of this error. Thank you for bringing this aspect of my function to my attention!

The 11 sec pause is located inside the loop that checks the status of the trade subsystem. You can set it to 15 or 30 seconds. It does not matter what you like.

zhuki:
I also wonder what to do if there is more than one Expert Advisor, and you cannot do without analysis.

For several Expert Advisors in one trading account, the error 146 (trading subsystem is busy) is handled. Function SetOrder waits until the trading subsystem is free.


Warning! I have edited the previous post. Made changes to the SetOrder function.

Reason: