Trades not executing while conditions are met/

 

While all conditions are met, my advisor is still not executing certain trades where it should be. For example: *** at this exact moment, the comment at the top left of a chart reads 5 true's, meaning each condition to sell off the current position has been met. However, the candle closes with no sell order executed. Why is this? Here is the code:


//+------------------------------------------------------------------+
//|                                        Ribbon Moving Average.mq5 |
//|                                                       Daniel Rad |
//|                                    instagram.com/itsdasanidaniel |
//+------------------------------------------------------------------+
#property copyright "Daniel Rad"
#property link      "instagram.com/itsdasanidaniel"
#property version   "1.00"
#include<Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

CTrade trade;

// Inputs
input int maSignalSmoother = 5;
input double lotSize = 0.1;
double posNetProfit;
double posCurVol;

// Base Variables
int MASIGNAL;
int MA1;
int MA2;
int MA3;
int MA4;
int MA5;
int MA6;
int MA7;
int MA8;
int MA9;
int MA10;

double MASIGNALarr[];
double MA1arr[];
double MA2arr[];
double MA3arr[];
double MA4arr[];
double MA5arr[];
double MA6arr[];
double MA7arr[];
double MA8arr[];
double MA9arr[];
double MA10arr[];



int OnInit() // Function initiates algo
  {
   MASIGNAL = iMA(_Symbol, _Period, maSignalSmoother, 0, MODE_SMA, PRICE_CLOSE);
   MA1 = iMA(_Symbol, _Period, 10, 0, MODE_SMA, PRICE_CLOSE);
   MA2 = iMA(_Symbol, _Period, 15, 0, MODE_SMA, PRICE_CLOSE);
   MA3 = iMA(_Symbol, _Period, 20, 0, MODE_SMA, PRICE_CLOSE);
   MA4 = iMA(_Symbol, _Period, 25, 0, MODE_SMA, PRICE_CLOSE);
   MA5 = iMA(_Symbol, _Period, 30, 0, MODE_SMA, PRICE_CLOSE);
   MA6 = iMA(_Symbol, _Period, 35, 0, MODE_SMA, PRICE_CLOSE);
   MA7 = iMA(_Symbol, _Period, 40, 0, MODE_SMA, PRICE_CLOSE);
   MA8 = iMA(_Symbol, _Period, 45, 0, MODE_SMA, PRICE_CLOSE);
   MA9 = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE);
   MA10 = iMA(_Symbol, _Period, 55, 0, MODE_SMA, PRICE_CLOSE);
   Alert("Initialized Successfully");
   return(INIT_SUCCEEDED);
  }



void OnDeinit(const int reason)
  {
   IndicatorRelease(MASIGNAL);
   IndicatorRelease(MA1);
   IndicatorRelease(MA2);
   IndicatorRelease(MA2);
   IndicatorRelease(MA4);
   IndicatorRelease(MA5);
   IndicatorRelease(MA6);
   IndicatorRelease(MA7);
   IndicatorRelease(MA8);
   IndicatorRelease(MA9);
   IndicatorRelease(MA10);
  }



double getSlope(double y2, double y1) // Funciton returns slope value for 2 consecutive bars
  {
   double top = y2 - y1;
   double bottom = 2.0 - 1.0;
   double slope = top / bottom;
   return(slope);
  }



bool IsNewCandle() // Function checks if a new candle has been opened
  {
   static datetime saved_candle_time;
   if(iTime(_Symbol, PERIOD_CURRENT, 0) == saved_candle_time)
      return false;
   else
      saved_candle_time = iTime(_Symbol, PERIOD_CURRENT, 0);
   return true;
  }

int openPositions = 0; // Var tracks number of current positions
int buyOrdersCurrent = 0;
double dynamicLotSize = lotSize;



void OnTick()
  {
  
   double priceClose = (iClose(_Symbol, PERIOD_CURRENT, 0)); // Var holds current close price of bar
   double priceOpen = (iOpen(_Symbol, PERIOD_CURRENT, 0)); // Var holds current open price of bar
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); // Var holds current ask price of bar
   double bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits); // Var holds current bid price of bar
   double currentBalance = AccountInfoDouble(ACCOUNT_BALANCE); // Var holds account balance
   double currentEquity = AccountInfoDouble(ACCOUNT_EQUITY); // Var holds account equity
   
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if(!PositionSelectByTicket(PositionGetTicket(i)))
         continue;
      string posSymbol = PositionGetString(POSITION_SYMBOL);
      double posOpen = PositionGetDouble(POSITION_PRICE_OPEN);
      double posProfit = PositionGetDouble(POSITION_PROFIT);
      int posSwap = (int) PositionGetDouble(POSITION_SWAP);
      double posVol = PositionGetDouble(POSITION_VOLUME);
      posNetProfit = posProfit + posSwap;
      posCurVol = posVol;
     }
     
   if (PositionsTotal() == 0) 
      {
      
      posCurVol = 0.0;
      
      }
     
   CopyBuffer(MASIGNAL, 0, 0, 3, MASIGNALarr);
   CopyBuffer(MA1, 0, 0, 1, MA1arr);
   CopyBuffer(MA2, 0, 0, 1, MA2arr);
   CopyBuffer(MA3, 0, 0, 1, MA3arr);
   CopyBuffer(MA4, 0, 0, 1, MA4arr);
   CopyBuffer(MA5, 0, 0, 1, MA5arr);
   CopyBuffer(MA6, 0, 0, 1, MA6arr);
   CopyBuffer(MA7, 0, 0, 1, MA7arr);
   CopyBuffer(MA8, 0, 0, 1, MA8arr);
   CopyBuffer(MA9, 0, 0, 1, MA9arr);
   CopyBuffer(MA10, 0, 0, 1, MA10arr);
   
   
   if (PositionsTotal() == 0) 
      {
   
      buyOrdersCurrent = 0;
      dynamicLotSize = 0.1;
      
      }
   else if ((buyOrdersCurrent > 4) && (PositionsTotal() > 0))
      {
      
      dynamicLotSize = (0.1 * buyOrdersCurrent) / 2;
      
      if (dynamicLotSize > posCurVol)
         {
         
         dynamicLotSize = posCurVol;
         
         }
      
      }
   else if ((buyOrdersCurrent > 8) && (PositionsTotal() > 0))
      {
      
      dynamicLotSize = (0.1 * buyOrdersCurrent);
      
      if (dynamicLotSize > posCurVol)
         {
         
         dynamicLotSize = posCurVol;
         
         }
      
      }
      
   
   
   if(((priceClose > MA1arr[0]) && (priceClose > MA2arr[0]) && (priceClose > MA3arr[0]) && (priceClose > MA4arr[0]) && (priceClose > MA5arr[0]) && (priceClose > MA6arr[0]) && (priceClose > MA7arr[0]) && (priceClose > MA8arr[0]) && (priceClose > MA9arr[0]) && (priceClose > MA10arr[0])) || ((priceOpen > MA1arr[0]) && (priceOpen > MA2arr[0]) && (priceOpen > MA3arr[0]) && (priceOpen > MA4arr[0]) && (priceOpen > MA5arr[0]) && (priceOpen > MA6arr[0]) && (priceOpen > MA7arr[0]) && (priceOpen > MA8arr[0]) && (priceOpen > MA9arr[0]) && (priceOpen > MA10arr[0])))
     {
      if((IsNewCandle()) && (getSlope(MASIGNALarr[2], MASIGNALarr[1]) < 0) && (getSlope(MASIGNALarr[1], MASIGNALarr[0]) >= 0) && (posNetProfit > 0) && (PositionsTotal() > 0))
        {
         Alert("SELL SIGNAL - SLOPE CURRENT BAR: ", getSlope(MASIGNALarr[2], MASIGNALarr[1]), " SLOPE PREVIOUS BAR: ", getSlope(MASIGNALarr[1], MASIGNALarr[0]), " CONDITION: ", ((priceClose > MA1arr[0]) && (priceClose > MA2arr[0]) && (priceClose > MA3arr[0]) && (priceClose > MA4arr[0]) && (priceClose > MA5arr[0]) && (priceClose > MA6arr[0]) && (priceClose > MA7arr[0]) && (priceClose > MA8arr[0]) && (priceClose > MA9arr[0]) && (priceClose > MA10arr[0])) || ((priceOpen > MA1arr[0]) && (priceOpen > MA2arr[0]) && (priceOpen > MA3arr[0]) && (priceOpen > MA4arr[0]) && (priceOpen > MA5arr[0]) && (priceOpen > MA6arr[0]) && (priceOpen > MA7arr[0]) && (priceOpen > MA8arr[0]) && (priceOpen > MA9arr[0]) && (priceOpen > MA10arr[0])));
         trade.Sell(dynamicLotSize, NULL, bid, 0.0, 0.0, NULL);
         openPositions = openPositions - 1;
        }
     }
     
   if(((priceClose < MA1arr[0]) && (priceClose < MA2arr[0]) && (priceClose < MA3arr[0]) && (priceClose < MA4arr[0]) && (priceClose < MA5arr[0]) && (priceClose < MA6arr[0]) && (priceClose < MA7arr[0]) && (priceClose < MA8arr[0]) && (priceClose < MA9arr[0]) && (priceClose < MA10arr[0])) || ((priceOpen < MA1arr[0]) && (priceOpen < MA2arr[0]) && (priceOpen < MA3arr[0]) && (priceOpen < MA4arr[0]) && (priceOpen < MA5arr[0]) && (priceOpen < MA6arr[0]) && (priceOpen < MA7arr[0]) && (priceOpen < MA8arr[0]) && (priceOpen < MA9arr[0]) && (priceOpen < MA10arr[0])))
     {
      if((IsNewCandle()) && (getSlope(MASIGNALarr[2], MASIGNALarr[1]) > 0) && (getSlope(MASIGNALarr[1], MASIGNALarr[0]) <= 0))
        {
         Alert("BUY SIGNAL - SLOPE CURRENT BAR: ", getSlope(MASIGNALarr[2], MASIGNALarr[1]), " SLOPE PREVIOUS BAR: ", getSlope(MASIGNALarr[1], MASIGNALarr[0]), " CONDITION: ", ((priceClose < MA1arr[0]) && (priceClose < MA2arr[0]) && (priceClose < MA3arr[0]) && (priceClose < MA4arr[0]) && (priceClose < MA5arr[0]) && (priceClose < MA6arr[0]) && (priceClose < MA7arr[0]) && (priceClose < MA8arr[0]) && (priceClose < MA9arr[0]) && (priceClose < MA10arr[0])) || ((priceOpen < MA1arr[0]) && (priceOpen < MA2arr[0]) && (priceOpen < MA3arr[0]) && (priceOpen < MA4arr[0]) && (priceOpen < MA5arr[0]) && (priceOpen < MA6arr[0]) && (priceOpen < MA7arr[0]) && (priceOpen < MA8arr[0]) && (priceOpen < MA9arr[0]) && (priceOpen < MA10arr[0])));
         trade.Buy(0.1, NULL, ask, 0.0, 0.0, NULL);
         openPositions = openPositions + 1;
         buyOrdersCurrent = buyOrdersCurrent + 1;
        }
     }
     
     
   Comment((getSlope(MASIGNALarr[2], MASIGNALarr[1]) < 0), " ", (getSlope(MASIGNALarr[1], MASIGNALarr[0]) >= 0), " ", ((priceClose > MA1arr[0]) && (priceClose > MA2arr[0]) && (priceClose > MA3arr[0]) && (priceClose > MA4arr[0]) && (priceClose > MA5arr[0]) && (priceClose > MA6arr[0]) && (priceClose > MA7arr[0]) && (priceClose > MA8arr[0]) && (priceClose > MA9arr[0]) && (priceClose > MA10arr[0])) || ((priceOpen > MA1arr[0]) && (priceOpen > MA2arr[0]) && (priceOpen > MA3arr[0]) && (priceOpen > MA4arr[0]) && (priceOpen > MA5arr[0]) && (priceOpen > MA6arr[0]) && (priceOpen > MA7arr[0]) && (priceOpen > MA8arr[0]) && (priceOpen > MA9arr[0]) && (priceOpen > MA10arr[0])), " ", (posNetProfit > 0), " ", (PositionsTotal() > 0));

     
  }
//+------------------------------------------------------------------+
 
dasani daniel: my advisor is still not executing certain trades where it should be.
  1. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


  2. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)