Sending Double Orders

 
#include <CustomFunctions010.mqh>
int MN = 73;
int shortmaPeriod = 8;
int longmaPeriod = 200;
double maxRiskPerTrade = 0.02;
double lots = OrderLots();
int orderid;
int slippage = 5; 

int OnInit()
  {
   Alert("");
   Alert("The EA just started.");
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   Alert("");
   Alert("The EA just closed.");
  }

void OnTick()
{
double longcurrentprice = Close[0] + (Point*40);
double shortcurrentprice = Close[0] - (Point*40);
double longSL = Close[0] - (Point*100);
double shortSL = Close[0] + (Point*100);
 double ATR = NormalizeDouble(iATR(NULL,0,14,0),Digits); 
 double longentry = Ask;
 double shortentry = Bid;
// double longstopLossPrice = Bid - (Point*80);
 //double shortstopLossPrice = Ask + (Point*80);

if(!CheckIfOpenOrdersByMagicNumber(MN))
{
 for(int i = OrdersHistoryTotal()-1;i>=0;i--)
{ 
 if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
 {
  if(OrderType() == OP_BUY && OrderMagicNumber() == MN && ChartSymbol() == Symbol())
  {
   if (OrderClosePrice() <= OrderStopLoss())
   {
    //shortcurrentprice = Close[0] - (Point*40);   
     shortSL = Close[0] + (Point*100);
    int orderid = OrderSend(NULL,OP_SELL,lotsize(maxRiskPerTrade,shortentry,shortSL),shortentry,slippage,shortSL,0,NULL,MN);  
    if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
   }
  }
  else if (OrderType() == OP_SELL && OrderMagicNumber() == MN && ChartSymbol() == Symbol())
  {
   if (OrderClosePrice() >= OrderStopLoss())
   {
    //longcurrentprice = Close[0] + (Point*40);   
     longSL = Close[0] - (Point*100);
    int orderid = OrderSend(NULL,OP_BUY,lotsize(maxRiskPerTrade,longentry,longSL),longentry,slippage,longSL,0,NULL,MN);
    if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
   }      
  }
  else if (Open[0] < Close[0])
  {
   {int orderid = OrderSend(NULL,OP_BUY, lotsize(maxRiskPerTrade,longentry,longSL),longentry,slippage,longSL,0,NULL,MN);}
     if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
  }
  else if (Open[0] > Close[0])
  {
   {int orderid = OrderSend(NULL,OP_SELL, lotsize(maxRiskPerTrade,shortentry,shortSL),shortentry,slippage,shortSL,0,NULL,MN);}
     if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
  }
 } 
}
}
for(int i = OrdersTotal()-1;i>=0;i--)
{ 
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 {
  if(OrderType() == OP_BUY && OrderMagicNumber() == MN && ChartSymbol() == Symbol())
  { 
   if (OrderStopLoss() + (Point*150) <= Close[0])
   {
    //longcurrentprice = Close[0] + (Point*40);   
    double newlongSL = Close[0] - (Point*100);
 
    bool ans = OrderModify(OrderTicket(),OrderOpenPrice(),newlongSL,0,0);
    if (ans == true)
    {
     Alert("order modified");
    }          
   }
  }
  else if (OrderType() == OP_SELL && OrderMagicNumber() == MN && ChartSymbol() == Symbol())
  {
   if (OrderStopLoss() - (Point*150) >= Close[0]) 
   {
    //shortcurrentprice = Close[0] - (Point*40);   
    double newshortSL = Close[0] + (Point*100);
    
    bool ans = OrderModify(OrderTicket(),OrderOpenPrice(),newshortSL,0,0);
    if (ans == true)
    {
     Alert("order modified");
    } 
   }
  }
 }
}


 }

Howdy peeps! The EA is sending duplicate orders instead of 1, therefore doubling my risk. I tried removing the else if code blocks of (Open[0] < Close[0]) &  (Open[0] > Close[0]) thinking that maybe it ran through the code block before the order could execute but I received the same result when I removed those lines of code. How can I get around this? 

Also, when i try to back-test this it does not return any trades. No errors are generated. I think it is not reading the ordershistorytotal loop(althought it works fine trading live except for double orders), but from what i have read the back-tester works with that function. What could it be?  

 
if(!CheckIfOpenOrdersByMagicNumber(MN))
{
 if (lastordertype == 1)
 {
  shortSL = Close[0] + (Point*100);
  int orderid = OrderSend(NULL,OP_SELL,lotsize(maxRiskPerTrade,shortentry,shortSL),shortentry,slippage,shortSL,0,NULL,MN);  
  if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
  lastordertype = 0;
 }
 else if (lastordertype == 0)
 {
  longSL = Close[0] - (Point*100);
  int orderid = OrderSend(NULL,OP_BUY,lotsize(maxRiskPerTrade,longentry,longSL),longentry,slippage,longSL,0,NULL,MN);
  if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
  lastordertype = 1;
 }
 else if (Open[0] < Close[0])
 {
  {int orderid = OrderSend(NULL,OP_BUY, lotsize(maxRiskPerTrade,longentry,longSL),longentry,slippage,longSL,0,NULL,MN);}
   if (orderid < 0) Alert("order rejected. Order error: " + GetLastError()); 
   lastordertype = 0;
 }
 else if (Open[0] > Close[0])
 {
  {int orderid = OrderSend(NULL,OP_SELL, lotsize(maxRiskPerTrade,shortentry,shortSL),shortentry,slippage,shortSL,0,NULL,MN);}
    if (orderid < 0) Alert("order rejected. Order error: " + GetLastError());
    lastordertype = 1; 
 }

instead of using ordershistorytotal, I simply created a variable to compare it to last order(lastordertype is int value inside of ontick). The back-tester is now working, but orders are not sending as they should. If it gets stopped out on a long position, it will re-enter a long position. same for shorts. 

Actually,  see the above error in the code. mixed up lastordertype
 
Ok the duplicate orders stopped since the revised code above. Thanks anyway :-)