modify stop loss not working

 

Hello, 


Im trying to modify long positions on mt4 and im using a for loop to change the stop loss to move it up as the profit values are quite high and if it doesnt reach that value it sometimes ends in loss when it couldve still be very profitable, I dont know what im doing wrong and ive been stuck on it for a week. Can someone help!

ontick start-

assigned variables and stuff

//

//

for(x=0; x<=OrdersTotal(); x++)  {

if (OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true)

if(Ask>OrderOpenPrice()+0.5*MathMax(profit,profit2))

res = OrderModify(x,Ask,Ask,MathMax(profit,profit2)+0.0004,NULL,Blue);

else break;

}



if(MathMin(open1,close1)>halfway1)

order= OrderSend(NULL,NULL,0.01,open,2,bot-0.0002,MathMax(profit,profit2)+0.0004,NULL,NULL,NULL,Green);

x++;
 }

Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
micobez:

Hello, 

Im trying to modify long positions on mt4 and im using a for loop to change the stop loss to move it up as the profit values are quite high and if it doesnt reach that value it sometimes ends in loss when it couldve still be very profitable, I dont know what im doing wrong and ive been stuck on it for a week. Can someone help!

if(OrderClosePrice()>OrderOpenPrice()+0.5*MathMax(profit,profit2))

double newSL=???????

res = OrderModify(OrderTicket(),OrderOpenPrice(),newSL,OrderTakeProfit(),0,Blue);

what will be the new SL?

 

Hello! 


THank you for getting back to me so quickly,


the new SL will be 0.5*profit value 

 
micobez:

Hello! 


THank you for getting back to me so quickly,


the new SL will be 0.5*profit value 

well you will need to calculate it and replace the ????

 
And don't forget that you must always check that the SL has not already been modified.
 
for(x=0; x<=OrdersTotal(); x++)  {
if (OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true)
if(OrderClosePrice()>OrderOpenPrice()+0.6*MathMax(profit,profit2))
double nSL=OrderOpenPrice()+0.5*MathMax(profit,profit2);
res = OrderModify(OrderTicket(),OrderOpenPrice(),nSL,OrderTakeProfit(),NULL,Blue);
 
break;
}



if(MathMin(open1,close1)>halfway1)
order= OrderSend(NULL,NULL,0.01,open,2,bot-0.0002,MathMax(profit,profit2)+0.0004,NULL,NULL,NULL,Green);
x++;



It still isnt working :( 

granted it modifies orders but the sl doesnt change only the tp

 
micobez:
for(x=0; x<=OrdersTotal(); x++)  {
if (OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true)
if(OrderClosePrice()>OrderOpenPrice()+0.6*MathMax(profit,profit2))
double nSL=OrderOpenPrice()+0.5*MathMax(profit,profit2);
res = OrderModify(OrderTicket(),OrderOpenPrice(),nSL,OrderTakeProfit(),NULL,Blue);
 
break;
}



if(MathMin(open1,close1)>halfway1)
order= OrderSend(NULL,NULL,0.01,open,2,bot-0.0002,MathMax(profit,profit2)+0.0004,NULL,NULL,NULL,Green);
x++;


It still isnt working :( 

granted it modifies orders but the sl doesnt change only the tp

use brackets correctly {....}
your second if needs its own block

 

Please use the insert code button or Alt +S when posting code. I have edited it for you this time.

   for(x=0; x<=OrdersTotal()-1; x++)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderClosePrice()>OrderOpenPrice()+0.6*MathMax(profit,profit2))
            double nSL=OrderOpenPrice()+0.5*MathMax(profit,profit2);
         if(nSL-OrderStopLoss()>=_Point)
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),nSL,OrderTakeProfit(),0,Blue))
               Print("Modifying SL on Ticket #",OrderTicket()," failed. Error Code ",GetLastError());
        }

      //break;
     }

There is no need for the break.

It is not possible that the TP will be modified with that code as you use OrderTakeProfit()

 
  for(x=0; x<=OrdersTotal()-1; x++)
     {
      if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderClosePrice()>OrderOpenPrice()+0.6*MathMax(profit,profit2))
            double nSL=OrderOpenPrice()+0.5*MathMax(profit,profit2);
         if(nSL-OrderStopLoss()>=_Point)
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),nSL,OrderTakeProfit(),0,Blue))
               Print("Modifying SL on Ticket #",OrderTicket()," failed. Error Code ",GetLastError());
        }

      //break;
     }

Hello! 


Thank you so much for all the help so far, Im using the code prescribed above however it does not modify any orders still :( 
I really dont know what to do haha

 
micobez:

Thank you so much for all the help so far, Im using the code prescribed above however it does not modify any orders still :( 
I really dont know what to do haha

Obviously the code will only work for buy orders as you said in your first post.

Are the conditions met for modifying the stop loss?

If not, try different conditions to test it.

Is it trying to modify and failing? If so, you should get the error report in the Experts log (Journal im Strategy Tester).

You should also Normalize any calculated SL or TP.

 

yea its only for buy orders,

And the issue it has is it modifies the order immediately without meeting conditions and when it modifies it only removes the stop loss for some reason