Questions from Beginners MQL4 MT4 MetaTrader 4 - page 121

 
Ihor Herasko:

Write at least one block and show it. The next step is to suggest it here.

On arrays: here I have declared 4 arrays at global level:

// массивы, в которых будут храниться характеристики ордеров:
int _OrderTicket[],_OrderType[];
double _OrderOpenPrice[];
datetime _OrderOpenTime[];

Then I zeroed them out in the position opening function:

   // обнуляем массивы
   ArrayInitialize(_OrderTicket,0);
   ArrayInitialize(_OrderType,0);
   ArrayInitialize(_OrderOpenPrice,0);
   ArrayInitialize(_OrderOpenTime,0);

How do I fill them now? In the example I'm trying to do it with, an additional variable is applied, but I don't understand how to use it:

// переменная, которая будет хранить количество ордеров, 
// принадлежащих эксперту:
int _ExpertOrdersTotal=0;

Although it's not quite clear why I need all these arrays when I can get the values of the ticket, position type, open price and open time with the corresponding function.

 
novichok2018:

On arrays: here I have declared 4 arrays at global level:

I then zeroed them out in the position opening function:

How do I fill them now? The example I'm trying to use applies an additional variable, but I don't understand how to use it:

Although it's not quite clear why all these arrays, when I can get the values of the ticket, position type, open price and open time with the corresponding function.

Try writing from scratch, outputting all intermediate data using Alert(. For example

int ord=OrdersTotal();
if ! ord )
{
   Alert("Ордера отсутствуют. Выход");
   return;
} else   Alert("Всего ордеров = ", ord);

// Посмотрев, что получилось, добавляете:
int n;
int Ords[10];
for(n=0; n<ord && n<10; n++)
{
   OrderSelect(...
}

And if you take someone's example, take it completely and study it. Who knows what the author of the example is using arrays for

 
novichok2018:

On arrays: here I have declared 4 arrays at global level:

Use an array of structures. It will be much easier to access. Here is a variant with a static array. It is easier to understand. But I use dynamic arrays myself. But the code will get a bit larger with them.

#define MAX_ORDERS_CNT   int(500)
struct OrderInfo
{
   int      nTicket;
   int      nType;
   double   fOpenPrice;
   double   fSL;
   double   fTP;
   datetime dtOpenTime;
};

OrderInfo  arrstOrderInfo[MAX_ORDERS_CNT];

Then I zeroed them in the function of position opening:

No, you haven't. Because dynamic arrays are declared which have zero size by default. So in this case these four lines of code do nothing.

Now how do you fill them in? The example I'm trying to use has an additional variable, but I don't understand how to use it:

Next - a loop of orders is organized, in which each "own" order is stored in an array:

g_nOrderCnt = 0;
for (int i = OrdersTotal() - 1; i >= 0; --i)
{
   if (!OrderSelect(i, SELECT_BY_POS))
      continue;

   if (OrderSymbol() != Symbol())   // Если нужны ордера только по текущему символу, к графику которого прикреплен советник
      continue;

   if (OrderMagicNumber != i_nMagicNumber)  // Если имеется входной параметр советника i_nMagicNumber, в котором указан ID ордеров советника
      continue;

   if (g_nOrderCnt < MAX_ORDERS_CNT)
      continue;

   g_arrstOrderInfo[g_nOrdersCnt].nTicket = OrderTicket();
   g_arrstOrderInfo[g_nOrdersCnt].nType = OrderType();
   g_arrstOrderInfo[g_nOrdersCnt].fOpenPrice = OrderOpenPrice();
   g_arrstOrderInfo[g_nOrdersCnt].fSL = OrderStopLoss();
   g_arrstOrderInfo[g_nOrdersCnt].fTP = OrderTakeProfit();

   ++g_nOrdersCnt;
}

Although, I'm not quite sure what all those arrays are for, when I can get the values of the ticket, position type, open price and open time using the corresponding function.

It is much faster and more convenient to work with arrays, since in general, not all of the orders present in the account need to be processed by this particular EA. And we will save a lot of efforts and money as a result.

Besides, I mentioned above is a general case. Universalism is not always needed, of course. Usually, such arrays are created based on the requirements of a strategy. For example, we can divide orders by type right away: Buy, Sell, BuyStop, SellStop, BuyLimit, SellLimit. Then we will need four such arrays. But later, when making trading decisions, we will not have to go through the entire list of open orders again. We only need to know the number of orders of a certain type and refer to the necessary array.

There is one more important thing to consider: changing the array of orders during processing of one tick. It may well happen that one list of orders was received at the entry to OnTick, and another one somewhere in the middle. This will lead to an error in the program operation that is difficult to catch. And the array of orders that was already formed when entering OnTick will not change (unless, of course, you change it yourself during the execution of the program).

 
STARIJ:

string s=FileReadString(F1); // Read string of the text file
StringSplit(s, "," , a); // Comma-separated string elements into an array
datetime T1=StrToTime(a[4]); // Further, the transformation ...
int ord=StrToInteger(a[8]);
double Price=StrToDouble(a[12]);

Thank you, just what I wanted.



Next, there are 5 products made

We throw in an alert reading (any reading)

for example level "200"

When the alert goes off, something opens the pose, something deletes itself, something does other things. I want to implement a mechanism for giving a signal in this way


P.S.

I have finished what I wanted to, I can send it for review. Sheds perfectly. But it is forbidden to post here.

 
Ihor Herasko:

For example, it is possible to divide orders by type at once: Buy, Sell, BuyStop, SellStop, BuyLimit, SellLimit. Then we will need four such arrays. But then we will not need to go through the entire list of open orders again when making trade decisions. We only need to know the number of orders of a certain type and refer to the necessary array.

That is exactly what I need, or rather just forBuy and Sell. The main thing for my simple strategy is not to let the open positions interfere with each other to see the closing conditions. Maybe, we can do without arrays? I do not understand them: how to create them, how to address them - I'm in the dark. Maybe my situation will become clearer to you on the screenshot:

It seems that everything in the code is simple and works clearly, but this is the only case of incomprehension.

 
novichok2018:

This is exactly what I need, or rather only forBuy and Sell. For my simple strategy, the most important thing is that open positions do not interfere with each other's closing conditions. Maybe we can do without arrays? I do not understand them: how to create them, how to address them - I'm in the dark. Maybe my situation will become clearer to you on the screenshot:

It seems that everything in the code is simple and works clearly, but this is the only case of incomprehension.

Of course, in the simplest (and medium complexity) it's more convenient without arrays. And when you get to the point of using hundreds of orders, it's much easier to distinguish between the orders. For example, according to OrderType() - one to buy 0, the other one to sell 1. Your trader's strategy is good - you can gain 1 day or lose half of a day. The only thing we need is how to catch these arrows. And they are too frequent. It makes sense to first learn how to give signals: Buy and Sell
 
LRA:
Of course, in the simplest (and medium complexity) without arrays is more convenient. But when you get to the point of using hundreds of orders - then...

Could it be the reason for my situation that the log writes: 2018.01.25 20:22:12 2018_WPR14_AMarkets EURUSD,M5: OrderClose error 138 and repeats solidly to

2018.01.26 16:38:12 2018_WPR14_AMarkets EURUSD,M5: Alert: Total orders = 3 ? On the screenshot you can see that this period captures two close conditions of the SELL.

And the signals to open a position are triggered by a combination of several indicators and are closed by one indicator. And they do not work that often: it may be silent for a few days during five minutes. For example, since February 1 this year only 14 positions triggered.


 
novichok2018:

Could it be the reason for my situation that the log writes: 2018.01.25 20:22:12 2018_WPR14_AMarkets EURUSD,M5: OrderClose error 138 and repeats solidly to

2018.01.26 16:38:12 2018_WPR14_AMarkets EURUSD,M5: Alert: Total orders = 3 ? In the screenshot you can see that this period captures two close conditions

What is your situation? Are you saying that the terminal is making a log entry which is causing the error ? and what does 138 mean ? do you know where to look? How did you achieve this is a very rare error. What you see in the screenshot is half the battle. How to explain this to the Expert Advisor? You have to write some mathematical condition - instead of viewing the screenshot, the Expert Advisor operates with numbers

 
LRA:

Are you saying that the terminal makes a log entry, which causes the error? ... What does 138 mean? Do you know where to look?

No, I'm saying that the tester fails at this interval of history, which prevents the signals from working correctly. Because the requotes can't go on for 24 hours.

 
novichok2018:

No, what I mean to say is that there is a glitch in the tester at this point in the history, which prevents the signals from working correctly. Because requotes can't go on for 24 hours.

Requotes in the tester? That's the first I've heard. The tester is ideal - it doesn't even have slippage

...something's kicking the indicator - trending upwards. I'll buy ... Got it... Although it's not enough I wanted to move TP up - it already worked...

So set an EA that if it does that, then it will exit, and will continue on the next tick. And try it on the demo