Trade Context Busy and Old tick Solution Required

 

I have been trying to use 2 of the solution on this forum but it's not working could someone please have a look at it and let me know where I have gone wrong.

Regards

Kiwi

   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {
         IsTrade = False;
         if(OrderType() == OP_BUY) {
            //Close


   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+

   if (Buy1_1 < Buy1_2 ) Order = SIGNAL_BUY;
   if (Sell1_1 >= Sell1_2) Order = SIGNAL_SELL;




   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

            if(TradeIsBusy() < 0) 
        return(-1);
         if (UseStopLoss) StopLossLevel = Ask - StopLoss ; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid + TakeProfit; else TakeProfitLevel = 0.0;
         Ticket=-1;
         while (Ticket<0)
         {
         RefreshRates();
         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue < 0);
            }
            }
            else
            {

            if(TradeIsBusy() < 0) 
        return(-1);
         if (UseStopLoss) StopLossLevel = Ask - StopLoss; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid + TakeProfit; else TakeProfitLevel = 0.0;
         
         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue < 0);
         RefreshRates();
         if(Ticket < 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());

             TradeIsNotBusy();

}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;


         return(0);
          }
        }

 

Hi Kiwi

I find your code very difficult to read probably because I only like the way I write mine but it looks like you are continuously doing ordersends after errors and that is a recipe for disaster. Try putting some pauses in the loops. Also make sure your code isn't returning and then immediately coming back in on another tick and generating another order too quickly.

Hope this helps

 
Ruptor wrote >>

Hi Kiwi

I find your code very difficult to read probably because I only like the way I write mine but it looks like you are continuously doing ordersends after errors and that is a recipe for disaster. Try putting some pauses in the loops. Also make sure your code isn't returning and then immediately coming back in on another tick and generating another order too quickly.

Hope this helps

Thanks for the reply.

As I not to good at this coding lark cpould you please show me and example of what you are thinking.

Regards

Kiwi

 

Hi Kiwi

Some good info here https://www.mql5.com/en/articles/1412 especially the IsTradeAllowed(); stuff. After reading this you will be an expert and move on to the next error to solve.

 
Ruptor wrote >>

Hi Kiwi

Some good info here https://www.mql5.com/en/articles/1412 especially the IsTradeAllowed(); stuff. After reading this you will be an expert and move on to the next error to solve.

Thanks but I have been through that several times and been trying to impliment that into the EA bu its not going well and I am still getting the error.

Regards

Kiwi

 
I have attached the ea for someone to have a look a and maybe come up with a solution
Files:
reap.mq4  14 kb
 

Here is the include file.

Files:
Reason: