Unable to place buy or sell orders

 

Hi, recently I started getting error 4756 messages saying that my buy or sell order could not be placed. Is there something wrong with my code?

   if(Sell_Condition_1 && Sell_Condition_2 && Sell_Condition_3 && Sell_Condition_4 && Sell_Condition_5 && Sell_Condition_6 && Sell_Condition_7)
        {
         // any opened Sell position?
         if(Sell_opened || Buy_opened)
           {
            Alert("We already have a Sell position!!!");
            return;    // Don't open a new Sell Position
           }
         ZeroMemory(mrequest);
         ZeroMemory(mresult);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = 0.5;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits);
         mrequest.tp = NormalizeDouble(latest_price.bid - latest_price.bid,_Digits);
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }

   if(Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_Condition_4 && Buy_Condition_5 && Buy_Condition_6 && Buy_Condition_7)
        {
         // any opened Buy position?
         if(Buy_opened || Sell_opened)
           {
            Alert("We already have a Buy Position!!!");
            return;    // Don't open a new Buy Position
           }
         ZeroMemory(mrequest);
         ZeroMemory(mresult);
         mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // currency pair
         mrequest.volume = 0.5;                                                 // 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 is it?

         mrequest.tp = NormalizeDouble (latest_price.bid - latest_price.bid, _Digits );


Maybe after all:

         mrequest.tp = NormalizeDouble (latest_price.bid - TKP*_Point, _Digits );
 
Vladimir Karputov:

What is it?


Maybe after all:

I tried changing the tp and sl and the problem remains. And it doesnt explain why my buy orders weren't getting through
 

Try this tipOrderSend

It is recommended to check the request before sending it to a trade server. To check requests, use the OrderCheck() function. It checks if there are enough funds to execute the trade operation, and returns many useful parameters in the results of trade request checking: ...

Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
[in,out]  Pointer to a structure of MqlTradeResult type describing the result of trade operation in case of a successful completion (if true is returned). parameter are filled out correctly. If there are no errors, the server accepts the order for further processing. If the order is successfully accepted by the trade server, the OrderSend...
 
Did you check that latest_price is valid?
 
lippmaje:
Did you check that latest_price is valid?
how do you check if the latest price is valid? Sorry, I'm pretty new to this whole thing
 
Vladimir Karputov:

Try this tipOrderSend

If its not too much trouble, could you show me an example of how to use and fill up the commands in OrderCheck()? I'm new to this and don't really know how to do any of these things haha
 
stephaniesee :
If its not too much trouble, could you show me an example of how to use and fill up the commands in OrderCheck()? I'm new to this and don't really know how to do any of these things haha

Did you open the help for sure?

Open and read - there are only two parameters.

OrderCheck

The OrderCheck() function checks if there are enough money to execute a required  trade operation. The check results are placed to the fields of the MqlTradeCheckResult structure.

bool  OrderCheck(
   MqlTradeRequest&       request,      // request structure
   MqlTradeCheckResult&   result        // result structure
   );

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
 
stephaniesee:
how do you check if the latest price is valid? Sorry, I'm pretty new to this whole thing

Instead of latest_price use this, temporarily, just to make sure bid and ask are valid:

MqlTick tick;
SymbolInfoTick(_Symbol,tick);
// replace latest_price.bid with tick.bid and latest_price.ask with tick.ask
         Selling:
         mrequest.price = NormalizeDouble(tick.bid,_Digits);
         mrequest.sl = NormalizeDouble(tick.bid + STP*_Point,_Digits);
         mrequest.tp = NormalizeDouble(tick.bid - TKP*_Point,_Digits);
         Buying:
         mrequest.price = NormalizeDouble(tick.ask,_Digits);
         mrequest.sl = NormalizeDouble(tick.ask - STP*_Point,_Digits);
         mrequest.tp = NormalizeDouble(tick.ask + TKP*_Point,_Digits);

And check that STP and TKP are sufficiently large, for 5 digit symbols _Point is ten times smaller than a pip.

Reason: