Help - How to code a trade checker that automatically closes a trade under certain conditions

 

I work with some commercial EAs, but I want to code an EA that constantly checks the open trades and close them if certain conditions are met, for example.-


-Number of minutes the trade is open.

-Currency Pair being traded

-Amount of Profit or Loss at the moment of check.


I have this piece of code, that is supposed to work, but I need help to improve it and to make it work, please help me.


extern int MagicNumber = 0;
extern int NumberOfMinutes = 0;
extern int Slippage = 0;

double point;

int init()
{
point = Point * MathPow(10, Digits%2);
return(0);
}

int start()
{
if ( NumberOfMinutes > 0 )
{
CheckTrades();
}
return(0);
}

void CheckTrades()
{
for ( int i = 0; i < OrdersTotal(); i++ )
{
if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
{
if ( OrderMagicNumber() == MagicNumber )
{
if ( (Time[0] - OrderOpenTime())/60 >= NumberOfMinutes || OrderType() == OP_SELL || OrderSymbol() == "EURUSD" )
{
if ( OrderType() == OP_BUY )
{
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, White);
continue;
}
if ( OrderType() == OP_SELL )
{
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, White);
continue;
}
}
}
}
}
for ( i = 0; i < OrdersTotal(); i++ )
{
if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
{
if ( OrderMagicNumber() == MagicNumber )
{
if ( (Time[0] - OrderOpenTime())/60 >= NumberOfMinutes )
{
CheckTrades();
break;
}
}
}
}
return(0);
}

Reason: