Buy order not placing

 
 bool Buy_Condition_1 = (maVal[0]>maVal[1]) && (maVal[1]>maVal[2]); // MA-8 Increasing upwards
 
   bool Buy_Condition_3 = (adxVal[0]>Adx_Min);          // Current ADX value greater than minimum value (22)
   bool Buy_Condition_4 = (plsDI[0]>minDI[0]);          // +DI greater than -DI

//--- Putting all together   
  if(Buy_Condition_1)
     {
      if(Buy_Condition_3 && Buy_Condition_4)
        {
         // any opened Buy position?
         if (Buy_opened) 
         {
            Alert("We already have a Buy Position!!!"); 
            return;    // Don't open a new Buy Position
         }
         mrequest.action = TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = latest_price.ask;
         mrequest.sl =  (latest_price.ask-STP);
         mrequest.tp =  (latest_price.ask + TKP);
         mrequest.symbol = _Symbol;                                         // currency pair
         mrequest.volume = Lot;                                            // number of lots to trade
         mrequest.magic = EA_Magic;                                        // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                     // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                            // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         }
         }
         
         
   // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }
  
  /*

What's the reason the highlighted alert keeps showing? Even though all buy conditions are true.  Is there a way to debug this to see the code run automatically (without breakpoints) but not instantly? Thanks

 

What does GetLastError() print in those error cases?


Also: my suggestion is to place ResetLastError() before you call OrderSend(). In that case you will be sure that the error message in GetLastError() is the result of OrderSend().

 
         OrderSend(mrequest,mresult);
                    ⋘⋘⋘⋘⋘⋘⋘⋘⋘⋘ OrderSend was called here
         }          ⋘⋘⋘⋘⋘⋘⋘⋘⋘⋘ Condition 3 & 4
         }          ⋘⋘⋘⋘⋘⋘⋘⋘⋘⋘ Condition 1
                    ⋘⋘⋘⋘⋘⋘⋘⋘⋘⋘ OrderSend may not have been called here
   // get the result code ⋘⋘⋘⋘⋘⋘⋘ What result code?
Test your result codes after the call to OrderSend. Currently, you are testing them whether you call it or not.
 
WindmillMQL:

What does GetLastError() print in those error cases?


Also: my suggestion is to place ResetLastError() before you call OrderSend(). In that case you will be sure that the error message in GetLastError() is the result of OrderSend().

Get Last Error prints 

4756

Trade request sending failed

On the journal tab in MT5 (after a few more tries) sometimes prints:

  failed exchange buy 0.1 EURUSD at 1.24172 sl: -298.75828 tp: 1001.24172 [Invalid stops]

After moving ResetLastError() before OrderSend() , GetLastError() prints:

ERR_TRADE_POSITION_NOT_FOUND

4753

Position not found

and again sometimes prints the invalid stops error 

Not sure if it's something strange with my sl and tp numbers. I have it set at sl=30 and tp=100, multiplied by ten if it's a three/five digit quote. 

 
William Roeder:
Test your result codes after the call to OrderSend. Currently, you are testing them whether you call it or not.

Hi,

            ⋘⋘⋘⋘⋘⋘⋘⋘⋘⋘ OrderSend may not have been called here

I'm not quite sure what you mean. ^^^

Get result code comment is referring to the lines below it.

Codes checked: 

10008

TRADE_RETCODE_PLACED

Order placed

10009

TRADE_RETCODE_DONE

Request completed

 
m00nlyte: I'm not quite sure what you mean. ^^^

This is your code if Condition1 is false

  if(Buy_Condition_1)
     {
      ⋮ ⋘⋘⋘⋘ Not executed because of a false condition.
     }
   // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Buy order request could not be completed -error:",GetLastError());
            ResetLastError();           
            return;
           }

What are you testing?

 
William Roeder:

This is your code if Condition1 is false

What are you testing?

Yes that is correct. It is just that when all conditions are true, the code executes all the way to OrderSend() , but prints out the highlighted message, meaning the order did not place. It is also printing another message, invalid stops, see my response to  WindmillMQL. 

 
m00nlyte:
Get Last Error prints 

4756

Trade request sending failed

On the journal tab in MT5 (after a few more tries) sometimes prints:

  failed exchange buy 0.1 EURUSD at 1.24172 sl: -298.75828 tp: 1001.24172 [Invalid stops]

After moving ResetLastError() before OrderSend() , GetLastError() prints:

ERR_TRADE_POSITION_NOT_FOUND

4753

Position not found

and again sometimes prints the invalid stops error 

Not sure if it's something strange with my sl and tp numbers. I have it set at sl=30 and tp=100, multiplied by ten if it's a three/five digit quote. 

You have tried to place an order with a negative stoploss price. Such an order will not be accepted, so that's why you get an error message  "trade request sending failed".

To be honest: both the stoploss and profit taker prices seem strange to me. The EURUSD is usually about 1.1 or so. Why do you use a negative value for sl and a value of about one thousand as pt? That doesn't make sense to me.

Reason: