Error with OrderSend in MQL5

 

i am using the below code to check for existing positions and opening a trade but the ea still keeps opening multiple orders, help please.

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+


bool  BuyPlaced;
bool  SellPlaced;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);


            string sym=_Symbol;



            bool openPosition = PositionSelect(sym);
            long positionType = PositionGetInteger(POSITION_TYPE);
            //-------------------------------------------------------------------------------------------------------------------------------+
            double maxLossPerc = 0.02;
            double maxLossInPips = 10;
            double AccEquity = AccountInfoDouble(ACCOUNT_EQUITY);
            double LotSize = 100000;
            double tickValue = SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE);
            if(Digits()<=3)
               tickValue=tickValue/100;
            double maxLossDollar = AccEquity * maxLossPerc;
            double maxLossinQuoteCurr = maxLossDollar/tickValue;
            double optimalLotSize = NormalizeDouble(maxLossinQuoteCurr/(maxLossInPips*GetPipValue())/LotSize,2);

            double currentVolume = 0;
            if(openPosition == true)
               currentVolume = PositionGetDouble(POSITION_VOLUME);
         
            if(Strategy Logic){
                     if((SellPlaced == false && positionType != POSITION_TYPE_SELL))
                       {
                           request.action = TRADE_ACTION_DEAL;
                           request.type = ORDER_TYPE_SELL;
                           request.symbol = sym;
                           request.volume = optimalLotSize + currentVolume;
                           request.type_filling = ORDER_FILLING_IOC;
                           request.price = SymbolInfoDouble(sym,SYMBOL_BID);
                           request.sl = SymbolInfoDouble(sym,SYMBOL_BID) + (StopLoss * Point());
                           request.tp = SymbolInfoDouble(sym,SYMBOL_BID) - (TakeProfit * Point());
                           request.deviation = 50;
                           Print("SELL ",sym,": M",TF[j],", sell: ",sell,", min: ",min);
                           OrderSend(request,result);
                          }
                       }
                
            if(Strategy Logic){
                     if((positionType != POSITION_TYPE_BUY || openPosition == false))
                       {
                           request.action = TRADE_ACTION_DEAL;
                           request.type = ORDER_TYPE_BUY;
                           request.symbol = sym;
                           request.volume = optimalLotSize + currentVolume;
                           request.type_filling = ORDER_FILLING_IOC;
                           request.price = SymbolInfoDouble(sym,SYMBOL_ASK);
                           request.sl = SymbolInfoDouble(sym,SYMBOL_ASK) - (StopLoss * Point());
                           request.tp = SymbolInfoDouble(sym,SYMBOL_ASK) + (TakeProfit * Point());
                           request.deviation = 50;
                           OrderSend(request,result);
                          }
                       }
                       
            //+------------------------------------------------------------------+
           }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double  GetPipValue()
  {
   if(_Digits>=4)
      return 0.0001;

   else
      return 0.01;

  }
 

Logical error. It is necessary to correct: IF there is a position, then we go to

{

}
 
Vladimir Karputov:

Logical error. It is necessary to correct: IF there is a position, then we go to

i did not understand Sir

 
Ahmad861 :

i did not understand Sir

Have to do:

if(PositionSelect(sym))
   {


   }
 
Vladimir Karputov:

Have to do:

you mean to say first we check for open position and then execute the strategy ? right ?

 
Ahmad861:

you mean to say first we check for open position and then execute the strategy ? right ?

Yes.

 
Vladimir Karputov:

Yes.

Thanks a lot

Reason: