Problem with EA code

 

Hi Guys,

I am quite stuck on this EA I am constructing, and any help would be appreciated (code in file)

Just need a hand with this bit of code...
 
Funky:
based on the last successful order.
int ticket=0;
    for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderProfit()       > 0
    &&  OrderType()         <= OP_SELL){// Avoid cr/bal forum.mql4.com/32363#325360
        ticket = OrderTicket(); // ...
    }
if (ticket == 0) return; // No successful orders

 

How about this?

{
    double lots = 0;
    double takeprofit = 0, stoploss = 0;
    int neworder=0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId)
        {
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TradeUpPoint*PipValue*Point && (OrderStopLoss() < Ask-(TradeUpPoint+TrailingGap)*PipValue*Point))
            {
                stoploss = Ask-TrailingGap*PipValue*Point;
                takeprofit = Ask+NewTakeProfit*PipValue*Point;
                if (NewTakeProfit == 0) takeprofit = OrderTakeProfit();
                neworder=1;
                bool temp1 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
                if (temp1 == false)
                Print("OrderClose() error - ", ErrorDescription(GetLastError()));                 
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TradeUpPoint*PipValue*Point && (OrderStopLoss() > Bid+(TradeUpPoint+TrailingGap)*PipValue*Point))
            {
                stoploss = Bid+TrailingGap*PipValue*Point;
                takeprofit = Bid-NewTakeProfit*PipValue*Point;
                if (NewTakeProfit == 0) takeprofit = OrderTakeProfit();
                neworder=-1;
                bool temp2 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), White);
                if (temp2 == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));               
            }            
        }

}
    if(neworder==1)    
                {
                    int ticket = -1;
                    if (false)
                    ticket = OrderSend(Symbol(), OP_BUY, Lots_size, Ask, 4, 0, 0, "Expert", OrderId, 0, White);
                    else
                    ticket = OrderSend(Symbol(), OP_BUY, Lots_size, Ask, 4, stoploss, takeprofit, "Expert", OrderId, 0, White);
                    if (ticket > -1)
                    {
                        if (false)
                        {
                            OrderSelect(ticket, SELECT_BY_TICKET);
                            bool temp = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
                            if (temp == false)
                            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                        }
                        
                    }
                }
    if(neworder==-1)               
                {
                    int ticket2 = -1;
                    if (false)
                    ticket2 = OrderSend(Symbol(), OP_SELL, Lots_size, Bid, 4, 0, 0, "Expert", OrderId, 0, White);
                    else
                    ticket2 = OrderSend(Symbol(), OP_SELL, Lots_size, Bid, 4, stoploss, takeprofit, "Expert", OrderId, 0, White);
                    if (ticket2 > -1)
                    {
                        if (false)
                        {
                            OrderSelect(ticket2, SELECT_BY_TICKET);
                            bool temp4 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, White);
                            if (temp4 == false)
                            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                        }
                        
                    }
                }     

    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}
 

**edit- Thanks Raptor, I didn't see how to do it before, and had pasted it all in as a quote. You are right it took up 2 screens worth. I'll know next time now, cheers.


Thanks for your help guys, testing now....

 

Please use this to post code . . . it makes it easier to read.


. . . or if you have a lot of code attach it as a file.

 
Both solutions were very helpful in the end to be honest, thank you for your time working on a solution.
 

int lastOpenTime = 0, needleTicket = 0;
   
   for(int i = (OrdersTotal()-1); i >= 0; i --)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      int curOpenTime = OrderOpenTime();
      
      if(curOpenTime > lastOpenTime)
      {
         lastOpenTime = curOpenTime;
         needleTicket = OrderTicket();
      }
   }
   
   Print("result : T#",needleTicket);
   return(0);
  }

For future viewers, I also found this bit of code very helpful from tradeigel here:

https://forum.mql4.com/13279

 
Funky:
For future viewers, ..
  1. OP asked for last successful trade, not open trades.
  2. Always test return codes (orderSelect)
  3. You code show the latest open order, not the latest open order by THIS EA on this pair. Selecting last order opened - MQL4 forum
 
{
    double lots = 0;
    double takeprofit = 0, stoploss = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId_BUY)
        {
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TradeUpPointPips*PipValue*Point && (OrderStopLoss() < Ask-(TradeUpPointPips+TrailingGap)*PipValue*Point))
            {
                stoploss = Ask-TrailingGap*PipValue*Point;
                takeprofit = Ask+NewTakeProfit*PipValue*Point;
                if (NewTakeProfit == 0) takeprofit = OrderTakeProfit();
                bool var1 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), Colour);
                if (var1 == false)
                Print("OrderClose() error - ", ErrorDescription(GetLastError()));
                else
                {
                    int ticket = -1;
                    if (IsECNBroker)
                    ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, MaxSlippage, 0, 0, BuyExpertName, OrderId_BUY, 0, Colour);
                    else
                    ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, MaxSlippage, stoploss, takeprofit, BuyExpertName, OrderId_BUY, 0, Colour);
                    if (ticket > -1)
                    {
                        if (IsECNBroker)
                        {
                            OrderSelect(ticket, SELECT_BY_TICKET);
                            bool var2 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, Colour);
                            if (var2 == false)
                            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                        }
                        
                    }
                }
                
            }
         } 
            
            
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId_SELL)
         {   
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TradeUpPointPips*PipValue*Point && (OrderStopLoss() > Bid+(TradeUpPointPips+TrailingGap)*PipValue*Point))
            {
                stoploss = Bid+TrailingGap*PipValue*Point;
                takeprofit = Bid-NewTakeProfit*PipValue*Point;
                if (NewTakeProfit == 0) takeprofit = OrderTakeProfit();
                bool var3 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, OrderExpiration(), Colour);
                if (var3 == false)
                Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                else
                {
                    int ticket2 = -1;
                    if (IsECNBroker)
                    ticket2 = OrderSend(Symbol(), OP_SELL, LotSize, Bid, MaxSlippage, 0, 0, SellExpertName, OrderId_SELL, 0, Colour);
                    else
                    ticket2 = OrderSend(Symbol(), OP_SELL, LotSize, Bid, MaxSlippage, stoploss, takeprofit, SellExpertName, OrderId_SELL, 0, Colour);
                    if (ticket2 > -1)
                    {
                        if (IsECNBroker)
                        {
                            OrderSelect(ticket2, SELECT_BY_TICKET);
                            bool var4 = OrderModify(OrderTicket(), OrderOpenPrice(), stoploss, takeprofit, 0, Colour);
                            if (var4 == false)
                            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                        }
                        
                    }
                }
                
            }
            
        }

    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}


Yeah you are correct WHRoeder.

After writing that last comment, I did have problems with that, I was restricted to one pair/direction only... good on you for mentioning that for future readers, I was coming back to correct myself too.

Ah I am still beating my head on the wall around this one... as just one simple glitch now.

It operates perfectly now I've split them up this way. It will trade up ok the first and second time, but then it cycles through the send part of the code whenever it is doing a modify order of an order, that is more than 2 trade ups ago....

It can only handle a modify-send-modify... if it has to go modify-send-modify-modify-modify-modify for 4 previous trades in this example, it will split in an extra send order in between every modify... (see statement.... see time and trade number, it doubles orders up each modify order which I do not want). So 1 trade becomes 2 ok, but then 2 trades turn into 4, then 4 trades turn into 8 etc...

I want to stop it doubling up the orders, when it has more than one old one to modify... I just want one trade up at a time. In other words I just want it to turn 1 trade into 2 which turns into 3 then 4 etc.

It's funny some people probably want the exponential sort of effect, but not me... I must admit this one has seriously got me stumped.... any ideas???



 
Funky:
it doubles orders up each modify order which I do not want). So 1 trade becomes 2 ok, but then 2 trades turn into 4, then 4 trades turn into 8 etc...
  1. EA's must adjust for 4/5 digit brokers, TP, SL, AND slippage
    OrderSend(Symbol(), OP_BUY, LotSize, Ask, MaxSlippage, 0, 0
  2. if (OrderType() == OP_BUY && ...
          stoploss = Ask-TrailingGap*PipValue*Point;
    Buy at the ASK, stops are relative to the BID
  3. You've try to combine opening and modifying, thus you have a problem. Further more if the EA dies between the orderSend and the orderModify (such as OS crash, power glitch) you never recover. 1) count open orders 2) Decide whither to open new orders. 3) loop through all open orders and modify.
 

Thanks for your time WHRoeder.

Yeah I did have a filter for 5 digit brokers earlier on in the EA, so MaxSlippage, SL, and TP are already adjusted. Apologies you were not to know that, as I did not include that part of the code.

I think you nailed it in point 3 for me. Good point with the power glitch thing too if it died at that point exactly. It got me thinking, sometimes I have set the settings of TP and SL quite close to the action. Can I ask you if I get an OrderMoidfy() error here, would it not recover as you suggest if I had a power glitch at that point??

Yeah I have tried to modify old ticket first, then open new trade, then modify that ticket. It was the only way I could think of.

1) count open orders 2) Decide whither to open new orders. 3) loop through all open orders and modify.

Are you suggesting it might be best to add +1 to magic number each time, or do I do something fancy with the ordering here?

i=OrdersTotal()-1; i >= 0; i--

Yeah I am still learning code, my questions probably seem quite obvious. Thank you very much for steering me in the right direction, it saves me hours. Cheers.

Reason: