How can I code this?

 

I want the EA to place two opposite pending orders in order to what I put below but it makes just the one(first) pending order in both YELLOW or WHITE case. For example in the YELLOW case the EA would place just SELLSTOP(1.2). Please help if know my mistake.

        if (type == OP_BUY && level < MaxCycleLevels) 
        {
          SetOrder(OP_SELLSTOP, 1.2, sl, 0, sl- 0.0005, Time[i]);
          SetOrder(OP_BUYSTOP, 0.7, sl, sl-0.0005, 0, Time[i]);
        }
        if (type == OP_SELL && level < MaxCycleLevels)
        {
          SetOrder(OP_BUYSTOP, 1.2, sl, 0, sl+ 0.0005, Time[i]);
          SetOrder(OP_SELLSTOP, 0.7, sl, sl+ 0.0005, 0, Time[i]);
        }

the SetOrder() function is:

int SetOrder(int type, double lot, double price, double sl, double tp, int magic)
{
  int ticket = -1;
  ticket = OrderSend(Symbol(), type, lot, price, 0, sl, tp, DoubleToStr(magic, 0), magic);
  int err = GetLastError();
  if (ticket == -1 && err > 0)
    Print("err=", err, ", type=", type, ", price=", price, ", lot=", lot, ", sl=", sl, ", tp=", tp, ErrorDescription(err));
  return (ticket);
}
and Error 130 is reported.
 

It appears that you are using the 'sl' variable to set the price level of the stop orders.

You are setting both the sellstop and buystop at the same price.

If the intended price for the buy order is below the current Ask price, then you need to use a BUYLIMIT order.

If you want to set a BUY order at a future price, you use a STOP order. And this will be triggered when the price rises to meet it.

If you want to set a BUY order at a previous price, you use a LIMIT order. And this will be triggered with the price falls to meet it.

 
farrokhfa: For example in the YELLOW case the EA would place just SELLSTOP(1.2). Please help if know my mistake.
          SetOrder(OP_SELLSTOP, 1.2, sl, 0, sl- 0.0005, Time[i]);
          SetOrder(OP_BUYSTOP, 0.7, sl, sl-0.0005, 0, Time[i]);
If sl is below market then the buyStop is an immediate open so it fails. If sl is above the market the sellStop fails. So either Yellow or White case only one pending order gets set.
 
CodeMonkey:

It appears that you are using the 'sl' variable to set the price level of the stop orders.

You are setting both the sellstop and buystop at the same price.

If the intended price for the buy order is below the current Ask price, then you need to use a BUYLIMIT order.

If you want to set a BUY order at a future price, you use a STOP order. And this will be triggered when the price rises to meet it.

If you want to set a BUY order at a previous price, you use a LIMIT order. And this will be triggered with the price falls to meet it.




It works. Thank you very much...
Reason: