Simple code needed for EA (modify two open orders) - page 4

 

This from my reply a couple of posts ago . . . it is the simplest way . . .

"I set two pending orders at a specific time, specifically 23:00 GMT+2 I think. One of the pending orders as a sell stop and the other is a buy stop, both the orders are an equal distance away from the open from the 23:00 candle, in this case 14 pips. Both have a TakeProfit of 28 pips (in this case) and a StopLoss of 55 pips."

You can calculate where the original TP was by reference to the 23:00 candle, check the order and see if it still set to the original TP, if it is then it can be modified . . . if not then it has already been modified so don't modify it again.

 

Try this . . . . it compiles but I haven't tried it in the Strategy Tester yet . . .

Files:
madmax.mq4  10 kb
 
RaptorUK:

Try this . . . . it compiles but I haven't tried it in the Strategy Tester yet . . .

I'm getting 'OrderSend error 130' for some reason even though the SL and TP seem to be correct and the 'OrderSend's are laid out in the same way as the previous code somewhat. Why is this happening?

I took this part of the code

   int CountOpenOrders = 0;
   double Candle23Open; 
   int Spread = MarketInfo(Symbol(), MODE_SPREAD);
   
   Candle23Open = Open[ iBarShift(NULL, 0, StrToTime("23:00") ) ]; // price value of the Open of the 23:00 hour candle
   
   for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--)
      {
      if(OrderSelect(iPos,SELECT_BY_POS) && 
         OrderMagicNumber() == MagicNumber &&
         OrderSymbol() == Symbol() &&
         OrderType() < 2               //  less than 2 is either OP_BUY (0) or OP_SELL (1) 
         )
      CountOpenOrders++;
      } // end of for(int iPos  

   if(CountOpenOrders == 2)  //  we have 2 open orders,  should we modify or have the already been modified ?
      {
      for(iPos = OrdersTotal()-1; iPos >= 0 ; iPos--)
         {
         if(OrderSelect(iPos,SELECT_BY_POS) && 
            OrderMagicNumber() == MagicNumber &&
            OrderSymbol() == Symbol() 
            )
            {
            if(OrderType() == 1)               //  OP_SELL (1)  
               {
               if(OrderTakeProfit() > Candle23Open - ((TakeProfitModified - 1) * Point) + Spread )      //  check if TakeProfit has already been modified
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Candle23Open - (TakeProfitModified * Point) + Spread, 0, Blue))
                     {
                     Print("Error modifying order : ",GetLastError());
                     return(0);
                     }
               }
            
            if(OrderType() == 0)               //  OP_BUY (0)
               {
               if(OrderTakeProfit() < Candle23Open + ((TakeProfitModified - 1) * Point) )      //  check if TakeProfit has already been modified
                  if(!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Candle23Open + (TakeProfitModified * Point), 0, Blue))
                     {
                     Print("Error modifying order : ",GetLastError());
                     return(0);
                     }
                  
               }
                  
            }         
         }
      }
   
        
   } 

and placed it in the previous EA (the original one) and it opens the orders but does not modify them. Although I think if the OrderSend error were to be fixed in the EA you have provided it might work.

Oh and in this part,

if(!OrderModify

What is the ! for?

Thanks very much,

madmax3

 
madmax3:


Oh and in this part,

What is the ! for?

Thanks very much,

madmax3

Did you check the documentation for OrderModify() ? what does it return ?
 
madmax3:

I'm getting 'OrderSend error 130' for some reason even though the SL and TP seem to be correct and the 'OrderSend's are laid out in the same way as the previous code somewhat. Why is this happening?

Did you adjust your TP & SL to account for a 5 digit Broker ? is your Broker a 5 digit Broker ?
 

I got it to work, just missed out filling in a variable (distance from ask and bid). The OrderModify documentation doesn't explain the ! I believe.
The code seem to work well, one issue though is that it's only modifying buy trades when two trades are open and not both the buy and sell trades, I'm not sure why it's doing this. Any ideas why?

Thanks,

madmax3

 
madmax3:

I got it to work, just missed out filling in a variable (distance from ask and bid). The OrderModify documentation doesn't explain the ! I believe.
The code seem to work well, one issue though is that it's only modifying buy trades when two trades are open and not both the buy and sell trades, I'm not sure why it's doing this.

Thanks,

madmax3

OrderModify returns a bool . . . so ! in relation to a bool is NOT . . . so !OrderModify() means . . . if OrderModify returns false, i.e. fails

I'll take another look at your code . . . or my mod to it.

 
madmax3:

I got it to work, just missed out filling in a variable (distance from ask and bid). The OrderModify documentation doesn't explain the ! I believe.
The code seem to work well, one issue though is that it's only modifying buy trades when two trades are open and not both the buy and sell trades, I'm not sure why it's doing this. Any ideas why?

Thanks,

madmax3


Do you wanna have it modifying StopLoss for both Buy and Sell trades with same pip ??

Think again if that is what you want and you can figure out why it is not moddifying that way....

Also don't modify trades with every new point in right direction choose a trailingstep to allow modifying (xx Points can choose 10 or more to avoid a lot of tradecontext too busy errors)

"!" is something like not in coding

 
madmax3:

The code seem to work well, one issue though is that it's only modifying buy trades when two trades are open and not both the buy and sell trades, I'm not sure why it's doing this. Any ideas why?

Check the logs for errors . . . if running in the Strategy Tester check the journal tab, if on Live or Demo check the experts tab. Let me know what you find . . .
 
RaptorUK:
Check the logs for errors . . . if running in the Strategy Tester check the journal tab, if on Live or Demo check the experts tab. Let me know what you find . . .
2012.04.09 21:21:27 2012.01.17 00:59 Tester: order #1, sell 0.01 EURUSD is opened at 1.26498
2012.04.09 21:21:27 2012.01.17 02:21 Tester: order #2, buy 0.01 EURUSD is opened at 1.26795

2012.04.09 21:21:27 2012.01.17 02:21 MadMax EURUSD,H1: modify #2 buy 0.01 EURUSD at 1.26795 sl: 1.26218 tp: 1.27352 ok
2012.04.09 21:21:27 2012.01.17 03:00 MadMax EURUSD,H1: modify #2 buy 0.01 EURUSD at 1.26795 sl: 1.26218 tp: 1.27480 ok

For some reason it's modifying the buy trade twice (the second modification goes to the TP required, however on some trades it appears to keep on modifying at certain whole times like 9:00 or 10:00), there don't seem to be any actual errors as such though.


Reason: