Hi guys,
I am learning to code MQL4 , what function can i use to close multiple positions at once. If there is EA for that, i will appreciate if someone can post it.
Here is the cript I wrote, the problem is it does not work if there are more than 2 positions, it returns error #129
========================================================
extern double R.O.I=3; //Return On Investment in %
extern double L.O.I=-3; //Loss On Investment in %
int start()
{
// Variables, in which tickets of orders of each type will be
// memorized
int BuyStopOrder = 0, SellStopOrder = 0, BuyOrder = 0,
SellOrder = 0;
int _GetLastError = 0, _OrdersTotal = OrdersTotal();
// search in all open positions and memorize, positions of which
// type have already been opened:
for ( int z = _OrdersTotal - 1; z >= 0; z -- )
{
// if an error occurs at searching for a position, go
// to the next one
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print("OrderSelect(", z, ", SELECT_BY_POS) - Error #",
_GetLastError );
continue;
}
// if the position was opened not for the current symbol, skip it
if ( OrderSymbol() != Symbol() ) continue;
// depending on the position type, change value of the
// variable:
switch ( OrderType() )
{
case OP_BUY: BuyOrder = OrderTicket(); break;
case OP_SELL: SellOrder = OrderTicket(); break;
case OP_BUYSTOP: BuyStopOrder = OrderTicket(); break;
case OP_SELLSTOP: SellStopOrder = OrderTicket(); break;
}
}
if ( (AccountEquity()/AccountBalance()-1)*100>=R.O.I || (AccountEquity()/AccountBalance()-1)*100<=L.O.I)
{
if ( SellStopOrder > 0 )
{
if ( !OrderDelete( SellStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyStopOrder > 0 )
{
if ( !OrderDelete( BuyStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue) )
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
if ( SellOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
}
}
========================================================
Thanks in advance
Happy trading
I am learning to code MQL4 , what function can i use to close multiple positions at once. If there is EA for that, i will appreciate if someone can post it.
Here is the cript I wrote, the problem is it does not work if there are more than 2 positions, it returns error #129
========================================================
extern double R.O.I=3; //Return On Investment in %
extern double L.O.I=-3; //Loss On Investment in %
int start()
{
// Variables, in which tickets of orders of each type will be
// memorized
int BuyStopOrder = 0, SellStopOrder = 0, BuyOrder = 0,
SellOrder = 0;
int _GetLastError = 0, _OrdersTotal = OrdersTotal();
// search in all open positions and memorize, positions of which
// type have already been opened:
for ( int z = _OrdersTotal - 1; z >= 0; z -- )
{
// if an error occurs at searching for a position, go
// to the next one
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print("OrderSelect(", z, ", SELECT_BY_POS) - Error #",
_GetLastError );
continue;
}
// if the position was opened not for the current symbol, skip it
if ( OrderSymbol() != Symbol() ) continue;
// depending on the position type, change value of the
// variable:
switch ( OrderType() )
{
case OP_BUY: BuyOrder = OrderTicket(); break;
case OP_SELL: SellOrder = OrderTicket(); break;
case OP_BUYSTOP: BuyStopOrder = OrderTicket(); break;
case OP_SELLSTOP: SellStopOrder = OrderTicket(); break;
}
}
if ( (AccountEquity()/AccountBalance()-1)*100>=R.O.I || (AccountEquity()/AccountBalance()-1)*100<=L.O.I)
{
if ( SellStopOrder > 0 )
{
if ( !OrderDelete( SellStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyStopOrder > 0 )
{
if ( !OrderDelete( BuyStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue) )
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
if ( SellOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
}
}
========================================================
Thanks in advance
Happy trading