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
I still do not understand what exactly you want.
In principle, it doesn’t matter: you opened three positions or you opened one hundred positions. If they need to be modified - move stop loss, then the universal function is used.
In general, explain (if you cannot explain in words - draw pictures) - what you want to do.
For example, say I open 3 positions using 3 PositionOpen() instructions. I will number them position #1, #2, and #3. At a later point in time, once a condition is met, I want to modify the SL for #1 by 100pts, for #2 by 150pts, and for #3 by 300pts. Then, once some other condition is met, I want to close #1, but leave #2 and #3 open. Later still, I want to change the TP for #3, but leave it unchanged for #2.
All I want is to be able to refer to these positions by a number, by some identifier, so I can manipulate them independently in unique ways in my program using functions such as PositionModify() or PositionClose(). So I can track exactly what happens to each position and be sure that I'm making the right changes to the right one.
The CTrade modification functions take the position ticket as an argument, so, once I have opened a position, I need to somehow get the position ticket and store it for later use. I believe I have figured this out by using the order ticket via ResultOrder(), which appears to be the same as the position ticket.
I can confirm that this does work. After placing the trade using a CTrade object, use ResultOrder() to get the order ticket, which appears to always be equal to the resulting position ticket. Store this ticket in a ulong variable, and then use it in PositionModify() or PositionClose() to manipulate.
Thanks to everyone for their help.
Good if it works for you. But I want to add a warning : this is not a generic approach and depending of the exact trading environment this can fail for two reasons :
Good if it works for you. But I want to add a warning : this is not a generic approach and depending of the exact trading environment this can fail for two reasons :
Hi Alain, thanks for joining the discussion.
1. If it does not return the ticket immediately, it must still return the ticket at some time if the order is successfully placed. Would looping to wait for the ticket be a possibility? Could a ticket simply never be returned on a successful order?
2. This is a point of enormous confusion for me. In the MQL5 documentation, specifically here, it states this:
position
Ticket of a position. Should be filled in when a position is modified or closed to identify the position. As a rule it is equal to the ticket of the order, based on which the position was opened.
It says, "as a rule", so presumably it is equal by the program logic. Under what conditions would the ticket of a position not be equal to the ticket of an order? I really don't understand how tickets are assigned and why it would assign to a position a different ticket to that of the order that opened it.
Finally, what would your recommendation be to do the job that I'm trying to do? If order ticket != position ticket, is there a function that allows me to get the position ticket right after a position has been opened? There is ResultOrder() and ResultDeal(), but no "ResultPosition()". Presumably, since functions such as PositionClose() take the position ticket as an argument, there must be a way to retrieve an individual position ticket?
Once I have opened multiple positions, I don't want to have to loop through them to make modifications, because that doesn't allow me to specifically identify and manipulate. I just want to get a position ticket immediately after a position has been opened and store it in a variable for later use.
Hi Alain, thanks for joining the discussion.
1. If it does not return the ticket immediately, it must still return the ticket at some time if the order is successfully placed. Would looping to wait for the ticket be a possibility? Could a ticket simply never be returned on a successful order?
2. This is a point of enormous confusion for me. In the MQL5 documentation, specifically here, it states this:
position
Ticket of a position. Should be filled in when a position is modified or closed to identify the position. As a rule it is equal to the ticket of the order, based on which the position was opened.
It says, "as a rule", so presumably it is equal by the program logic. Under what conditions would the ticket of a position not be equal to the ticket of an order? I really don't understand how tickets are assigned and why it would assign to a position a different ticket to that of the order that opened it.
Finally, what would your recommendation be to do the job that I'm trying to do? If order ticket != position ticket, is there a function that allows me to get the position ticket right after a position has been opened? There is ResultOrder() and ResultDeal(), but no "ResultPosition()". Presumably, since functions such as PositionClose() take the position ticket as an argument, there must be a way to retrieve an individual position ticket?
Once I have opened multiple positions, I don't want to have to loop through them to make modifications, because that doesn't allow me to specifically identify and manipulate. I just want to get a position ticket immediately after a position has been opened and store it in a variable for later use.
If the simple approach you was using is working for you, then just keep it IF you know exactly when it can fail. If your EA never run in such situation, then all is fine.Otherwise use the already mentioned event handlers.
You can loop if you want but it's not a good approach. The good one is to use OnTrade() or OnTradeTransaction() to catch the price once the order is executed. I didn't say it will be different, but that it's possible to be different in some cases. For example partial closing, or adding to a position (netting account), maybe some others I didn't think about right now. If we talk about opening order only then all should be ok identifying a position ticket and the order ticket. (sorry if I confused you I had not read all the topic).
If the simple approach you was using is working for you, then just keep it IF you know exactly when it can fail. If your EA never run in such situation, then all is fine.Otherwise use the already mentioned event handlers.
I found more information here.
POSITION_TICKET
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the POSITION_IDENTIFIER property.
So basically, a position ticket may differ from the order ticket if swaps are charged or the server refreshes, or something similar to this. I would not feel confident in my EA if it isn't prepared to deal with this eventuality. There is another way to identify a position, a POSITION_IDENTIFIER, which never changes:
POSITION_IDENTIFIER
Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.
Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.
When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.
However, the only way to use this identifier seems to be with HistorySelectByPosition, which I can't see the use of. If I have the POSITION_IDENTIFIER, is there some function I can use it in to retrieve the most recent position ticket assigned to that identifier? There is PositionSelectByTicket, I need something like, "PositionSelectByIdentifier".
Thank you for the tip on using OnTrade(). I will do more research into that.
I wrote this function to make sure I am always using the position ticket that matches the ticket of the order that created it:
It seems to work. What do you think? Documentation states that the POSITION_IDENTIFIER does not change throughout a position's life cycle and corresponds to the ticket of the order used to open the position. This function passes the stored ticket of the order and compares it to the POSITION_IDENTIFIER of each position. It should always return the corresponding position ticket even if that has changed. So this function can be used to link order tickets to position tickets.
Hope this helps someone.
For example, say I open 3 positions using 3 PositionOpen() instructions. I will number them position #1, #2, and #3. At a later point in time, once a condition is met, I want to modify the SL for #1 by 100pts, for #2 by 150pts, and for #3 by 300pts. Then, once some other condition is met, I want to close #1, but leave #2 and #3 open. Later still, I want to change the TP for #3, but leave it unchanged for #2.
All I want is to be able to refer to these positions by a number, by some identifier, so I can manipulate them independently in unique ways in my program using functions such as PositionModify() or PositionClose(). So I can track exactly what happens to each position and be sure that I'm making the right changes to the right one.
The CTrade modification functions take the position ticket as an argument, so, once I have opened a position, I need to somehow get the position ticket and store it for later use. I believe I have figured this out by using the order ticket via ResultOrder(), which appears to be the same as the position ticket.
Listen carefully to what Alain Verleyen tells you and do not be confused to remember the ticket! This is dangerous!
I offer a very simple and elegant way: you have opened three positions, which means they will have different opening times. You just have to go around the entire list of positions and sort them by age: the oldest Stop loss is 100, the younger - 150 and the youngest 300 points.
Listen carefully to what Alain Verleyen tells you and do not be confused to remember the ticket! This is dangerous!
I offer a very simple and elegant way: you have opened three positions, which means they will have different opening times. You just have to go around the entire list of positions and sort them by age: the oldest Stop loss is 100, the younger - 150 and the youngest 300 points.
Position Crawl Cycle