Questions from Beginners MQL5 MT5 MetaTrader 5 - page 727

 
Vitaly Muzichenko:

Can you tell me how to select an object when it is created, so that it doesn't lose its selection and doesn't select itself when the user deselects it. It was easy in the fourth, but it doesn't work in the fifth.

It used to be, and it worked just fine:

void SetHLine(...,double p,bool sel) {
  if(ObjectFind(chart_ID,nm)!=0) {
    ObjectCreate(chart_ID,nm,OBJ_HLINE,0,0,0);
    ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);
    ...
  }
   ObjectSetDouble(chart_ID,nm,OBJPROP_PRICE,p);
}

As you can see, when creating, we can select or not select the object being created at once and it will not change anymore and there will be no reference to it, but it doesn't work in the fifth one, it creates it without selection.

How to make mql5 so that the object can be selected at creation and then managed manually when needed - to select/not to select

OBJPROP_SELECTABLE
 

Well, it is, but the icon doesn't stand out

void SetArrow(string nm,string tooltxt="",datetime t=0,double p=0,color col=clrRed,long type=0) {
if(ObjectFind(0,nm)!=0) {
    ObjectCreate(0,nm,type==0?OBJ_ARROW_BUY:OBJ_ARROW_SELL,0,0,0);
    ObjectSetInteger(0,nm,OBJPROP_COLOR,col);
    ObjectSetInteger(0,nm,OBJPROP_SELECTED,true);
    ObjectSetInteger(0,nm,OBJPROP_SELECTABLE,true);
    ObjectSetInteger(0,nm,OBJPROP_HIDDEN,false);
    ObjectSetString(0,nm,OBJPROP_TOOLTIP,tooltxt);
   }
    ObjectSetDouble(0,nm,OBJPROP_PRICE,p);
    ObjectSetInteger(0,nm,OBJPROP_TIME,t);
}
 
Vitaly Muzichenko:

Well it is, but the icon is not highlighted

void SetArrow(string nm,string tooltxt="",datetime t=0,double p=0,color col=clrRed,long type=0) {
if(ObjectFind(0,nm)!=0) {
    ObjectCreate(0,nm,type==0?OBJ_ARROW_BUY:OBJ_ARROW_SELL,0,0,0);
    ObjectSetInteger(0,nm,OBJPROP_COLOR,col);
    ObjectSetInteger(0,nm,OBJPROP_SELECTED,true);
    ObjectSetInteger(0,nm,OBJPROP_SELECTABLE,true);
    ObjectSetInteger(0,nm,OBJPROP_HIDDEN,false);
    ObjectSetString(0,nm,OBJPROP_TOOLTIP,tooltxt);
   }
    ObjectSetDouble(0,nm,OBJPROP_PRICE,p);
    ObjectSetInteger(0,nm,OBJPROP_TIME,t);
}

Everything is highlighted:

Highlighting

Files:
OBJ_ARROW.mq5  22 kb
 
Vladimir Karputov:

Everything stands out:

Shit, I ruled one function and checked another, everything works. Thank you! I'm going to bed)
 

I've noticed a strange feature, if you set it this way, the lines are not highlighted when you create

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);

But if you do the opposite, then there is no problem with selection:

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);

For some reason, replacing summands changes the sum?

 
Vitaly Muzichenko:

I've noticed a strange feature, if you set it this way, the lines are not highlighted when you create

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);

But if it's the other way round, there's no problem with selection:

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);

For some reason, replacing summands changes the sum?

Apparently, when

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);

last command, the chart is updated

 
Vitaly Muzichenko:

I've noticed a strange feature, if you set it this way, it doesn't select lines when you create

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);

But if it's the other way round, it doesn't have any problems:

ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);
ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);

For some reason, replacing summands changes the sum?

Initially, when you create an object, you probably set its default property as non-selectable.

Then you select it (but the object is not selectable), consequently nothing is selected

Then you set it as selectable. But it's "too late to drink the borjomi" ...
 

So here's the full code:

void SetVLine(long chart_ID=0,string nm="nm",datetime tm=0,int wd=1,int st=0,color cl=clrRed,
                                             string tooltxt="",bool sel=false,bool back=false) {
if(ObjectFind(chart_ID,nm)<0) {
    ObjectCreate(chart_ID,nm,OBJ_VLINE,0,tm,0);
    ObjectSetInteger(chart_ID,nm,OBJPROP_TIME,tm);
    ObjectSetInteger(chart_ID,nm,OBJPROP_BACK,back);
    ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);
    ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTED,sel);
  // ObjectSetInteger(chart_ID,nm,OBJPROP_SELECTABLE,true);
    ObjectSetString(chart_ID,nm,OBJPROP_TOOLTIP,tooltxt);
    ObjectSetInteger(chart_ID,nm,OBJPROP_COLOR,cl);
    ObjectSetInteger(chart_ID,nm,OBJPROP_WIDTH,wd);
    ObjectSetInteger(chart_ID,nm,OBJPROP_STYLE,st);
   }
}

Yes, double-checked on all object types, and you have to put"SELECTABLE" first and then"SELECTED"

Thanks for your participation and help in solving it!

 
In MetaTrader5 it is possible to generate an EA in 3 blocks. 1 block gives signals from an indicator or from several indicators. 2 block trailing bars. 3 block risks.

Can I suggest 1. Can I add custom indicators in 1 block? If yes, how should I do it?

2. is it possible to correct the code so that trades are opened in both directions (now, for example, if a buy trade is opened, the sell trade is not opened until the buy trade is closed)
 
Comments not related to this topic have been moved to "Questions from MQL4 MT4 MetaTrader 4 beginners".
Reason: