Enter trade and Exit trade with Fractal Indicator

 
hi Everyone,

I have written a code for trying to enter a trade and exit a trade based on an fractal indicator.

however, im only able to exit trade with trade profit but unable to exit a trade with the fractal. for example...... :

-Enter buy trade with an up arrow , and exit the trade when down arrow shows up. 
-Entering Sell trade with a down arrow, and exit trade when up arrow shows up .

Guidance from anyone are greatly appreciated.

Thank you 
//+------------------------------------------------------------------+
//|                                                     JustBuy2.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

double TakeProfit;
double StopLoss;

input double InpLotSize = 0.01;
input int InpTakeProfitPts = 100;
input int InpStopLossPts   = 100; 
int orderID;


//double MaxSpread = 100 * GetPipValue(); // Unit here is pips
//double spread = Ask - Bid;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double point = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
   TakeProfit     = InpTakeProfitPts * point;
   StopLoss       = InpStopLossPts *point;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//if (!NewBar())return;


if (OrdersTotal() <1)
{
   double UpArrow = iCustom (NULL,0,"fractal",0,1);
   Print ("Up arrow value" , UpArrow);
      
   double DownArrow = iCustom (NULL,0,"fractal",1,1);
   Print ("Down arrow value" , DownArrow);
   
   if (UpArrow!=EMPTY_VALUE);
   {
    double OpenPrice= Ask;
      double takeProfit = OpenPrice + TakeProfit ;
      double stopLoss = OpenPrice -StopLoss ; // better no stop lost 
     
      
      orderID = OrderSend(NULL,OP_BUY,InpLotSize,OpenPrice,0,0,takeProfit);
      if(orderID < 0) Alert("order rejected. Order error: " + GetLastError());
   }

   
   if (DownArrow!=EMPTY_VALUE);
   {
      double OpenPrice= Bid;
      double takeProfit = OpenPrice - TakeProfit ;
      double stopLoss = OpenPrice +StopLoss ; // better no stop lost 
      
      orderID = OrderSend(NULL,OP_SELL,InpLotSize,OpenPrice,0,0,takeProfit);
      if(orderID < 0) Alert("order rejected. Order error: " + GetLastError());
   }

}


else 
{
   double UpArrow = iCustom (NULL,0,"fractal",0,1);
   Print ("Up arrow value" , UpArrow);
      
   double DownArrow = iCustom (NULL,0,"fractal",1,1);
   Print ("Down arrow value" , DownArrow);
   
    if(OrderSelect(orderID,SELECT_BY_TICKET)==true)
     {
      int orderType = OrderType();
      
      if(orderType ==0 )//long position
            {
               for (int b= OrdersTotal() -1; b >=0; b--)
               {
               if (OrderSelect (b, SELECT_BY_POS, MODE_TRADES))
                  {
                  if (OrderSymbol() == OrderSymbol())
                     {
                     if (OrderType() == OP_BUY)
                        
                        if (DownArrow!=EMPTY_VALUE);
                           {
                           orderID = OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
                           if(orderID < 0) Alert("order rejected. Order error: " + GetLastError());
                           }
                        }
                     }
                  }
                }
               
            }
            else //if short
            {
                for (int b= OrdersTotal() -1; b >=0; b--)
                  {
                  if (OrderSelect (b, SELECT_BY_POS, MODE_TRADES))
                     {
                     if (OrderSymbol() == OrderSymbol())
                        {
                        if (OrderType() == OP_SELL)
                           {
                           if (UpArrow!=EMPTY_VALUE);
                              {
                              orderID = OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
                              if(orderID < 0) Alert("order rejected. Order error: " + GetLastError());
                              }}}}}  
            }
      }
      



  }
//+------------------------------------------------------------------+


double GetPipValue()
{
   if(_Digits >=4)
   {
      return 0.0001;
   }
   else
   {
      return 0.01;
   }
}


bool NewBar()
{
   static datetime prevTime = 0;
   datetime currentTime = iTime(Symbol(),Period(),0);
   if (currentTime != prevTime)
   {
   prevTime = currentTime;
   return(true);
   }
   return (false);
}