I'm getting invalid stops despite normalizing price and taking spread and stop level into account

 
HI, As I stated in the tittle I'm getting an "invalid stops" error during backtesting. It doesnt happen on every entry attempt. Just once every couple trades.

I've tried Normalizing Price, both using NormalizeDouble and Normalizing with tick size. I also made sure the stoploss is placed further than spread + stop level.
Implimenting this got rid of most of the errors but some still persist. 

Here is the code assosiated with entry.

if(ShandelierSignal == Signal_Sell && posTicket == 0 && SMASignal == Signal_Under)
         {
            double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            double spread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) * _Point;
            double  stop_level = SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) * _Point;
            _sl = NormalizeDouble(getHigh(spread + stop_level, bid), 5);
            double stop_pips = _sl - bid;
            
            double lots = CalculateLots(1, stop_pips);   
            _tp = NormalizeDouble(bid -  ((_sl - bid) * 1.3), 5);
            bool marketError = market.Sell(lots, _Symbol, bid, _sl,  _tp, "sell");
            posTicket = market.ResultOrder();
            TradesEntered++;
            
            if(!marketError)
           {
               Print(market.ResultRetcodeDescription(), " ", _tp, " ", _sl, " ", bid);
               Print("Pips: ", stop_pips, " min pips: ", spread + stop_level, " sl: ", _sl);
               TesterStop();
           }
           else
          {
            Print("tp: ", _tp, " bid: ", bid, " sl: ", _sl);
          }
         }




double  getHigh(double min_sl_distance, double price)
{
   for(int i=SMAPeriod - 1; i >= 0; i--)
   {
      double pips = swingHighs[i] - price;
      if( pips > min_sl_distance && swingHighs[i] != 0 && pips > minStopPips)
      {
         Print(swingHighs[i]);
         return swingHighs[i];
      }
      else
      {
         Print("Too Close: ", swingHighs[i], " < ", price + min_sl_distance);
      }
   }    
   
   if(minStopPips > min_sl_distance)
   {
      Print("Used chosen minimum pips");
      return price + minStopPips;
   }
   else
   {
      Print("Used stop level: ", price + min_sl_distance);
      return price + min_sl_distance + 0.0005;
   }

}

double  getLow(double min_sl_distance, double price)
{
   
   for(int i=SMAPeriod - 1;i >= 0;i--)
   {
      double pips = price - swingLows[i];
     if( pips > min_sl_distance && swingLows[i] != 0 && pips > minStopPips)
     {
         Print(i, ": ", swingLows[i]);
         Print(min_sl_distance);
         return swingLows[i];
     }
     else
       {
         Print("Too Close: ", swingLows[i], " > ", price - min_sl_distance);
       }
   }      
   
   if(minStopPips > min_sl_distance)
   {
      Print("Used chosen minimum pips");
      return price - (minStopPips);
      
   }
   else
   {
      Print("Used stop level: ", price - (min_sl_distance));
      return price - (min_sl_distance);
      
   }
}

double CalculateLots(double RiskPercentage, double SLDistance )
{
   double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   
   if(tickSize == 0 || tickValue == 0|| lotStep == 0)
     {
         Print(__FUNCTION__, " Error while getting account info");
     }
   
   double riskedMoney = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercentage / 100;
   double moneyLotStep = (SLDistance/tickSize) * lotStep;
   
   if(moneyLotStep == 0)
     {
         Print(__FUNCTION__, " Cannot calculate lots");
     }
   double lots = MathFloor(riskedMoney / moneyLotStep) * lotStep;
   
   if (lots < 0.01)
   {
      lots = 0.01;
   }
   return lots;

}

I can provide the whole EA to make testing easier.

I've also added a file containing the errors printed by the journal. Just in case it helps, pips is the distance of the stoploss from the entry price and min pips is spread + stop level


Could this be caused by my history data? it really only started being this bad after reinstalling mt5.

Thanks for taking a look.

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
All requests to execute trade operations are sent as a structure of a trade request MqlTradeRequest using function OrderSend() . The function...
Files:
 
i'VE ADDED the following to try furthur debugging

Print("SELL");
Print("bid: ", bid);
Print("entry price: ", SymbolInfoDouble(_Symbol, SYMBOL_BID) );
Print("SL: ", _sl);
Print("tp: ", _tp);
Print("Freeze Level: ", SymbolInfoInteger(_Symbol, SYMBOL_TRADE_FREEZE_LEVEL) * _Point);
Print("Stop Level: ", stop_level);
TesterStop();
I attached a picture with the messages.

It seems like the bid is changing during the processing of the onTick function. Is this right? I thought Price only changes once per tick. How do I handle this?
Files: