Working OrderClose in demo and real accounts

 

Hello

These codes consist of two keys, the first key closes half of the first open trade and the second key closes completely.

The keys work in the demo account but do not work in the real account.

Please help me, What's problem?!

void OnTick()
  {
   ButtonCreate(0,"closeHalf",0,10,350,85,25,CORNER_LEFT_UPPER,"closeHalf","Arial",10,clrBlack,clrDeepSkyBlue,clrNONE,false,false,false,true,0);
   ButtonCreate(0,"Close",0,10,380,85,25,CORNER_LEFT_UPPER,"Close","Arial",10,clrBlack,clrHotPink,clrNONE,false,false,false,true,0);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(sparam== "closeHalf")
     {
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
        {

         int closeHalfOrder=OrderClose(OrderTicket(),OrderLots()/2,OrderOpenPrice(),3,0);
        }
     }

   if(sparam== "Close")
     {
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
        {
         int close=OrderClose(OrderTicket(),OrderLots(),OrderOpenPrice(),3,0);
        }
     }
  }
//+------------------------------------------------------------------+
bool ButtonCreate(const long              chart_ID=0,               // chart's ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,                 // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',  // background color
                  const color             border_clr=clrNONE,       // border color
                  const bool              state=false,              // pressed/released
                  const bool              back=false,               // in the background
                  const bool              selection=false,          // highlight to move
                  const bool              hidden=true,              // hidden in the object list
                  const long              z_order=0)                // priority for mouse click
  {
//--- reset the error value
   ResetLastError();
//--- create the button
   if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0))
     {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
     }
//--- set button coordinates
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- set button size
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set text color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set background color
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//--- set border color
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- set button state
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
//--- enable (true) or disable (false) the mode of moving the button by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
 

Incorporate this function in your code and call it with the close position . 

Im assuming you are in absolute control of the trades that occur in the account , or you are certain your users will be in (referring to selecting by position 0 always)

Here is the close function : 

//assuming you are in control of the positions and what is traded in your ea :
bool CloseOrder(int position,
                int attempts,
                uint timeout,
                int slippage,
                bool refresh_needed)
{
bool result=false;
int attempted=0;
double cp=0;
color clr=clrBlue;
bool findit=OrderSelect(position,SELECT_BY_POS,MODE_TRADES);
//if position selected
if(findit)
  {
  int    ticket=OrderTicket();
  int    type=OrderType();
  double lots=OrderLots();
  //attempts loop
  while(!result&&attempted<=attempts)
  {
  attempted++;
  if(refresh_needed){RefreshRates();}
  if(type==OP_BUY){cp=Bid;}
  if(type==OP_SELL){cp=Ask;clr=clrRed;}
  result=OrderClose(ticket,lots,cp,slippage,clr);
  //if succesful or not and more attempts delay 
  if(result||(!result&&attempted<attempts)){Sleep(timeout);}
  }
  //attempts loop ends here 
  }
//if position selected ends here
return(result);
}
 
Lorentzos Roussos:

Incorporate this function in your code and call it with the close position . 

Im assuming you are in absolute control of the trades that occur in the account , or you are certain your users will be in (referring to selecting by position 0 always)

Here is the close function : 

Thank you so much sir.