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
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 ????
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
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
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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!