buggy MlQ4 ??? Trailing stop code not working

 

The following is some trailing stop loss code that compiles ok but then when i run the EA on backtesting it just doesnt act to put a trailing stop loss on. The trade just runs off into "infinite" profit actually, as when I open the trade I dont specify a TP value and at simulation end the trade is in very large profit.

Can anyone see anything wrong with this code? Why doesnt it successfully set the trailing Stop loss?

I am a bit frustrated - MLQ4 seems very buggy and in times like this I dont know whether it is me or just some failing in MLQ4.

// -------------------------------------------------------

for (int khv=1; khv<=OrdersTotal(); khv++)
{
if(OrderSelect(khv-1,SELECT_BY_POS)==true) // cycling through open orders
{
double Trailing_alt = one/10; // 500/10 = 50
double suchnsuchpoint_alt = one/2; // 500/2 = 250

if (OrderType() == OP_BUY && OrderComment() == "altb" && OrderProfit () > suchnsuchpoint_alt && Trailing_alt > 0)
{
if(NormalizeDouble(Bid,Digits)-OrderOpenPrice() > Point*Trailing_alt)
{
if(OrderStopLoss() < NormalizeDouble(Bid,Digits)-Point*Trailing_alt)
{OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid,Digits)-Point*Trailing_alt, OrderTakeProfit(), 0);}
}
}


if (OrderType() == OP_SELL && OrderComment() == "alts" && OrderProfit () > suchnsuchpoint_alt && Trailing_alt > 0)
{
if(OrderOpenPrice()-NormalizeDouble(Ask,Digits) > Point*Trailing_alt)
{
if(OrderStopLoss() > NormalizeDouble(Ask,Digits)+Point*Trailing_alt)

{OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask,Digits)+Point*Trailing_alt, OrderTakeProfit(), 0);}
}
}


}
}

------------------------------------------------------------------------

What I find really bizare is that the following trailing stop loss code - which I have in my EA to set the trail of a different set of trades DOES actually work, and has the same form really::

for (int khe=1; khe<=OrdersTotal(); khe++)
{
if(OrderSelect(khe-1,SELECT_BY_POS)==true) // cycling through open orders
{
double Trailing = one/10; // 500/10 = 50
double suchnsuchpoint = one/2; // 500/2 = 250

if (OrderType() == OP_BUY && OrderComment() == "pb" && OrderProfit () > suchnsuchpoint && Trailing > 0)
{
if(NormalizeDouble(Bid,Digits)-OrderOpenPrice() > Point*Trailing)
{
if(OrderStopLoss() < NormalizeDouble(Bid,Digits)-Point*Trailing)
{OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid,Digits)-Point*Trailing, OrderTakeProfit(), 0);}
}
}


if (OrderType() == OP_SELL && OrderComment() == "ps" && OrderProfit () > suchnsuchpoint && Trailing > 0)
{
if(OrderOpenPrice()-NormalizeDouble(Ask,Digits) > Point*Trailing)
{
if(OrderStopLoss() > NormalizeDouble(Ask,Digits)+Point*Trailing)

{OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask,Digits)+Point*Trailing, OrderTakeProfit(), 0);}
}
}


}
}

 

First:

for (int khe=1; khe<=OrdersTotal(); khe++) is wrong, becuase you start to count at zero (0), so must be corret for (int khe=0; khe<=OrdersTotal()-1; khe++)

Second:

more correct will be otherwise: for (int khe=OrdersTotal()-1; khe>=0; khe--)


Third:

mostly the bug is in front of computer, not inside (joke only!!!!)

 
  1. mostly the bug is in front of computer, not inside (joke only!!!!)
    PICNIC = Problem In Chair, Not In Computer. Use code ID ten T (Id10t.)

  2. for (int khv=1; khv<=OrdersTotal(); khv++)
    {
    if(OrderSelect(khv-1,SELECT_BY_POS)==true) // cycling through open orders
    
    This makes the EA incompatible with any other (including itself on other charts) Always filter by magic number and pair. If the EA can open multiple orders, always count down. You MUST count down when closing/deleting. if(true==true) is redundent.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  3. double Trailing = one/10; // 500/10 = 50
    double suchnsuchpoint = one/2; // 500/2 = 250
    You didn't show 'one' You can't move stops closer than MarketInfo(Symbol(), MODE_STOPLEVEL)*Point (On IBFX 3 pips 30 points)
  4. if (OrderType() == OP_BUY && OrderComment() == "altb" && 
        OrderProfit () > suchnsuchpoint_alt && Trailing_alt > 0)
    
    Don't filter by comment. Dealers can change it. Always use the magic number. OrderProfit() is in the account currency (Dollars/Euro/GBP...) not points.

  5. if(NormalizeDouble(Bid,Digits)-OrderOpenPrice() > Point*Trailing_alt)
    
    No need to normalize Bid/Ask/Open[] etc. As long as you don't exceed stoplevel or try to compare doubles with ==, no need to ever normalize.