Can't send a sell order. 4756 10004

 
Hello everyone. My EA successfully sends buy orders but for some reason cannot send sell orders. When I try to send a sell order OrderSend function fails with 4756 error code and MqlTradeResult retcode is 10004. The following  is a part of my code that sends orders. 
void openPosition(string buyOrSell, double givenStopLoss, double givenTakeProfit, double givenLots, MqlTick &givenTick)
{
   MqlTradeRequest myRequest= {};
   MqlTradeResult myResult = {};
   ZeroMemory(myRequest);
   ZeroMemory(myResult);

   myRequest.action = TRADE_ACTION_DEAL;
   myRequest.magic = myMagicNo;
   myRequest.symbol = Symbol();
   myRequest.volume = givenLots;
   myRequest.type_filling = ORDER_FILLING_IOC;
   myRequest.comment = "no comment";
   myRequest.price = NormalizeDouble(givenTick.ask,Digits());
   myRequest.deviation = 50;//I made this number out of my ass(The maximal price deviation, specified in points)

   if(buyOrSell == "buy")   //buy
      {
         //Print("Buy!");
         myRequest.sl = 0.0;//NormalizeDouble(givenTick.ask - (givenStopLoss * _Point), Digits());
         myRequest.tp = 0.0;//NormalizeDouble(givenTick.ask + (givenTakeProfit * _Point), Digits());
         //Print("GivenTick.ask: ",givenTick.ask, " StopLoss:", myRequest.sl, " TakeProfit:", myRequest.tp);
         myRequest.type = ORDER_TYPE_BUY;
      }
   else if(buyOrSell == "sell")     //sell
      {
         //Print("Sell!");
         myRequest.sl = 0.0;//NormalizeDouble(givenTick.bid + (givenStopLoss * _Point), Digits());
         myRequest.tp = 0.0;//NormalizeDouble(givenTick.bid - (givenTakeProfit * _Point), Digits());
         //Print("GivenTick.bid: ",givenTick.bid, " StopLoss:", myRequest.sl, " TakeProfit:", myRequest.tp);
         myRequest.type = ORDER_TYPE_SELL;
      }
   else
      {
         Print("Wrong buyOrSell value given to openPosition function!");
      }

//send the order and check the return value
   if(!OrderSend(myRequest, myResult))   //if OrderSend returns false(iow if the OrderSend has failed)
      {
         Print("ERROR! openPosition.OrderSend has failed with error code:", GetLastError());
         Print("result retcode:", myResult.retcode);
         ResetLastError();
         if(buyOrSell == "buy")
            {
               Print("GivenTick.ask: ",givenTick.ask, " StopLoss:", myRequest.sl, " TakeProfit:", myRequest.tp);
            }
         else if(buyOrSell == "sell")
            {
               Print("GivenTick.bid: ",givenTick.bid, " StopLoss:", myRequest.sl, " TakeProfit:", myRequest.tp);
            }
         return;
      }
   else
      {
         if(buyOrSell == "buy")
            buyOpen = true;
         else if(buyOrSell == "sell")
            sellOpen = true;
         //Print("An order is given!");
      }

}
 
mirac baver ozturk:
Hello everyone. My EA successfully sends buy orders but for some reason cannot send sell orders. When I try to send a sell order OrderSend function fails with 4756 error code and MqlTradeResult retcode is 10004. The following  is a part of my code that sends orders. 

Why not use the CTrade class?


#include <Trade\Trade.mqh>

CTrade trade;

void OnTick(){

//Your logic

trade.Buy() //pass parameters in braces
trade.Sell() // pass parameters in braces


}
 
mirac baver ozturk:
myRequest.price = NormalizeDouble(givenTick.ask,Digits());

You are using:

myRequest.price = NormalizeDouble(givenTick.ask,Digits());

for both sending order Buy and also sending order Sell?

 

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

 
Nino Guevara Ruwano #:

You are using:

for both sending order Buy and also sending order Sell?

this solved my problem. thank you.

 
William Roeder #:

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

Thank you so much William, I recently started with an EA as well, and doing a course related I was adviced about this but I can not understand this really well. I think I am biased for the brokers as I do not have to worry about that, just putting my order above/bellow price when I want to go short or long but I get really confused with bid and ask price
Reason: