Download MetaTrader 5

Need Help with code

To add comments, please log in or register
vaknineyal
57
vaknineyal  

Hi All,

I cannot understand why this code not open any order and also it doesn't give any error.

Update - I Added the code down on this topic.

Thanks A lot.

Ubzen
5394
Ubzen  
vaknineyal
57
vaknineyal  

Sorry Hop it look OK now.

int start()
  {
//----
  
   // -- Check if there is already open position with that Symbol
   
   if(OrderSelect(NULL,SELECT_BY_TICKET)==true)
      {
         if(OrderSymbol() == Symbol())
            {
               CheckForTrailStop();
            }
      }
  
    //-- Open Long 
 
   if (LastUpperBolinger <= LastUpperKeltner) 
      {
         if (Close[1]>UpperBolinger) 
            {
               if (UpperBolinger > UpperKeltner) 
                  {
                     if (MACD_M > MACD_S) 
                        {
                          OpenLongPosition();
                         }
                  }
              }
         }

//-- Open Short 

   if (LastLowerBolinger >= LastLowerKeltner)
      {
         if (Close[1]<LowerBolinger)
            {
               if (LowerBolinger < LowerKeltner)
                  {
                     if (MACD_M < MACD_S)
                        {        
                         OpenShortPosition();
                        }
                  }
               }
          }
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Open Long Order Procedure                                       |
//+------------------------------------------------------------------+

int OpenLongPosition ()
   {
      
      double LowPrice = iLowest(Symbol(),0,MODE_LOWER,10,0);  
      
      if (OrderSelect(NULL,SELECT_BY_TICKET)== true)
         {
            if (OrderSymbol() == Symbol())
               {
                  Alert ("Cannot Open Position - Already have Opened Position");
                  CheckForTrailStop();
               }
         else  OrderSend(Symbol(),OP_BUY, 0.1, Ask, 3, LowPrice, 0, NULL, 0, 0, Green); 
   
      }
//----
   return(0);
   }
   
//+------------------------------------------------------------------+
//| Open Short Order Procedure                                      |
//+------------------------------------------------------------------+
  
int OpenShortPosition ()
   {
      
      double HighPrice = iHighest(Symbol(),0,MODE_UPPER,10,0); 
      
      if (OrderSelect(NULL,SELECT_BY_TICKET)== true)
         {
            if (OrderSymbol() == Symbol())
               {
                  Alert ("Cannot Open Position - Already have Opened Position");
                  CheckForTrailStop();
               }
         else  OrderSend(Symbol(),OP_SELL, 0.1, Bid, 3, HighPrice, 0, NULL, 0, 0, Blue); 
   
      }
//----
   return(0);
   }
   
//+------------------------------------------------------------------+
//| Trailing Stop Procedure                                        |
//+------------------------------------------------------------------+
   
int CheckForTrailStop()
   {
      if(OrderSelect(NULL,SELECT_BY_TICKET)==true)
         {
            if(OrderSymbol() == Symbol())
               {
                  if (OrderType() == OP_BUY)
                     {
                        if (Bid > OrderOpenPrice() + 100*Point)
                           {
                              OrderModify(OrderTicket(),OrderOpenPrice(),Bid-100*Point,OrderTakeProfit(),0,Green);
                           }
                      }
                   if (OrderType() == OP_SELL)
                     {
                        if(Ask < OrderOpenPrice() - 100*Point)
                           {
                              OrderModify(OrderTicket(),OrderOpenPrice(),Ask-100*Point,OrderTakeProfit(),0,Blue);
                           }
                      }
                }
           }               
//----
   return(0);
Keith Watford
Moderator
9641
Keith Watford  

Does the ticket number NULL exist?

if(OrderSelect(NULL,SELECT_BY_TICKET)==true)

if it doesn't, this will never be true

vaknineyal
57
vaknineyal  

Ohhh not sure about that, I am tring to find if there is any open order and if yes check if the order is the same symbol as I want to open.

So how should I write it ?

Ubzen
5394
Ubzen  
vaknineyal:

Hi All,

I cannot understand why this code not open any order and also it doesn't give any error.

Update - I Added the code down on this topic.

Thanks A lot.

What error# and message does it give you?
vaknineyal
57
vaknineyal  
ubzen:
What error# and message does it give you?


No error just not open any position.
Ubzen
5394
Ubzen  
vaknineyal: No error just not open any position.

Check out the Book OrderSelect_Loop.

https://book.mql4.com/functions/trading.

whroeder1
14779
whroeder1  
vaknineyal: Sorry Hop it look OK now.
  1. Why didn't you edit your original post as requested?
  2.    if(OrderSelect(NULL,SELECT_BY_TICKET)==true)
    NULL is not a valid ticket number, this if never executes
  3.          else  OrderSend(Symbol(),OP_SELL, 0.1, Bid, 3, HighPrice, 0, NULL, 0, 0, Blue); 
    
    What are Function return values ? How do I use them ? - MQL4 forum
darksideofthemoon
57
darksideofthemoon  
vaknineyal:

Ohhh not sure about that, I am tring to find if there is any open order and if yes check if the order is the same symbol as I want to open.

So how should I write it ?


You have to do: if(OrdersTatal() > 0) { ... }

vaknineyal
57
vaknineyal  

I change the code to look like that ... but it still doesn't open any order

int start()
  {
//----
  
  
   
   if(OrderSelect(12345,SELECT_BY_TICKET)==true)
      {
         if(OrderSymbol() == Symbol())
            {
               CheckForTrailStop();
            }
      }
  
    //-- Open Long
 
   if (LastUpperBolinger <= LastUpperKeltner) 
      {
         if (Close[1]>UpperBolinger) 
            {
               if (UpperBolinger > UpperKeltner) 
                  {
                     if (MACD_M > MACD_S) 
                        {
                          OpenLongPosition();
                         }
                  }
              }
         }

//-- Open Short 

   if (LastLowerBolinger >= LastLowerKeltner)
      {
         if (Close[1]<LowerBolinger)
            {
               if (LowerBolinger < LowerKeltner)
                  {
                     if (MACD_M < MACD_S)
                        {        
                         OpenShortPosition();
                        }
                  }
               }
          }
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Open Long Order Procedure                                       |
//+------------------------------------------------------------------+

int OpenLongPosition ()
   {
      
      double LowPrice = iLowest(Symbol(),0,MODE_LOWER,10,0);  
      
      if (OrderSelect(12345,SELECT_BY_TICKET)==true)
         {
            if (OrderSymbol() == Symbol())
               {
                  Alert ("Cannot Open Position - Already have Opened Position");
                  CheckForTrailStop();
               }
          }
       if (OrderSelect(12345,SELECT_BY_TICKET)==false)
         {  
           OrderSend(Symbol(),OP_BUY, 0.1, Ask, 3, LowPrice, 0, NULL, 12345, 0, Green); 
         }
      }
//----
   return(0);
  
   
//+------------------------------------------------------------------+
//| Open Short Order Procedure                                      |
//+------------------------------------------------------------------+
  
int OpenShortPosition ()
   {
      
      double HighPrice = iHighest(Symbol(),0,MODE_UPPER,10,0); 
      
      if (OrderSelect(12345,SELECT_BY_TICKET)==true)
         {
            if (OrderSymbol() == Symbol())
               {
                  Alert ("Cannot Open Position - Already have Opened Position");
                  CheckForTrailStop();
               }
       if (OrderSelect(12345,SELECT_BY_TICKET)==false)
         {
            OrderSend(Symbol(),OP_SELL, 0.1, Bid, 3, HighPrice, 0, NULL, 0, 0, Blue); 
         }
      }
   }
//----
   

   
//+------------------------------------------------------------------+
//| Trailing Stop Procedure                                        |
//+------------------------------------------------------------------+
   
int CheckForTrailStop()
   {
      if(OrderSelect(12345,SELECT_BY_TICKET)==true)
         {
            if(OrderSymbol() == Symbol())
               {
                  if (OrderType() == OP_BUY)
                     {
                        if (Bid > OrderOpenPrice() + 100*Point)
                           {
                              OrderModify(OrderTicket(),OrderOpenPrice(),Bid-100*Point,OrderTakeProfit(),0,Green);
                           }
                      }
                   if (OrderType() == OP_SELL)
                     {
                        if(Ask < OrderOpenPrice() - 100*Point)
                           {
                              OrderModify(OrderTicket(),OrderOpenPrice(),Ask-100*Point,OrderTakeProfit(),0,Blue);
                           }
                      }
                }
           }               
//----
   return(0);
   }   
12
To add comments, please log in or register