setting SL to be the lowest price based on the numbers of bars strategy

 

Hi I want to make a stoploss strategy using the lowest price of numbers of bars (in my case 5 bars). this is the code that I came up with:

int highestPriceShift  = iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,5,0);
int lowestPriceShift   = iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,5,0);
//
double highestPrice = iHigh(Symbol(),PERIOD_CURRENT,highestPriceShift);
double lowestPrice = iLow(Symbol(),PERIOD_CURRENT,lowestPriceShift);


void OnTick()
{
   if(newBar && BUYCondition)
   {
    double stoplossPrice = lowestPrice;
   
    double lotSize       = NormalizeDouble(OptimalLotSize(Risk_Per_Trade,Ask,stopLossPrice)/lotType,2);
    if(lotSize < MarketInfo(Symbol(),MODE_MINLOT))
    lotSize = MarketInfo(Symbol(),MODE_MINLOT);

    orderId              = OrderSend(Symbol(),OP_BUY,lotSize,Ask,slippage,stopLossPrice,0,"Strong BUY",MagicNumber)
   }

   //stoploss
   if(OrderSelect(orderId,SELECT_BY_TICKET))
   {
    if(OrderSymbol() == Symbol() && OrderType()==OP_BUY)
      {
       double trailingStop = lowestPrice;
       if(newBar && OrderStopLoss() != trailingStop)
          {
           bool resSL = OrderModify(orderId,OrderOpenPrice(),trailingStop,OrderTakeProfit(),0);
           if(!resSL)
              {
               Print("Error in Order Modify. Error code=",GetLastError());
               return;
              }
            else
               Print("Order modify successfully for ticket: ", OrderTicket());

          }
      }
   }
}

When I backtest the EA, the EA doesn't recognize the stoploss strategy and set the stoploss to a weird place like the image I attach. Is anyone can tell me which part of the code that make this strategy doesn't work?

Files:
Capture.PNG  27 kb
 
int highestPriceShift  = iHighest(Symbol(),PERIOD_CURRENT,MODE_HIGH,5,0);
int lowestPriceShift   = iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,5,0);
//
double highestPrice = iHigh(Symbol(),PERIOD_CURRENT,highestPriceShift);
double lowestPrice = iLow(Symbol(),PERIOD_CURRENT,lowestPriceShift);
Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 2013.02.10

 
Yes that's where I made my mistake, thanks for pointing out