
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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().
{
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.
{
// when click this button - do something...
}
Thanks in advance.
its not inside of the orderloop so it would not know the OrderTicket().
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.
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.
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 "#"
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.
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
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.
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?
{
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...):
{
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());
}
}
}