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

 
Vitaly Muzichenko:

Here's the tweaked one, the original one is just plain gloomy.

P.S. Dot.mq4 - full original

Sorry for the long time, I was busy with work.

Here you go:

//+------------------------------------------------------------------+
//|                                                      VMTest1.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                             https://mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_plots   3
//--- plot ARR
#property indicator_label1  "MaOSC"
#property indicator_type1   DRAW_COLOR_ARROW
#property indicator_color1  clrLimeGreen,clrOrangeRed,clrSilver
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot ArrUP
#property indicator_label2  "Up"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot ArrDN
#property indicator_label3  "Down"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrCrimson
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//---
#include <MovingAverages.mqh>

//--- input parameters
input uint     InpA=40;
//--- indicator buffers
double         BufferMAOSC[];
double         BufferColorsARR[];
double         BufferArrUP[];
double         BufferArrDN[];
double         BufferMAV[];
double         BufferMAA[];
double         BufferTMP[];
//--- gv
int a=0;
int per=0;
int val=0;
int handle_mav=INVALID_HANDLE;
int handle_maa=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferMAOSC,INDICATOR_DATA);
   SetIndexBuffer(1,BufferColorsARR,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,BufferArrUP,INDICATOR_DATA);
   SetIndexBuffer(3,BufferArrDN,INDICATOR_DATA);
   SetIndexBuffer(4,BufferMAV,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,BufferMAA,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6,BufferTMP,INDICATOR_CALCULATIONS);
   
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,225);
   PlotIndexSetInteger(2,PLOT_ARROW,226);
//---
   ArraySetAsSeries(BufferMAOSC,true);
   ArraySetAsSeries(BufferColorsARR,true);
   ArraySetAsSeries(BufferArrUP,true);
   ArraySetAsSeries(BufferArrDN,true);
   ArraySetAsSeries(BufferMAV,true);
   ArraySetAsSeries(BufferMAA,true);
   ArraySetAsSeries(BufferTMP,true);
//---
   a=(InpA<4 ? 4 : InpA);
   per=(int)floor(sqrt(a));
   val=(int)floor(a/1.9);
//---
   handle_mav=iMA(NULL,PERIOD_CURRENT,val,0,MODE_SMA,PRICE_LOW);
   if(handle_mav==INVALID_HANDLE)
     {
      Print("Не удалось создать хэндл индикатора MA(",val,"), ошибка ",GetLastError());
      return INIT_FAILED;
     }
   handle_maa=iMA(NULL,PERIOD_CURRENT,a,0,MODE_SMA,PRICE_LOW);
   if(handle_maa==INVALID_HANDLE)
     {
      Print("Не удалось создать хэндл индикатора MA(",a,"), ошибка ",GetLastError());
      return INIT_FAILED;
     }
//---
   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[])
  {
//---
   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-2;
      ArrayInitialize(BufferMAOSC,EMPTY_VALUE);
      ArrayInitialize(BufferColorsARR,2);
      ArrayInitialize(BufferArrUP,EMPTY_VALUE);
      ArrayInitialize(BufferArrDN,EMPTY_VALUE);
      ArrayInitialize(BufferMAV,0);
      ArrayInitialize(BufferMAA,0);
      ArrayInitialize(BufferTMP,0);
     }
//--- Подготовка данных
   int count=(limit>1 ? rates_total : 1),copied=0;
   copied=CopyBuffer(handle_mav,0,0,count,BufferMAV);
   if(copied!=count) return 0;
   copied=CopyBuffer(handle_maa,0,0,count,BufferMAA);
   if(copied!=count) return 0;

//--- Предварительный расчёт
   for(int i=limit;i>WRONG_VALUE;i--)
      BufferTMP[i]=2.0*BufferMAV[i]-BufferMAA[i];
      
//--- Расчёт индикатора
   if(SimpleMAOnBuffer(rates_total,prev_calculated,a,per,BufferTMP,BufferMAOSC)==0)
      return 0;
   for(int i=limit;i>WRONG_VALUE;i--)
     {
      BufferArrUP[i]=EMPTY_VALUE;
      BufferArrDN[i]=EMPTY_VALUE;
      BufferColorsARR[i]=(BufferMAOSC[i]>BufferMAOSC[i+1] ? 0 : BufferMAOSC[i]<BufferMAOSC[i+1] ? 1 : 2);
      if((BufferColorsARR[i]==0 && BufferColorsARR[i+1]==1) || (BufferColorsARR[i]==0 && BufferColorsARR[i+1]==2))
         BufferArrUP[i]=BufferMAOSC[i];
      if((BufferColorsARR[i]==1 && BufferColorsARR[i+1]==0) || (BufferColorsARR[i]==1 && BufferColorsARR[i+1]==2))
         BufferArrDN[i]=BufferMAOSC[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Valeriy Yastremskiy:

I understand this idea, it's useful, but it doesn't work for me in this case. I need to time the Tester: stop button pressed in the indicator

it is not certain that after pressing the button the prints will be written to the log

so try to write every new bar in the file all the functions of working with the environment - banning trading, testing, terminating the program.....https://docs.mql4.com/ru/check

if no help, then you can not, then look for something by means of WinAPI

HH: make a button and click it - press it, do all the actions, and then let the visualizer finishes the run idly, imho faster than search

 
Igor Makanu:

it is not certain that prints are written to the log after the button is pressed

so try to write into the file each new bar all functions to work with the environment - banning trading, testing, terminating the program.....

If it does not help, then you can not, then look for something by means of WinAPI

HH: Make a button and press it - press it, do all the actions, and then let the visualizer finishes the run idly, imho quicker than looking for

Print stop button in Tester magazine : stop button pressed

But I haven't found it in the docs. For the Expert Advisor stop is called by OnDynit, for the indicator only the print in the journal. The indicator has stopped testing and that's it. There is actually a pause. But it is not printed in the journal.

Note

In general, it is not hard to delete the indicator or close the window manually. But the question is interesting. It's like access to real time through a request to a third-party symbol) It's supposed to somehow track the end of testing with the stop button).

 
Valeriy Yastremskiy:

Print of stop button in Tester magazine : stop button pressed

But I haven't found it in the docs. For the Expert Advisor stop is called by OnDeinit, for the indicator only print in the journal. The indicator stops testing and that's it. There is actually a pause. But it is not printed in the journal.

Note

In general, it is not hard to delete the indicator or close the window manually. But the question is interesting. It's like access to real time through a request to a third-party symbol) It seems it should somehow track the end of the test button stop).

don't know

search the forum IsTesting() - found similar discussions

ZZY: I think the log prints out the terminal, but I need the MQL code to do it

ZZZY: write EA, imho, the visualization of the indicator is tedious ))))

 
Artyom Trishkin:

I'm sorry it took so long - I've been busy with work.

Here you go:

The changes are drastic, but that's not the point.

I'll deal with iMAOnArray, that was the goal

Thank you!

 
Vitaly Muzichenko:

The changes are dramatic, but that's not the point.

I will deal with iMAOnArray, that was the goal

Thank you!

It's just that everything here is exactly the same as there. But in MQL5 it is as easy as that.

 

Hello. Why when deleting a constant not described inObjectCreate();

For instance, z_order or hidden? Will it cause an error after compilation? They don't take part in drawing the line.

bool VLineCreate(const long            chart_ID=0,        // ID графика 
                 const string          name="VLine",      // имя линии 
                 const int             sub_window=0,      // номер подокна 
                 datetime              time=0,            // время линии 
                 const color           clr=clrRed,        // цвет линии 
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // стиль линии 
                 const int             width=1,           // толщина линии 
                 const bool            back=false,        // на заднем плане 
                 const bool            selection=true,    // выделить для перемещений 
                 const bool            ray=true,          // продолжение линии вниз 
                 const bool            hidden=true,       // скрыт в списке объектов 
                 const long            z_order=0)         // приоритет на нажатие мышью 
  { 
//--- если время линии не задано, то проводим ее через последний бар 
   if(!time) 
      time=TimeCurrent(); 
//--- сбросим значение ошибки 
   ResetLastError(); 
//--- создадим вертикальную линию 
   if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time,0)) 
     { 
      Print(__FUNCTION__, 
            ": не удалось создать вертикальную линию! Код ошибки = ",GetLastError()); 
      return(false); 
     } 
 
Дмитрий:

Hello. Why when deleting a constant not described inObjectCreate();

For instance, z_order or hidden? Will it cause an error after compilation? They don't take part in drawing the line.

When deleting one of the input function's parameters, be careful with commas and closing parentheses.

 
Alexey Viktorov:

When deleting one of the input parameters of a function, be careful of the commas and closing brackets.

Alexey, thank you. I paid attention to punctuation first of all. The script has reduced to a minimum, left necessary for ObjectCreate()(anchor point, symv, AND TD). And it turns out that only in the header those constants in the code anywhere do not occur) on this and the question arose, why delete at least one error occurs. And the question arises, there may be mandatory conditions with a list of constants in the header. function header)))?
 
Dzmitry Zaitsau:
Alexey, thank you. I paid attention to punctuation first of all. The script was reduced to a minimum, I left the necessary for ObjectCreate()(anchor point, symv, AND TD). And it turns out that only in the header those constants in the code anywhere do not occur) on this and the question arose, why delete at least one error occurs. And the question arises, there may be mandatory conditions with a list of constants in the header. function header)))?

If you could write down what error appears, it would be easier to understand, otherwise it comes out as telepathy - guess what error I have)