Trailiing stop issues?

 

Hi all,


I just wondered if anyone could see any issues with my trailing stop? For some reason, all the trades seem to close for a small loss, and dont appear to be trailing. Also, would this alter the SL on my other trades, not placed by the EA? If so, can i filter them to this EA on this chart only?


Heres my code:


void trailingStop()
{
   if(use_Trailing_Stop==true)
   {
      TrailBuy(trailingStopValue);
      TrailSell(trailingStopValue);
   }
}

void TrailBuy(int trailValue)
{
   for(int i=0; i<OrdersTotal(); i++)
   {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderSymbol()==Symbol())
         {
            if(OrderType()==OP_BUY&&OrderMagicNumber()==02)
            {
               if(Bid-OrderOpenPrice()>trailValue*Point)
               {
                  if(OrderStopLoss()<Bid-trailValue*Point)
                  {
                     ModifyStopLoss(Bid-trailValue*Point);
                  }
               }
            }
         }
      }
   }
}

void TrailSell(int trailValue)
{
   for(int i=0; i<OrdersTotal(); i++)
   {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
         if (OrderSymbol()==Symbol())
         {
            if(OrderType()==OP_SELL&&OrderMagicNumber()==02)
            {
               if(OrderOpenPrice()-Ask>trailValue*Point)
               {
                  if(OrderStopLoss()>Ask+trailValue*Point || OrderStopLoss()==0)
                  {
                     ModifyStopLoss(Ask+trailValue*Point);
                  }
               }
            }
         }
      }
   }
}

void ModifyStopLoss(double thesl)
{
   bool fm;
   fm=OrderModify(OrderTicket(),OrderOpenPrice(),thesl,OrderTakeProfit(),0,CLR_NONE);
}


Cheers!

 
brucey2343: I just wondered if anyone could see any issues with my trailing stop?
  1.                if(Bid-OrderOpenPrice()>trailValue*Point)
                   {
                      if(OrderStopLoss()<Bid-trailValue*Point)
                      {
                         ModifyStopLoss(Bid-trailValue*Point);
    Those are the same, redundant conditions. Don't copy and paste code repeatedly - compute the new SL once, then compare it to the current.

  2.    for(int i=0; i<OrdersTotal(); i++){
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must Find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 2 #16

    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask) or (be direction independent and use) OrderClosePrice().

  3.                   if(OrderStopLoss()>Ask+trailValue*Point || OrderStopLoss()==0)
    
    You don't check for zero in buy orders.

  4. brucey2343: would this alter the SL on my other trades, not placed by the EA? If so, can i filter them to this EA on this chart only?
    That is why you filter by MN (for other EAs and manual trading,) and symbol (in case you forget to change it on other symbols.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

Reason: