expert advisor - miscellaneous questions - page 29

 
honest_knave:
Post up your code where you get the OrderTicket to make the name.

( When someone needs some code - I delays to post it quickly - because I use OBJECT_**** functions, it is little different from object documentations )

Below code is in OnTick().

for(i=OrdersTotal()-1;i>=0;i--)
  {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if(Symbol()!=OrderSymbol()) continue;
   ObjectCreate(0,"#"+IntegerToString(OrderTicket())+" -"+"Object Button",OBJ_BUTTON,0,0,0);
  }

Now, I try to call that object by the OnChartEvent() like below.

if(id==CHARTEVENT_OBJECT_CLICK && sparam=="#"+IntegerToString(OrderTicket())+" -"+"Object Button")
  {
   // when click this button - do something...
  }

Thanks in advance.

 
its not inside of the orderloop so it would not know the OrderTicket().
 
Marco vd Heijden:
its not inside of the orderloop so it would not know the OrderTicket().
So, is there no chance?
 
Max Enrik:
So, is there no chance?

Most things are possible. But I am unclear on what you are trying to achieve. What is this button? What do you want to do when it is clicked?

You can probably get your desired result, but your approach might need changing.

 
honest_knave:

Most things are possible. But I am unclear on what you are trying to achieve. What is this button? What do you want to do when it is clicked?

You can probably get your desired result, but your approach might need changing.


My purpose is this button object could shows tickets nubers and their profit when I click them, that order cloud close.

I wrote this comment from mobile device. So, I will start to research about this issue after 10 hours later.  Thanks for your comments.
 
Max Enrik:

My purpose is this button object could shows tickets nubers and their profit when I click them, that order cloud close.

I wrote this comment from mobile device. So, I will start to research about this issue after 10 hours later.  Thanks for your comments.

Have a unique tag in the object names that identifies these buttons. You seem to be using "#"

if(id==CHARTEVENT_OBJECT_CLICK && StringSubstr(sparam,0,1)=="#") // this is one of your order buttons being clicked

Then extract the order number from either the rest of the object name, or the button text.

No need to use OrderTicket() at this stage.
 

 

His objects are identified by the orderticket.

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

So that every object has the ticket number in it's string name to be able to find which order the object belongs to.

This means that every object will hold "#" in it's string name.

So essentially on a button click he first has to extract the order ticket number from the object string name, and then run the orderselectloop, and when the selected orderticket matches the orderticket number extracted from the button string name, he will have found the order.

Or he can just simply

SELECT_BY_TICKET

To select the order inside of the chart event.

Now it would be a lot easier if he just give the object the ticket number/name and nothing else.

ObjectCreate(0,"IntegerToString(OrderTicket()),OBJ_BUTTON,0,0,0);

Because then he can just directly select the ticket or compare the numbers.

 

I was last day tried OrderSelect() Loop. Now first I will try it once again soon.
Then I will research deeply your latest comments.

Thanks for your valuable times. 

 

I try below code in my test.mq4 file so below code is working perfectly, and so far I do not faced any problems, but I am still testing it.

Q:  Can you improve below code if it is bad, please?

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

      if(sparam=="#"+IntegerToString(OrderTicket())+" -"+"Object Button")
        {
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE))
           {
            Print("# Error ",GetLastError());
           }
        }
     }
  }

Thanks in advance.

( I am working on it ) 

 

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)
Reason: