expert advisor - miscellaneous questions - page 30

 
honest_knave:

A cursory examination suggests it should work. But you could improve the efficiency.

Think about how many different objects you have drawn on your chart. If you click *any* of those objects, you are going to loop through every single order and try to find a match. Many objects probably have nothing to do with orders. This is inefficient.

Which is why I suggested checking the first character of sparam for "#". This automatically identifies the object as being related to an order.

Furthermore, there is no need to loop through the orders. The object name and/or the object text already contains the ticket number. So extract the ticket number from there and close the order

Example: 

You open an order. It is given ticket number 12345

Create an OBJ_BUTTON with name "#12345

The code in OnChartEvent() would be something like this (uncompiled, untested, late at night...):

if(id==CHARTEVENT_OBJECT_CLICK && StringSubstr(sparam,0,1)=="#") // this is an order button
  {
   int ticket=(int)StringSubstr(sparam,1); // extract the ticket number
   if(OrderSelect(ticket,SELECT_BY_TICKET)) // select the order
     {
      if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE)) // close the order
        {
         Print("# Error ",GetLastError());
        }
     }
  }
(you'll probably want to handle the OrderSelect too, in case of failure)

Wow! I did not know about that.

So, you mentioned twice, and I exactly did not figure out about that.
Now I will test it for myself and bring it to my main EA file.

Thanks so much more.

 

I use OrderTicket() for other objects, now I see when I click that objects - order close.
I liked you method, but looks like it's influence other objects.

Other objects name like this here is one of them.

"#"+IntegerToString(OrderTicket())+" -"+"Object HLine"

Thanks in advance.

 
Max Enrik:

I use OrderTicket() for other objects, now I see when I click that objects - order close.
I liked you method, but looks like it's influence other objects.

Other objects name like this here is one of them.

"#"+IntegerToString(OrderTicket())+" -"+"Object HLine"

Thanks in advance.

The more elegant solution is to change your naming system 

Nevertheless, you can still strip out the order number. It just depends on the exact naming format.

But before all that, what is the OBJPROP_TEXT of the button? Do any other objects have the same OBJPROP_TEXT?

 
honest_knave:

The more elegant solution is to change your naming system
Nevertheless, you can still strip out the order number. It just depends on the exact naming format.
But before all that, what is the OBJPROP_TEXT of the button? Do any other objects have the same OBJPROP_TEXT?

Yeah! I know I can change names of Button object names ( also I use Prefix ) if I change button names then I could change a lot of things, e.g delete system and other things which is all of them linked each others.

Below are Button and HLine names.

"#"+IntegerToString(OrderTicket())+" -"+"Object HLine"
"#"+IntegerToString(OrderTicket())+" -"+"Object Button"

( of course, object names a little a bit more different, but same method )

Thanks in advance. 

 

 Market Closed 

Maybe I found, but I am not sure.
If this is not good, please let me know.

if(id==CHARTEVENT_OBJECT_CLICK && StringFind(sparam,"close order button",0)>=0)

Thanks in advance. 

//--- 2nd  times edited

Hey! Man!

Thanks a lot. So far no problems. Correctly works!

 

#Crossover Objects - Open

I several times faced this issue, sometimes I passed sometimes I gave up, now I would not like to give up once again.
I have 2 objects " HLine and Button " they crossovers, I mean when I click one of them, both selected this is not good thing for me. ( BUTTON Width = 20, HLINE Width = as you know )

Q:  How can I do when I click BUTTON object HLINE object won't selected while they crossovers, please?
     ( I can easily select HLINE where they not crossover )

Thanks in advance.

( I am working on it, any good comment would be better, please. )

 

#Profit in Pips - Open | #Profit in Pips - Closed for now - I solved it

Mr. William - I use your " Pip, Point ... " calculation codes.
Now I try to get Profit in pips value with below codes. I have 2 orders, one of them shows correctly ( OP_BUY ), 2nd of them not shows correctly - 399 - actual 400 ( OP_SELL ).

Q:  Am I doing wrong for profit in pips calculation, please?

change_to_pips( OrderTakeProfit() - OrderOpenPrice() )

Thanks in advance.

( last day I checked out forums in different websites - that was not beneficially for me )

 

Forum on trading, automated trading systems and testing trading strategies

MetaTrader 4 Build 574 with Updated MQL4 Language and Market of Applications Released

whroeder1, 2014.01.27 14:11

  1. Don't write unread­able, in­compre­hens­ible code like that.
    if (MyOrdersTotal() == 0 && (Hour()​>=​StartHour ​&& Hour()​<​EndHour && StartHour​<​EndHour) || (Hour()​>=​StartHour || Hour()​<​EndHour && StartHour​>​EndHour))
    Make readable and simplify and always paren­thes­ize when mixing ands/ors.
    bool    isHourOKnorm = StartHour < EndHour
                         && StartHour <= Hour() && Hour() < EndHour,
            isHourOKwrap = StartHour > EndHour
                         && (StartHour <= Hour() || Hour() < EndHour),
         isHourOK       = isHourOKnorm || isHourOKwrap;
    if(MyOrdersTotal() == 0 && isHourOK)
  2. What about the case where StartHour == EndHour (i.e. all 24?) See my code and avoid 24 hour wrap.

One of amazing " if " operator example of a complex condition from Mr. William.
Just thanks!


I struggled latest 2 days to fix my " if " operator complex condition, but I can't solve my issue until I found above great example.

 

Please someone help me, I really struggle to solve this issue.

#296

Thanks in advance. 

 

Below code sometimes works perfect, sometimes not. I can't solve this issue. When I use single order, it works perfectly, but when I open multiple orders, this function does not work correctly.
Please help me, and a little a bit more explains ( that what is wrong at the below code ) that would be better for me.

( Now, I am working on it. )

if(id==CHARTEVENT_OBJECT_CLICK && (StringFind(sparam,Button_1,0)>=0 || StringFind(sparam,Button_2,0)>=0))
  {
   ticketnumber=(int) StringSubstr(sparam,1);

   if(OrderSelect(ticketnumber,SELECT_BY_TICKET))
     {
      if(StringFind(sparam,Button_1,0)>=0)
        {
         // ...
        }

      if(StringFind(sparam,Button_2,0)>=0)
        {
         //...
        }

      for(i=OrdersTotal()-1; i>=0; i--)
        {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
         if(Symbol()!=OrderSymbol()) continue;

         update_1();
         update_2();
         update_3();
        }
     }
  }

Thanks in advance.

Reason: