Learning logic - page 12

 

Victor, I think the switch is better here - it's faster and more visual.

bool IsOrderType(int type)
{
   switch (type)
   {
      case OP_BUY:
      case OP_SELL:
      case OP_BUYLIMIT:
      case OP_SELLLIMIT:
      case OP_BUYSTOP:
      case OP_SELLSTOP:
         return (true);
   }
   return (false);
}


Oh, by the way -- reasonable optimization of work in the tester -- I've been using it lately.

int FindLastOpenTime(int tip, int imagic) 
{
   int Res=-1;
   int lOrderOpenTime=-1;
   
   for (int i = OrdersTotal() - 1; i >= 0; i--) 
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))   continue;
      if (OrderSymbol() != Symbol())                     continue;
      if (OrderMagicNumber() != imagic)                  continue;
      if (!(tip==-1 || isOrderType(tip)))                continue;

      if (IsTesting()) return (OrderTicket());  // тут

      if (lOrderOpenTime==-1) { 
         lOrderOpenTime=OrderOpenTime(); 
         Res=OrderTicket();
      } else if (lOrderOpenTime<OrderOpenTime()) {
         lOrderOpenTime=OrderOpenTime(); 
         Res=OrderTicket();
      }
   }
   return (Res);
}

By the way, the function is called FindLastOpenTime and returns a ticket.

May be it is better so?

datetime FindLastOpenTime(int tip, int imagic, int& ticket) 
{
   //...
}
 
TheXpert:

Victor, I think the switch is better here - it's quicker and clearer.


It's really clearer.
 

TheXpert:

By the way, the function is called FindLastOpenTime, ...

The idea is that
if (lOrderOpenTime<OrderOpenTime()) {
         lOrderOpenTime=OrderOpenTime(); 
         Res=OrderTicket();
      }
it may start working right at -1, I'm not quite sure why it needs an external if...else. Return, imho, is really better lOrderOpenTime, then return -1 will allow to catch the error.
 
Yeah, I didn't.
 

is it logical to use this kind of thing?

int Z[];

int Fn()
{
....
  int Z = 0;

  Z++;
....

return(Z);
}
start()
{
   .......
   Z[f]=3;
   ......
}
 
No. The logical z (small) inside. In general, it's best not to mess around with such things.
 

always killed the logic of such a construction in cyclic operands :

if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != imagic) continue;
if (!(tip==-1 || isOrderType(tip))) continue;

see help :

The continue statement transfers control to the beginning of the nearest external while or for statement, causing the beginning of the next iteration. This operator is the opposite of break.

It is not clear at all, and if an order doesn't pass the conditions then what? Exit the loop? if continue operator is directly opposite to break operator ...

For me, the standard logic is clearer and more understandable :

int FindLastOpenTime(int type, int imagic){int time = 0;
  for(int i = OrdersTotal() -1 ;i>=0;i--){
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
        if(OrderSymbol()==Symbol()){
           if(OrderType()==type){
              if(OrderMagicNumber()==magic){
                 if(OrderOpenTime()>time){
                    time=OrderOpenTime();
                 }
              }
           }
        }
     }
  }
  return(time);
}  
 
TheXpert:

Puncture #2. Logic (logic) and brevity have little correlation.

A prime example right out of MQL, which, by the way, many people are not squeamish about using.

This is not logic -- it's a murder of logic. In addition, it is a potential breeding ground for implicit errors.


Not sure where the puncture is? And why the second one? If it is the second, where is the first?
 
Integer:
For those who don't have an office or zip.

Dima add something for those who don't have metaeditor.exe :-)

 
Vinin:


Victor, congratulations again !