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

 
Alexey Viktorov #:

No Makar, it looks like this:

The ObjectFind() function returns the number of the main window. And this number is 0. On the other hand, 0 == false, so it follows that if the function returns 0, it still returns false. Of course, I would not use this because if the function returns -1, i.e. an error, we may consider the location of the object not in the main window. On top of that, you never know what fixes will be made...

So don't argue and don't study how to do it...

The ObjectFind() function does not return the number of the main window. It will return the number of window where the object was found (0, 1, 2...) or -1 if the object doesn't exist.

I wonder if "-1" is false or true.

The booltype is used to store the boolean values true or false, with a numeric representation of 1 or 0 respectively .
 
MakarFX #:

The ObjectFind() function does not return the main window number. It is the number of window where the object is found (0,1,2...) or -1 if there is no object.

I was talking about specific case where object is located in main window. Don't argue and don't learn how to do it...

 

MakarFX #:

I wonder if "-1" is false or true?

The booltype is intended to store the boolean values true or false, whose numeric representation is 1 or 0, respectively .

false is only 0. Everything else is true

 
Alexey Viktorov #:

false is only 0. Everything else is true

"-1" is also true? Already checked. Really only "0" is false
 
Alexey Viktorov #:

No Makar, it looks like this:

The ObjectFind() function returns the number of the main window. And this number is 0. On the other hand, 0 == false, so it follows that if the function returns 0, it still returns false. Of course, I would not use this because if the function returns -1, i.e. an error, we may consider the location of the object not in the main window. On top of that, you never know what fixes will be made...

So don't argue and don't learn how to do it...

I realize that you should not do so, but I had to for the sake of aesthetics (== 0 in every condition looks ugly). And I only need the object in the main window.

 
Nerd Trader #:

I know I shouldn't do this, but I had to for the sake of aesthetics (== 0 in every condition looks ugly). And I only need the object in the main window.

Then do the following

if(ObjectFind("Buy Stop") || ObjectFind("not allowed"))

if there is no object, you'll get "-1", i.e. true

 
Reworked with option ==0.

Problem: Yellow and grey lines ("Stop Sell" and "not allowed") are constantly created and flickering when the button is pressed on the chart. And ObjectCreate() constantly returns true instead of false, i.e. it creates as if in another window and that could explain the flickering... But the window is only the main one and there are no anomalies with the green line ("Buy Stop") as well as with the red one ("Stop Loss").

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
    datetime static dt          = 0;
    double   static price       = 0;
    int      static window      = 0;
    string   static line_name;

  if(id==CHARTEVENT_OBJECT_CLICK){
    ...
    //Нажатие на SO в зависимоти от расположения панели создаёт линию
    if(sparam=="Button Stop Order"){
      if(panel_position == 1 || panel_position == 2){
        line_name = "Buy Stop";
        CreateLine(line_name, clrGreen);
      }
      if(panel_position == 3 || panel_position == 4){
        line_name = "Sell Stop";
        CreateLine(line_name, clrYellow);
      }
    }
    //Нажатие на линию удаляет её и создаёт красную
    if(sparam=="Buy Stop" || sparam=="Sell Stop"){
      ObjectDelete(line_name);
      line_name = "Stop Loss";
      CreateLine(line_name, clrRed);
    }
    //Нажатие на красной линии удаляет её и отжимает кнопку
    if(sparam=="Stop Loss"){
      ObjectDelete(line_name);
      button_name = "Button Stop Order";
      ms=GetMicrosecondCount();
    }
  }
  //Если кнопка SO нажата, то линия будет следовать за курсором
  if(ObjectGetInteger(0,"Button Stop Order",OBJPROP_STATE)){
    int x = (int)lparam;
    int y = (int)dparam;
    //Переведём координаты X, Y в дату и время
    if(ChartXYToTimePrice(0,x,y,window,dt,price)){
      //Линия меняет цвет с зелёного на желтый
      if(price < Bid){
        if(ObjectFind("Buy Stop") == window || ObjectFind("not allowed") == window){
          ObjectDelete(line_name);
          line_name = "Sell Stop";
          CreateLine(line_name, clrYellow);
        }      
      }
      //Линия меняет цвет с желтого на зелёный
      if(price > Ask){
        if(ObjectFind("Sell Stop") == window || ObjectFind("not allowed") == window){
          ObjectDelete(line_name);
          line_name = "Buy Stop";
          CreateLine(line_name, clrGreen);
        }
      }
      //Линия меняет цвет на серый если в зоне спреда
      if(price > Bid && price < Ask){
        if(ObjectFind("Buy Stop") == window || ObjectFind("Sell Stop") == window){
          ObjectDelete(line_name);
          line_name = "not allowed";
          CreateLine(line_name, clrGray);
        }
      }
      //Прикрепляем линию к курсору
      if(id == CHARTEVENT_MOUSE_MOVE)
        if(ObjectMove(0,line_name,0,0,price))
          ChartRedraw(0);
    }
  }else if(ObjectFind(line_name) == window) Print(ObjectDelete(line_name));
  //Если нажать Esc то удалится линия и кнопка отожмется
  if(id == CHARTEVENT_KEYDOWN && lparam == 27){
      button_name = "Button Stop Order";
      ms=GetMicrosecondCount();
  }
}
//----------------------------------------------------------------------
void CreateLine(const string name, const color clr)
{
  Print(ObjectCreate(0, name, OBJ_HLINE, 0,0,0));
  ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
  ObjectSetInteger(0, name, OBJPROP_STYLE, 3); 
}
 
MakarFX #:

Then do the following

if there is no object, you get "-1", i.e. true

I've tried it, I just need to change object names (invert) for the exception to work. But this way no lines are created at all.

if(ObjectFind("Sell Stop") || ObjectFind("Stop Loss")){
  ObjectDelete(line_name);
  line_name = "Sell Stop";
  CreateLine(line_name, clrYellow);
} 
 
Nerd Trader #:

I've tried it, only you have to change the name of the objects. But this doesn't create any lines at all.

It works for me and for you too, only the price is "0".
 
MakarFX #:
I have it created and so do you, only the price is "0".
Yes indeed 8 pieces, but it is not visible on the chart, the fact that the price is 0 does not matter, the line still clings to the cursor (follows it)
Reason: