Pending Stop Orders

 

Hello guys,

I am working on an EA to trade horizontal channels’ break-outs. It’s not fully automated EA as I have to find a channel and then feed its high and low manually into the EA. Basically, I want it to substitute placing 2 individual manual orders, a buy stop on the top and a sell stop on the bottom, because individual platform orders cannot be OCO (to the best of my knowledge).

The difficulty that I’m having with this EA is that how to use OP_BUYSTOP & OP_SELLSTOP with these 2 conditions.

   int ticket;
   if (TradeDirection == 1)
   {
      ticket = OrderSend(Symbol(), OP_BUY, Lots, ChannelH, 3, ChannelL, 0, "Long", MagicNumber, 0, Green);
      if (ticket <= 0) Print("Error opening long order: ", GetLastError());
   }
   else if (TradeDirection == 2)
   {
      ticket = OrderSend(Symbol(), OP_SELL, Lots, ChannelL, 3, ChannelH, 0, "Short", MagicNumber, 0, Red);
      if (ticket <= 0) Print("Error opening short order: ", GetLastError());
   }

As you can see I’m using OP_BUY & OP_SELL instead.

I've done some research but could not find what I'm looking for. Would some genius help?

 
Why don't you simply test if Bid > ChannelH If it is open the buy else return and wait.
 
WHRoeder:
Why don't you simply test if Bid > ChannelH If it is open the buy else return and wait.

I did not get you. Can you explain please?
 
tapo:

I did not get you. Can you explain please?

If you are trading "manually" then it is convenient to use BUY_STOP and SELL_STOP orders. With an EA it is just sitting there waiting for the price to change. When the price reaches the desired threshold it can just do a market order. The only reason to use a BUY_STOP in an EA would be if you are trading through news events and the BUY_STOP would get in to the market faster than an EA by virtue of the connection speed to the broker.

If you still want to use a BUY_STOP entry then try it without stop loss and take profit values. Once that works, put them back in one at a time to help you see which one is causing the problem.

 
dabbler:

If you are trading "manually" then it is convenient to use BUY_STOP and SELL_STOP orders. With an EA it is just sitting there waiting for the price to change. When the price reaches the desired threshold it can just do a market order. The only reason to use a BUY_STOP in an EA would be if you are trading through news events and the BUY_STOP would get in to the market faster than an EA by virtue of the connection speed to the broker.

If you still want to use a BUY_STOP entry then try it without stop loss and take profit values. Once that works, put them back in one at a time to help you see which one is causing the problem.


I've tested it and I think I've got you. Maybe instead, I need to create an EA to delete a stop pending order when an opposite order is triggered. But let's understand this first.

void OpenPosition()
{
   int ticket;
   if (TradeDirection == 1)
   {
      ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, ChannelH, 0, OrderStopLoss(), 0, "Long", MagicNumber, 0, Green);
      if (ticket <= 0) Print("Error opening long order: ", GetLastError());
   }
   else if (TradeDirection == 2)
   {
      ticket = OrderSend(Symbol(), OP_SELLSTOP, Lots, ChannelL, 0, OrderStopLoss(), 0, "Short", MagicNumber, 0, Red);
      if (ticket <= 0) Print("Error opening short order: ", GetLastError());
   }
}

For OP_BUYSTOP, it neither worked with 0, ChannelL nor OrderStopLoss().

Whereas for OP_SELLSTOP, it did not work with 0 only, and did with ChannelH and OrderStopLoss().

The error shown is "Error opening long order: 130" which is about invalid stop orders. Why is that? It worked fine with OP_SELLSTOP.

Files:
bo_1.txt  2 kb
 
 ticket = OrderSend(Symbol(), OP_BUYSTOP, Lots, ChannelH, 0, OrderStopLoss(), 0, "Long", MagicNumber, 0, Green);
You can NOT use OrderStopLoss() until AFTER an OrderSelect(). You don't have the ticket number yet. Open the stop and THEN set TP/SL.
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){                                                     OptParameters();
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */