Error #3 with OP_SELLSTOP / OP_BUYLIMIT

 

This is my first EA which I'm trying to backtest, and I run into weird problem.

if ( 
          (Close[2] < Open[2]) &&     
          (Close[1] > Open[1]) &&     
          (Close[1] > Open[2])        

        ) 
          {
           TakeProfit = ( High[1] - Low[1] ) * 1 ;
           StopLoss = ( High[1] - Low[1] ) * 1 ;
         
           ticket = OrderSend(Symbol(),               // symbol for trading
                  OP_BUYLIMIT,                 // operation type
                  NormalizeDouble((AccountEquity() * Lots) / (StopLoss * 100000), 2),        // number of lots
                  Ask,                    // preferred price of the trade 
                  1,                      // max. slippage
                  High[1] - StopLoss,         // stoploss 
                  High[1] + TakeProfit,       // takeprofi
                  Order_Comment,          // comment
                  MagicNumber,            // magic number
                  14400,            // order expiration
                  Blue);                  // arrow color
        if(ticket<0) {
         Print("OrderSend failed with error #",GetLastError());
         return(0);
       } 

Code above gives Error 3. What's strange, when I change OP_BUYLIMIT to OP_BUY it works!

Anyone could help me on this?

 

Err # 3 Invalid trade parameters, read them in include under stderror.mqh

Of course it is an error, you set pending order price at ask or within freeze level and/or stop level :(, and that order expiration ?, try to replace that with zero.

 
I don't understand, what's causing problem here exactly? expiration is 4hours, is it wrong?
 
reuptake:
I don't understand, what's causing problem here exactly? expiration is 4hours, is it wrong?

Exactly ?

ticket = OrderSend(Symbol(),                         // symbol for trading
                  OP_BUYLIMIT,                       // operation type
                  NormalizeDouble((AccountEquity() * Lots) / (StopLoss * 100000), 2),        // number of lots
                  Ask,                               // preferred price of the trade 
                  1,                                 // max. slippage
                  High[1] - StopLoss,                // stoploss 
                  High[1] + TakeProfit,              // takeprofi
                  Order_Comment,                     // comment
                  MagicNumber,                       // magic number
                  14400,                             // order expiration
                  Blue);

For OP_BUYLIMIT the opening price should below Bid price, expiration date may be disable in some trade server and you get error Trade expiration denied, before sending any order check the size of lot size, is the lot size comply with

MarketInfo (Symbol(), MODE_MINLOT); 
MarketInfo (Symbol(), MODE_LOTSTEP);
MarketInfo (Symbol(), MODE_MAXLOT);

Open MetaEditor, Navigator window (Ctrl + D), dictionary tab, select MQL4 Reference > Trading functions > OrderSend and read all description of it.

Have fun :)

 
Expiration is a future date, your 14400 corresponds to Thu, 01 Jan 1970 04:00:00 GMT
 
These gurus are correct

I would like to expand on this if you wonder why the changing it from OP_BUYLIMIT to OP_BUY works ?
It is because of exactly what the other guru's indicated about the (14400)

OP_BUY is to buy right now at market price,
OP_BUYLIMIT is buy at future price at future time

See here:
https://docs.mql4.com/trading/OrderSend
Noticed on this page (expiration) next to that says (pending orders only)

This means that the (14400) will be disregarded for OP_BUY market orders, and will place the order
And will NOT work for OP_BUYLIMIT because (14400) it's referring to a time that has already passed as the guru's have indicated
So the main thing to fix is to use and accurate time which will be in the future of the current market time in order for OP_BUYLIMIT to work

I hope this expanded viewpoint helps
Thanks
 
OP_BUY is to buy right now at market price,
OP_BUYLIMIT is buy at a LOWER future price at future time
OP_BUYSTOP is buy at a HIGHER future price at future time
Reason: