Need HELP with EA (automating orders with parameters based on closed orders)

 

I'm trying to make an expert advisor that selects an order when it closes out at a loss and re-enters it. It shouldn't be a problem with pending orders.. but this doesn't work at all. Can anyone help?

int start()
{
//----
Orders=OrdersTotal();
Count=OrdersHistoryTotal()-1;

if(Orders>OrdersTotal())
{
if(OrderProfit() < 0)
{
OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
OrderSend(Symbol(),OrderType(),OrderLots(),OrderOpenPrice(),7,OrderStopLoss(),OrderTakeProfit(),"",OrderMagicNumber(),0,CLR_NONE);
}
}
//----
return(0);
 
OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
if(OrderProfit() < 0) {
OrderSend(Symbol(),OrderType(),OrderLots(),???,7,???,????,"",OrderMagicNumber(),0,CLR_NONE); }
 
patterns:

I'm trying to make an expert advisor that selects an order when it closes out at a loss and re-enters it. It shouldn't be a problem with pending orders.. but this doesn't work at all. Can anyone help?

int start()
{
//----
Orders=OrdersTotal();
Count=OrdersHistoryTotal()-1;

if(Orders>OrdersTotal())
{
if(OrderProfit() < 0)
{
OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
OrderSend(Symbol(),OrderType(),OrderLots(),OrderOpenPrice(),7,OrderStopLoss(),OrderTakeProfit(),"",OrderMagicNumber(),0,CLR_NONE);
}
}
//----
return(0);
  1. if(Orders>OrdersTotal()) // ALWAYS FALSE


 
qjol:
Thanks! This enables the sending of the order, but it also seems to loop it... the same order keeps getting sent. Any suggestions on how to fix it?
 
WHRoeder:


Hey! Thanks for responding. I appreciate the feedback. Okay so for the code you referred to... it's weird. I used that bit of code for an alert indicator (inserted at bottom), which works. I thought that this way the OrderSend() would only send out once as this indicator does. Because I set the Orders = OrderTotal, it only activates once when an order closes (or opens in this instance..) they are equal but changes once an order is placed or closed.. and then it equalizes again. So in this example the orders>orderstotal isn't always false. When I try to implement this into the expert advisor for reissuing closed orders once, it doesn't work. I can't figure out how to make this simple concept work. Help?

int Orders;

//+------------------------------------------------------------------+
int start()
  {
   if(Orders>OrdersTotal()) SendMail("Order Closed", "");
   if(Orders<OrdersTotal()) SendMail("Order Opened", "");
   Orders=OrdersTotal();
   return(0);
  }
//+------------------------------------------------------------------+
 
if(OrderProfit() < 0)
{
   OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);
    OrderSend(Symbol(), OrderType(), OrderLots(), OrderOpenPrice(), 7, OrderStopLoss(), 
              OrderTakeProfit(), "", OrderMagicNumber(),0,CLR_NONE);
}
You can't use OrderProfit until the select. OrderSelect(Count) assumes the EA only creates orders and runs on only one chart and that it has opened a previous one. All could be false.
    for(int pos=HistoryTotal()-1; pos >= 0; pos--) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders
    &&  OrderMagicNumber()  == magic.number             // w/ my magic number,
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL){    // Avoid cr/bal https://forum.mql4.com/32363
        // found last open order
        if(OrderProfit() < 0) OrderSend(...)
        break; // last open order only.
    }