I searched there is no built in function for this purpose. Can anyone recommend a solution. For example, a position id gotten from a deal or order that is used to generate a ticket number
- Position Identifier
- Discussion of article "DoEasy. Controls (Part 31): Scrolling the contents of the ScrollBar control"
- Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes
Perhaps this code helps you, it sets alll position by symbol:
int total = PositionsTotal(); for(int i=0; i<total; i++) { string symbol = PositionGetSymbol(i); if(PositionSelect(symbol)) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); long type = PositionGetInteger(POSITION_TYPE); if(type == POSITION_TYPE_BUY) { if(openPrice + (sl_points*_Point) * BE_RR < ask) { if(sl != openPrice) { trade.PositionModify(symbol, openPrice, tp); Print("SL movido a BE en SELL ", symbol); } } } if(type == POSITION_TYPE_SELL) { if(openPrice - (sl_points*_Point) * BE_RR > bid) { if(sl != openPrice) { trade.PositionModify(symbol, openPrice, tp); Print("SL movido a BE en SELL ", symbol); } } } } }
Also this code:
if(TimeCurrent()>TrandingEndTime) // Check if it the time is higher than the close time { for(int i=PositionsTotal()-1; i >= 0; i--) // for cicle for getting all the positions avilable { CPositionInfo pos; // Getting information each of the positions if(pos.SelectByIndex(i)) // Checking each initial position { trade.PositionClose(pos.Ticket()); // Closing each position } } }
Also this one:
for(int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket <= 0) continue; if(!PositionSelectByTicket(ticket)) continue; double currentVolume = PositionGetDouble(POSITION_VOLUME); double closeVolume = currentVolume * CierrePosicion; double Apertura = PositionGetDouble(POSITION_PRICE_OPEN); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); long type = PositionGetInteger(POSITION_TYPE); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double distanciaSL = MathAbs(Apertura - sl); if(type == POSITION_TYPE_BUY) { trade.PositionClosePartial(ticket, closeVolume); } else if(type == POSITION_TYPE_SELL) { trade.PositionClosePartial(ticket, closeVolume); } }
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