Unable to correctly prevent over trading

 

Hello,


I'm working on modifications to an EA that uses a grid and reactivates lines when the market moves a set distance away. For testing purposes i'm using a grid size of 10 pips and the same activation distance. In order to prevent constant orders on the same lines i want to limit it to 1 buy and 1 sell on any given line. I just can't seem to get the logic correct. What i'm seeing with the current code is that it is allowing trades when it should not and it is not really preventing anything and i can't seem to wrap my head around what i'm dong wrong.


Here is an excerpt from the Journal tab in the tester as you can see it takes Buy for orders 3 and 5 on the same line :

2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: Buy order Opened !
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: open #5 buy 0.10 GBPUSD at 1.23722 ok
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.236 2019.09.10 09:45:39  Automatic lines Grid Martingale GBPUSD,H1: LineBuy = 1.23722
2019.10.14 19:54:39.205 2019.09.10 09:44:38  Automatic lines Grid Martingale GBPUSD,H1: Sell order Opened !
2019.10.14 19:54:39.205 2019.09.10 09:44:38  Automatic lines Grid Martingale GBPUSD,H1: open #4 sell 0.10 GBPUSD at 1.23622 ok
2019.10.14 19:54:39.205 2019.09.10 09:44:38  Automatic lines Grid Martingale GBPUSD,H1: LineSell = 1.23622
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: Buy order Opened !
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: open #3 buy 0.10 GBPUSD at 1.23723 ok
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: do not prevent Buy trade as none exists on this level
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: select open orders
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: check for open orders
2019.10.14 19:54:39.174 2019.09.10 09:43:29  Automatic lines Grid Martingale GBPUSD,H1: LineBuy = 1.23722

And here is the buy portion of the code that is giving me the trouble:

        //only allow 1 buy trade per line----------------------------------------
        if (LineBuy > 0 && Ask >= LineBuy)
        {
        Print ("LineBuy = ",LineBuy);
           for (i = 0; i < OrdersTotal(); i++)
           {
           Print ("check for open orders");
            if (OrderSelect(i, SELECT_BY_POS))
              {
           Print ("select open orders");
                if (OrderType() == OP_BUY && OrderOpenPrice()== LineBuy)
                    {
                     prevent=true;
                     Print ("Prevent Buy trade as one exists on this line",line);
                    }
                    else
                    {
                    Print ("do not prevent Buy trade as none exists on this level");
                     prevent=false;
                    }
              }
           }
//only allow 1 buy trade per line----------------------------------------
        
                if (prevent)
                {
                        LineBuy = 0;
                        Print ("buy trade prevented due to existing trade on same level");
                }
                else
                {               
                        // open Buy
                        //Print ("Account Ballance ",AccountBalance(),"AccountEquity",AccountEquity(),"AccountFreeMargin",AccountFreeMargin(),"AccountFreeMarginMode",AccountFreeMarginMode());
                        double sl = trigger_distance > 0 ? NormalizeDouble(Bid - (Distance * 3), _Digits) : 0;
                        double tp = takeprofit > 0 ? NormalizeDouble(Bid + TP, _Digits) : 0;
                        int ticket = OrderSend(NULL, OP_BUY, GetLots(Ask, SL), Ask, slippage, 0, tp, NULL, magic, 0, clrGreen);
                        if (ticket < 0)
                        {
                                Print("Error BUY : ", GetLastError());
                                //CloseOrders();
                                if (!equity_reset)
                                {
                                 ExpertRemove();
                                }



As always i appreciate any help that is offered.


Thanks!!!

 

OrderOpenPrice()== LineBuy


Wrong.

The equality of two real numbers can't be compared. In most cases, two seemingly identical numbers can be unequal because of different values in the 15th decimal place. In order to correctly compare two real numbers, compare the normalized difference of these numbers with zero. "


if (OrderType() == OP_BUY && fabs(OrderOpenPrice()-LineBuy)<0.5*Distance )


would be better.


https://www.mql5.com/en/docs/basis/types/double

Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
Documentation on MQL5: Language Basics / Data Types / Real Types (double, float)
  • www.mql5.com
Real types (or floating-point types) represent values with a fractional part. In the MQL5 language there are two types for floating point numbers.The method of representation of real numbers in the computer memory is defined by the IEEE 754 standard and is independent of platforms, operating systems or programming languages. Floating-point...
 
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum