Questions from Beginners MQL5 MT5 MetaTrader 5 - page 989

 
Aleksey Vyazmikin:

How do I find the reason for this message?

Profiler

 
Artyom Trishkin:

You don't know how to check the return value for zero? And don't know how to exit OnCalculate() with a return of zero? Have you never tried to search for information on the website?

  1. Getting the required value from the function
  2. Check if it is zero
    1. If it is zero, then return 0;
    2. If it is not zero, then to step 3
  3. Working with this value
I never thought that I will be telling the basics of logic on this resource to a man with the rating exceeding 13 thousand...

This is not an indicator.

I intentionally specified whether I should do checks at each call or one at start of OnTick is enough...

Returns are not acceptable for me ... and to loop the wait in the tester is impossible, and in the real should work (or not?).

My rating is not for programming, I am not a programmer, and I am learning this complicated business only thanks to people like you, I really appreciate feedback from knowledgeable people.

Thank you for your understanding.

 
Artyom Trishkin:

Profiler

What if it happens once a day or less?

 
Hello. For some reason Handel only transmits when set to all periods, but I set to a specific period and there is nothing. I don't know what the problem is. Tried typing in handel directly not from global variables, result is the same. What's wrong? I need the value to be from one period.
//+------------------------------------------------------------------+
//|                                                        77777.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   0
//+----------------------------------------------+
//|  Параметры отрисовки индикатора 1            |
//+----------------------------------------------+
//----
input string xBars="2018.11.19 00:00";            //Дата начала отсчета
input bool DataBars=false;                        //Считать по дате (true)
input int InpMAShift=1;                           //Сдвиг бара расчета 
input ENUM_TIMEFRAMES Timeframes=PERIOD_D1;       //Таймфрейм скользящей
input ENUM_MA_METHOD  Method=MODE_EMA;            //Метод  расчета скользящей
input ENUM_APPLIED_PRICE AppliedPrice=PRICE_CLOSE; //Расчет цены скользящей
input ENUM_LINE_STYLE MAStyle=STYLE_DASH;         //Стиль всех скользящих линий
input int InpMAPeriod1=5;                         //Скользящая 1 периода

//----
int handle_ma,shift;
double BufferPrice[];
//---
int period;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   period=(InpMAPeriod1<1 ? 1 : InpMAPeriod1);
//---
   SetIndexBuffer(2,BufferPrice,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(BufferPrice,true);
//--- create MA's handles
   ResetLastError();

//----------------------------------------------------------------+
//                     ПРОБЛЕМА ТУТ 
//
//----------------------------------------------------------------+
   handle_ma=iMA(NULL,PERIOD_D1,period,0,MODE_SMA,PRICE_CLOSE);         //ПРОБЛЕМА ТУТ 
 //  handle_ma=iMA(NULL,PERIOD_CURRENT,period,0,MODE_SMA,PRICE_CLOSE);      // БЕЗ ПРОБЛЕМЫ
   if(handle_ma==INVALID_HANDLE)
     {
      Print("The iMA (",(string)period,") object was not created: Error ",GetLastError());
      return INIT_FAILED;
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 | 
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  ObjectDelete(0,"Proba");
   Comment("");
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- Проверка количества доступных баров
   if(rates_total<4) return 0;
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-1;
      ArrayInitialize(BufferPrice,0);
     }
//--- Подготовка данных
   int count=(limit>1 ? rates_total : 2),copied=0;
   copied=CopyBuffer(handle_ma,0,0,count,BufferPrice);
   if(copied!=count) return 0;
//----   
if(DataBars==false)
    shift=InpMAShift;
 else
    shift=iBarShift(NULL,PERIOD_CURRENT,StringToTime(xBars)); 
    
 double ma1=BufferPrice[shift];
 datetime tm=iTime(NULL,Timeframes,shift);
 
 Comment(NormalizeDouble(BufferPrice[shift],_Digits),"    ",shift);
 
 
   HLine("Proba","",tm,ma1,MAStyle,1,Red);

//--- return value of prev_calculated for next call

   return(rates_total);
  }
//+------------------------------------------------------------------+
void HLine(string name,
           string text,
           datetime tm,
           double price,
           int stl,
           int wid,
           color clr)
  {
   if(ObjectFind(0,name)!=0)
     {
      ObjectCreate(0,name,OBJ_HLINE,0,tm,price);
      ObjectSetInteger(0,name,OBJPROP_TIME, tm);
      ObjectSetString(0,name,OBJPROP_TEXT,text); 
      ObjectSetString(0,name,OBJPROP_FONT,"Times New Roman"); 
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,10); 
      ObjectSetInteger(0,name,OBJPROP_STYLE, stl);
      ObjectSetInteger(0,name,OBJPROP_WIDTH, wid);
      ObjectSetInteger(0,name,OBJPROP_COLOR, clr);
//--- скроем (true) или отобразим (false) имя графического объекта в списке объектов 
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true); 
     }
  }

 
kopeyka2:
Hello. I can't find anything, for some reason the handel only transfers when it's set to all periods, but I set it to a certain period and nothing happens. I don't know what the problem is. I tried to input data not from global variables directly into handel, the result is the same. What's wrong?
      ResetLastError();
   if(CopyBuffer(handle_ma,0,0,count,BufferPrice)<0)
     {
      PrintFormat("Failed to copy data from the handle_ma indicator, error code %d",GetLastError());
      return(0.0);
     }
//   copied=CopyBuffer(handle_ma,0,0,count,BufferPrice);

Study the error, Artem is right in saying that you should check everything 10 times...

2019.01.25 20:12:26.169 Test777 (Si Splice,M1)  Failed to copy data from the handle_ma indicator, error code 4806

ERR_INVALID_ARRAY

4006

Array of unsuitable type, unsuitable size or corrupted dynamic array object


ERR_INDICATOR_DATA_NOT_FOUND

4806

Requested data not found


 
kopeyka2:
Hello. I've noticed that the handel only transfers when it's set for all periods, but when I set it for a certain period, nothing happens. I do not understand what the problem is. Tried typing in handel directly not from global variables, result is the same. What's wrong? I need the value to be from the same period.

You take examples from the CodeBase from Scriptor's indicators, don't you? Right? Then look in his code (recently published) for examples that have MTF in their name, but without MCP.

MTF = MultiTimeFrame, MCP = MultiCurrencyPair. I.e., if you want to get data on the current symbol, but from another timeframe than the chart period, then you need an example of a multi-timeframe indicator. In MCP you should not get involved yet - there on the OOP with the use of the standard classes. You will get confused.

For example, this calculation:

//--- Подготовка данных
   int count=(limit>1 ? rates_total : 2),copied=0;
   copied=CopyBuffer(handle_ma,0,0,count,BufferPrice);
   if(copied!=count) return 0;

It's only for MA working on the current period and symbol. For working on a different period, the calculation will be different. Look it up - everything is close, and the direction I gave you.

 
Artyom Trishkin:

You take examples from the CodeBase from Scriptor's indicators, don't you? Right? Then look in his code (recently published) for examples that have MTF in their name, but without MCP.

MTF = MultiTimeFrame, MCP = MultiCurrencyPair. I.e., if you want to get data on the current symbol, but from another timeframe than the chart period, then you need an example of a multi-timeframe indicator. In MCP you should not get involved yet - there on the OOP with the use of the standard classes. You will get confused.

For example, this calculation:

it is only for MA working on current period and symbol. For work on a different period, the calculation will be different. Search - everything is close, and I gave you the direction.

Thank you...
 
Aleksey Vyazmikin:

Study the error, Artem is right in saying that you should check everything 10 times...

ERR_INVALID_ARRAY

4006

Array of unsuitable type, unsuitable size or corrupted dynamic array object

Thank you
 
kopeyka2:
Thank you

Please, but I copied the wrong error, it should be

ERR_INDICATOR_DATA_NOT_FOUND

4806

The requested data was not found

The point is that the number of bars is different for different TFs and this should be taken into account.

 
Aleksey Vyazmikin:

Please, but I copied the wrong error, it should be

ERR_INDICATOR_DATA_NOT_FOUND

4806

The requested data was not found

The point is that the number of bars is different for different TFs and this should be taken into account.

I was just checking it. But the PROBLEM is that having set handel on one constant period, in

CopyBuffer the value on other timeframes is NOT PERMANENT. On the set handle period the value was, but on the smaller timeframes 0.0 (zero). Moved it around the buffer in search... zero. Why is it not passed toCopyBuffer ?

Reason: