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
}
//================================================================
- 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 -
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++.- They are initialized once on program load.
- They don't update unless you assign to them.
- 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:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
- Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - Inflation - MQL4 programming forum
-
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 -
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. - 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

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.