Automatic move to break even only works on buy trades.

 

Hello,


As the subject states my automatic move to break even function is only working on buy trades. It is not working on sell trades and i can't see what i'm doing wrong. I'm probably just too close to it but any pointers would be helpful. This function is doing 3 similar things at once. Extra Risk management just prevents multiple buy and sell trades from being open at the same time when i'm using grid strategy. Break Even is just what it sounds like. There is also an incremental trail that takes all open trades and stacks their stop losses so if i'm using move to break even and not extra risk management then i could have several trades a few levels apart and when the market turns around they all stop out profitable at  the same time.

Code for module is as follows:


// Extra Risk Management & Breakeven & Trail
        bool prevent = false;
        for (int i = 0; i < OrdersTotal(); i++)
        {
           if (OrderSelect(i, SELECT_BY_POS))
           {
              // Breakeven
              if ((OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice()) || (OrderType() == OP_SELL && OrderStopLoss() > OrderOpenPrice()))
              {
                if (extra_risk_management && !prevent)
                {
                        prevent = true;
                }
                else if (OrderMagicNumber() == magic && OrderSymbol() == _Symbol)
                {
                        double sl = 0;
                        int type = OrderType();
                                        if (type == OP_BUY && Bid > OrderOpenPrice() + BETrigger)
                                        {
                                                sl = OrderOpenPrice() + BESize;
                                        }
                                        else if (type == OP_SELL && Ask < OrderOpenPrice() - BETrigger)
                                        {
                                                sl = OrderOpenPrice() - BESize;
                                        }
                                        if (sl > 0)
                                        {
                                                Modify(sl);
                                // Trail
                                                Trail(type, sl);
                                        }
                                        
                }
              }
           }
        }

Thanks in advance!

 
Your OrderSelect loop only selects buy orders.
for (int i = 0; i < OrdersTotal(); i++){
   if (OrderSelect(i, SELECT_BY_POS)){
      // Breakeven
      if ((OrderType() == OP_BUY & …
 
William Roeder:
Your OrderSelect loop only selects buy orders.

Thank you!!! One of those things where i've been staring at it too long and not really seeing it anymore. Another set of eyes is all it takes sometimes.

 
dkamerond:

Hello,


As the subject states my automatic move to break even function is only working on buy trades. It is not working on sell trades and i can't see what i'm doing wrong. I'm probably just too close to it but any pointers would be helpful. This function is doing 3 similar things at once. Extra Risk management just prevents multiple buy and sell trades from being open at the same time when i'm using grid strategy. Break Even is just what it sounds like. There is also an incremental trail that takes all open trades and stacks their stop losses so if i'm using move to break even and not extra risk management then i could have several trades a few levels apart and when the market turns around they all stop out profitable at  the same time.

Code for module is as follows:


Thanks in advance!

In this line

if ((OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice()) || (OrderType() == OP_SELL && OrderStopLoss() > OrderOpenPrice()))

If currently sell order has no stop loss (==0) your condition will never be true. So, you should add another option, like this:

if ((OrderType() == OP_BUY && OrderStopLoss() < OrderOpenPrice()) || (OrderType() == OP_SELL && (OrderStopLoss() > OrderOpenPrice() || OrderStopLoss()==0)))
Reason: