Checking Open Orders for different Currency Pairs doesnt work

 

Hi community,


I just started learning MQL4 and although there are some similar problems solved on this forum I can't figure out my problem. I started doing the followoing code to place an Order depending on the behavciour of the Stochastic Oscillator, which worked as intended:

int init()
{
   return (0);
}

int deinit()
{
   return (0);
}

int start()
{
   double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double presSS  = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
   
   while (OrdersTotal() == 0)
   { 
            if ((presSS < 30) && (presSM > presSS))
            {
               OrderSend (Symbol(), OP_BUY, 0.1, Ask, 30, 0, 0, "Entry", 0, 0, Green);
            }
            else if ((presSS > 70) && (presSM < presSS))
            {
               OrderSend (Symbol(), OP_SELL, 0.1, Bid, 30, 0, 0, "Entry", 0, 0, Green);
            }
   }
  return (0);

}


As only one order in total gets opened at a time I tried to insert a function that checks, wether there is an existing order for the relevant currency pair, but every attempt fails. Either countless orders get placed or no order at all gets placed. Here is my latest attempt, which is resulted in nothing actually happening:


int init()
{
   return (0);
}

int deinit()
{
   return (0);
}

int start()
{
   double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double presSS  = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
   
   for (int i = 0; i < OrdersTotal(); i++)
   {   
      if (OrderSelect(i, SELECT_BY_POS) == true)
         {
            if (OrderSymbol() == Symbol())
               {
               break;
               }
         }
      else
         {
            if ((presSS < 30) && (presSM > presSS))
            {
               OrderSend (Symbol(), OP_BUY, 0.1, Ask, 30, 0, 0, "Entry", 0, 0, Green);
            }
            else if ((presSS > 70) && (presSM < presSS))
            {
               OrderSend (Symbol(), OP_SELL, 0.1, Bid, 30, 0, 0, "Entry", 0, 0, Green);
            }
         }
   }
  return (0);

}

Thanks in advance for any advice you can give me!
 
reXmo:

Hi community,


I just started learning MQL4 and although there are some similar problems solved on this forum I can't figure out my problem. I started doing the followoing code to place an Order depending on the behavciour of the Stochastic Oscillator, which worked as intended:

Thanks in advance for any advice you can give me!


 Please edit your post . . . 



Please use this to post code . . . it makes it easier to read.

 
reXmo:


As only one order in total gets opened at a time I tried to insert a function that checks, wether there is an existing order for the relevant currency pair, but every attempt fails. Either countless orders get placed or no order at all gets placed. Here is my latest attempt, which is resulted in nothing actually happening:


It's good to see someone trying before asking  :-)

int init()
   {
   return (0);
   }

int deinit()
   {
   return (0);
   }

int start()
   {
   double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double presSS  = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
   
   for (int i = 0; i < OrdersTotal(); i++)    //  if you have no orders the loop won't run as i is never less than 0
      {   
      if (OrderSelect(i, SELECT_BY_POS) == true)
            {
            if (OrderSymbol() == Symbol())
               {
               break;
               }
            }
         else
            {
            if ((presSS < 30) && (presSM > presSS))
               {
               OrderSend (Symbol(), OP_BUY, 0.1, Ask, 30, 0, 0, "Entry", 0, 0, Green);  // this code is within the loop that never runs . . . 
               }
            else if ((presSS > 70) && (presSM < presSS))
               {
               OrderSend (Symbol(), OP_SELL, 0.1, Bid, 30, 0, 0, "Entry", 0, 0, Green); // this code is within the loop that never runs . . .
               }
            }
      }  //  end of for loop

   return (0);
   }
 

So I would make these changes . . .

int init()
   {
   return (0);
   }

int deinit()
   {
   return (0);
   }

int start()
   {
   double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double presSS  = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);

   int OrdersForMyPair = 0;    //  added to keep count of orders found for the currency pair I am interested in

   for (int i = 0; i < OrdersTotal(); i++)    
      {   
      if (OrderSelect(i, SELECT_BY_POS) == true)
         {
         if (OrderSymbol() == Symbol())
            {
            OrdersForMyPair++;             //  increment the count
            }
         }

      }  //  end of for loop

   if(OrdersForMyPair == 0)    // only place orders if there are already none open
      {
      if ( presSS < 30 && presSM > presSS )
         {
         OrderSend (Symbol(), OP_BUY, 0.1, Ask, 30, 0, 0, "Entry", 0, 0, Green);  
         }
      else if ( presSS > 70 && presSM < presSS )
         {
         OrderSend (Symbol(), OP_SELL, 0.1, Bid, 30, 0, 0, "Entry", 0, 0, Green); 
         }
      }

   return (0);
   }

 
Next you need to add code to tell you if your OrderSend() functions worked and if they didn't what error happened and any other useful info, Bid, Ask, etc.  that will help you identify what happened.

Read this:  What are Function return values ? How do I use them ?

 
RaptorUK:

It's good to see someone trying before asking  :-)

Indeed.

 

reXmo:

Hi community,

I just started learning MQL4 and although there are some similar problems solved on this forum I can't figure out my problem. I started doing the followoing code to place an Order depending on the behavciour of the Stochastic Oscillator, which worked as intended:


int init()
{
        return (0);
}

int deinit()
{
        return (0);
}

int start()
{
        double presSM = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
        double presSS  = iStochastic (Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);

        for (int i=OrdersTotal()-1; i>=0; i--) /* Counting Down Is Better For Most Cases */
        {   
                if (OrderSelect(i, SELECT_BY_POS) == true)
                {
                        if (OrderSymbol() == Symbol())
                        {
                                return(0);                                              /* Exits The Start() If Order Found*/
                        }
                }
        }
        
        

        if ((presSS < 30) && (presSM > presSS))
        {
                OrderSend (Symbol(), OP_BUY, 0.1, Ask, 30, 0, 0, "Entry", 0, 0, Green);
                /* 
                Stoploss TakeProfit and Slippage Should Adjust For 4/5 Digit Brokers
                You'll Want to Research How To Fix That.
                */
        }
        
        else if ((presSS > 70) && (presSM < presSS))
        {
                OrderSend (Symbol(), OP_SELL, 0.1, Bid, 30, 0, 0, "Entry", 0, 0, Green);
        }
        return (0);

}

/* 
        Other Notes. Its Better Use Functions Which Returns The Order Count Instead Of Exiting
        The Start Function Like that. When You Start To Learn About Functions, Use Them.
        
        You May Want To Check If The OrderSend Worked And Return Error# If It Fails...
        I'm Guilty For Not Doing This Within Strategy Tester...However You Want This For Live.

        I Didn't Test The Above Codes. Good Luck ;)
*/
 
for (int i = 0; i < OrdersTotal(); i++)    // if you have no orders the loop
      {                                    //  won't run as i is never less than 0
      if (OrderSelect(i, SELECT_BY_POS) == true)
            {
            if (OrderSymbol() == Symbol())
               {
               break;
               }
            }
         else
            {
            if ((presSS < 30) && (presSM > presSS)) ...
  1. If you have no open order the loop won't run and nothing will open
  2. If the first order (the oldest) is not the chart's symbol, you'll open a new order every tick
  3. TryOr
    for (int i = OrdersTotal() - 1; i>=0; i--)
    if (OrderSelect(i, SELECT_BY_POS)
    &&  OrderSymbol() == Symbol()
       ) return; // Found one, don't open another.
    
    if ((presSS < 30) && (presSM > presSS)) ...
    bool isOpen = false;
    for (int i = OrdersTotal() - 1; i>=0; i--)
    if (OrderSelect(i, SELECT_BY_POS)
    &&  OrderSymbol() == Symbol()
       ) isOpen = true;
    if (!isOpen){
       if ((presSS < 30) && (presSM > presSS)) ...
    }
    
  4. Always count down Loops and Closing or Deleting Orders - MQL4 forum
  5. You would never write IF( (2+2) == 4) == true) would you? Then never write IF(bool == true). IF(bool) and IF(!bool) is sufficient.
 
Thank you so much guys. It works now as intended and I learned a lot about the logic behind certain things. It is very encouraging to recieve such a top-notch assistance here!
Reason: