This is the first:
Ticket position is:
(ulong)PositionGetInteger(POSITION_TICKET)
but not:
PositionGetInteger(POSITION_IDENTIFIER);
Second: the position must be bypassed TO ZERO (sample code from all my advisers)
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions if(m_position.SelectByIndex(i)) if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
This is the first:
Ticket position is:
but not:
Second: the position must be bypassed TO ZERO (sample code from all my advisers)
Thank you for your response.
I tried
(ulong)PositionGetInteger(POSITION_TICKET)
as well, but I got the same as with:
PositionGetInteger(POSITION_IDENTIFIER);
(I'll change it back :-) )
My OnTick() Functuion starts with:
void OnTick() { MqlTradeRequest request; MqlTradeResult result; MqlDateTime dt; ZeroMemory(request);I use 'ZeroMemory', is that what you mean?
Hi,
I'm placing BUY_STOP and SELL_STOP orders.
E.g. SELL_STOP oder gets Position/Ticket # 11
When SELL_STOP order triggers, the Open Position has also the Position/Ticket # 11.
Interrelation of orders, deals and positions
The platform allows you to easily track how a position was opened or how a deal was performed. Each trading operation has its unique ID called a "ticket". Each order and deal receive a ticket relating to their relevant position. Each deal receives a ticket of an order, by which it was concluded.
If a position was affected by multiple deals, for example in the case of a partial closing or increasing volumes, each of the deals feature the position's ticket. This makes it easy to track the entire history of the position as a whole.
If trading operations are sent to an exchange or a liquidity provider, they additionally feature an ID from an external system. This allows additional tracking of the interrelation of operations away from the platform.
- www.metatrader5.com
Add:
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.
POSITION_TICKET value corresponds to MqlTradeRequest::position.
long
- www.mql5.com
And now attention to what your algorithm should look like:
* First: you need to use trading classes - they are lighter and require fewer lines
#include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> //--- CPositionInfo m_position; // object of CPositionInfo class CTrade m_trade; // object of CTrade class CSymbolInfo m_symbol; // object of CSymbolInfo class
* You place a pending order (DO NOT REMEMBER A TICKET)
* In the adviser, simply write the correct trailing procedure (AGAIN: THERE IS NO SENSE TO USE A SAVED TICKET OF A PENDING ORDER)
//+------------------------------------------------------------------+ //| Trailing | //| InpTrailingStop: min distance from price to Stop Loss | //+------------------------------------------------------------------+ void Trailing(const double stop_level) { /* Buying is done at the Ask price | Selling is done at the Bid price ------------------------------------------------|---------------------------------- TakeProfit >= Bid | TakeProfit <= Ask StopLoss <= Bid | StopLoss >= Ask TakeProfit - Bid >= SYMBOL_TRADE_STOPS_LEVEL | Ask - TakeProfit >= SYMBOL_TRADE_STOPS_LEVEL Bid - StopLoss >= SYMBOL_TRADE_STOPS_LEVEL | StopLoss - Ask >= SYMBOL_TRADE_STOPS_LEVEL */ if(InpTrailingStop==0) return; for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions if(m_position.SelectByIndex(i)) if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic) { double price_current = m_position.PriceCurrent(); double price_open = m_position.PriceOpen(); double stop_loss = m_position.StopLoss(); double take_profit = m_position.TakeProfit(); double ask = m_symbol.Ask(); double bid = m_symbol.Bid(); //--- if(m_position.PositionType()==POSITION_TYPE_BUY) { if(price_current-price_open>m_trailing_stop+m_trailing_step) if(stop_loss<price_current-(m_trailing_stop+m_trailing_step)) if(m_trailing_stop>=stop_level && (take_profit-bid>=stop_level || take_profit==0.0)) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(price_current-m_trailing_stop), take_profit)) if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify BUY ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); if(InpPrintLog) { RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); } continue; } } else { if(price_open-price_current>m_trailing_stop+m_trailing_step) if((stop_loss>(price_current+(m_trailing_stop+m_trailing_step))) || (stop_loss==0)) if(m_trailing_stop>=stop_level && ask-take_profit>=stop_level) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(price_current+m_trailing_stop), take_profit)) if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","Modify SELL ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); if(InpPrintLog) { RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); } } } } }
Later I will give the full advisor code ...
And now attention to what your algorithm should look like:
* First: you need to use trading classes - they are lighter and require fewer lines
* You place a pending order (DO NOT REMEMBER A TICKET)
* In the adviser, simply write the correct trailing procedure (AGAIN: THERE IS NO SENSE TO USE A SAVED TICKET OF A PENDING ORDER)
Later I will give the full advisor code ...
Thank you very much for your detailed answer. It gave me a clearer understanding of differences between Orders, Positions, Tickets and deals.
I also found the core issue of my problem. It is in the first code snippet:
I tried do select the trading position by assigning the ticket number to the 'order' instead of the 'position'.
Using
'request.order' is correct for changing parametes of a pending order. But as soon the pending order has triggered 'request.position' has
to be used.
// in this loop we're checking all opened positions for(i=0; i<PositionsTotal(); i++) { // processing orders with "our" symbols only if((Symbol()==PositionGetSymbol(i)) && (PositionGetInteger(POSITION_MAGIC) == MA_MAGIC)) { ticket = PositionGetInteger(POSITION_IDENTIFIER); request.order= ticket; //Wrong, because the pending order has already triggert and we are now hav to handl 'Positions' request.position= ticket; //correct
Hey,
sorry, one question again about your Code, @Vladimir Kaputov:
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of open positions if(m_position.SelectByIndex(i)) if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
The last line:
Isn't it possible that with the same Magic-No. and in the syme symbol there are two positions running (hedging mode)?
So if i want to modify just on i should have previously saved the ticket id, right?
The Code above would just find the first of the both, but which one it is, wouldn't be clear. Am i understanding it right?
Thx & Regards!
Hey,
sorry, one question again about your Code, @Vladimir Kaputov:
The last line:
Isn't it possible that with the same Magic-No. and in the syme symbol there are two positions running (hedging mode)?
So if i want to modify just on i should have previously saved the ticket id, right?
The Code above would just find the first of the both, but which one it is, wouldn't be clear. Am i understanding it right?
Thx & Regards!
Let me explain: it makes no difference at all - netting or hedging, these are three universal lines. They work ONLY for the current symbol and for a SPECIFIC Magic. The ultimate goal is to CHOOSE A POSITION. You do not need to save any tickets.
In general, I advise you to first think about what exactly you need to get, and then apply the code.
Thank you very much for your detailed answer. It gave me a clearer understanding of differences between Orders, Positions, Tickets and deals.
I also found the core issue of my problem. It is in the first code snippet:
I tried do select the trading position by assigning the ticket number to the 'order' instead of the 'position'.
Using 'request.order' is correct for changing parametes of a pending order. But as soon the pending order has triggered 'request.position' has to be used.
i still doesn't understand the differences between Orders, Positions, Tickets and Deals...:(
I will try to give a world example to make it a little easier!
Suppose you go to a workshop and request them to make you 5 custom pens, so you submit them an "Order".
The workshop owner then goes to his suppliers and orders the parts, such as the metal for the casing, and ink for the cartridge and wood for the box, etc. These are the "Deals".
And finally, he is only able to make 4 pens instead of the 5 because of lack of materials and charges you even a little more due to wood becoming more expensive (slippage). So, your final "Position" is only 4 pens, instead of the 5 you ordered, and is more expensive than original estimates.
Obviously this is very simplified, but it is just to make it a little easier for you to understand.
In trading, the "Order" is what you request; the "Deals" are the individual dealings that take place between different sources in order to fulfill your request, and the "Position" is the final outcome of all those dealings, which may or may not fulfill your original request, both in price and in volume.
I hope that helps. I suggest you read the following article on the subject: Orders, Positions and Deals in MetaTrader 5
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm placing BUY_STOP and SELL_STOP orders.
E.g. SELL_STOP oder gets Position/Ticket # 11
When SELL_STOP order triggers, the Open Position has also the Position/Ticket # 11.
Now I want to change the the TAKE PROFIT value of this Position. To select the order to be changed, I use this command, which gives me the Position # 11 for the ticket:
But I always get the errror:From OrderCheck i get the following Errors (see Code below):
In the Trading Tab the open Position shows the open position with ticket # 11.
I try to change the TP with this code:
In the Journal I can find following information:
Do I have to use the 'deal number' instead (and how to get them)?
Could you plese help me, how to change TP in an open position triggered by a Buy-Stop order?