Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 659

 
Roman Sharanov:

I also found a serious drawback, too many calls to CopyClose()

Can you tell me how to copy quotes from an array of currency pairs in a loop?

That would appear to be a double array, that's why MQL doesn't support it, like %.

MQL4 seems to work with two-dimensional dynamic arrays. You should change the first dimension with ArayResize(), and pass the second one toCopyClose()

Or a universal solution from the developers, because two-dimensional dynamic arrays do not work in MQL5, wrap a one-dimensional array in a structure and create an array of such structures.... If you get the hang of it, consider that you've almost created a class, and it's more optimal - a class with methods that load the data and store it in the class properties (fields)
 
Igor Makanu:

MQL4 seems to work with two-dimensional dynamic arrays, use ArayResize() to change the first dimension andCopyClose() to change the second.

or a universal solution from the developers, because 2-dimensional dynamic arrays do not work in MQL5, wrap a one-dimensional array in a structure and create an array of such structures.... If you get the hang of it, consider that you've almost created a class, and it's more optimal - the class with the method that loads the data and stores it in the class properties (fields)

Thank you and I will try

 

Another question about the indicator, I don't know what else to do here

I need to print the difference between closing of the pairs entered and the moving average of them.

I do not know what else to do.

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 2
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_type2 DRAW_LINE
#property indicator_style2 STYLE_SOLID
#property indicator_width1 1
#include <MovingAverages.mqh>

input string active_1 = "EURUSD";
input string active_2 = "USDJPY";
input ENUM_TIMEFRAMES timeframe = PERIOD_H1;
input int ma_period = 30;

double firstBuffer[], secondBuffer[], dataBuffer[], maBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, dataBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, maBuffer, INDICATOR_DATA);
   SetIndexBuffer(2, firstBuffer, INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, secondBuffer, INDICATOR_CALCULATIONS);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---

   CopyClose(active_1,timeframe,0,rates_total,firstBuffer);
   CopyClose(active_2,timeframe,0,rates_total,secondBuffer);

   int first, bar;
   if(prev_calculated == 0) first = begin; else first = prev_calculated - 1;

   for(bar = first; bar<rates_total; bar++){
      dataBuffer[bar] = firstBuffer[bar]-secondBuffer[bar];
   }

   ExponentialMAOnBuffer(rates_total,prev_calculated,begin,ma_period,dataBuffer,maBuffer);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Igor Makanu:

Orders should be enumerated in a loop, not on every tick... To be more precise, a tick came and all of your orders are counted in the loop: for(j=0;j<OrderTotal;j++)

OrdersTotal() shows how many orders are open (including pending orders, including orders on all symbols... in total... Here is how many orders are opened in the terminal - this is how many )))).

The order numbering is like in the arrays, from 0 to OrdersTotal()-1 - this is the last order, and you probably have a non-existent ticket number [OrdersTotal()] or some other information in the journal on every tick, which is difficult to guess.

Yes I do - and I want it to go through all the orders. The function was a bit different at first, but I've simplified it as much as possible in the hope of finding the cause and fixing it. Full consecutive retracement only happens when ticks are rare or after I remove the EA from the chart and no new ticks come in.

I always thought that if the EA has entered the body of a function, it should not react to other ticks until it exits the loop, but it seems that this is not the case. How can I get around this situation?

int test()
{
int total;
total = OrdersTotal();
log("total = " + total +"; ");

for (int j = 0; j < total; j++)
{
log("j = " + j +"; ");
}
return(0);
}

 
Андрей:
Why do we even need information on all orders every tick? Wouldn't it be better to introduce restrictions on order viewing, e.g. every new minute, five minutes, ..., hour?

The problem is not even that I need information for every tick, but that, if I understand correctly, the loop does not have time to finish before a new tick arrives and the new tick somehow breaks the account. Here's another screenshot, so that you don't have to look for the previous one. It shows that the EA has counted up to 52 and then jumped to 93. I had a hard time believing that this is because of new ticks, but I cannot find any other reason, because with rare ticks the account finishes fine, as well as the last cycle after the EA was removed from the chart.

I have already simplified the code as much as possible:

int test()
{
int total;
total = OrdersTotal();
log("total = " + total +"; ");

for (int j = 0; j < total; j++)
{
log("j = " + j +"; ");
}
return(0);
}

Files:
test.jpg  82 kb
 
Artyom Trishkin:

How do you know that? It doesn't.

Until all calculations that started with the arrival of the tick are completed, all subsequent ticks are skipped.

As I wrote just above, I have the same situation, only I have already simplified it to a point and come to the conclusion that not only can I not normally do any calculation, but just go through the orders one by one and print their numbers in the log.

I too have always believed that "Until all calculations that started with the arrival of a tick have been completed, all subsequent ticks are skipped". I wish it were so, but I can't find any other reason, other than new ticks, yet.

 
Artyom Trishkin:

No one will tell without the code.

Here is an extremely simplified code, which has the same problem - new ticks throw off successive output of order numbers.

int test()
{
int total;
total = OrdersTotal();
log("total = " + total +"; ");

for (int j = 0; j < total; j++)
{
log("j = " + j +"; ");
}
return(0);
}

 
Algotrader18:

Here is an extremely simplified code that has the same problem - new ticks throw off the sequential output of order numbers.

int test()
{
int total;
total = OrdersTotal();
log("total = " + total +"; ");

for (int j = 0; j < total; j++)
{
log("j = " + j +"; ");
}
return(0);
}

Does this code really compile without errors? I don't believe it...
 
Roman Sharanov:

Another question about the indicator, I don't know what else to do here

I need to print the difference between closing of the pairs entered and the moving average of them.

In the end it just displays the difference without MA, what is the problem?

This is cool!

Roman!

I, for example, am a successful trader with 20 years of experience.

I do not understand why you need "the difference between the closes of the pairs entered, anda moving average of them".

Moreover, do you know how to build a moving average of these differences? - Fantastic!

Can you simply draw the moving average of the quotes? - Also no?

So what is the problem?

 
Algotrader18:

Here is an extremely simplified code which has the same problem - new ticks throw off the sequential output of order numbers.

int test()
{
int total;
total = OrdersTotal();
log("total = " + total +"; ");

for (int j = 0; j < total; j++)
{
log("j = " + j +"; ");
}
return(0);
}

this code works, do it by analogy:

int NumberOfOrders(int magic_)
  {
   int i,res=0,k=OrdersTotal(); string sy=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderMagicNumber()==magic_) && (OrderSymbol()==sy)) res++;
        }
     }
   return(res);
  }
Reason: