please help me how can i select position, and modify its TP and keep its SL as it was without modification
it was easy on mql4 using ordermodify(orderticket(), orderopenprice(), orderstoploss(), OrderOpenprice()+25*pips)
thanks in advance
Maybe u can try this. Mine is as below, if the price move 100 pips further and it will modify the SL.
But need to include (#include <Trade/Trade.mqh>) at the beginning.
for (int i = PositionsTotal()-1 ; i >= 0 ; i--){
ulong posTicket = PositionGetTicket(i);
if(PositionSelectByTicket(posTicket)){
if(PositionGetString(POSITION_SYMBOL) == _Symbol){
double posSl = PositionGetDouble(POSITION_SL);
double posTp = PositionGetDouble(POSITION_TP);
double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY){
if( iclose1 > ( posOpenPrice + 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT))){
double sl = posOpenPrice;
if(sl > posSl){
trade.PositionModify(posTicket,sl,posTp);
Print("Over 100 pips, Breakeven!");
}
}
}else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL){
if( iclose1 < ( posOpenPrice - 1000 * SymbolInfoDouble(_Symbol,SYMBOL_POINT))){
double sl = posOpenPrice;
if(sl < posSl){
trade.PositionModify(posTicket,sl,posTp);
Print("Over 100 pips, Breakeven!");
}
}
}
}
}
}
void Singgle_TP() //================ { double NewTPB=0, NewTPS=0, NewSLB=0, NewSLS=0; //---------------------------------------- for(int i=PositionsTotal()-1; i>=0; i--) { if(m_position.SelectByIndex(i)) { if(New_TP != 0.0) { NewTPB = NormalizeDouble(m_position.PriceOpen() + New_TP *PairPip, Spipsfactor) ; NewTPS = NormalizeDouble(m_position.PriceOpen() - New_TP *PairPip, Spipsfactor ) ; }else{ NewTPB = m_position.TakeProfit(); NewTPS = m_position.TakeProfit(); } if(New_SL !=0.0 ) { NewSLB = NormalizeDouble(m_position.PriceOpen() - (New_SL*PairPip), Spipsfactor) ; NewSLS = NormalizeDouble(m_position.PriceOpen() + (New_SL*PairPip), Spipsfactor ) ; }else { NewSLB = m_position.StopLoss(); NewSLS = m_position.StopLoss(); } string order_commB = " [Pair : "+ _Symbol+ " ]--[ Ask: "+ DoubleToString(m_symbol.Ask(),Spipsfactor)+ "]--[ Open Order: "+ DoubleToString(m_position.PriceOpen(),Spipsfactor) + "]--[ SL: "+ DoubleToString(m_position.StopLoss(),Spipsfactor)+" => "+ DoubleToString(NewSLB,Spipsfactor) + "]--[ TP: "+ DoubleToString(m_position.TakeProfit(),Spipsfactor)+" => "+ DoubleToString(NewTPB,Spipsfactor)+" ]"; string order_commS = " [Pair : "+ _Symbol+ " ]--[ Bid: "+ DoubleToString(m_symbol.Bid(),Spipsfactor)+ "]--[ Open Order: "+ DoubleToString(m_position.PriceOpen(),Spipsfactor) + "]--[ SL: "+ DoubleToString(m_position.StopLoss(),Spipsfactor)+" => "+ DoubleToString(NewSLS,Spipsfactor) + "]--[ TP: "+ DoubleToString(m_position.TakeProfit(),Spipsfactor)+" => "+ DoubleToString(NewTPS,Spipsfactor)+" ]"; if(m_position.Symbol() == _Symbol && m_position.Magic() == Magic_Ea && m_position.PositionType() == POSITION_TYPE_BUY ) { if( ( NewTPB != 0.0 && NewTPB != m_position.TakeProfit() ) || ( NewSLB != 0.0 && NewSLB != m_position.StopLoss() ) ) if(m_trade.PositionModify(m_position.Ticket(), NewSLB, NewTPB)) Print("Modify TP/SL BUY : "+order_commB); else Print("Modify TP/SL BUY FAILED : "+order_commB + get_error_message(GetLastError())); Sleep(100); } if(m_position.Symbol() == _Symbol && m_position.Magic() == Magic_Ea && m_position.PositionType() == POSITION_TYPE_SELL ) { if( ( NewTPS != 0.0 && NewTPS != m_position.TakeProfit() ) || ( NewSLS != 0.0 && NewSLS != m_position.StopLoss() ) ) if(m_trade.PositionModify(m_position.Ticket(), NewSLB, NewTPB)) Print("Modify TP/SL SELL :"+order_commS); else Print("Modify TP/SL SELL FAILED:"+ order_commS+ get_error_message(GetLastError())); Sleep(100); } //=================== } } //------------------------------------------------------------------------------------------------------------- }try this, friend..

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, my code is:
how please modify tp of position and keep sl as it was