Learning logic - page 10

 

Probably something like this, if without parameters:

bool IsActiveType()
{
   int type = OrderType();
   return (type == OP_BUY || type == OP_SELL);
}

bool IsAnySellType()
{
   int type = OrderType();
   return (type == OP_SELL || type == OP_SELLLIMIT || type == OP_SELLSTOP);
}

{
   if (IsActiveType())
   {
      //...
   }

   if (IsAnySellType())
   {
      //...
   }
}

 
//+----------------------------------------------------------------------------+
//|  Возвращает количество позиций.                                            |
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1 - любая позиция)                    |
//|    mn - MagicNumber                (-1 - любой магик)                      |
//+----------------------------------------------------------------------------+
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal(), kp=0;
  if (StringLen(sy)==1 && StringGetChar(sy, 0)==48) sy=Symbol();
 
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) kp++;
          }
        }
      }
    }
  }
  return(kp);
}
 
Mathemat:

Probably something like this, if without parameters:

bool IsActiveType(int type = -1)
{
   if (type < 0) type = OrderType();
   return (type == OP_BUY || type == OP_SELL);
}
 
This is with a parameter, but you don't have to specify one.
 
I meant to remove the parameters from the calls. And I can't remove them from the function description.
 

How's that, it's an integration into the existing naming system of order property functions:

   bool OrderIsActiveType(int type = -1)
   {
      if (type < 0) type = OrderType();
      return (type == OP_BUY || type == OP_SELL);
   }


   if (OrderIsActiveType())
   {
      //...
   }

   if (OrderIsAnySellType())
   {
      //...
   }
 

It's not obvious, so it doesn't work for me.

And somehow it's essentially the same, but with a parameter it's clearer, so there's no point in defaulting to it, imho.

_______

We are already doing too much of that. Let's give real codes for redesign, discussion etc.
 

But I got it out of a pile of code:

bool SafelyCloseOrder(int orderticket, string operation = "")
{
   bool closed = false;
   LastError = 0;
   string ordercomment = "";
   
   for(int attempt = 0; attempt <= 10 && !closed; attempt++)
   if (OrderSelect(orderticket, SELECT_BY_TICKET, MODE_TRADES))
   if (WaitForConnection(6, operation))
   {  
      ordercomment = OrderComment();
      if (OrderType() == OP_BUY) 
         closed = OrderClose(orderticket, OrderLots(), NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID), 
                    MarketInfo(OrderSymbol(), MODE_DIGITS)), Set_slippage, Blue);
      else                       
         closed = OrderClose(orderticket, OrderLots(), NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK), 
                    MarketInfo(OrderSymbol(), MODE_DIGITS)), Set_slippage, Red);
      LastError = LastError();
      if (LastError != 0)
         Print(WindowExpertName() + " " + operation + " " + ErrorDescription(LastError));
      if (!closed)
      {  Sleep(3000 + attempt * 1000);
         RefreshRates();
      }
   }
   if (closed)
   {  Alert(WindowExpertName() + " " + operation + " Ордер " + orderticket + " " + ordercomment + "  закрыт!");
      PlaySound("Case.wav");
      Sleep(555);
   }
   else
   {  Alert(WindowExpertName() + " " + operation + " Не закрывается ордер " + orderticket + " " + ordercomment + "!");
      PlaySound("NT_Ball_BAMMM!.WAV");
      Sleep(555);
   }
   return(closed);
}
 

How about this?

// основные битовые маски
#define ORD_SELL             1
#define ORD_BUY              2
#define ORD_MARKET           4
#define ORD_LIMIT            8
#define ORD_STOP             16
#define ORD_INVALID          32

// комбинированные битовые маски
#define ORD_MARKET_SELL      5        // ORD_SELL | ORD_MARKET
#define ORD_MARKET_BUY       6        // ORD_BUY | ORD_MARKET
#define ORD_LIMIT_SELL       9        // ORD_SELL | ORD_LIMIT
#define ORD_LIMIT_BUY        10       // ORD_BUY | ORD_LIMIT
#define ORD_STOP_SELL        17       // ORD_SELL | ORD_STOP
#define ORD_STOP_BUY         18       // ORD_BUY | ORD_STOP
#define ORD_PENDING          24       // ORD_STOP | ORD_LIMIT

// получение битовой маски для текущего ордера
int OrderTypeBitmask() {
   int orderType = OrderType();
   int bitmask = ORD_INVALID;
   switch (orderType) {
      case OP_SELL:
         bitmask = ORD_MARKET_SELL;
         break;
      case OP_BUY:
         bitmask = ORD_MARKET_BUY;
         break;
      case OP_SELLLIMIT:
         bitmask = ORD_LIMIT_SELL;
         break;
      case OP_BUYLIMIT:
         bitmask = ORD_LIMIT_BUY;
         break;
      case OP_SELLSTOP:
         bitmask = ORD_STOP_SELL;
         break;
      case OP_BUYSTOP:
         bitmask = ORD_STOP_BUY;
         break;
   }
   return (bitmask);
}

// проверка по маске
bool CheckMask(int value, int mask) {
   return ((value & mask) != 0);
}

// пример:
for (int i = OrdersTotal() - 1; i >= 0; i--) {
   // выбор ордера, отбрасывание по символу, magic'у и т.п.
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      continue;
   if (OrderSymbol() != Symbol)
      continue;
   ...

   // обработка ордера
   int ordType = OrderTypeBitmask();

   bool removeOrder = false;
   if ((signal == SIG_BUY) && CheckMask(ordType, ORD_SELL))
      removeOrder = true;
   else if ((signal == SIG_SELL) && CheckMask(ordType, ORD_BUY))
      removeOrder = true;

   if (!removeOrder)
      continue;

   if (CheckMask(ordType, ORD_MARKET))
      RemoveMarketOrder();
   else if (CheckMask(ordType, ORD_PENDING))
      RemovePendingOrder();
}
 
Mathemat:

Probably something like this, if without parameters:

bool IsActiveType()
{
   int type = OrderType();
   return (type == OP_BUY || type == OP_SELL);
}

this is incorrect to use due to the fact that orderselect may return false and IsActiveType() will return 0, i.e. OP_BUY, which is incorrect...