Please give me some guidelines where to look

 

I have written an EA that uses averaging and close all Buy or Sell trades at a certain profit. I use an emergency SL that is quite wide but no TP, of course.

I find that often (not always) it seems to keep a trade in memory and waits for trades to close before it is activated. (Example given below) This is in spite of the conditions for the trade to be opened not true at the time of opening the trade.

The conditions for thetrade (see snippet of code below) was negative, i.e. StochBuyTrade == false (I monitored this closely in the Journal and also on a text object on the chart) but it still opened another BUY trade!

The thing is sometimes it does not but sometimes it does. There is no clear reason for this. All I can think that at some point it registered a true condition for the trade in memory and although at the time when there were no other trades (condition is Sell == 0 or Buy == 0) it activated a wrong trade. 

Is there a way that I can continuously clear all memory in variables (except for setting them to "false" or "0" in the code?

Here is the snippet: (As you will see I immediately make StochSellTrade and StochBuyTrade "false" as soon as there is a successsful trade. I also set these variables == false in other parts of the code.

    if(WPR_OB == true && StochHigh == true && Sell == 0) { if(Stoch1 < StochHighLevel2) StochSellTrade = true; } if(WPR_OS == true && StochLow == true && Buy == 0) { if(Stoch1 > StochLowLevel2) StochBuyTrade = true; } 
  

 if(
StochSellTrade == true && Sell == 0 && (Direction == 1 || Direction == 2))

     {

      SellLotSize = NormalizeDouble(FixedLotSize * SellLot,2);

      // Verify Lotsize
      if(SellLotSize < MarketInfo(Symbol(),MODE_MINLOT))
        {
         SellLotSize = MarketInfo(Symbol(),MODE_MINLOT);
        }
      else
         if(SellLotSize > MarketInfo(Symbol(),MODE_MAXLOT))
           {
            SellLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
           }

      if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1)
        {
         SellLotSize = NormalizeDouble(SellLotSize,1);
        }
      else
         SellLotSize = NormalizeDouble(SellLotSize,2);


      SellStopLoss = Ask + (StopLoss * UsePoint);
      ///SellStopLoss = BarClose + (StopLoss * UsePoint);

      SellTakeProfit =  Bid - (TakeProfit * UsePoint);
      //SellTakeProfit =  BarClose - (TakeProfit * UsePoint);


      if(IsTradeContextBusy())
         Sleep(10);

      SellTicket = OrderSend(Symbol(),OP_SELL,SellLotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"OBOS-New system",MagicNumber,0,Red);


      if(SellTicket == -1)
        {
         ErrorCode = GetLastError();
         ErrDesc = ErrorDescription(ErrorCode);

         ErrAlert = StringConcatenate("Open OBOS Sell Order - Error ",ErrorCode,": ",ErrDesc);
         Alert(ErrAlert);

         ErrLog = StringConcatenate(" Lots: ",SellLotSize," Price: ",Bid," Stop: ",StopLoss," Profit: ",TakeProfit);
         Print(ErrLog);

        }
      SellCurrPair = Symbol();
      StochHigh = false;
      StochSellTrade = false;
      PlaySound("Alert.wav");
      Alert("ALERT: OBOS-New System SELL trade opened ", SellCurrPair +  "  Timeframe = " + Period());
      SendMail("OBOS-New System SELL trade opened ", SellCurrPair + " Date = " + TimeToStr(TimeCurrent(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeCurrent(),TIME_MINUTES) + "; " + "GMT = +3");
     }

Print("StochSellTrade is ",StochSellTrade);

   if(
StochBuyTrade == true && Buy == 0 && (Direction == 1 || Direction == 3))

     {

      // Calculate Lotsize

      BuyLotSize = NormalizeDouble(FixedLotSize * BuyLot,2);

      // Verify Lotsize
      if(BuyLotSize < MarketInfo(Symbol(),MODE_MINLOT))
        {
         BuyLotSize = MarketInfo(Symbol(),MODE_MINLOT);
        }
      else
         if(BuyLotSize > MarketInfo(Symbol(),MODE_MAXLOT))
           {
            BuyLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
           }

      if(MarketInfo(Symbol(),MODE_LOTSTEP) == 0.1)
        {
         BuyLotSize = NormalizeDouble(BuyLotSize,1);
        }
      else
         BuyLotSize = NormalizeDouble(BuyLotSize,2);

      if(IsTradeContextBusy())
         Sleep(10);


      BuyStopLoss = Bid - (StopLoss * UsePoint);
      //  BuyStopLoss = BarClose - (StopLoss * UsePoint);

      BuyTakeProfit = Ask + (TakeProfit * UsePoint);
      //BuyTakeProfit = BarClose + (TakeProfit * UsePoint);

      // Print("BuyStopLoss is ", BuyStopLoss);

      BuyTicket = OrderSend(Symbol(),OP_BUY,BuyLotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"OBOS-New system",MagicNumber,0,Green);


      if(BuyTicket == -1)
        {
         ErrorCode = GetLastError();
         ErrDesc = ErrorDescription(ErrorCode);

         ErrAlert = StringConcatenate("OBOS Buy Order - Error ",ErrorCode,": ",ErrDesc);
         Alert(ErrAlert);

         ErrLog = StringConcatenate(" Lots: ",BuyLotSize," Price: ",Ask," Stop: ",StopLoss," Profit: ",TakeProfit);
         Print(ErrLog);

        }
      BuyCurrPair = Symbol();
      StochLow = false;
      
StochBuyTrade = false;
      PlaySound("Alert.wav");
      Alert("ALERT: OBOS-New BUY trade opened ", BuyCurrPair + "  Timeframe = " + Period());
      SendMail("OBOS-New System BUY trade opened ", BuyCurrPair + " Date = " + TimeToStr(TimeCurrent(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeCurrent(),TIME_MINUTES) + "; " + "GMT = +3");
     }
StochSellTrade = false;
StochBuyTrade = false;

   

 

Is there nobody that can help me with this problem?

It is very frustrating not to be able to find the reason for these "wrong' trades happening and it is spoiling my results.

Hope there is somebody with the necessary know-how to help me out!