• Sign up >>

My First Ea: Need Help (Can not Delete Orders)

To add comments, please log in or register
kingnights1
22
kingnights1  

Hii Guys

This is my first ea,.. 

It should delete any bending orders after hitting the target,.. i surrender ... i do not know why it does not work.

 any help guys




extern double Lot = 0.01;

extern double Lot2 = 0.01;

extern double LotFactor = 2;

extern int StopLoss = 40;

extern int TakeProfit = 40;

extern int magic1 = 334000;

extern int magic2 = 334001;

extern int Slippage = 5;



extern int TotalOrders = 8;

extern int EnterHour = 4;

extern int StopAt = 25;

extern double Zone1 = 50;

extern double ZoneX = 50;

double OpenPrice0;

bool OpenPrice;

double Lot3;

int start()

{

int ticket1;

      if(OrdersTotal()<TotalOrders)

      {

         if(OrdersTotal()==0)

         {

            ticket1=OrderSend(Symbol(),OP_BUY,Lot2,Ask,Slippage,Ask-Zone1*Point-ZoneX*Point,Ask+ZoneX*Point, "BUY",magic1,0, clrBlue);

            OpenPrice = OrderSelect(0,SELECT_BY_POS,MODE_TRADES);

             OpenPrice0 = OrderOpenPrice();

            

            OpenSellStop();

         }                  

            else

               if((OrdersTotal()>1 && OrdersTotal()<= TotalOrders))

               {

                     int count = countpending();

                         if (count ==0)

                           {

                              OrderSelect((OrdersTotal()-1), SELECT_BY_POS,MODE_TRADES); // يختار الأمر الأور للشراء

                                 if (OrderType()==OP_BUY)

                                 {

                                    OpenSellStop();

                                 }

                                    else if(OrderType()==OP_SELL)

                                    {

                                    OpenBuyStop();

                                    }

                                     else if( Ask >= (OpenPrice0+ZoneX*Point) || Ask <= (OpenPrice0 -Zone1*Point-ZoneX*Point))

                                       {

                                        for(int i=0;i<=OrdersTotal();i++)

   {

     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

     

       OrderDelete(OrderTicket());

     } 

                                          }

                                 }

         

                      }

          }

       

         

return(0);

}



//================================================



void OpenSellStop()

{

   int ticket, expiration;

   double point;

   

   point = MarketInfo(Symbol(),MODE_POINT);

   expiration=NULL;

   double price = Bid-StopAt*point;

   

   while(true)

   {

   Lot3=LotFactor*Lot2;

   Lot +=LotFactor;

   ticket= OrderSend(Symbol(),OP_SELLSTOP,Lot,OpenPrice0-Zone1*Point,0,OpenPrice0+ZoneX*Point,OpenPrice0-Zone1*Point-ZoneX*Point,"Sell Stop", magic1,expiration,clrGreen);

   if (ticket <0) Print("Error =",GetLastError());

   else { Print("Ticket =",ticket); break;}

   

   Sleep(10000);

   }

   



}



//================================================



void OpenBuy()

{

 Lot +=LotFactor;

OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,Ask-StopLoss,Ask+TakeProfit*Point, "BUY",magic1,0, clrBlue);



}



//================================================



void OpenBuyStop()

{

   int ticket, expiration;

   double point;

   

   point = MarketInfo(Symbol(),MODE_POINT);

   expiration=NULL;

   double price = Ask+StopAt*point;

   

   while(true)

   {

    Lot +=Lot3;

   ticket= OrderSend(Symbol(), OP_BUYSTOP,Lot,OpenPrice0,0,OpenPrice0-ZoneX*Point-Zone1*Point,OpenPrice0+ZoneX*Point,"Buy Stop",magic1,expiration,clrYellow);

    if (ticket <0) Print("Error =",GetLastError());

   else { Print("Ticket =",ticket); break;}

   

   Sleep(10000);

   }

   

}



//================================================



int countpending()

{

int s=0;

for (int i=0; i < OrdersTotal(); i++)

   {

   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

   if (OrderType()>OP_SELL)

      {

      s++;

      }

    }

return(s);

} 



void CloseAllOrders()

{

   for (int i=0; i<OrdersTotal();i++)

   {

      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUY||OrderType()==OP_SELL)

      {

         int j=0;

         while(j=!5)

         {

         OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrPink);

         j++;

         }

      }

    }

}



void deleteallpendingorders()

{

   for(int i=0;i<=OrdersTotal();i++)

   {

     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

     

       OrderDelete(OrderTicket());

     }   

  

}
lippmaje
1092
lippmaje  

Please edit your post to show the code in code style.

When deleting or closing orders you always need to loop back to front:

//for(int i=0;i<=OrdersTotal();i++)

for(int i=OrdersTotal()-1;i>=0;i--)
William Roeder
20410
William Roeder  
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You don't open any pending orders, therefor you can't delete them.

  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
      For In First Out (FIFO rules-US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: #1 № 11 ACCOUNT_FIFO_CLOSE

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().
    4. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect/Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
                Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
                MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

Keith Watford
Moderator
21036
Keith Watford  
kingnights1:

Please edit your post as requested and also remove all the unnecessary blank lines so that your code is easier to read.

kingnights1
22
kingnights1  
lippmaje:

Please edit your post to show the code in code style.

When deleting or closing orders you always need to loop back to front:

I have tried it ,.. but unfortunately it does not work  
lippmaje
1092
lippmaje  
Please show the current status of your work and explain what is not working.
Jefferson Metha
2561
Jefferson Metha  
lippmaje:

Please edit your post to show the code in code style.

When deleting or closing orders you always need to loop back to front:

for(int i=0;i<OrdersTotal();i++)

even this works

Keith Watford
Moderator
21036
Keith Watford  

Jefferson Metha:

for(int i=0;i<OrdersTotal();i++)

even this works

Always count down when closing/deleting orders

To add comments, please log in or register