Download MetaTrader 5
To add comments, please log in or register
Do you want to become a well-known developer? Execute the orders of traders in the Freelance service!
TraderJP
8
TraderJP 2016.09.21 22:49 

I am new to MQL4 and have been trying to solve the following problem all day. Currently the code below will buy when the indicator raises up above -40 from below. However, I cannot get it to close that same order when it raises above +40. (The same is true of the sell signal in the opposite direction)


extern int StopLoss=30;
extern int Upper = 40;
extern int Lower = -40; 
extern double LotSize = 0.01;
extern int MagicNumber = 1234;
bool Work=true;

int start()
{
 double CurrentInd=iCustom("EURUSD",1,"Indicator",1,0);
 double PreviousInd=iCustom("EURUSD",1,"Indicator",1,1);
  
   if (PreviousInd<Lower && CurrentInd>Lower && OrdersTotal () == 0)
           {
         OrderSend (Symbol (), OP_BUY, LotSize, Ask, 3, Ask - (StopLoss*0.00001), NULL, NULL, MagicNumber);
           };

   if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)
{
OrderClose(MagicNumber, LotSize, Bid,10);
};

 

  
   if (PreviousInd>Upper && CurrentInd<Upper && OrdersTotal () == 0)
         {
       OrderSend (Symbol (), OP_SELL, LotSize, Bid, 3, Bid + (StopLoss*0.00001), NULL, NULL, MagicNumber);
          };

  if (OrdersTotal () ==1 && OrderType ()==OP_SELL && PreviousInd>Lower && CurrentInd<Lower)
{
OrderClose(MagicNumber, LotSize, Ask,10);
}; 
}


Keith Watford
Moderator
7950
Keith Watford 2016.09.22 00:34  
Look up the help file for OrderClose(), you cannot close an order with the magic number.
TraderJP
8
TraderJP 2016.09.22 09:55  
GumRai:
Look up the help file for OrderClose(), you cannot close an order with the magic number.

 Thanks GumRai. I've changed it to the below but still having problems

 if (PreviousInd<Lower && CurrentInd>Lower && OrdersTotal () == 0)
           {
        int ticket;
         ticket = OrderSend (Symbol (), OP_BUY, LotSize, Ask, 3, Ask - (StopLoss*0.00001), NULL);
           };

if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)
   {
OrderClose(ticket, LotSize, Bid,10);
};
Keith Watford
Moderator
7950
Keith Watford 2016.09.22 11:51  
if (OrdersTotal () == 1 && OrderType ()==OP_BUY && PreviousInd<Upper && CurrentInd>Upper)

You have to select an order before you can access OrderType()

/
To add comments, please log in or register