function to return orderType

 

I have a button when clicked will create an immediate or pending buy order.

I have a function that determines whether the order should be a buy if the current price is within +- 5 pips of the entering price that is inputed by the user. If the entry price is greater than +- 5 pips of the current price then it is a pending order.

The problem is the logic is not sending an immediate buy order when the current price +- 5pips. When the price is greater than +- 5pips the pending orders seem ok. No errors.

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      int whichOrderType;
      int ticket;
      
      double entryprice=StrToDouble(ObjectGetString(0,"EditEntry",OBJPROP_TEXT));
      double sl=StrToDouble(ObjectGetString(0,"EditSL",OBJPROP_TEXT));
      double tp=StrToDouble(ObjectGetString(0,"EditTP2",OBJPROP_TEXT));
      string comment=ObjectGetString(0,"Comment",OBJPROP_TEXT);

      string name="Buy";
      if(ObjectGetInteger(0,name,OBJPROP_STATE)==true)
        {
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
         whichOrderType = DecideOrderType(name, entryprice); //not complete
         if(whichOrderType == OP_BUY)entryprice = Ask;
         ticket=OrderSend(Symbol(),whichOrderType,LotSize,entryprice,50,sl,tp,comment,MagicNumber,0,clrNONE);
         if(ticket<1)
            {
             Print("Order send error BUY order, error code: "+whichOrderType, GetLastError());
             return;  
            }
         else
              Print("Order BUY sent successfuly");
        }
      name="Sell";
      if(ObjectGetInteger(0,name,OBJPROP_STATE)==true)
        {
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
         whichOrderType = DecideOrderType(name,entryprice); // not complete
         if(whichOrderType == OP_BUY)entryprice = Bid;     
         ticket=OrderSend(Symbol(),whichOrderType,LotSize,entryprice,50,sl,tp,NULL,MagicNumber,0,clrNONE);
                  if(ticket<1)
            {
             Print("Order send error SELL order, error code: ", GetLastError());
             return;  
            }
         else
              Print("Order SELL sent successfuly");
        }
     }
   
  }

 int DecideOrderType (string type, double entryprice)
  {

   double myAsk, myBid;

   if (type == "Buy")
   {
      myAsk = MarketInfo(Symbol(),MODE_ASK);

      //if entryprice is within 5 pips of current price then PendingOrderType = OP_BUY
      //If entryprice is below < current price then PendingOrderType = OP_BUYLIMIT
      if(myAsk + 5*myPoint < entryprice)
      {
         return(OP_BUYSTOP);
      }
      if(myAsk - 5*myPoint > entryprice)
      {
         return(OP_BUYLIMIT);
      }
      else
      {
         return(OP_BUY);
      }
   }
   if (type == "Sell")
   {
      myBid = MarketInfo(Symbol(),MODE_BID);

      //if entryprice is within 5 pips of current price then PendingOrderType = OP_SELL
      //If entryprice is below > current price then PendingOrderType = OP_SELLLIMIT
      //if entryprice is above > current price then PendingOrderType = OP_SELLSTOP
      if(myBid + 5*myPoint < entryprice)
      {
         return(OP_SELLLIMIT);
      }
      if(myBid - 5*myPoint > entryprice)
      {
         return(OP_SELLSTOP);
      }
      else
      {
         return(OP_SELL);
      }
   }

  }
double SetPoint()
{
   double mPoint;

   if (Digits < 4)
      mPoint = 0.01;
   else
      mPoint = 0.0001;

   return(mPoint);
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The...
 
You should sell at the Bid, not the Ask, for market orders. For limit and stop orders you certainly don't want them to execute at Bid/Ask, rather at entryPrice.
 
lippmaje:
You should sell at the Bid, not the Ask, for market orders. For limit and stop orders you certainly don't want them to execute at Bid/Ask, rather at entryPrice.
Yes sorry thanks, I copied that from a previous version of the code where I didnt have OnChartEvent completed. I have updated the code now. Any advice welcome on how to return OP_SELL or OP_BUY if the entry price is within 5pips above or below the current market price.