EA for each currency pair - what code to open only once per currency pair? - page 3

 

I changed the closing mechanism from:

for (int j = 0; j < OrdersTotal(); j ++) 
   {
      if (OrderSelect(j, SELECT_BY_POS, MODE_TRADES)) // If there is the next one        
          {                                          // Analyzing orders:        
              if (OrderSymbol()==Symbol())continue;       // Another security    

//--------------Exit criteria (Exit Buy)    

                    
                if (CloseBuy1_1 >= CloseBuy1_2) Order = SIGNAL_CLOSEBUY;      // Trigger for CloseBuy (more can be added)
                if (CloseSell1_1 <= CloseSell1_2) Order = SIGNAL_CLOSESELL;   // Trigger for Closesell (more can be added)
                         
                        if (Order == SIGNAL_CLOSEBUY) 
                         {
                         OrderClose()

to

for (int k = 0; k < OrdersTotal(); k ++) 
   {
      OrderSelect(k, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) 
      {
        if(OrderType() == OP_BUY) 
         {   

//--------------Exit criteria (Exit Buy)    

                    
                if (CloseBuy1_1 >= CloseBuy1_2) Order = SIGNAL_CLOSEBUY;      // Trigger for CloseBuy (more can be added)
                if (CloseSell1_1 <= CloseSell1_2) Order = SIGNAL_CLOSESELL;   // Trigger for Closesell (more can be added)
                         
                        if (Order == SIGNAL_CLOSEBUY) 
                         {
                         OrderClose(....)

and now everything works as expected. Even though I don't understand why is that. Because i really don't understand the additional lines in the second code. But thank you guys!

 
grey.unit:

I change the closing mechanism from:

to

and now everything works as expected. Thank you guys!

It probably won't always work . . . when closing open orders or deleting pending orders within a loop you MUST count down not up . . . you need to change your loop. And you need to put the chack back in for Symbol() . . . otherwise the EA for EURUSD will end up potentially closing orders for GBPUSD . . . what you needed to do was get rid of the continue . . . or make the == into !=
 
grey.unit:

I changed the closing mechanism from:

to

and now everything works as expected. Even though I don't understand why is that. Because i really don't understand the additional lines in the second code. But thank you guys!

Your second code is correct but a little funny. Open MetaEditor, navigator window (Ctrl + D), dictionary tab, and select MQL4 Reference > Standard constants > Trade operations.

 
RaptorUK:
It probably won't always work . . . when closing open orders or deleting pending orders within a loop you MUST count down not up . . . you need to change your loop. And you need to put the chack back in for Symbol() . . . otherwise the EA for EURUSD will end up potentially closing orders for GBPUSD . . . what you needed to do was get rid of the continue . . . or make the == into !=

Just to get you right. What you say is that if I modify the first code to:

for (int j = 0; j < OrdersTotal(); j --) 
   {
      if (OrderSelect(j, SELECT_BY_POS, MODE_TRADES)) // If there is the next one        
          {                                          // Analyzing orders:        
              if (OrderSymbol()==Symbol())       // Another security    
                 {

//--------------Exit criteria (Exit Buy)    

                    
                if (CloseBuy1_1 >= CloseBuy1_2) Order = SIGNAL_CLOSEBUY;      // Trigger for CloseBuy (more can be added)
                if (CloseSell1_1 <= CloseSell1_2) Order = SIGNAL_CLOSESELL;   // Trigger for Closesell (more can be added)
                         
                        if (Order == SIGNAL_CLOSEBUY) 
                         {
                         OrderClose(....)

then it also should work?? Because I tried this and it's just opening one order per currency pair again.

Or if i change the second code to (k++ to k--) it also opens only one trade per currency pair.

for (int k = 0; k < OrdersTotal(); k --) 
   {
      OrderSelect(k, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) 
      {
        if(OrderType() == OP_BUY) 
         {   
 
grey.unit:

Just to get you right. What you say is that if I modify the first code to:

then it also should work?? Because I tried this and it's just opening one order per currency pair again.

Or if i change the second code to (k++ to k--) it also opens only one trade per currency pair.

I'm talking about the first code, the loop you are using where you close orders . . . it must decrement . . . otherwise you will miss orders. I see you have removed the continue and added { } braces, very good :-)

Opening one Order per currency pair or one Order in total ?

 
RaptorUK:

Opening one Order per currency pair or one Order in total ?

Yea, in total of course. Okay 2nd code:


for (int j = 0; j < OrdersTotal(); j ++) 
   {
      if (OrderSelect(j, SELECT_BY_POS, MODE_TRADES)) // If there is the next one        
          {                                          // Analyzing orders:        
              if (OrderSymbol()==Symbol())continue;       // Another security    

When we are talking about the loop I think you mean this:

for (int j = 0; j < OrdersTotal(); j ++)

Actually i would change it to something like this:

for (int j = OrdersTotal()+1 ; j > OrdersTotal(); j --) 
   {

This line opens only one Order per currency Pair (as desired!)

Is the loop correct regarding to this problem now? :

RaptorUK:

It probably won't always work . . . when closing open orders or deleting pending orders within a loop you MUST count down not up . . . you need to change your loop. And you need to put the chack back in for Symbol() . . . otherwise the EA for EURUSD will end up potentially closing orders for GBPUSD . . . what you needed to do was get rid of the continue . . . or make the == into !=
 
grey.unit:

Yea, in total of course. Okay 2nd code:


When we are talking about the loop I think you mean this:

Actually i would change it to something like this:

This line opens only one Order per currency Pair (as desired!)

Is the loop correct regarding to this problem now? :

No, do this . . .

for (int j = OrdersTotal()-1 ; j >= 0 ; j --)   // start at OrdersTotal() -1 ,  finish when j = 0

The first order position is 0 so if you have 5 orders the positions are 0, 1, 2, 3, 4 . . . so the last is OrdersTotal() - 1 . . . so to count down you start at OrdersTotal()-1 and end at 0

 
Okay, thanks alot for your help!
 
RaptorUK:

No, do this . . .

The first order position is 0 so if you have 5 orders the positions are 0, 1, 2, 3, 4 . . . so the last is OrdersTotal() - 1 . . . so to count down you start at OrdersTotal()-1 and end at 0

So it should look like this:

for (int j = OrdersTotal()-1 ; j = 0 ; j --) 

because you wrote this:

for (int j = OrdersTotal()-1 ; j >= 0 ; j --) 

or am I wrong?

 
grey.unit:

So it should look like this:

because you wrote this:

or am I wrong?

You are dead wrong. Your code never gets executed !!!. Use RaptorUK's.
Reason: