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

 
Alexey Viktorov:

The problem is in this line

Since I want to check several currencies on one bar, I should check the time and currency to avoid repeating an alert on one bar and one symbol but allow an alert on the same bar with a different symbol. At first glance, we need one more array with flags whether the symbol is viewed or not.

In general, we have to either add a symbol check to this line, or repeat the loop only when a new bar appears. But I have a fear that when a new bar appears on the symbol with this indicator, a new bar has not yet appeared on another symbol.

The conclusion: we must strain our head muscles to determine whether a new bar appears on each symbol separately, but at the same time not to stretch the number of lines to infinity. I have no ready-made solution. And I don't like to suggest it by writing code...

And I haven't solved his problem?

Maybe I missed something somewhere - I wrote the code on my knee.

 
Artyom Trishkin:

And I haven't solved his problem?

Maybe I missed something somewhere - I wrote the code on my knee.

Artem, the last sentence of my post explains everything. Didn't even look at your code. Opened the first unread question with a quote from my previous reply, I answered and then saw the other advice. I don't doubt the correctness of your code, even from my knee...

 
Alexey Viktorov:

Artyom, the last sentence of my post explains everything. Didn't even look at your code. Opened the first unread, question with a quote of my previous answer, I answered and then saw other advice. I don't doubt the correctness of your code, even from my knee...

Would have said lazy ;)

Even at first glance you can see that the code depends on ticks on the current symbol. And thus we must remove the loop from OnTick() and shove it into a timer. And slightly modify the definition of time alerts.

 

Hello all, there is a misunderstanding of the process, in the function "start" in the cycle mm++; and follow the changes in it through Print(); so the question: "Why instead of 1,2,3,4, etc. pop up four-digit numbers? "

the code itself

int start()

  {
   int    counted_bars=IndicatorCounted();
   int limit, i;
                                               int mm=0;     // ======= ПЕРВОЕ========
  

   if(counted_bars<0) return(-1);

   limit=(Bars-counted_bars)-1;

for (i=limit; i>=0;i--)
    { 
       if (TimeDayOfWeek(Time[i]) != 0)
       {if (High[i+1]>LastHigh) LastHigh=High[i+1];if (Low[i+1]<LastLow) LastLow=Low[i+1];}
 

if (TimeDay(Time[i])!=TimeDay(Time[i+1]) && TimeDayOfWeek(Time[i])!=0)
   {
      
                                                   mm++; // ==========ВТОРОЕ==========
       
   
   
                                                   Print(" TEST= ",mm);// ========ТРЕТЬЕ========
  
   LastLow=Open[i]; LastHigh=Open[i];

  /* ObjectMove("Pivot", 0, Time[i],P);
     ObjectMove("S1", 0, Time[i],S1);
     ObjectMove("R1", 0, Time[i],R1); */
   }
   
   // S1Buffer[i]=S1;
   // R1Buffer[i]=R1;

}

//----
   return(0);
  }
//+------------------------------------------------------------------+
 
виталик:

Hello all, there is a misunderstanding of the process, in the function "start" in the cycle mm++; and follow the changes in it through Print(); so the question: "Why instead of 1,2,3,4, etc. pop up four-digit numbers? "

the code itself


At first run, 10 secondsIndicatorCounted()=0

 limit=(Bars-counted_bars)-1;

solimit= max bars in history-1

 
Alekseu Fedotov:


At first run, for 10 secondsIndicatorCounted()=0

meanslimit= max bars in history-1

still not clear, declaration and initialization with zero outside the loop int mm=0; and placed in the loop mm++; print() shows four-digit numbers changing with a difference of one unit only in descending direction for some reason
 
виталик:
still not clear, declaration and initialization with zero outside the loop int mm=0; and placed in the loop mm++; print() shows four-digit numbers changing with a difference of one unit, only in descending direction for some reason
Not all prints are printed in this log. Look in the file.
 

Alexey Viktorov:

...you have to strain your head muscles...

Thank you to everyone who responded.

Artyom Trishkin: Like this:

Artem, why do you write that you haven't solved it? It sends the signals for all the tools, but it only loads the system heavily. Is it possible to reduce the load?

Restricting the check on a new bar won't work because there will also be conditions that must be checked on every tick.

I try to add OnTimer() to your code. It worked in EA when OnTick() but how to do it with OnCalculate ?

int OnInit()
  {
  EventSetMillisecondTimer(1);
//--- indicator buffers mapping
   IndicatorShortName("UP DN");
   ushort sz=SetSymbols(Symbols,symbols_array);
   if(sz==0) {
      Print("Список символов пуст!");
      return(INIT_FAILED);
      }
//---
   return(INIT_SUCCEEDED);
  }
  
  void OnDeinit(const int reason)
{// ---
        EventKillTimer();
 // ---
}
void OnTimer()
  {
   RefreshRates();
  // OnTick();
   OnCalculate();
  }

 
Ibragim Dzhanaev:

Can you tell me why orders are not opening?


OK, it will open:
//пересечение вверх
bool PeresVverh()
  {//Объявление пересечения вверх
   //if(Ma(MaPer1,2)>Ma(MaPer2,2))
      if(Ma(MaPer1,1)>=Ma(MaPer2,1))
         if(Ma(MaPer1,0)<Ma(MaPer2,0))
            return(true);
   return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool PeresVniz()
  {//Объявление пересечения вниз
   //if(Ma(MaPer1,2)<Ma(MaPer2,2))
      if(Ma(MaPer1,1)<=Ma(MaPer2,1))
         if(Ma(MaPer1,0)>Ma(MaPer2,0))
            return(true);
   return(false);
  }

 
Sile Si:

Thank you to everyone who responded.

Artyom, why do you write that you haven't solved it? It's signalling on all the instruments, but it's loading the system heavily. Is there any way to reduce the load?

The limit of checking on a new bar won't do because there will be also conditions that have to be checked on every tick.

In your code, I try to add OnTimer(), it worked in EA when OnTick() but how to do it with OnCalculate ?


Try it with a timer for 200 milliseconds.

//+------------------------------------------------------------------+
//|                                                      Test_01.mq4 |
//|              Copyright 2017, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property indicator_chart_window

input string Symbols = "EURUSD, GBPUSD, USDJPY"; // Список символов, разделитель - запятая
//---
struct SSymbolsData
  {
   string   name;       // Имя символа
   datetime time_alert; // Время последнего алерта
  };
SSymbolsData symbols_array[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   EventSetMillisecondTimer(200);
   IndicatorShortName("UP DN");
   ushort sz=SetSymbols(Symbols,symbols_array);
   if(sz==0) {
      Print("Список символов пуст!");
      return(INIT_FAILED);
      }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   for(int i=0; i<ArraySize(symbols_array); i++) {
      if(Condition(symbols_array[i].name,1)==ORDER_TYPE_BUY) {
         datetime tm=iTime(symbols_array[i].name,PERIOD_CURRENT,0);;
         if(symbols_array[i].time_alert!=tm) {
            Alert("UP - " ,symbols_array[i].name,", time: ",TimeToString(tm,TIME_DATE|TIME_MINUTES));
            symbols_array[i].time_alert=tm;
            }
         }
      if(Condition(symbols_array[i].name,1)==ORDER_TYPE_SELL) {
         datetime tm=iTime(symbols_array[i].name,PERIOD_CURRENT,0);
         if(symbols_array[i].time_alert!=tm) {
            Alert("Down - " ,symbols_array[i].name,", time: ",TimeToString(tm,TIME_DATE|TIME_MINUTES));
            symbols_array[i].time_alert=tm;
            }
         }
      }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int Condition(string symbol_name,int shift) {
   MqlRates array[];
   if(CopyRates(symbol_name,PERIOD_CURRENT,shift,2,array)==2){
      if(array[0].open<array[0].close && array[1].open>array[1].close) return(ORDER_TYPE_BUY);
      if(array[0].open>array[0].close && array[1].open<array[1].close) return(ORDER_TYPE_SELL);
      }
   return(WRONG_VALUE);
}
//+------------------------------------------------------------------+
ushort SetSymbols(string symbols_list,SSymbolsData &array[]){
   symbols_list+=","; // Добавим признак конца строки
   short beg=WRONG_VALUE, end=1, len=(short)StringLen(symbols_list);
   string sy="";
   Print(__FUNCTION__," > ",symbols_list); // Посмотрим символы в строке
   while(beg<len) {
      beg++;
      end=(short)StringFind(symbols_list,",",beg);
      if(end==beg || end<0) continue;
      sy=StringSubstr(symbols_list,beg,end-beg);
      if(CheckSymbol(sy,array) || !IsPresentSymbol(sy)) continue;
      ushort sz=(ushort)ArraySize(array);
      ArrayResize(array,sz+1);
      array[sz].name=sy;
      array[sz].time_alert=0;
      //--- Посмотрим корректность найденного символа и записи его в массив
      Print("beg=",IntegerToString(beg,2,'0'),", end=",IntegerToString(end,2,'0'),", sy=|",sy,"|",", in array[",sz,"]=",array[sz].name);
      }
   return((ushort)ArraySize(array));
}
//+------------------------------------------------------------------+
bool CheckSymbol(string symbol_name,SSymbolsData &array[]){
   for(short i=0; i<ArraySize(array); i++) if(array[i].name==symbol_name) return(true);
   return(false);
}
//+------------------------------------------------------------------+
bool IsPresentSymbol(string symbol_name){
   for(ushort i=0; i<SymbolsTotal(false); i++){
      if(SymbolName(i,false)==symbol_name) {
         SymbolSelect(symbol_name,true);
         return(true);
         }
      }
   return(false);
}
//+------------------------------------------------------------------+
Reason: