Max Orders (Buy/Sell) in an EA


Hi Everybody,

Request : I need a script/ code to add to EA to ensure EA opens a maximum of x orders (x buy/x sell) at a time.

If a script, it will make sure once x orders are reached on MT5 terminal, it can not open more until it decreases.

Back Story For transparency : I am currently optimizing an EA to a tick scalper. The EA opens orders per tick and takes minimum profit, sort of a ask/bid price hugger

The problem is that if left alone, the EA can open a lot of orders increasing margin requirement.

Before I came here, I performed due diligence in searching threads for the solution, I did not find. Most threads were left unanswered. Perhaps, it's not possible.

Kindly point out such if it exists already, Thanks a ton.

Attached is the EA code.

tikitaka.mq5  14 kb

Good day Everyone, I have solved this problem. I only thought it right to share. 

I could not develop a script but I found code needed to stop EA from opening past a maximum of x orders.

Included below is a snippet you should add to existing EA and where you should add it in your EA and EA can stop opening past a maximum of x orders

#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>

CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object

input int   InpBuyMaxOrder = 2;
input int   InpSellMaxOrder = 2;

void OnTick()
   int      count_buys     = 0;
   double   volumne_buys   = 0.0;
   int      count_sells    = 0;
   double   volumne_sells  = 0.0;
   // you can then use InpBuyMaxOrder >= count_buys and InpSellMaxOrder >= count_sells as a condition to open trades
void CalculateAllPositions(int &count_buys,double &volumne_buys,
                           int &count_sells,double &volumne_sells)
   count_buys  =0;   volumne_buys   = 0.0;
   count_sells =0;   volumne_sells  = 0.0;
   for(int i=PositionsTotal()-1;i>=0;i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
            else if(m_position.PositionType()==POSITION_TYPE_SELL)