What is Wrong with this code?

 

Hi,

Im trying to write an ea and im having trouble opening orders, i also have this ea in multiple charts windows. Currently it only opens one order at a time. What i want to happen is that the ea will only open 1 order per symbol .

      { 
                //Print("openbuy true"); 
                Ototal = OrdersTotal();
                if(Ototal == 0){
                        openbuy=true;
                        }
                else{
                        for(cnt = 0; cnt < Ototal; cnt++)
                        {
                        //Print("Order select");
                        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);   
                                if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic){
                                        Print("opensbuy false");
                                        openbuy=false;
                                }
                         }
                }
       }

PS. Should the magic number be the same in all eas?

 

If the EA is on different symbols then you can use OrderSymbol() to distinguish the Orders . . . if you had the EA on the same Symbol but different timeframes then you would need to use a different magic number for each EA.

 
deVries:


This is wrong

Your EA has no order but can't open trades because you don't check if there is no order from EA

You are checking if there is no order at all...

Read the rest of the code . . .
 
Let me give you a clue : Why don't you try not to use if ... :)
 
onewithzachy:
Let me give you a clue : Why don't you try not to use if ... :)


Haha, the clue didn't help, i have been trying this for some time now, could someone post a code that would work in my case ?

My case:

1 - same time frame per symbol

2 - multiple symbols open on mt4

3 - same ea on those charts

4 - one order per symbol/chart

Appreciate all help =}

 
investguy:
What i want to happen is that the ea will only open 1 order per symbol .

PS. Should the magic number be the same in all eas?
  1. As long as you differentiate the magic number AND symbol, all EA copies can use the same MN. If you are also running the EA on the same symbol but different time frames, then you need a unique MN per time frame.
  2. Count the number of open orders per EA and symbol.
    int count = 0;
        for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
            OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
        &&  OrderMagicNumber()  == Magic.Number                 // my magic number
        &&  OrderSymbol()       == chart.symbol                 // and my pair.
        ){ count++; }
    bool openbuy = (count == 0);

 
investguy:


Haha, the clue didn't help, i have been trying this for some time now, could someone post a code that would work in my case ?

My case:

1 - same time frame per symbol

2 - multiple symbols open on mt4

3 - same ea on those charts

4 - one order per symbol/chart

Appreciate all help =}

Your code should work as far as I can see . . . but I would do this . .

{ 
   openbuy=true;  //  yes we can place an order
   for(cnt = 0; cnt < OrdersTotal(); cnt++)
      {
       if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) &&   //  do we have an open order for this symbol ?
          OrderSymbol() == Symbol() )
         {
         Print("opensbuy false");
         openbuy=false;    //  no we cannot place an order
         break;
         }
      }
}
 
RaptorUK:

Your code should work as far as I can see . . . but I would do this . .

See ... no if :).

RaptorUK you forget the magic.

 
onewithzachy:

See ... no if :).

RaptorUK you forget the magic.

No if ? you mean the OrderSelect() ? OK it's not great but it should still be semi-functional . . .

No Magic is needed . . .

 
RaptorUK:

No if ? you mean the OrderSelect() ? OK it's not great but it should still be semi-functional . . .

No Magic is needed . . .

No, I mean the "if" in the original code :).

if(Ototal == 0)
 
onewithzachy:

No, I mean the "if" in the original code :).

OK, what is wrong with that ? if there are no open orders an order can be opened . . . ( openbuy = true ) if there are orders the next block of code is run, it loops through the open orders and checks the symbol and the magic number, so if it finds and open order that has a match on the symbol and magic number no trade can be opened ( openbuy = false )

Am I missing something ?

Reason: