EA wont take Sell Trades

 

Not sure what the problem is hoping another set of eyes will help. Thanks

//+------------------------------------------------------------------+
//|                                                       FRIDAY.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict  //Throws errors in complier 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
string BotName = "Friday";
int Magic = 83831;

double LotsToTrade = 0.02;
double StopLoss = 200;
double ProfitTarget = 450;

int MaxTrades = 1;

bool LongSetup = False;
bool ShortSetup = False;

int MaxCloseSpreadPips = 10;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function        Runs Every tick                                     |
//+------------------------------------------------------------------+
void OnTick()
{
    double SlowMA = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
    double MA_Thirteen = iMA(NULL, 0, 13, 0, MODE_SMA, PRICE_CLOSE, 0);
    double MA_Eight = iMA(NULL, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 0);
    double MA_EightPrev = iMA(NULL, 0, 8, 0, MODE_SMA, PRICE_CLOSE, 1);
    double MA_Five = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
    
//********* BUY PARAMETERS ************** 
    if (GetTotalOpenTrades() < MaxTrades)
    {
      
      if ( (MA_Thirteen > SlowMA) &&
            (MA_Eight > SlowMA) &&
            (MA_Five > SlowMA) )
      {
         if ( (MA_Five > MA_Eight) &&
              (MA_EightPrev < MA_Thirteen) &&
              (MA_Eight > MA_Thirteen) ) 
         {
            LongSetup = True;
         }
      }
             
      if (LongSetup == True)
      {
         int OrderResult = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 5, Ask-(StopLoss*Point), Ask+ (ProfitTarget*Point), "BUY", Magic, 0, clrAqua); 
         LongSetup = False;
      }
    }
    
//********* SELL PARAMETERS ************   
    else if (GetTotalOpenTrades() < MaxTrades)
    {
      if ( (MA_Thirteen < SlowMA) &&
            (MA_Eight < SlowMA) &&
            (MA_Five < SlowMA) ) 
      {      
         if ( (MA_Five < MA_Eight) &&
              (MA_EightPrev > MA_Thirteen) &&
              (MA_Eight < MA_Thirteen))
              
         {
            ShortSetup = True;
         }
            
      }
      
      if (ShortSetup == True)
      {
         int OrderResult = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 5, Bid+(StopLoss*Point), Bid-(ProfitTarget*Point), "BUY", Magic, 0, clrCrimson);
         ShortSetup = False;
      }
    }  
    
}
//+------------------------------------------------------------------+
//Return the total number of open Trades
int GetTotalOpenTrades()
{
   int TotalTrades = 0;
   
   //Loop through open orders and add them to TotalTrades
   for (int t=0; t<OrdersTotal(); t++)
   {
      if (OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() != Symbol()) continue;
         if (OrderMagicNumber() != Magic) continue;
         if (OrderCloseTime() != 0) continue;
      
       TotalTrades = (TotalTrades + 1); 
     }
   }
   return TotalTrades;
}

void CloseAllTrades()
{
   int CloseResult = 0;
   
   for (int t=0; t<OrdersTotal(); t++)
   {
      if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderType() == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips, clrGreen);
         if (OrderType() == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips, clrRed);
         
         t--;
         
      }
   }
   return;

}

double GetTotalProfits()
{
   double TotalProfits = 0.0;
   
   for (int t=0; t<OrdersTotal(); t++)
   {
      if (OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderMagicNumber() != Magic) continue;
         if (OrderSymbol() != Symbol()) continue;
         if (OrderCloseTime() !=0) continue;
         TotalProfits = (TotalProfits + OrderProfit());
      }
   }
   return TotalProfits;

}
 
  if (GetTotalOpenTrades() < MaxTrades)
   {
    //Buys
   }
  else if (GetTotalOpenTrades() < MaxTrades)
   {
    //Sells
   }

The else will never be executed.

 
Keith Watford:

The else will never be executed.

so it is just a bracket issue?

 
Tiberious: so it is just a bracket issue?

No it is a logic error. When in doubt, think!

 
Tiberious:

so it is just a bracket issue?

Do you remember this post in your earlier topic?

Keith Watford:

You wrote the code so you should know what it is supposed to do.

When it doesn't execute as you expect, check your code carefully.

As I said before...

Keith Watford:

Then, as a coder you should be asking yourself "Why doesn't it print the error??"

The obvious answer is because there was no error!

If there was no error, then it can only be because there was no attempt to close any trade (or because a trade was closed).

What are the possible reasons for  there being no attempt to close any trade?

Only possible answers.......

There was not a trade open with the symbol.

The OrderSelect() failed.

The function was not called.

Add prints in your code to find the correct answer.

Did you check all the above?

      if (GetTotalOpenTrades() < MaxTrades) {
      //
   }
      
      else if (GetTotalOpenTrades() < MaxTrades) {
      //No code within these braces will be executed!
 }

I have removed the actual code from the above and an error is obvious.

You have already had this same mistake pointed out to you!

I am not going to waste any more time trying to help you as you ignore the advice and just repeat the same errors.

Reason: