Code Help - Order close and modify
Sorry. I've continued to test this and found some additional information. It appears that the code will work, when I have only one order. That is, only a single order in my account. Any other orders seem to get ignored, even if it's a different currency and the expert is attached to different charts.
So, here's the scenario, just to be clear. At 10 pips, I want to close 1 lot out of a 5 lot order. The new profit target will get incremented by 10 pips, so, the next profit target is 20 pips. The function for this works. At 15 pips, I want my stop loss moved to breakeven. This fails.
Can someone help me how to get multiple order operations within a single expert working?
Thanks.
void ManageTrade() { // Create another set of variables used in the code. // These variables provide an abbreviated way of calling variables. // Mostly, they are more easily written in the code than using the user // friendly external variables. int ti=Target_Increment; //This variable will not change. double cl=Close_Lots; // This variable will not change. // Additional variables used in the code. int trange = 0; // Use a range versus a specific pip amount as prices may get jumped. int totalorders = OrdersTotal(); // Calls a function to get all the open orders. // Starts initial "for" loop. This loop will go through all the open orders. // If a target is reached, the script will close a portion of the trade. for(int j=0; j<totalorders;j++) { Comment(ft); OrderSelect(j, SELECT_BY_POS, MODE_TRADES); if(ft == 0) ft = First_Target; if(OrderType()==OP_BUY) { // Get the current pip amount on a buy order. curPipValue = (Bid - OrderOpenPrice())/Point; if(ft == 0) ft = First_Target; trange=ft+5; // Check if the current pip amount is within the appropriate range if(curPipValue >= ft-1 && curPipValue <= trange) { // First, if target is reached, then take profit. if(OrderClose(OrderTicket(), cl, Bid, 3, Red)) { // Increment First_Target ft += ti; //Comment(ft); return(0); } } } else if(OrderType()==OP_SELL) { // Get the current pip amount on a sell order. curPipValue = (OrderOpenPrice()-Ask)/Point; if(ft == 0) ft = First_Target; trange=ft+5; // Check if the current pip amount is within the appropriate range if(curPipValue >= ft-1 && curPipValue <= trange) { if(OrderClose(OrderTicket(), cl, Ask, 3, Red)) { // Increment First_Target ft +=ti; //Comment(ft); return(0); } } } } } void MoveStops() { // Starts initial "for" loop. This loop will go through all the open orders. // If a target is reached, the script will move the stop loss. int sd=Stop_Differential; // This variable will not change. // Additional variables used in the code. int trange = 0; // Use a range versus a specific pip amount as prices may get jumped. int totalorders = OrdersTotal(); // Calls a function to get all the open orders. for(int j=0; j<totalorders;j++) { OrderSelect(j, SELECT_BY_POS, MODE_TRADES); if(fs == 0) fs = First_Stop; trange=fs+5; if(OrderType()==OP_BUY) { // Get the current pip amount on a buy order. curPipValue = (Bid - OrderOpenPrice())/Point; // Check if the current pip amount is within the appropriate range if(Move_Stops==true) { if(curPipValue >= First_Stop && curPipValue <= trange) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Stop_Differential*Point, OrderTakeProfit(),0,Blue); } return(0); } } else if(OrderType()==OP_SELL) { // Get the current pip amount on a buy order. curPipValue = (OrderOpenPrice()-Ask)/Point; // Check if the current pip amount is within the appropriate range if(Move_Stops==true) { if(curPipValue >= First_Stop && curPipValue <= trange) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Stop_Differential*Point, OrderTakeProfit(),0,Blue); } return(0); } } } }