Problems with double Orders

 

Hello, I wrote a bot that should open ten BuyLimit Orders.

The BuyLimit Orders should be max. 0.00010 under the Ask price with an distance of 0.00001 of each other.

That works,

but there shouln´t be an takeprofit twice in buy or buylimit orders, but it happens.

Something doesn´t work in my check.

Can someone tell me, what I did wrong?

Thanks!

Code:

//+------------------------------------------------------------------+
//|                                                BuyLimitTest1.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
double price;
bool exist;
double priceInAskArea;
double pricetakeprofit;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Comment(Ask);
   price = Ask;
   priceInAskArea = price;
   
   for(int i=10; i>0; i--)
     {
      price -= 0.00001;
      pricetakeprofit = (price + 0.00005);
      existfunction();
      if(exist == true)
        {
        
        }
      else
        {
         bool openorder = OpenOrder();
         if(openorder == true)
           {

           }
         else
           {
            bool OS = OrderSend(_Symbol,OP_BUYLIMIT,0.01,price,3,0,price+0.00005,NULL,11,0,Red);
            if(OS == false)
              {
               Print("ERROR: ",GetLastError());
              }            
           }
        }
     }
   for(int i=OrdersTotal();i>0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderType() == 2)
            if(OrderMagicNumber() == 11)
               if(OrderSymbol()== _Symbol)
                 {
                  bool inaskarea = InAskArea();
                  if(inaskarea == true)
                    {
                     
                    }
                  else
                    {
                     bool OC = OrderDelete(OrderTicket(),clrNONE);
                     if(OC == true)
                       {
                        
                       }
                     else
                       {
                        Print("Order: ",OrderTicket()," couln´t be closed. Error: ",GetLastError());
                       }
                    }
                 }
     }
  }
//+------------------------------------------------------------------+

void existfunction()
  {
   exist = false;
   for(int i=OrdersTotal(); i>=0; i--)
          {    
           if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           if(OrderType() == 2)
                   if(OrderMagicNumber()==11)
                   if(OrderSymbol()==_Symbol)
                      if(OrderOpenPrice() == price)
                     {
                      exist = true;
                     }
          }
  
  }

bool InAskArea()
  {
   
   bool returnvariable = false;
   
   for(int i = 10; i>0; i--)
     {
      priceInAskArea -= 0.00001;
      if(OrderOpenPrice() == priceInAskArea)
        {
         returnvariable = true;
        }
     }
   return returnvariable;
  }

bool OpenOrder()
  {
   bool returnvariable = false;
   for(int i=OrdersTotal();i>=0 ;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber() == 11)
            if(OrderSymbol() == _Symbol)
               if(OrderTakeProfit() == pricetakeprofit)
                 {
                  returnvariable = true;
                 }
  
     }
   return returnvariable;
  }
Reason: