Help for noob

 

Hi,

First EA:

I would like to open a position in the opposite way when a SL has been reached:

Exemple: OP_BUY has been closed. So check if StopLossPrice() == CloseOrderPrice()

So... I would like to create a SELL_LIMIT @ this price or 2 pips away (broker's market conditions)


I'm not sure that the following program works properly

CODE:

/* Reverse strategy */

void Reverse() {
//double ts;
for (int i = OrdersTotal()-1; i >= 0; i --) {

OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
datetime ctm=OrderOpenTime();
if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_BUY && ctm>0 && OrderClosePrice()== OrderStopLoss()) {
OrderSend(Symbol(), OP_SELLLIMIT, Lotsize, OrderClosePrice()+Distance*Point*10, 2, OrderClosePrice()+StopLoss*Point*10, OrderClosePrice()-TakeProfit_short*Point*10, "Reverse on BUY", magicalNumber);}

if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicalNumber && OrderType() == OP_SELL && ctm>0 && OrderClosePrice()== OrderStopLoss()) {
OrderSend(Symbol(), OP_BUYLIMIT, Lotsize, OrderClosePrice()-Distance*Point*10, 2, OrderClosePrice()-StopLoss*Point*10, OrderClosePrice()+TakeProfit_short*Point*10, "Reverse on SELL", magicalNumber);}
}
}


Also, as you can see in the picture attached, it creates thousands of pending orders, that I don't want obvouisly.


Thank you for helping a NOOB : )

 

Your OrderSelect might fail, you should check it has worked.

ctm = 0 is a valid Order, yet you check if ctm > 0

I don't see any picture attached . . .

If you call this function on every tick it will run through all your closed orders that match the Symbol and Magic number and place opposite orders for each of them . . . on each tick. Maybe you should keep an array holding the ticket numbers of the closed orders that you have already placed opposite trades for.

Please use the SRC button to post code . . .

 

  1. Always test return codes
  2. Your count is for current orders list not history
        for(int iPos=OrdersHistoryTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)      // Only orders w/
        &&  OrderMagicNumber() == Magic.Number                  // my magic number
        &&  OrderSymbol()      == chart.symbol                  // and my pair.
        &&  OrderType()        <= OP_SELL// Avoid cr/bal https://www.mql5.com/en/forum/126192
        ){
    

  3. OrderClosePrice()== OrderStopLoss())
    
    Will not work on 5 digit brokers (except in the tester.) When price reaches (seldom) or passes (usually) the SL, the SL becomes a market order to close the position. OrderClosePrice will rarely equal OrderStopLoss. In addition doubles rarely compare equal.
 

Hi guys,

Thank you for helping,

Indeed, answers found @ https://forum.mql4.com/20886#153668

Reason: