Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1339

 
Kira27 #:
Hi!!! Tell me where I went wrong, I don't understand it myself)

Out of 275 tools, it processes 27 and then gets stuck, occupies memory, and produces an error string when the script is forced to terminate.

If this array

   double bufer_MA[];  //Буфер для хендла

then using it in

         CopyBuffer(MA200,0,TimeCurrent(),5,bufer_MA); // заполнение масива bufer_MA хендлом MA200 выбранного тайма

it's not right at all.

Or have I misunderstood something?

 
Alexey Viktorov #:

If this array

then its use in

is not right at all.

Or did I misunderstand something?

There's a lot of things wrong with it.

 
Alexey Viktorov #:

If this array

then using it in



Пример:



//+------------------------------------------------------------------+ 
//|                                              TestCopyBuffer3.mq5 | 
//|                        Copyright 2009, MetaQuotes Software Corp. | 
//|                                              https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "2009, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00"
 
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//---- plot MA 
#property indicator_label1  "MA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- input parameters 
input bool               AsSeries=true; 
input int                period=15; 
input ENUM_MA_METHOD     smootMode=MODE_EMA; 
input ENUM_APPLIED_PRICE price=PRICE_CLOSE; 
input int                shift=0; 
//--- indicator buffers 
double                   MABuffer[]; 
int                      ma_handle; 
//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- indicator buffers mapping 
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA); 
   Print("Параметр AsSeries = ",AsSeries); 
   Print("Индикаторный буфер после SetIndexBuffer() является таймсерией = ", 
         ArrayGetAsSeries(MABuffer)); 
//--- set short indicator name 
   IndicatorSetString(INDICATOR_SHORTNAME,"MA("+period+")"+AsSeries); 
//--- set AsSeries (depends on input parameter) 
   ArraySetAsSeries(MABuffer,AsSeries); 
   Print("Индикаторный буфер после ArraySetAsSeries(MABuffer,true); является таймсерией = ", 
         ArrayGetAsSeries(MABuffer)); 
//--- 
   ma_handle=iMA(Symbol(),0,period,shift,smootMode,price); 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 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[]) 
  { 
//--- check if all data calculated 
   if(BarsCalculated(ma_handle)<rates_total) return(0); 
//--- we can copy not all data 
   int to_copy; 
   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total; 
   else 
     { 
      to_copy=rates_total-prev_calculated; 
      //--- last value is always copied 
      to_copy++; 
     } 
//--- try to copy 
   if(CopyBuffer(ma_handle,0,0,to_copy,MABuffer)<=0) return(0); 
//--- return value of prev_calculated for next call 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+
 

is not right at all.

Or did I misunderstand something?

Why wrong? Because in the example to the function


CopyBuffer

and use

 

Alexey Viktorov #:


Or did I misunderstand something?

The point of the script is to go through each instrument in the Market Watch on timeframes from 1min to 4H to find the price above the MA200 and if this fact is detected, to generate an alert with the name of the symbol with the price above the MA on all 6 timeframes.

 
Artyom Trishkin #:

There are a lot of things wrong with it.

Be more specific, if you don't mind)

 
Kira27 #:

Be more specific, if you don't mind)

Array indexing is just plain creepy... Copying the same data in a loop... Creating new handles in a loop - no words...

I don't even want to look at the rest of them, sorry

 
Artyom Trishkin #:

Array indexing is just plain creepy... Copying the same data in a loop... Creating new handles in a loop - no words...

I don't even want to look at the rest of the code, sorry

I don't understand about indexing of arrays, what arrays you don't like? The data on line 43 of CopyBuffer function copies handles on different timeframes, from 1 minute to 4H. 5 MA indicator prices are copied, for the first 5 bars, and both handle and array with 5 bars are zeroed at each iteration

ArrayFree(bufer_MA);


and the handle is deleted.

 IndicatorRelease(MA200); //Удаление хендла

If you can propose an algorithm for handling 274 symbols and each of them on 6 timeframes in some other way, I would be happy to. It's just that the thread is kind of called -- Questions from Beginners. If I were a pro, I probably wouldn't have any questions.

 

creation of handles in different situations seems not costly, it's just a pointer without preloading? the main work is done with CopyBuffer

there is no need to remove the handle in the loop, you may need to check it. Personally, I have a constant problem only when creating the first one; it is solved by Sleep() to load a chart or the function of pre-loading quotes

the developers should have decided it themselves, speed is not important in OnInit, I meant the first indicator with 7 symbols, the error is always showing up at different iterations

----------

how does creating a handle work, in a special loop is it necessary to check availability or is just another creation enough (i.e. overwriting)?

 
Fast235 #:

creation of handles in different situations seems not costly, it's just a pointer without preloading? the main work is done with CopyBuffer

there is no need to remove the handle in the loop, you may need to check it. Personally, I have a constant problem only when creating the first one; it is solved by Sleep() to load a chart or the function of pre-loading quotes

the developers should have decided it themselves, speed is not important in OnInit, I meant the first indicator with 7 symbols, the error is always showing up at different iterations

----------

how does creating a handle work, in a special loop is it necessary to check availability or just another creation is enough (i.e. overwriting)?

Are you talking to me or is it a question for local experts?)

 
Kira27 #:

Are you talking to me, or is that a question for the experts here?)

About creating and removing handles,

It works, I'm interested in the right way, I hope the developers will tell me.

Reason: