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

 
Galim_V:

This is how to modify

there are many errors in the code, in

if(OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss_SELL,0,0,Green))

it is better to normalize prices, then they will work, then the server will give out an error of "wrong prices", it is desirable to normalizeStopLoss_SELL

and most likely, you need to check if the StopLoss is equal to zero - it is not desirable to compare real numbers to be equal, you should try to compare real numbers to > or <

 
Jessy111:

Help me fix a bug in the indicator, it doesn't draw high and low of the day when it's on the last candle of the hour.

I have sketched out the indicator, your code does not like working with strings, I would write it simpler, here is an example. Although if your task is to take into account server time offset, then the other way round is needed.

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot line1
#property indicator_label1  "HighDay"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#property indicator_label2  "LowDay"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

static int LastDay=-1,daystart;
static double dhigh,dlow;
//--- indicator buffers
double         H[],L[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,H);
   SetIndexBuffer(1,L);
   IndicatorDigits(_Digits);
   LastDay=-1;
   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 i,j,limit;
   if(prev_calculated==0)
     {
      limit=rates_total-1;
      LastDay=-1;
      daystart=limit;
      dhigh = high[limit];
      dlow  = low[limit];
// расчет истории      
      for(i=limit; i>=0 && !IsStopped(); i--)
        {
         if(LastDay!=TimeDay(time[i]))
           {
            LastDay=TimeDay(time[i]);
            j=daystart;
            daystart=i;
            while(j>daystart)
              {
               H[j]=dhigh;
               L[j]=dlow;
               j--;
              }
            dhigh = high[i];
            dlow  = low[i];
           }
         dhigh= fmax(dhigh,high[i]);
         dlow =  fmin(dlow,low[i]);
        }
// рисуем текущий день при первом запуске
      i=daystart;
      while(i>=0)
        {
         H[i]=dhigh;
         L[i]=dlow;
         i--;
        }
     }
   else
     {
// рисуем текущий день на каждом тике
      i=0;
      dhigh = high[0];
      dlow  = low[0];
      while(TimeDay(time[0])==TimeDay(time[i]))
        {
         dhigh= fmax(dhigh,high[i]);
         dlow =  fmin(dlow,low[i]);
         i++;
        }
      while(i>=0)
        {
         H[i]=dhigh;
         L[i]=dlow;
         i--;
        }
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+

My example is also not an example, I wrote it fast, I think I should not have counted from the end of history to the beginning at first run, but from zero bar to the history, the code would have been shorter and would have been able to calculate the current day faster - the cycle would have been shorter,

But I checked it in the tester, it seems to work without problems, let it be that way, it's late, I'm sleepy... I made a bad example (((

 
Igor Makanu:

there are a lot of errors in the code, in

it is better to normalize prices, then they will work, then the server will give out an error of "wrong prices", it is desirable to normalizeStopLoss_SELL

And most likely, you need to check whether the StopLoss is equal to zero, and it is not desirable to compare real numbers to be equal, you should try to compare real numbers to > or <

That's right. I just showed a place in the code, why it doesn't modify market orders: requesting a stop from the market will give 0.

 
Igor Makanu:

Here's a sketched indicator, I don't like your code with strings, I would write it simpler, here's an example. Although if your task is to take into account server's time offset, then you have to do it differently.

My example is also not an example, I wrote it fast, I think I should not have counted from the end of history to the beginning at first run, but from zero bar to the history, the code would have been shorter and would have been able to calculate the current day faster - the cycle would have been shorter,

But I checked it in the tester, it seems to work without problems, let it be that way, it's late, I'm sleepy... Bad example done (((

It's not my code, I found it on the Internet, I can't write indicators. :)

For what it's worth a bad example, I really liked your indicator, it works in the Strategy Tester and on real charts.

Many thanks for the quick help!

May I also ask you to remove the lines that draw the channel? I have marked them with red arrows on the screenshot.

And if it's not too much trouble at all, please add the function to turn off/on the formation of lines on the current day. I marked them on the screenshot with blue arrows. https://prnt.sc/kuuj3e

Thanks in advance!

Скриншот
Скриншот
  • prnt.sc
Снято с помощью Lightshot
 

Hello Dear!

Please advise how to execute the OnInit() procedure;

after Expert Advisor variables have been changed!?

When placing the Expert Advisor on the chart, there is OnInit(); but when variables are changed, there is no OnInit!?

How to identify the event of the Expert Advisor variable change!?

Please help with advice, or rather with code, how to do it!?

 
Игорь:

Hello Dear!

Please advise how to execute the OnInit() procedure;

after Expert Advisor variables have been changed!?

When placing the Expert Advisor on the chart, there is OnInit(); but when variables are changed, there is no OnInit!?

How to identify the event of the Expert Advisor variable change!?

Please help with advice, or rather with code, how to do it!?

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int var1 = 1;
//+------------------------------------------------------------------+
int OnInit()
  {
      Print("Вызов OnInit(), var1=",var1);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason){}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

  }
//+------------------------------------------------------------------+

Experiment with this code, read the message in the EA log (switch the TF, enter the variable, restart the terminal without closing the EA...)

and the second option is to describe at global scope variable with modifier static

#property strict

input int var1=1;
static bool FirstRun=true;
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("Вызов OnInit(), var1=",var1);
// раскоментируйте FirstRun=true; если нужно контролировать вызов OnInit()
//   FirstRun=true;     
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason){}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(FirstRun)
     {
      Print("Это первый запуск эксперта");
      FirstRun=false;
     }
  }
//+------------------------------------------------------------------+
 
Igor Makanu:

experiment with such code, read the message in the Expert Advisor log (switch the TF, enter the variable, reload the terminal without closing the Expert Advisor...)

and the second option is to describe at global scope variable with modifier static

At the global scope with the modifier static????? This is some kind of perversion.

 
Alexey Viktorov:

On a global level with the static modifier static????? This is some kind of perversion.

But from my experience, when MT4 builds were often updated, it's better to write them according to classic C++, than to listen to the answer "write the code correctly" when communicating with developers



ZS: I remember why I started to write the static modifier globally, if I want to make sure I won't "lose" the variable value during MQL-program run. Earlier (about 5 years ago) the #property strict precompiler directive didn't exist and when enabling files (#include or library - I don't remember anymore) I "lost my variable" once, as the variable names coincided with the variable name.I was cursed by MQL but later I found this mistake by accident. But if I would write a static modifier the compiler would display a compilation error in that case too.

The current compiler is MT4, pretty handy and reliable, and with the directive #property strict, maybe I don't need such precautions, but I write static as a habit when I don't want to "lose a value" of a variable

ZZZY: this modifier does not affect the speed of execution of MQL-program, and it's convenient for me to open my code after some time, and having seen static I will know exactly, that this variable has a value, which is important "not to lose" during program execution

 
Igor Makanu:

But from my experience, when MT4 builds were often updated, it's better to write them according to classic C++, than to listen to the answer "write the code correctly" when communicating with developers



ZS: I remember why I started to write the static modifier globally, if I want to make sure I won't "lose" the variable value during MQL-program run. Earlier (about 5 years ago) the #property strict precompiler directive didn't exist and when enabling files (#include or library - I don't remember anymore) I "lost my variable" once, as the variable names coincided with the variable name.I was cursed by MQL but later I found this mistake by accident. But if I would write a static modifier the compiler would display a compilation error in that case too.

The current compiler is MT4, pretty handy and reliable, and with the directive #property strict, maybe I don't need such precautions, but I write static as a habit when I don't want to "lose a value" of a variable

ZZZY: this modifier does not affect the speed of execution of MQL-program, and it's convenient for me to open my code after some time, and having seen static I will know exactly, that this variable has a value, which is important "not to lose" during program execution

The compiler will generate a warning if the variable names coincide with those in the include file. To me, a warning equals an error and I react to them the same way. And global-level variables with a static modifier equals butter.

But... If you like to write like that, no one can forbid you.

 
Alexey Viktorov:

But... But... if you like writing it this way, no one can forbid you.

I have doubts about my uniqueness in this matter... I'm not the only one, searching through the MQL5 folder I found such declarations with static modifier on a global level in several source code of MT5 delivery

Even in the article "RANDOM DECISION FOREST IN THE SUMMARY" in the source code I found a declaration on the global level: static datetime last_time=0;

so I wrote as I see fit to allocate and will be, I do not 100% of variables at the global level describing as static

God protects the needy, said the nun... ))))