Why does my closing and opening always causes infinite looping - page 2

 
int start()
  {
   if(Bars<100)
     {
      Print("Bars less than 100");
      return(0);
     }

   bool OpenBar=true;
   if(Enter_on_Open_Bar)
     {
      if(iVolume(Symb,0,0)>1)
        {
         OpenBar=false;
        }
     }
   Comment("Preliminary Process Completed");
   Print("Preliminary Process Completed");

   int Ticket=0,
   OType=-1;

   double Price=0,
   SL = 0,
   TP = 0,
   Lot= 0;

   int Total=0;
   for(int Counter=1; Counter<=OrdersTotal(); Counter++)
     {
      if(OrderSelect(Counter-1,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()!=Symb) continue;
         if(OrderType()>1) // Pending order found
           {
            Alert("Pending order detected. EA doesn't work.");
            Comment("Pending order detected. EA doesn't work.");
            Print("Pending order detected. EA doesn't work.");
            return(0);                             // Exit start()
           }
         Total++;
         if(Total<1)
           {
            Alert("Several market orders. EA doesn't work.");
            return(0);                             // Exit start()
           }
         Ticket=OrderTicket();
         OType =OrderType();
         Price =OrderOpenPrice();
         SL =OrderStopLoss();
         TP =OrderTakeProfit();
         Lot=OrderLots();
        }
     }

   Comment("Order accounting success!");
   Print("Order accounting success!");

   int Current=1;
   double EMA_1,EMA_1_Past,EMA_2,EMA_2_Past,EMA_3,EMA_3_Past;

   bool  open_buy=false,
   open_sell =  false,
   close_buy =  false,
   close_sell=  false;

   EMA_1=iMA(Symb,0,6,0,MODE_EMA,PRICE_CLOSE,Current);
   EMA_1_Past=iMA(Symb,0,6,0,MODE_EMA,PRICE_CLOSE,Current+1);
   EMA_2=iMA(Symb,0,26,0,MODE_EMA,PRICE_CLOSE,Current);
   EMA_2_Past=iMA(Symb,0,26,0,MODE_EMA,PRICE_CLOSE,Current+1);
   EMA_3=iMA(Symb,0,50,0,MODE_EMA,PRICE_CLOSE,Current);
   EMA_3_Past=iMA(Symb,0,50,0,MODE_EMA,PRICE_CLOSE,Current+1);

   if(EMA_1>EMA_3 && EMA_1_Past<EMA_3_Past && OpenBar)
     {
      open_buy=true;
      open_sell =  false;
      close_sell=true;
     }

   if(EMA_1<EMA_3 && EMA_1_Past>EMA_3_Past && OpenBar)
     {
      open_sell=  true;
      open_buy = false;
      close_buy=  true;
     }
   double  Buy_Stoploss=Bid-Stop_Loss*Point,
   Buy_Take_Profit=Bid+Take_Profit*Point,
   Sell_Stoploss=Ask+Stop_Loss*Point,
   Sell_Take_Profit=Ask-Take_Profit*Point;
   int Mod_Order=0;

   if(OrdersTotal()<1 && open_buy==true)
     {
      RefreshRates();
      Ticket=OrderSend(Symb,OP_BUY,Lot_Size,Ask,3,0,0,NULL,
                       Magic_Number,0,clrGreen);
      if(Ticket>0)
        {
         Mod_Order=OrderModify(Ticket,OrderOpenPrice(),Buy_Stoploss,
                               Buy_Take_Profit,0,clrGreen);
        }
     }
   if(OrdersTotal()<1 && open_sell==true)
     {
      RefreshRates();
      Ticket=OrderSend(Symb,OP_SELL,Lot_Size,Bid,3,0,0,NULL,
                       Magic_Number,clrRed);
      if(Ticket>0)
        {
         Mod_Order=OrderModify(Ticket,OrderOpenPrice(),Sell_Stoploss,
                               Sell_Take_Profit,0,clrRed);
        }
     }
   if(OrdersTotal()>0)
     {
      if(close_buy==true)
        {
         for(int Counter=OrdersTotal()-1; Counter>=0; Counter--)
           {
            if(OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES)==true)
              {
               if(OrderType()==OP_BUY)
                 {
                  RefreshRates();
                  Comment("Closing Buy Order!");
                  Print("Closing Buy Order!");
                  bool Ans=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrYellow);
                 }
              }
           }
        }
      if(close_sell==true)
        {
         for(int Counter=OrdersTotal()-1; Counter>=0; Counter--)
           {
            if(OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES)==true)
              {

               if(OrderType()==OP_SELL)
                 {
                  RefreshRates();
                  Comment("Closing Sell Order!");
                  Print("Closing Sell Order!");
                  bool Ans=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrYellow);
                 }
              }
           }
        }
     }
   return(0);
  }
 

This is the complete code, I have tried learning from multiple sources, many like to use While(true) loop, but, it always goes into infinite loops. I still have the problem where it will not always trade at every cross. If I do not do it the way as shown it will go into infinite orders.  I want it to do every cross in strategy tester so I can study things because I plan to add a RSI filter later then also a add a pull back, but, even though I now have it to where it can open and close only once per cross, it does not have the same precision as if (EMA_1 > EMA_3) and if (EMA_1 < EMA_3) does.

Once I can get past this barrier, then I add in the RSI filter where it will enter trades if below or above a certain RSI level. Then after that, if the option to do pull backs is enabled it will go off 26 or 50. Of course, I plan to have two exit strategies,  that is use moving average to stop loss or do a ordercloseby function. Still my biggest obstacle is just having my ea not placing an order immediately upon being placd onto a chart, to do only one trend trade per cross and to recognize each cross. 

My code is not complete because I discovered that Traders Way is a ECN broke with 3 ans 5 digits.


I did forget to mention that I rarely just copy and past codes from another source, I do test, which is why I have so many Print and Comment commands. So, for me, I am probably taking the harder way around things but I am learning  how to do this without having to completely copy things.

 
   int Total=0;
   //
   //
   //
         Total++;
         if(Total<1)
           {
            Alert("Several market orders. EA doesn't work.");
            return(0);                             // Exit start()
           }

Pointless piece of code. Total can never be smaller than 1 as you have just increased it by 1

 
Keith Watford:

Pointless piece of code. Total can never be smaller than 1 as you have just increased it by 1

Yea, I noticed that. Because when the A is running, it is going to rely on the ticket and orders total, then the remaining filters comes from things like magic number, symbol name, if additional string is added, the type of order and so on. I really do not need to explain much detail because I am the noob and you guys have the experience.

I noticed the default tutorial provided to us is useless  as some things will not trigger and others causes infinite loops.

 
I figured out what was going on with my results. I really do need to run my tests on Ticks.
 
I am going to soon be deleting this thread because I have had all problems fixed. Even understood how the Enter on Open Bar was affecting my crosses.