Breaking the For Loop cycle.

 
Morning,

So I have a function which is designed to Select an order from the ledger through the OrderSelect(..) feature. 

It also selects the OrderSymbol(...) as well as the Magic Number(..).

If any of these return False, it's supposed to return a Print Message (for demonstration purposes as seen below) 

I know how the For loop works, and I get that the OnTick() function is supposed to execute a function on every incoming tick, but despite me trying the break; function, the BarTime = Time[0], with the return; function, but I still get a continuous loop of the Print(...); function on every tick.

What actually is the function which breaks the loop whoever any of these conditions fail?

Thank you.

//+------------------------------------------------------------------+
//|                                                     template.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime          BarTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

BarTime                 =  Time[0];
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

for(int s = OrdersTotal()-1; s>= 0; s--){
   
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)){
      
         if(Symbol() == OrderSymbol()){
         
            if(Period() == OrderMagicNumber()){
                           
               //Do a function                        

            } else { 
            
               
 Print("Magic Number does not match"); 
               
                  return; 
            
            }
         
         } else { 
         
             
Print("Symbol does not match"); 
             
               return; 
            
         }
      
      } else { 
   
         Print("The Order was not selected");
         
            return; 
   
      }
            
   } 
   
  }
//+------------------------------------------------------------------+
 
TheHonestPrussian:
Morning,

So I have a function which is designed to Select an order from the ledger through the OrderSelect(..) feature. 

It also selects the OrderSymbol(...) as well as the Magic Number(..).

If any of these return False, it's supposed to return a Print Message (for demonstration purposes as seen below) 

I know how the For loop works, and I get that the OnTick() function is supposed to execute a function on every incoming tick, but despite me trying the break; function, the BarTime = Time[0], with the return; function, but I still get a continuous loop of the Print(...); function on every tick.

What actually is the function which breaks the loop whoever any of these conditions fail?

Thank you.

Why do you compare Period() to magic number?

*-also instead of printing the same "This message should only be seen once" - which is not correct because it depends on the type of open orders, print for each condition a different message that provides data on what happened, FI for the first one
The symbol of the order is not the symbol of the chart

The second(after correcting Period())
The magic of the order is different than the sought magic

etc.

 
Amir Yacoby #:

Why do you compare Period() to magic number?

Because I trade the same Symbol() over more than one time frame, so it attributes to the function to a particular chart timeframe as opposed to multiple charts with the same symbol. 

 
TheHonestPrussian #:

Because I trade the same Symbol() over more than one time frame, so it attributes to the function to a particular chart timeframe as opposed to multiple charts with the same symbol. 

ok, just print different messages and find the particular message being printed, compare that to the open orders and you have the solution

 
Amir Yacoby #:

ok, just print different messages and find the particular message being printed, compare that to the open orders and you have the solution

This isn't about the open orders. I have this EA on multiple charts, and because it's a for loop, it will perform the loop the moment a new order is placed and on all the open charts.

Print("This message should only be seen once");

The above message is only a test to ensure that the the EA execute the function once, this isn't the case. 

It will run on all the open charts (which is fair enough) but I need to break that cycle so it only executes the function once. The 

Print("The Order was not selected");

comes up a billion times despite me having a break; function in place. I don't get why it's ignoring this. 

As a result, I'm getting the Error  message which means OUT_OF_MEMORY. This is why .

 
Amir Yacoby #:

ok, just print different messages and find the particular message being printed, compare that to the open orders and you have the solutioRight,

So so I'm getting this, as you can see, this is a continuous loop on the symbol mismatch. 

This is what is supposed to happen, as it should not execute orders on any chart where the execution conditions are NOT met, by why the continuous print messages when I've told it to break? 

0       09:51:59.800    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:00.003    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:00.216    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:00.467    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:00.467    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:00.745    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:00.916    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:00.916    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:00.948    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:00.962    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:01.056    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:01.166    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:01.182    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:01.245    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:01.401    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:01.463    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:01.463    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:01.572    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:01.572    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:01.682    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:01.682    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:01.915    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:01.915    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:01.947    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:02.104    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:02.307    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:02.525    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:02.648    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:02.773    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:02.832    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:02.864    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:02.895    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:02.958    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:03.021    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:03.052    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:03.113    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:03.208    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:03.239    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:03.239    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:03.301    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:03.425    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:03.456    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:03.519    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:03.628    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:03.628    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:03.786    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:03.817    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:03.848    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:04.048    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:04.094    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:04.285    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:04.570    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:04.599    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:04.754    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:05.314    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:05.348    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:05.378    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:05.439    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:05.532    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:05.532    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:05.630    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:05.630    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:05.812    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:06.285    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:06.285    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:06.362    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:07.251    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:07.388    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:07.466    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:07.545    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:07.592    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:07.656    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:07.687    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:07.780    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:07.875    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:07.904    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:07.966    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:08.030    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:08.106    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:08.106    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:08.216    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:08.309    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:08.373    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:08.404    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:08.593    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:08.687    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:08.717    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:08.983    hammertime_5.0.1 CADJPY,Daily: Symbol does not match
0       09:52:09.233    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:09.406    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:09.464    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:09.605    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:09.636    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:09.747    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:09.810    hammertime_5.0.1 GBPCHF,Daily: Symbol does not match
0       09:52:09.810    hammertime_5.0.1 AUDCHF,Daily: Symbol does not match
0       09:52:09.964    hammertime_5.0.1 GBPJPY,Daily: Symbol does not match
0       09:52:10.106    hammertime_5.0.1 CADJPY,Daily: Symbol does not match

 

1. Your loop runs on all your orders, not on symbols (and not when a new order appears). As you posted, you have orders open on all those symbols.

2. On each tick, each EA on each chart attached, is checking all the orders (other symbols than the chart included) and prints a message on the first order not of that chart's symbol, and exits - until next tick which will print it again because it reads the orders again.

Now it depends what you want to achieve with that loop.


Exiting on the first order which is not of the chart - I don't see a reason for that. So in case I'm right, first thing to do is remove the return when the order symbol does not match - instead place a 'continue' which goes to next order in the loop.

second, remove the printed message when the symbol does not match
.
Also, you can see in the log from which ea attached to which chart symbol/timeframe the message came from - just try to locate one ea on one chart.


And state what you want to achieve with that loop.

*Maybe you think that your loop runs when a new order appears - but that's not the case as you placed it in the OnTick.

If that's the case, then you need OnTradeTransaction and use different approach

 
TheHonestPrussian #:

This isn't about the open orders. I have this EA on multiple charts, and because it's a for loop, it will perform the loop the moment a new order is placed and on all the open charts.

The above message is only a test to ensure that the the EA execute the function once, this isn't the case. 

It will run on all the open charts (which is fair enough) but I need to break that cycle so it only executes the function once. The 

comes up a billion times despite me having a break; function in place. I don't get why it's ignoring this. 

As a result, I'm getting the Error  message which means OUT_OF_MEMORY. This is why .

This code will not run the moment a new new order is placed, it will run everytime there is a new tick.

When it runs on every tick it will print out a message if there are any orders at all so the code is doing what you have told it to do.

Scanning all the orders every tick on every chart is a complete waste of resources. 

 
Paul Anscombe #:

This code will not run the moment a new new order is placed, it will run everytime there is a new tick.

When it runs (every tick) it will print out a message if there are any orders at all.

But as can be seen from the prints you posted, there are open orders and on many symbols.

So how come you expect it not to print them each tick?

 
Amir Yacoby #:

But as can be seen from the prints you posted, there are open orders and on many symbols.

So how come you expect it not to print them each tick?

I expect it not to print on each tick because I want to tell it to stop printing on the first tick immediately after the order is place and to execute the for loop. Nothing works though.

By default, yes, it it will execute whatever function we code on each tick, but if I tell it to stop (either with a break function or else) then that's what I expected to have happen.


 
Paul Anscombe #:

This code will not run the moment a new new order is placed, it will run everytime there is a new tick.

When it runs on every tick it will print out a message if there are any orders at all so the code is doing what you have told it to do.

Scanning all the orders every tick on every chart is a complete waste of resources. 

" Scanning all the orders every tick on every chart is a complete waste of resources. "

Exactly, which is why I want to stop that