EA not closing orders

 
+
void OnTick()
  {
   Print("OnTick() called");
/*
*/
//shows acoount leverage in chart windows on the left
   Comment("Account #",AccountNumber(),
   " leverage is ",AccountLeverage(),
      "lot 1 size is",MarketInfo(Symbol(),MODE_LOTSIZE),
      "lot 0.1 size is",MarketInfo(Symbol(),MODE_LOTSIZE)/1,
      "lot 0.01 size is",MarketInfo(Symbol(),MODE_LOTSIZE)/100,
      "Magin required for 0.01 buy order",MarketInfo(Symbol(),MODE_MARGINREQUIRED)/100);

//Parabolic iSAR decision skeleton
//opnes the first order in the trading session


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   if(onlyonce==0)
     {

      if() //timeframe,step,maximum will be extern w
        {
         //open sell
         onlyonce=1; orderswitch=1; RefreshRates(); Print(onlyonce,orderswitch);
         OpenSell(magic);
        }
      else if()
        {
         //open buy
         onlyonce++; orderswitch=2; RefreshRates();Print("onlyonce",onlyonce,",orderswitch",orderswitch);
         OpenBuy(magic);
        }
     }
//Used for the rest of the trading
   if(onlyonce==1 )//opens sell if previous is buy
     {
      //select buy
      if(OrderSelect(OrdersTotal(),SELECT_BY_POS,MODE_TRADES)==true)//selects last order by time meaning the most recently opened
        {
         Print("order selected:",OrderTicket());
         //close buy
         if((OrderClose(OrderTicket(),0.01,Ask,3,Blue))==true)   //select by pos is 0-the first order to have opened,1- the seocnd order,2-the 3rd order
           {                                                     //so to solvethe problem you need { orderstotal()-1    } while cheking if { !=-1 }
            Print("Order closed");

           }
         else
           {
            Print("OrderClose failed:",GetLastError(),ErrorDesc(GetLastError()));
           }
        }
      else 
      {
      Print("OrderSelect failed:",GetLastError(),ErrorDesc(GetLastError()));
      }
      //open sell
      orderswitch=1; RefreshRates(); Print(onlyonce,orderswitch);
      OpenSell(magic);
     }
   else if()//opens buy if previous is sell
     {
      //select sell
      if(OrderSelect(OrdersTotal(),SELECT_BY_POS,MODE_TRADES)==true)
        {
         Print("order selected:",OrderTicket());
         //close sell
         if(OrderClose(OrderTicket(),0.01,Bid,3,Red)==true) //maybe i need to put the orderticket into a vari
                                                            // able int order_id==orderticket(); and the 
                                                            //order id must be saved in the opensell and buy func
           {
            Print("Order closed");

           }
         else
           {
            Print("OrderClose failed:",GetLastError(),ErrorDesc(GetLastError()));
           }
        }
      else 
      {
      Print("OrderSelect failed:",GetLastError(),ErrorDesc(GetLastError()));
      }   
      //open buy
      orderswitch=2; RefreshRates(); Print(onlyonce,orderswitch);
      OpenBuy(magic);

     }
   Print("OnTick() Ended");
//---
  }
//+------------------------------------------------------------------+

I made this EA to buy order when isar is below Ask and sell when isar is over the Bid,it compiles without problem,but it doesnt closes any position,since i want only one order this is a problem.

Also when i debug and put a breakpoint inside the OniTick it simply jumps over that breakpoint and the arrow that shows where it stopped dissapears.

Also when i am testing this ea in the journal nothing is printed,only the first and last print commands.

Also i have tried any known to me method to selectt the open order and close it be it variables in the function,be it orderstotal()-1 nothing worked .

Also i think my metaeditor is broken,but lets fix the above.

Any help is welcome.

I made an edit and cut out the most important part,

Files:
 
theo1996:

I made this EA to buy order when isar is below Ask and sell when isar is over the Bid,it compiles without problem,but it doesnt closes any position,since i want only one order this is a problem.

Also when i debug and put a breakpoint inside the OniTick it simply jumps over that breakpoint and the arrow that shows where it stopped dissapears.

Also when i am testing this ea in the journal nothing is printed,only the first and last print commands.

Also i have tried any known to me method to selectt the open order and close it be it variables in the function,be it orderstotal()-1 nothing worked .

Also i think my metaeditor is broken,but lets fix the above.

Any help is welcome.

I made an edit and cut out the most important part,

Your MetaEditor is not broken. There are just some issues with the code. I will mention some of them:

/*
I have not tested, but I think this would only work in strategy tester. 
The first parameter of OrderSelect() here should be a ticket number as displayed on your terminal. 
If there are no orders in the market, this EA would try to select an order with a ticket number of 0!
*/
if(OrderSelect(OrdersTotal(),SELECT_BY_POS,MODE_TRADES)==true)



/*
You should check the type of the order before closing it, 
or at least use OrderClosePrice() since in your code, the order is already selected before closing. 
*/
if(OrderClose(OrderTicket(),0.01,Bid,3,Red)==true)