# Invalid stops

I'm new to mql5, I don't know why it gives me an error, [invalid stops], it's a simple EA you must open a buy trade according to the condition and sell on stop loss or sell all trades on the next candle.

I am thankful for any kind of help....

```#include  <Trade/Trade.mqh>

bool time_passed = true;

int prev_num_velas = 0;

void OnTick() {

int num_velas = Bars(_Symbol,_Period);

if (num_velas > prev_num_velas) {

//Print(PositionsTotal(), "                                       \$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$");
for (int i=PositionsTotal()-1;i>=0; i--){

//--- failure message

} else {

}

}

//---- Candle7 OHLC
double O9=NormalizeDouble(iOpen(Symbol(),_Period,9),4);
double C9=NormalizeDouble(iClose(Symbol(),_Period,9),4);

//---- Candle7 OHLC
double O8=NormalizeDouble(iOpen(Symbol(),_Period,8),4);
double C8=NormalizeDouble(iClose(Symbol(),_Period,8),4);

//---- Candle7 OHLC
double O7=NormalizeDouble(iOpen(Symbol(),_Period,7),4);
double C7=NormalizeDouble(iClose(Symbol(),_Period,7),4);

//---- Candle6 OHLC
double O6=NormalizeDouble(iOpen(Symbol(),_Period,6),4);
double C6=NormalizeDouble(iClose(Symbol(),_Period,6),4);

//---- Candle5 OHLC
double O5=NormalizeDouble(iOpen(Symbol(),_Period,5),4);
double C5=NormalizeDouble(iClose(Symbol(),_Period,5),4);

//---- Candle4 OHLC
double O4=NormalizeDouble(iOpen(Symbol(),_Period,4),4);
double C4=NormalizeDouble(iClose(Symbol(),_Period,4),4);

//---- Candle3 OHLC
double O3=NormalizeDouble(iOpen(Symbol(),_Period,3),4);
double C3=NormalizeDouble(iClose(Symbol(),_Period,3),4);

//---- Candle2 OHLC
double O2=NormalizeDouble(iOpen(Symbol(),_Period,2),4);
double C2=NormalizeDouble(iClose(Symbol(),_Period,2),4);
double L2=NormalizeDouble(iLow(Symbol(),_Period,2),4);

//---- Candle1 OHLC
double O1=NormalizeDouble(iOpen(Symbol(),_Period,1),4);
double C1=NormalizeDouble(iClose(Symbol(),_Period,1),4);

if (O1 > C1 && O2 < C2 && O3 > C3 && O4 > C4 && O5 > C5 && O6 > C6 && O7 > C7 && time_passed == true) {

for(int x=8; x<=500; x++) {

double O10=NormalizeDouble(iOpen(Symbol(),_Period,x),4);
double C10=NormalizeDouble(iClose(Symbol(),_Period,x),4);

if (O10 < C10 && C10 < C4) {

double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
//Print("ACA: ", Ask, "    ACA: ", Bid-5000*_Point);

time_passed = false;
EventSetTimer(60*1*2);

//Print("###########################################################################################    ",_Period, "   NUEVO");
break;

}
}

}

//Print(PositionsTotal(), "                                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
prev_num_velas = num_velas;
}
}
//+------------------------------------------------------------------+

void OnTimer() {
time_passed = true;
}```

The cycle of enumeration of positions must be written correctly. 1. Cycle bypass. 2. Choosing a position - this step is the most important!!!

See example:

```//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
int CalculateAllPositions()
{
int total=0;
for(int i=PositionsTotal()-1; i>=0; i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
total++;
//---
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
{
ulong ticket=PositionGetTicket(index);
if(ticket>0);
{
if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
{
total++
}
}
}
//---
return(total);
}```

The cycle of enumeration of positions must be written correctly. 1. Cycle bypass. 2. Choosing a position - this step is the most important!!!

See example:

I appreciate your help, but this still doesn't resolve the [invalid stops] error

Edinson Perez # :

I appreciate your help, but this still doesn't resolve the [invalid stops] error

You didn't show the corrected code - I don't believe the words, I believe only if there is MQL5 code :)

You didn't show the corrected code - I don't believe the words, I believe only if there is MQL5 code :)

Corrected code? but the error is not generated by PositionsTotal()

Edinson Perez # :

Corrected code? but the error is not generated by PositionsTotal()

Invalid stops
• 2022.03.20
• www.mql5.com
I'm new to mql5, I don't know why it gives me an error, [invalid stops], it's a simple EA you must open a buy trade according to the condition and...

Step 1: ALWAYS use the 'MQL5 Wizard' - this is very important!

After that, I will show you how to go through step 2.

Paso 1: SIEMPRE use el ' Asistente MQL5 ' - ¡esto es muy importante!

Siga el paso 1 y muestre su resultado.

Después de eso, le mostraré cómo realizar el paso 2.

```#include  <Trade/Trade.mqh>
CSymbolInfo *m_symbol;

bool time_passed = true;

int prev_num_velas = 0;

void OnTick() {

int num_velas = Bars(_Symbol,_Period);

if (num_velas > prev_num_velas) {

//Print(PositionsTotal(), "                                       \$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$");
/*
for (int i=PositionsTotal()-1;i>=0; i--){

//--- failure message

} else {

}

}
*/
int total=0;
for(int i=PositionsTotal()-1; i>=0; i--) {
ulong ticket=PositionGetTicket(i);
if(ticket>0){
if(PositionGetString(POSITION_SYMBOL)==Symbol()) {// && PositionGetInteger(POSITION_MAGIC)==InpMagic)

total = total + 1;
}
}
}

Print("Total: ", total);

//---- Candle7 OHLC
double O9=NormalizeDouble(iOpen(Symbol(),_Period,9),4);
double C9=NormalizeDouble(iClose(Symbol(),_Period,9),4);

//---- Candle7 OHLC
double O8=NormalizeDouble(iOpen(Symbol(),_Period,8),4);
double C8=NormalizeDouble(iClose(Symbol(),_Period,8),4);

//---- Candle7 OHLC
double O7=NormalizeDouble(iOpen(Symbol(),_Period,7),4);
double C7=NormalizeDouble(iClose(Symbol(),_Period,7),4);

//---- Candle6 OHLC
double O6=NormalizeDouble(iOpen(Symbol(),_Period,6),4);
double C6=NormalizeDouble(iClose(Symbol(),_Period,6),4);

//---- Candle5 OHLC
double O5=NormalizeDouble(iOpen(Symbol(),_Period,5),4);
double C5=NormalizeDouble(iClose(Symbol(),_Period,5),4);

//---- Candle4 OHLC
double O4=NormalizeDouble(iOpen(Symbol(),_Period,4),4);
double C4=NormalizeDouble(iClose(Symbol(),_Period,4),4);

//---- Candle3 OHLC
double O3=NormalizeDouble(iOpen(Symbol(),_Period,3),4);
double C3=NormalizeDouble(iClose(Symbol(),_Period,3),4);

//---- Candle2 OHLC
double O2=NormalizeDouble(iOpen(Symbol(),_Period,2),4);
double C2=NormalizeDouble(iClose(Symbol(),_Period,2),4);

//---- Candle1 OHLC
double O1=NormalizeDouble(iOpen(Symbol(),_Period,1),4);
double C1=NormalizeDouble(iClose(Symbol(),_Period,1),4);

if (O1 > C1 && O2 < C2 && O3 > C3 && O4 > C4 && O5 > C5 && O6 > C6 && O7 > C7 && time_passed == true) {

for(int x=8; x<=500; x++) {

double O10=NormalizeDouble(iOpen(Symbol(),_Period,x),4);
double C10=NormalizeDouble(iClose(Symbol(),_Period,x),4);

if (O10 < C10 && C10 < C2) {

double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
Print("ACA: ", Ask, "    ACA: ", Bid-5000*_Point);

/*
ulong ticket;
double newsl;

for(int i=0; i<PositionsTotal();i++) {
ticket=PositionGetTicket(i);

if(ticket>0) {

request.position = ticket;   // ticket of the position
double precio = request.price    = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
request.symbol   = PositionGetString(POSITION_SYMBOL);     // symbol
request.sl       = PositionGetDouble(POSITION_SL);                // Stop Loss of the position
request.tp       = PositionGetDouble(POSITION_TP);                // Take Profit of the position
request.magic    = 100;                                    // MagicNumber of the position

Print("Aca estoy");
if(newsl>request.sl) {
request.sl = newsl;
if(!OrderSend(request,result)) {
PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
}
PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
}
}
}*/

//////ulong ticker = OrderTicket();
//////double precio = OrderOpenPrice();
//apertura = OrderOpenPrice();

/// if (OrderSelect(trade_ticket) != 0) {

///    trade.OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-5000*_Point, 0, OrderExpiration());
//OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice()-5000*_Point, 0, OrderExpiration());
/// }

time_passed = false;
EventSetTimer(60*2*2);
//Print(PositionsTotal(), "   \$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$");
//Print("###########################################################################################    ",_Period, "   NUEVO");
break;

}
}

}

//Print(PositionsTotal(), "                                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
prev_num_velas = num_velas;
}
}
//+------------------------------------------------------------------+

void OnTimer() {
time_passed = true;
}```

Invalid stops

Step 1: ALWAYS use the 'MQL5 Wizard' - this is very important!

After that, I will show you how to go through step 2.

Step #2:

Step #3:

Reason: