Trailing Stop depending on the last 3 bars not working

 

Hi there,


i recently restarted programming MQL4 and made myself a Trailing Stop EA. Sadly the following version, which shall "trail" the Stop Loss depending on the Highs and Lows of the 3 most recent closed candles does absolutely nothing when activated:


int init()
{

   return (0);
}

int deinit()
{
   return (0);
}

int start()
{
   double PointValue;

   for (int j = 0; j < OrdersTotal(); j++)
   {
      OrderSelect (j, SELECT_BY_POS, MODE_TRADES);
      
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) 
      {   
         PointValue = 0.0001;
      }
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) 
      {
         PointValue = 0.01;
      }
      else 
      {
         PointValue = MarketInfo (OrderSymbol(), MODE_POINT);
      }
      
      double LastThreeLowest = iLowest (Symbol(), 0, MODE_LOW, 3, 0);
      double LastThreeHighest = iHighest (Symbol(), 0, MODE_HIGH, 3, 0);
      
      if ((OrderType() == OP_BUY) && (OrderStopLoss() < LastThreeLowest))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), LastThreeLowest, OrderTakeProfit(), Red);     
      }
      else if ((OrderType() == OP_SELL) && (OrderStopLoss() > LastThreeHighest))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), LastThreeHighest, OrderTakeProfit(), Red);
      }
      else if ((OrderType() == OP_SELL) && (OrderStopLoss() == 0))
      {
         OrderModify (OrderTicket(), OrderOpenPrice(), LastThreeHighest, OrderTakeProfit(), Red);
      }
   }
   return(0);
}
 
   for (int j = 0; j < OrdersTotal(); j++)
   {
      OrderSelect (j, SELECT_BY_POS, MODE_TRADES);
      
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) 
      {   
         PointValue = 0.0001;
      }
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) 
      {
         PointValue = 0.01;
      }
      else 
      {
         PointValue = MarketInfo (OrderSymbol(), MODE_POINT);
      }
      
      double LastThreeLowest = iLowest (Symbol(), 0, MODE_LOW, 3, 0);
      double LastThreeHighest = iHighest (Symbol(), 0, MODE_HIGH, 3, 0);

What Symbol() has the trade you try to modify are you sure it is same as chart your trailingstop is attached...

MagicNumber not important ???

Stoplevel ???

Freezelevel ???

and not checking why modify trade fails

Read again because it is told you before : What are Function return values ? How do I use them ?

 
You are identifying the Bar shift with the highest and lowest, but not the value
double LastThreeLowest = iLowest (Symbol(), 0, MODE_LOW, 3, 0);
double LastThreeHighest = iHighest (Symbol(), 0, MODE_HIGH, 3, 0);
 
Those and what does iLowest return?
 
GumRai:

You are identifying the Bar shift with the highest and lowest, but not the value


Ah thanks for clarifiing. I was under the impression, that iLowest and iHighest already gave me a value. Therefor I added
   double LowestValue = iLow (Symbol(), 0, LastThreeLowest);
   double HighestValue = iHigh (Symbol(), 0, LastThreeHighest);

and now it seems to work as intended.



Thanks to everybody for their time. Much appreciated.

 
  double LowestValue = iLow (Symbol(), 0, LastThreeLowest);
  double HighestValue = iHigh (Symbol(), 0, LastThreeHighest);


You should use OrderSymbol() or filter the trades where OrderSymbol()==Symbol().