help with orderclose

 
extern int period=13;
extern int ma_method=MODE_SMA;
extern int applied_price=PRICE_CLOSE;
extern int shift=0;

extern double lot_size=0.1;
extern int slippage=30;
extern double SL=0;

extern double TP=1000; //take profit at 1000 pips

double DeMarkerIndex=iDeMarker(NULL,0,period,shift);

double forceIndex=iForce(NULL,0,period,ma_method,applied_price,shift);

int cnt;

int ticket;

string symb=Symbol();

int gle=0;

if (DeMarkerIndex>=0.8 && forceIndex>=300)

{

//open sell if no sell exists

bool sellExist=false;

for(cnt=OrdersTotal(); cnt>=0; cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_SELL)

sellExist=true;

}

if (!sellExist)

{

ticket=OrderSend(symb,OP_SELL,lot_size,Bid,slippage,SL,Ask-TP*Point);

if (OrderSelect(ticket, SELECT_BY_TICKET)==true)

{ Print("Open sell at price ", OrderOpenPrice()); }

else

{ Print("Open sell at price ",Bid," failed, error=",GetLastError());

return(false); }

}

//close buy

for(cnt=OrdersTotal(); cnt>=0; cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_BUY)

{

double myBid=MarketInfo(symb,MODE_BID);

OrderClose(OrderTicket(),OrderLots(),myBid,slippage,White);

gle=GetLastError();

if(gle==0)

{ Print("Close buy at price ",myBid); }

else

{ Print("Close buy at price ",myBid," failed, error=",gle);}

}

}

}

else

{

if (DeMarkerIndex<=0.3 && forceIndex<=-300)

{

//open buy if no buy exists

bool buyExist=false;

for(cnt=OrdersTotal(); cnt>=0; cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_BUY)

buyExist=true;

}

if (!buyExist)

{

ticket=OrderSend(symb,OP_BUY,lot_size,Ask,slippage,SL,Bid+TP*Point);

if(OrderSelect(ticket, SELECT_BY_TICKET)==true)

{ Print("Open buy at price ", OrderOpenPrice()); }

else

{ Print("Open buy at price ",Ask," failed, error=",GetLastError());

return(false);

}

}

//Close sell

for(cnt=OrdersTotal(); cnt>=0; cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_SELL)

{

double myAsk=MarketInfo(symb,MODE_ASK);

OrderClose(OrderTicket(),OrderLots(),myAsk,slippage,Red);

gle=GetLastError();

if(gle==0)

{ Print("Close sell at price",myAsk); }

else

{ Print("Close sell at price ",myAsk," failed, error=",gle); }

}

}

}

}

The error messages are

1. volume limit is exceeded.

2.invalid ticket for OrderClose function.

3.OrderClose error 4051.

Most parts of my codes are copied from a successfully running code, I don't know why it cannot work in my program. Thanks a lot for your help.

 

You may find the answers you need in this post. Also...

//Close sell

for(cnt=OrdersTotal(); cnt>=0; cnt--)
{
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

   if(OrderType()==OP_SELL)

{...
...shouldn't cnt=OrdersTotal()-1?
 

Alguien me puede ayudar


Trato  de cerrar todas las posiciones de un par


pero me da error 4055,  la verdad no le he llegado a ver el origen


 void Cierra()

{

  string Par=Symbol(); 

  int total=OrdersTotal();

  bool Exito;

   

  

  //for(int i=OrdersTotal()-1;i>=0;i--)

  for(int i=0; i<OrdersTotal(); i++)

  {

   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true);

   // OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);   

   // OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);

  

    {

  

       // comprobación de posición abierta 

    if  ( OrderSymbol()==Par)

    {

     

       // Si es Compra

       if (OrderType()==OP_BUY) Exito=OrderClose(OrderTicket(), OrderLots(),Bid, 3 ,clrNONE);   //    si es compra lo cierrro   

       // Si es Venta

       if (OrderType()==OP_SELL) Exito=OrderClose(OrderTicket(), OrderLots(),Ask,3,clrNONE);   //    si es venta lo cierrro   


       if(Exito == false)

         {

          Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

          Sleep(3000);

         } //Exito = false 

       if(Exito == true)

         {

          Alert("Order " , OrderTicket() , " Cerrado con Exito.", GetLastError() );

          Sleep(3000);

         } //Exito = false

         

       

       

    }  // OrderSymbol

    

    

    

    } //OrderSelect

  } //int i

  

}

//================================================================

 
  1. newlearner: extern int period=13;
    Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. double DeMarkerIndex=iDeMarker(NULL,0,period,shift);
    double forceIndex=iForce(NULL,0,period,ma_method,applied_price,shift);
    
    Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum

  3. if (DeMarkerIndex>=0.8 && forceIndex>=300){
    
    You can't just write code, it must be in one of the event functions.
              Client Terminal Events - MQL4 programs - MQL4 Reference

  4.  for(cnt=OrdersTotal(); cnt>=0; cnt--){
    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    If there are n open orders, their positions are [0 … n-1] Your first OrderSelect always fails.
  5. Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Reason: