Download MetaTrader 5
To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
shoaib9522
15
shoaib9522 2016.10.22 07:06 
extern double lots =  0.01;
extern int takeprofit = 30;
extern int magic1 = 302;
extern int slippage = 3;
extern double lotfactor = 0.50;
extern int totalORders = 8;
extern int EnterHour = (4);

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---

//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert start function                                             |
//+------------------------------------------------------------------+
int start()
  {
//----
 
 
 int time = TimeHour(Time[0]);
 
 
 if(time==EnterHour)
 {
 
      if(OrdersTotal()<=totalORders)
      {
      
      if(OrdersTotal()==0)
      {
        openbuy();
        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(OrdersTotal()==totalORders)
  {
  
  
     closeallorders();
     
  }
  
  }
                       
                    
            
//----
   return(0);
      }
 
//+------------------------------------------------------------------+
void opensellstop()
{


     int  ticket,expiration;
     double point;
//----
     point=MarketInfo(Symbol(),MODE_POINT);
     expiration=NULL;
     double price = Bid-50*point;
//----
     while(true)
     {
      ticket=OrderSend(Symbol(),OP_SELLSTOP,lots,price,0,0,price-takeprofit*point,"SellStop",magic1,expiration,Green);
      if(ticket<=0) Print("Error = ",GetLastError());
      else { Print("ticket = ",ticket); break; }
      //----10 seconds wait
      Sleep(10000);
      }
      
 lots+=lotfactor;




}

void openbuy()
{
OrderSend(Symbol(), OP_BUY, lots, Ask,  slippage, NULL, Ask+takeprofit*Point, "Buy Trade", magic1, 0, Blue);
lots+=lotfactor;
}


void openbuystop()
{


    int ticket, expiration;
    double point;
//----
    point=MarketInfo(Symbol(),MODE_POINT);
    expiration=NULL;
    double price = Ask+50*point;
//----
    while(true)
      {
       ticket=OrderSend(Symbol(),OP_BUYSTOP,lots,price,0,0,price+takeprofit*Point,"buystop",magic1,expiration,Green);
       if(ticket<=0) Print("Error = ",GetLastError());
       else { Print("ticket = ",ticket); break;}
       //---- 10 seconds wait
       Sleep(10000);
      }
lots+=lotfactor;

}


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)// try 5 times to ensure excution
  {
  OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Pink);
  j++;
  }
  }
  }
  }
Files:
Keith Watford
Moderator
7984
Keith Watford 2016.10.22 07:54  

Please do not double post, I have deleted your other topic.

OrderSelect returns a bool, so you should only work with the selected order if it returns true.

if(OrderSelect()  )
   {
    //do something
   }
else
   {
    //Print the error - GetLastError()
   }

.

shoaib9522
15
shoaib9522 2016.10.22 08:42  
GumRai:

Please do not double post, I have deleted your other topic.

OrderSelect returns a bool, so you should only work with the seleted order if it returns true.

.

where i have to use this code can u please explain me in details i am new here and also new in making expert adviser
peter.MT4Web
158
peter.MT4Web 2016.10.22 08:52  

If you don't understand the answer, you don't understand your own code.

So start to learn coding in C and MQL4 or pay someone to do it for you.

Keith Watford
Moderator
7984
Keith Watford 2016.10.22 09:13  
shoaib9522:
where i have to use this code can u please explain me in details i am new here and also new in making expert adviser

Anybody who wrote code like that would not need to ask for more explanation.

It would be a waste of time explaining in detail as you did not write that code and you would not understand.

If you want to learn coding start with something much simpler.

Mary Clark
122
Mary Clark 2016.10.24 03:42  

See where he gave you the logic.

 


=if(OrderSelect()  )   {
    //do something
   }
else
   {
    //Print the error - GetLastError() 

   } 

 

Your code just does

 OrderSelect(blah blah blah); 

You need to put it in an if statement so like your code

 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)// try 5 times to ensure excution
  {
  OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Pink);
  j++;
  } 

  becomes this (and you can't close an order 5 times so this checks the boolean whether it was closed or not. 

 void closeallorders()
  {
  for(int i=0; i< OrdersTotal(); i++)
  {
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
     { 
        if(OrderType() == OP_BUY||OrderType() == OP_SELL)
  	{
  		int j=0; 
                bool closed = false;
                while(j!=5 && closed==false)// try 5 times to ensure excution
  		{
  			closed = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Pink);
  			j++;
  		
 	}
     }

  } 

Keith Watford
Moderator
7984
Keith Watford 2016.10.24 04:02  
        if(OrderType() == OP_BUY||OrderType() == OP_SELL)
  	{
  		int j=0; 
                bool closed = false;
                while(j!=5 && closed==false)// try 5 times to ensure excution
  		{
  			closed = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Pink);
  			j++;
  		} 

You can't usually close a Sell at Bid

 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage, Pink)

will work no matter if it is a Buy or a Sell.

Also when re-trying, you should re-select the order to make sure that OrderClosePrice() is current, or RefreshRates() if closing at Bid or Ask.

whroeder1
13641
whroeder1 2016.10.24 12:11  
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading)
shoaib9522
15
shoaib9522 2016.10.29 08:12  
GumRai:

You can't usually close a Sell at Bid

will work no matter if it is a Buy or a Sell.

Also when re-trying, you should re-select the order to make sure that OrderClosePrice() is current, or RefreshRates() if closing at Bid or Ask.

thank you soo much all of u i solve acording to that whats written here abow its now 0 warning and  0 error i tested it on demo but after opening first order it starts giving error of 130 why ? how to solve ?
whroeder1
13641
whroeder1 2016.10.29 13:03  
shoaib9522 i tested it on demo but after opening first order it starts giving error of 130 why ? how to solve ?
Previously answered.
/
To add comments, please log in or register