1 (edited by footon 2020-07-27 20:58:29)

Topic: Programming problems with OrdersTotal

hi everyone, I'm going to create an input variable for the OrdersTotal for each Symbol, but it is really difficult for me, in part it was successful but only the total market orders and not those of each single Symbol count, do you have any suggestions?
I've tried everything but I can't understand how the functions work
I tried to change the magic number, to remove the magic number but nothing, can you help me please?

HERE IS THE CODE


void UpdatePosition()

  //extern int tradecount =0;
  { string symbol= Symbol();
  
   posType   = OP_FLAT;
   posTicket = 0;
   posLots   = 0;
   int total = OrdersTotal();
   
// for(int pos=total-1; pos>=5; pos--) // Not Work

   for(int pos=total-1; pos>=tradecont-1; pos--)
    
     {
      if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES) &&
         OrderSymbol()==symbol && 
         OrderMagicNumber()==Magic_Number)
        {
         posType       = OrderType();
         posLots       = OrderLots();
         posTicket     = OrderTicket();
         posStopLoss   = OrderStopLoss();
         posTakeProfit = OrderTakeProfit();
         break;
         }}}

Re: Programming problems with OrdersTotal

Hi Tignola,

What exactly are you trying to achieve? Are you trying to restrict an EA so that it can only have X amount of trades open per symbol? For example 2? So 2 trades for AUDUSD, another 2 for EURUSD etc, and if the EA attempted to open another trade on either of those pairs it wouldn't allow it?

Re: Programming problems with OrdersTotal

hello, yes exactly what I would like to do

burrup.lambert wrote:

Hi Tignola,

What exactly are you trying to achieve? Are you trying to restrict an EA so that it can only have X amount of trades open per symbol? For example 2? So 2 trades for AUDUSD, another 2 for EURUSD etc, and if the EA attempted to open another trade on either of those pairs it wouldn't allow it?

4 (edited by burrup.lambert 2020-07-30 19:56:36)

Re: Programming problems with OrdersTotal

Compiled. Untested.

#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2
void UpdatePosition()
{
   posType   = OP_FLAT;
   posTicket = 0;
   posLots   = 0;
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];

   for(int pos=total-1; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         int ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
         
         if (SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
         OrderSymbol()==_Symbol &&
         OrderMagicNumber()==Magic_Number)
         {
            posType       = OrderType();
            posLots       = OrderLots();
            posTicket     = OrderTicket();
            posStopLoss   = OrderStopLoss();
            posTakeProfit = OrderTakeProfit();
            break;
         }
      }
   }
}

Re: Programming problems with OrdersTotal

burrup.lambert wrote:

Compiled. Untested.

#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2
void UpdatePosition()
{
   posType   = OP_FLAT;
   posTicket = 0;
   posLots   = 0;
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];

   for(int pos=total-1; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         int ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
         
         if (SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
         OrderSymbol()==_Symbol &&
         OrderMagicNumber()==Magic_Number)
         {
            posType       = OrderType();
            posLots       = OrderLots();
            posTicket     = OrderTicket();
            posStopLoss   = OrderStopLoss();
            posTakeProfit = OrderTakeProfit();
            break;
         }
      }
   }
}

thank you very much for the answer, but unfortunately it doesn't work,I really can't find anything on the net, I've been trying for days,I also tried to replace some parameters but it doesn't work

Re: Programming problems with OrdersTotal

If you want some help I'm going to need more than 'doesn't work'! I can gaurantee that code is 90% of the way there.

7 (edited by Tignola 2020-07-30 23:26:46)

Re: Programming problems with OrdersTotal

The problem is always the same, it cannot make a count of the operations per single chart, I did both in backtest and in live but nothing, indeed if I do not change anything to your code only one operation opens per chart
Cannot count the trades by single chart, always counts the total trades, with your code opens a single operation although it should open more based on how I set the EA



   double ind1val1 = iCCI(NULL,0,Ind1Param0,PRICE_TYPICAL,1);
   bool ind1longcci  = ind1val1 > Ind1Param1 + sigma;        
   bool ind1shortcci = ind1val1 < Ind1Param1 - sigma;
 
  // Example for buy or sell in the market specially designed for testing

I try very simply with this entry to TF1 minut so I have fast results in the backtest
Sorry but this thing is driving me crazy, thank you for your patience

burrup.lambert wrote:

If you want some help I'm going to need more than 'doesn't work'! I can gaurantee that code is 90% of the way there.

Re: Programming problems with OrdersTotal

Ignore above. Sorry had the logic in the wrong function. Try replace the OpenPosition function with the one below. The CArrayString SymbolList is slightly redundant as I assume you are only running this on a single pair. You may want to remove the Magic and Symbol check depending on your use-case.

Compiled, untested.

#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2

void OpenPosition(int command)
  {
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];
   int ExistingIndex = 0;
   for(int pos=total; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
      }
   }
   if (!(SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
      OrderSymbol()==_Symbol &&
      OrderMagicNumber()==Magic_Number)) return;
  
   for(int attempt=0; attempt<TRADE_RETRY_COUNT; attempt++)
     {
      int    ticket     = 0;
      int    lastError  = 0;
      bool   modified   = false;
      double stopLoss   = GetStopLoss(command);
      double takeProfit = GetTakeProfit(command);
      string comment    = IntegerToString(Magic_Number);
      color  arrowColor = command==OP_BUY ? clrGreen : clrRed;

      if(IsTradeContextFree())
        {
         double price=MarketInfo(_Symbol,command==OP_BUY ? MODE_ASK : MODE_BID);
         if(setProtectionSeparately)
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
            if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
              {
               modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
              }
           }
         else
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,stopLoss,takeProfit,comment,Magic_Number,0,arrowColor);
            lastError=GetLastError();
            if(ticket<=0 && lastError==130)
              {
               ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
               if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
                 {
                  modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
                 }
               if(ticket>0 && modified)
                 {
                  setProtectionSeparately=true;
                  Print("Detected ECN type position protection.");
                 }
              }
           }
        }

      if(ticket>0)
         break;

      lastError=GetLastError();
      if(lastError!=135 && lastError!=136 && lastError!=137 && lastError!=138)
         break;

      Sleep(TRADE_RETRY_WAIT);
      Print("Open Position retry no: "+IntegerToString(attempt+2));
     }
  }

Re: Programming problems with OrdersTotal

burrup.lambert wrote:

Ignore above. Sorry had the logic in the wrong function. Try replace the OpenPosition function with the one below. The CArrayString SymbolList is slightly redundant as I assume you are only running this on a single pair. You may want to remove the Magic and Symbol check depending on your use-case.

Compiled, untested.

#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2

void OpenPosition(int command)
  {
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];
   int ExistingIndex = 0;
   for(int pos=total; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
      }
   }
   if (!(SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
      OrderSymbol()==_Symbol &&
      OrderMagicNumber()==Magic_Number)) return;
  
   for(int attempt=0; attempt<TRADE_RETRY_COUNT; attempt++)
     {
      int    ticket     = 0;
      int    lastError  = 0;
      bool   modified   = false;
      double stopLoss   = GetStopLoss(command);
      double takeProfit = GetTakeProfit(command);
      string comment    = IntegerToString(Magic_Number);
      color  arrowColor = command==OP_BUY ? clrGreen : clrRed;

      if(IsTradeContextFree())
        {
         double price=MarketInfo(_Symbol,command==OP_BUY ? MODE_ASK : MODE_BID);
         if(setProtectionSeparately)
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
            if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
              {
               modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
              }
           }
         else
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,stopLoss,takeProfit,comment,Magic_Number,0,arrowColor);
            lastError=GetLastError();
            if(ticket<=0 && lastError==130)
              {
               ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
               if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
                 {
                  modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
                 }
               if(ticket>0 && modified)
                 {
                  setProtectionSeparately=true;
                  Print("Detected ECN type position protection.");
                 }
              }
           }
        }

      if(ticket>0)
         break;

      lastError=GetLastError();
      if(lastError!=135 && lastError!=136 && lastError!=137 && lastError!=138)
         break;

      Sleep(TRADE_RETRY_WAIT);
      Print("Open Position retry no: "+IntegerToString(attempt+2));
     }
  }


This is the result fromBacktest
2020.08.04 15:02:03.461    2019.05.31 00:01:17  Testing pass stopped due to a critical error in the EA

Result from real time
2020.08.04 15:05:00.589    TEST MULTI OPERAZIONE EURGBP,M1: array out of range in 'TEST MULTI OPERAZIONE.mq4' (208,30)

however I thank you very much for the help you are giving me .
I'm trying everything too, but I can't find anything that is fine

Re: Programming problems with OrdersTotal

#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2

void OpenPosition(int command)
  {
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];
   int ExistingIndex = 0;
   for(int pos=total - 1; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
      }
   }
   if (!(SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
      OrderSymbol()==_Symbol &&
      OrderMagicNumber()==Magic_Number)) return;
  
   for(int attempt=0; attempt<TRADE_RETRY_COUNT; attempt++)
     {
      int    ticket     = 0;
      int    lastError  = 0;
      bool   modified   = false;
      double stopLoss   = GetStopLoss(command);
      double takeProfit = GetTakeProfit(command);
      string comment    = IntegerToString(Magic_Number);
      color  arrowColor = command==OP_BUY ? clrGreen : clrRed;

      if(IsTradeContextFree())
        {
         double price=MarketInfo(_Symbol,command==OP_BUY ? MODE_ASK : MODE_BID);
         if(setProtectionSeparately)
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
            if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
              {
               modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
              }
           }
         else
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,stopLoss,takeProfit,comment,Magic_Number,0,arrowColor);
            lastError=GetLastError();
            if(ticket<=0 && lastError==130)
              {
               ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
               if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
                 {
                  modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
                 }
               if(ticket>0 && modified)
                 {
                  setProtectionSeparately=true;
                  Print("Detected ECN type position protection.");
                 }
              }
           }
        }

      if(ticket>0)
         break;

      lastError=GetLastError();
      if(lastError!=135 && lastError!=136 && lastError!=137 && lastError!=138)
         break;

      Sleep(TRADE_RETRY_WAIT);
      Print("Open Position retry no: "+IntegerToString(attempt+2));
     }
  }

11 (edited by Tignola 2020-08-04 18:41:50)

Re: Programming problems with OrdersTotal

burrup.lambert wrote:
#include <arrays/arraystring.mqh>
#define SymbolOrderLimit 2

void OpenPosition(int command)
  {
   int total = OrdersTotal();
   
   CArrayString SymbolList;
   int SymbolListOrderCount[];
   int ExistingIndex = 0;
   for(int pos=total - 1; pos>=0; pos--)
   {
      if(OrderSelect(pos,SELECT_BY_POS))
      {
         ExistingIndex = SymbolList.SearchLinear(OrderSymbol());
         if (ExistingIndex == -1)
         {        
            SymbolList.Add(OrderSymbol());
            if (ArrayRange(SymbolListOrderCount, 0) < SymbolList.Total()) ArrayResize(SymbolListOrderCount, SymbolList.Total());
            ExistingIndex = SymbolList.Total() - 1;
            SymbolListOrderCount[ExistingIndex] = 1;
         }
         else 
         {
            SymbolListOrderCount[ExistingIndex]++;
         }
      }
   }
   if (!(SymbolListOrderCount[ExistingIndex] < SymbolOrderLimit && 
      OrderSymbol()==_Symbol &&
      OrderMagicNumber()==Magic_Number)) return;
  
   for(int attempt=0; attempt<TRADE_RETRY_COUNT; attempt++)
     {
      int    ticket     = 0;
      int    lastError  = 0;
      bool   modified   = false;
      double stopLoss   = GetStopLoss(command);
      double takeProfit = GetTakeProfit(command);
      string comment    = IntegerToString(Magic_Number);
      color  arrowColor = command==OP_BUY ? clrGreen : clrRed;

      if(IsTradeContextFree())
        {
         double price=MarketInfo(_Symbol,command==OP_BUY ? MODE_ASK : MODE_BID);
         if(setProtectionSeparately)
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
            if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
              {
               modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
              }
           }
         else
           {
            ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,stopLoss,takeProfit,comment,Magic_Number,0,arrowColor);
            lastError=GetLastError();
            if(ticket<=0 && lastError==130)
              {
               ticket=OrderSend(_Symbol,command,Entry_Amount,price,10,0,0,comment,Magic_Number,0,arrowColor);
               if(ticket>0 && (Stop_Loss>0 || Take_Profit>0))
                 {
                  modified=OrderModify(ticket,0,stopLoss,takeProfit,0,clrBlue);
                 }
               if(ticket>0 && modified)
                 {
                  setProtectionSeparately=true;
                  Print("Detected ECN type position protection.");
                 }
              }
           }
        }

      if(ticket>0)
         break;

      lastError=GetLastError();
      if(lastError!=135 && lastError!=136 && lastError!=137 && lastError!=138)
         break;

      Sleep(TRADE_RETRY_WAIT);
      Print("Open Position retry no: "+IntegerToString(attempt+2));
     }
  }

the same error... i think is impossible sad
but do I have to cancel the for in update position loop?

   for(int pos=total-1; pos>=0; pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS) &&
         OrderSymbol()==_Symbol &&
         OrderMagicNumber()==Magic_Number)

this